1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

Add rspec matchers for Maybe and (Strict)?Equals

`expect(a).to eq_just b` <=> `a.IsJust && a.FromJust == b`
`expect(a).to be_successful` <=> `a.IsJust && a.FromJust`
`expect(a).to strict_eq b` <=> if a is Maybe: `a.IsJust && a.FromJust.StrictEquals(b)`; else `a.StrictEquals(b)`
`expect(a).to v8_eq b` <=> same as above but for `Equals` instead of `StrictEquals`
This commit is contained in:
Georgy Angelov 2015-08-01 18:30:53 +00:00
parent bafd35c278
commit f24985c23c
7 changed files with 94 additions and 67 deletions

View file

@ -12,7 +12,7 @@ describe V8::C::Array do
a.Set(@ctx, 0, o)
expect(a.Length).to eq 1
expect(a.Get(@ctx, 0).FromJust().Equals(o)).to eq true
expect(a.Get(@ctx, 0)).to v8_eq o
end
it 'can be initialized with a length' do

View file

@ -24,8 +24,8 @@ describe V8::C::Function do
fn.Call(@ctx.Global, [one, two, 3])
expect(@ctx.Global.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'one')).FromJust().StrictEquals(one)).to be true
expect(@ctx.Global.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'two')).FromJust().StrictEquals(two)).to be true
expect(@ctx.Global.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'one'))).to strict_eq one
expect(@ctx.Global.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'two'))).to strict_eq two
expect(@ctx.Global.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'three')).FromJust().Value()).to eq 3
end
@ -77,7 +77,7 @@ describe V8::C::Function do
expect(callback.this.GetIdentityHash()).to eql @ctx.Global().GetIdentityHash()
expect(callback.is_construct_call).to be false
expect(callback.data).not_to be_nil
expect(callback.data.StrictEquals(data)).to be true
expect(callback.data).to strict_eq data
end
end

View file

@ -12,39 +12,25 @@ describe V8::C::Object do
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
value = V8::C::String.NewFromUtf8(@isolate, 'bar')
maybe = o.Set(@ctx, key, value)
expect(maybe.IsJust()).to be true
expect(maybe.FromJust()).to be true
maybe = o.Get(@ctx, key)
expect(maybe.IsJust()).to be true
expect(maybe.FromJust().Utf8Value).to eq 'bar'
expect(o.Set(@ctx, key, value)).to be_successful
expect(o.Get(@ctx, key)).to strict_eq value
end
it 'can determine if a key has been set' do
o = V8::C::Object.New(@isolate)
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
o.Set(@ctx, key, key)
maybe = o.Has(@ctx, key)
expect(maybe.IsJust()).to be true
expect(maybe.FromJust()).to eq true
expect(o.Set(@ctx, key, key)).to be_successful
expect(o.Has(@ctx, key)).to be_successful
end
it 'can delete keys' do
o = V8::C::Object.New(@isolate)
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
o.Set(@ctx, key, key)
maybe = o.Delete(@ctx, key)
expect(maybe.IsJust()).to be true
expect(maybe.FromJust()).to eq true
maybe = o.Has(@ctx, key)
expect(maybe.IsJust()).to be true
expect(maybe.FromJust()).to eq false
expect(o.Set(@ctx, key, key)).to be_successful
expect(o.Delete(@ctx, key)).to be_successful
expect(o.Has(@ctx, key)).to eq_just false
end
describe '#SetAccessor' do
@ -65,14 +51,11 @@ describe V8::C::Object do
info.GetReturnValue.Set(get_value)
end
o.SetAccessor(@ctx, key, getter, nil, data)
expect(o.SetAccessor(@ctx, key, getter, nil, data)).to be_successful
expect(o.Get(@ctx, key)).to strict_eq get_value
maybe = o.Get(@ctx, key)
expect(maybe.IsJust).to be true
expect(maybe.FromJust.StrictEquals(get_value)).to be true
expect(get_name.Equals(key)).to be true
expect(get_data.StrictEquals(data)).to be true
expect(get_name).to v8_eq key
expect(get_data).to strict_eq data
end
it 'can set setters' do
@ -89,14 +72,12 @@ describe V8::C::Object do
set_value = value
end
o.SetAccessor(@ctx, key, proc { }, setter, data)
expect(o.SetAccessor(@ctx, key, proc { }, setter, data)).to be_successful
maybe = o.Set(@ctx, key, data)
expect(maybe.IsJust).to be true
expect(maybe.FromJust).to be true
expect(o.Set(@ctx, key, data)).to be_successful
expect(set_data.StrictEquals(data)).to be true
expect(set_value.StrictEquals(data)).to be true
expect(set_data).to strict_eq data
expect(set_value).to strict_eq data
end
end

View file

@ -35,7 +35,7 @@ describe V8::C::Symbol do
end
it "returns different symbols for different registries" do
expect(global.StrictEquals(api)).to be false
expect(global).to_not strict_eq api
end
end

View file

@ -1,30 +1,3 @@
require 'v8/init'
module V8ContextHelpers
module GroupMethods
def requires_v8_context
around(:each) do |example|
bootstrap_v8_context(&example)
end
end
end
def bootstrap_v8_context
@isolate = V8::C::Isolate.New
V8::C::HandleScope(@isolate) do
@ctx = V8::C::Context::New(@isolate)
begin
@ctx.Enter
yield
ensure
@ctx.Exit
end
end
end
end
RSpec.configure do |c|
c.include V8ContextHelpers
c.extend V8ContextHelpers::GroupMethods
end
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |helper| require_relative helper }

28
spec/support/context.rb Normal file
View file

@ -0,0 +1,28 @@
module V8ContextHelpers
module GroupMethods
def requires_v8_context
around(:each) do |example|
bootstrap_v8_context(&example)
end
end
end
def bootstrap_v8_context
@isolate = V8::C::Isolate.New
V8::C::HandleScope(@isolate) do
@ctx = V8::C::Context::New(@isolate)
begin
@ctx.Enter
yield
ensure
@ctx.Exit
end
end
end
end
RSpec.configure do |c|
c.include V8ContextHelpers
c.extend V8ContextHelpers::GroupMethods
end

View file

@ -0,0 +1,45 @@
module V8Matchers
extend RSpec::Matchers::DSL
matcher :eq_just do |expected|
match do |maybe|
return false unless maybe.IsJust
maybe.FromJust == expected
end
end
matcher :be_successful do
match do |maybe|
return false unless maybe.IsJust
maybe.FromJust
end
end
matcher :strict_eq do |expected|
match do |object|
if object.respond_to? 'IsJust'
return false unless object.IsJust
object = object.FromJust
end
object.StrictEquals(expected)
end
end
matcher :v8_eq do |expected|
match do |object|
if object.respond_to? 'IsJust'
return false unless object.IsJust
object = object.FromJust
end
object.Equals(expected)
end
end
end
RSpec.configure do |c|
c.include V8Matchers
end