require "spec_helper" RSpec.describe Burlap::Hash do it "inherits from ordered hash" do expect(described_class.ancestors).to include(ActiveSupport::OrderedHash) end it { is_expected.to respond_to(:__type__) } it { is_expected.to respond_to(:__type__=) } describe "#__type__" do it "defaults to an empty string" do expect(described_class.new.__type__).to eq("") end end describe "#[]" do subject(:burlap_hash) { described_class[input] } context "when input is a Hash" do let(:input) { {} } it "is compatible with Hash#[]" do expect(burlap_hash.keys).to eq([]) expect(burlap_hash.values).to eq([]) end it "is empty string" do expect(burlap_hash.__type__).to eq("") end end context "when one argument is given" do let(:input) { { "one" => "two", "three" => "four" } } it "is compatible with Hash#[]" do expect(burlap_hash.keys).to eq(%w[one three]) expect(burlap_hash.values).to eq(%w[two four]) end it "is empty string" do expect(burlap_hash.__type__).to eq("") end end context "when a __type__ is passed" do let(:input) { { some: "options", __type__: "is ignored" } } it "is compatible with Hash#[]" do expect(burlap_hash.keys).to eq(%i[some __type__]) expect(burlap_hash.values).to eq(["options", "is ignored"]) end it "is empty string" do expect(burlap_hash.__type__).to eq("") end end context "when a second argument sets type directly" do subject(:burlap_hash) { described_class[input, "com.java.hashmap"] } context "without a passed __type__" do let(:input) { { some: "options" } } it "is compatible with Hash#[]" do expect(burlap_hash.keys).to eq([:some]) expect(burlap_hash.values).to eq(["options"]) end it "sets __type__ directly" do expect(burlap_hash.__type__).to eq("com.java.hashmap") end end context "with a passed __type__" do let(:input) { { some: "options", __type__: "is ignored" } } it "is compatible with Hash#[]" do expect(burlap_hash.keys).to eq(%i[some __type__]) expect(burlap_hash.values).to eq(["options", "is ignored"]) end it "sets __type__ directly" do expect(burlap_hash.__type__).to eq("com.java.hashmap") end end end end describe "#inspect" do subject(:burlap_hash) { described_class[[%w[one two], [:three, 4]], "Fred"] } it "does not contain OrderedHash" do expect(burlap_hash.inspect).not_to include("OrderedHash") end it "starts with Burlap::Hash" do expect(burlap_hash.inspect).to match(/#"two", :three=>4})) end it "contains the type, inspected" do expect(burlap_hash.inspect).to include(%{__type__="Fred"}) end end describe "#to_burlap" do subject(:burlap) { burlap_hash.to_burlap } let(:burlap_hash) { described_class[{ value: "something" }, "org.ruby-lang.string"] } context "wrapping a string" do # # org.ruby-lang.string # value # something # it "returns a string" do expect(burlap).to be_a_kind_of(String) end it "has a map root" do expect(burlap).to match(/^/) expect(burlap).to match(%r{$}) end it "has a nested type node" do expect(burlap).to match(%r{org.ruby-lang.string}) end it "has nested value nodes" do expect(burlap).to match(%r{value}) expect(burlap).to match(%r{something}) end end context "wrapping multiple keys" do let(:burlap_hash) { described_class[[["name", "caius durling"], ["age", 101]], "burlap.user"] } before do @doc = Nokogiri::XML(burlap) end it "returns a string" do expect(burlap).to be_a_kind_of(String) end it "is correct" do xml_string = <<-XML burlap.user name caius durling age 101 XML format_xml_as_burlap(xml_string) expect(burlap).to eq(xml_string) end it "has a type element" do expect(burlap).to match(%r{burlap.user}) end it "has a name keypair" do expect(burlap).to match(%r{namecaius durling}) end it "has an age keypair" do expect(burlap).to match(%r{age101}) end end context "wrapping nested keys" do let(:burlap_hash) { described_class[[["people", { "name" => "caius durling", "age" => 101 }]], "burlap-peoples"] } it "returns a string" do expect(burlap).to be_a_kind_of(String) end it "is generated properly, including nested elements" do xml_string = <<-XML burlap-peoples people name caius durling age 101 XML xml_string.gsub!(/(^|\n)\s*/m, "") expect(burlap).to eq(xml_string) end end context "wrapping empty arrays" do let(:burlap_hash) { described_class[{ numbers: [] }] } it "returns a string" do expect(burlap).to be_a_kind_of(String) end it "is generated properly" do xml_string = <<-XML numbers 0 XML xml_string.gsub!(/(^|\n)\s*/m, "") expect(burlap).to eq(xml_string) end end end end