spec/object_spec.rb in strong_json-0.1.1 vs spec/object_spec.rb in strong_json-0.1.2

- old
+ new

@@ -30,14 +30,25 @@ it "rejects objects with missing fields" do type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric)) expect{ type.coerce(b: "test") }.to raise_error(StrongJSON::Type::Error) end + end + describe "optional" do 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({})).to eq({}) + end + + it "preserves if present" do + type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Optional.new(StrongJSON::Type::Base.new(:numeric))) + expect(type.coerce({ a: "-123" })).to eq({ a: "-123" }) + end + + it "preserves nil if present" do + type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Optional.new(StrongJSON::Type::Base.new(:numeric))) + expect(type.coerce({ a: nil })).to eq({ a: nil }) end end describe "#merge" do let (:type) { StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric)) }