spec/object_spec.rb in strong_json-0.0.4 vs spec/object_spec.rb in strong_json-0.1.0
- old
+ new
@@ -7,27 +7,25 @@
b: StrongJSON::Type::Base.new(:string))
expect(type.coerce(a: 123, b: "test")).to eq(a: 123, b: "test")
end
- it "drops unspecified fields" do
+ it "rejects unspecified fields" do
type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric))
- expect(type.coerce(a: 123, b: true)).to eq(a: 123)
+ expect { type.coerce(a:123, b:true) }.to raise_error(StrongJSON::Type::UnexpectedFieldError)
end
- describe "prohibited" do
- it "rejects field with any value" do
- type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:prohibited))
-
- expect{ type.coerce(a: 123, b: true) }.to raise_error(StrongJSON::Type::Error)
+ describe "ignored" do
+ it "ignores field with any value" do
+ type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric), b: StrongJSON::Type::Base.new(:ignored))
+ expect(type.coerce(a: 123, b: true)).to eq(a: 123)
end
it "accepts if it does not contains the field" do
- type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:prohibited))
-
- expect(type.coerce(b: true)).to eq({})
+ type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric), b: StrongJSON::Type::Base.new(:ignored))
+ expect(type.coerce(a: 123)).to eq(a: 123)
end
end
it "rejects objects with missing fields" do
type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric))
@@ -36,11 +34,11 @@
end
it "accepts missing field if optional" do
type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Optional.new(StrongJSON::Type::Base.new(:numeric)))
- expect(type.coerce(b: "test")).to eq({})
+ expect(type.coerce({})).to eq({})
end
end
describe "#merge" do
let (:type) { StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric)) }
@@ -61,10 +59,9 @@
describe "#except" do
let (:type) { StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric), b: StrongJSON::Type::Base.new(:string)) }
it "return object which ignores given fields but preserve others" do
ty2 = type.except(:a)
-
- expect(ty2.coerce(a: 123, b: "test")).to eq({ b: "test" })
+ expect(ty2.coerce(b: "test")).to eq({ b: "test" })
end
end
end