require 'xing/specdoc/document' describe Xing::SpecDoc::Document do let :path do "whatever_path" end subject :doccer do Xing::SpecDoc::Document.new(path, response_json) end let :our_hash do { "number" => 127, "string" => "a string", "string_list" => [ "a", "b", "c" ], "number_list" => [ 1, 2, 3 ], "object" => { "number" => 127, "string" => "a string", "string_list" => [ "a", "b", "c" ], "number_list" => [ 1, 2, 3 ] } } end let :other_hash do our_hash.dup end let :response_json do our_hash.to_json end context "with an empty content" do it "should work okay" do doc = Xing::SpecDoc::Document.new("somewhere", "") expect(doc.parsed_body).to eq({}) expect(doc.pretty_body).to eq("") end end context "should not register difference" do let :is_diff do false end it "from an identical JSON" do expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "from changed number values" do other_hash["number"] = 321 expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "from changed string values" do other_hash["string"] = "another string" expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "from changed list values" do other_hash["string_list"] = [ "z","y","x" ] other_hash["number_list"] = [ 4,5,6 ] expect(doccer.different_from?(other_hash)).to eq(is_diff) end end context "should register differences" do let :is_diff do true end it "when string changes to number" do other_hash["string"] = 1234 expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "when number changes to string" do other_hash["number"] = "tuesday" expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "when a field is removed" do other_hash.delete("string") expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "when a field is added" do other_hash["new_string"] = "hello" expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "when a list shortens" do other_hash["string_list"] = our_hash["string_list"][0..-2] expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "when a list lengthens" do other_hash["number_list"] = our_hash["number_list"] + [5] expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "when a member of list changes from number to string" do other_hash["number_list"] = our_hash["number_list"][0..-2] + ["string"] expect(doccer.different_from?(other_hash)).to eq(is_diff) end it "when a member of list changes from string to number" do other_hash["string_list"] = our_hash["string_list"][0..-2] + [37] expect(doccer.different_from?(other_hash)).to eq(is_diff) end end end