spec/struct_spec.rb in paradocs-1.0.22 vs spec/struct_spec.rb in paradocs-1.0.23

- old
+ new

@@ -1,10 +1,10 @@ require 'spec_helper' require 'paradocs/struct' describe Paradocs::Struct do - it "works" do + it 'works' do friend_class = Class.new do include Paradocs::Struct schema do field(:name).type(:string).present @@ -51,11 +51,11 @@ expect(invalid_instance.errors['$.title']).not_to be_nil expect(invalid_instance.errors['$.friends[1].name']).not_to be_nil expect(invalid_instance.friends[1].errors['$.name']).not_to be_nil end - it "is inmutable by default" do + it 'is inmutable by default' do klass = Class.new do include Paradocs::Struct schema do field(:title).type(:string).present @@ -64,19 +64,19 @@ end end instance = klass.new expect { - instance.title = "foo" + instance.title = 'foo' }.to raise_error NoMethodError expect { instance.friends << 1 }.to raise_error RuntimeError end - it "works with anonymous nested schemas" do + it 'works with anonymous nested schemas' do klass = Class.new do include Paradocs::Struct schema do field(:title).type(:string).present @@ -123,11 +123,11 @@ user = klass.new(name: 'Ismael', friends: [{age: 43}]) expect(user.friends.first.salutation).to eq 'my age is 43' end - it "wraps regular schemas in structs" do + it 'wraps regular schemas in structs' do friend_schema = Paradocs::Schema.new do field(:name) end klass = Class.new do @@ -145,11 +145,11 @@ }) expect(instance.friends.first.name).to eq 'Ismael' end - it "#to_h" do + it '#to_h' do klass = Class.new do include Paradocs::Struct schema do field(:title).type(:string).present @@ -183,11 +183,11 @@ data = new_instance.to_h data[:title] = 'nope' expect(new_instance.to_h[:title]).to eq 'foo' end - it "works with inheritance" do + it 'works with inheritance' do klass = Class.new do include Paradocs::Struct schema do field(:title).type(:string).present @@ -216,11 +216,11 @@ expect(instance.title).to eq 'foo' expect(instance.email).to eq 'email@me.com' expect(instance.friends.size).to eq 2 end - it "implements deep struct equality" do + it 'implements deep struct equality' do klass = Class.new do include Paradocs::Struct schema do field(:title).type(:string).present @@ -266,11 +266,11 @@ expect(s1 == s2).to be true expect(s1 == s3).to be false expect(s1 == s4).to be false end - it "#merge returns a new instance" do + it '#merge returns a new instance' do klass = Class.new do include Paradocs::Struct schema do field(:title).type(:string).present @@ -299,10 +299,24 @@ expect(copy.title).to eq 'bar' expect(copy.desc).to eq 'no change' expect(copy.friends.first.name).to eq 'jane' end + it 'passes the environment to the schema' do + klass = Class.new do + include Paradocs::Struct + + schema do + field(:age).type(:integer) + end + end + + new_instance = klass.new({}, { key: :value }) + + expect(new_instance.send(:_results).environment).to eq({ key: :value }) + end + describe '.new!' do it 'raises a useful exception if invalid data' do klass = Class.new do include Paradocs::Struct @@ -317,8 +331,22 @@ expect(e.errors['$.title']).not_to be nil end valid = klass.new!(title: 'foo') expect(valid.title).to eq 'foo' + end + + it 'passes the environment to the schema' do + klass = Class.new do + include Paradocs::Struct + + schema do + field(:title).type(:string).present + end + end + + new_instance = klass.new!({ title: 'test' }, { key: :value }) + + expect(new_instance.send(:_results).environment).to eq({ key: :value }) end end end