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:
parent
bafd35c278
commit
f24985c23c
7 changed files with 94 additions and 67 deletions
|
@ -12,7 +12,7 @@ describe V8::C::Array do
|
||||||
a.Set(@ctx, 0, o)
|
a.Set(@ctx, 0, o)
|
||||||
expect(a.Length).to eq 1
|
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
|
end
|
||||||
|
|
||||||
it 'can be initialized with a length' do
|
it 'can be initialized with a length' do
|
||||||
|
|
|
@ -24,8 +24,8 @@ describe V8::C::Function do
|
||||||
|
|
||||||
fn.Call(@ctx.Global, [one, two, 3])
|
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, 'one'))).to strict_eq one
|
||||||
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, 'two'))).to strict_eq two
|
||||||
expect(@ctx.Global.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'three')).FromJust().Value()).to eq 3
|
expect(@ctx.Global.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'three')).FromJust().Value()).to eq 3
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ describe V8::C::Function do
|
||||||
expect(callback.this.GetIdentityHash()).to eql @ctx.Global().GetIdentityHash()
|
expect(callback.this.GetIdentityHash()).to eql @ctx.Global().GetIdentityHash()
|
||||||
expect(callback.is_construct_call).to be false
|
expect(callback.is_construct_call).to be false
|
||||||
expect(callback.data).not_to be_nil
|
expect(callback.data).not_to be_nil
|
||||||
expect(callback.data.StrictEquals(data)).to be true
|
expect(callback.data).to strict_eq data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,39 +12,25 @@ describe V8::C::Object do
|
||||||
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
|
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
|
||||||
value = V8::C::String.NewFromUtf8(@isolate, 'bar')
|
value = V8::C::String.NewFromUtf8(@isolate, 'bar')
|
||||||
|
|
||||||
maybe = o.Set(@ctx, key, value)
|
expect(o.Set(@ctx, key, value)).to be_successful
|
||||||
expect(maybe.IsJust()).to be true
|
expect(o.Get(@ctx, key)).to strict_eq value
|
||||||
expect(maybe.FromJust()).to be true
|
|
||||||
|
|
||||||
maybe = o.Get(@ctx, key)
|
|
||||||
expect(maybe.IsJust()).to be true
|
|
||||||
expect(maybe.FromJust().Utf8Value).to eq 'bar'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'can determine if a key has been set' do
|
it 'can determine if a key has been set' do
|
||||||
o = V8::C::Object.New(@isolate)
|
o = V8::C::Object.New(@isolate)
|
||||||
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
|
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
|
||||||
|
|
||||||
o.Set(@ctx, key, key)
|
expect(o.Set(@ctx, key, key)).to be_successful
|
||||||
|
expect(o.Has(@ctx, key)).to be_successful
|
||||||
maybe = o.Has(@ctx, key)
|
|
||||||
expect(maybe.IsJust()).to be true
|
|
||||||
expect(maybe.FromJust()).to eq true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'can delete keys' do
|
it 'can delete keys' do
|
||||||
o = V8::C::Object.New(@isolate)
|
o = V8::C::Object.New(@isolate)
|
||||||
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
|
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
|
||||||
|
|
||||||
o.Set(@ctx, key, key)
|
expect(o.Set(@ctx, key, key)).to be_successful
|
||||||
|
expect(o.Delete(@ctx, key)).to be_successful
|
||||||
maybe = o.Delete(@ctx, key)
|
expect(o.Has(@ctx, key)).to eq_just false
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#SetAccessor' do
|
describe '#SetAccessor' do
|
||||||
|
@ -65,14 +51,11 @@ describe V8::C::Object do
|
||||||
info.GetReturnValue.Set(get_value)
|
info.GetReturnValue.Set(get_value)
|
||||||
end
|
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(get_name).to v8_eq key
|
||||||
expect(maybe.IsJust).to be true
|
expect(get_data).to strict_eq data
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'can set setters' do
|
it 'can set setters' do
|
||||||
|
@ -89,14 +72,12 @@ describe V8::C::Object do
|
||||||
set_value = value
|
set_value = value
|
||||||
end
|
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(o.Set(@ctx, key, data)).to be_successful
|
||||||
expect(maybe.IsJust).to be true
|
|
||||||
expect(maybe.FromJust).to be true
|
|
||||||
|
|
||||||
expect(set_data.StrictEquals(data)).to be true
|
expect(set_data).to strict_eq data
|
||||||
expect(set_value.StrictEquals(data)).to be true
|
expect(set_value).to strict_eq data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ describe V8::C::Symbol do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns different symbols for different registries" do
|
it "returns different symbols for different registries" do
|
||||||
expect(global.StrictEquals(api)).to be false
|
expect(global).to_not strict_eq api
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,3 @@
|
||||||
require 'v8/init'
|
require 'v8/init'
|
||||||
|
|
||||||
module V8ContextHelpers
|
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |helper| require_relative helper }
|
||||||
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
|
|
||||||
|
|
28
spec/support/context.rb
Normal file
28
spec/support/context.rb
Normal 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
|
45
spec/support/v8_matchers.rb
Normal file
45
spec/support/v8_matchers.rb
Normal 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
|
Loading…
Add table
Reference in a new issue