spec/lazier/hash_spec.rb in lazier-3.3.10 vs spec/lazier/hash_spec.rb in lazier-3.4.0

- old
+ new

@@ -36,18 +36,68 @@ expect(reference.b.c).to eq(2) expect(reference["b"]["f"]).to eq(4) end end + describe "#compact" do + it "should remove blank keys" do + expect({a: 1, b: nil}.compact).to eq({a: 1}) + end + + it "should use a custom validator" do + expect({a: 1, b: nil, c: 3}.compact {|k, v| v == 1 || k == :c}).to eq({b: nil}) + end + + it "should not be destructive" do + reference = {a: 1, b: nil} + reference.compact + expect(reference).to eq({a: 1, b: nil}) + end + end + + describe "#compact!" do + it "should remove blank keys" do + reference = {a: 1, b: nil} + reference.compact! + expect(reference).to eq({a: 1}) + end + + it "should use a custom validator" do + reference = {a: 1, b: nil, c: 3} + reference.compact! {|k, v| v == 1 || k == :c} + expect(reference).to eq({b: nil}) + end + end + describe "#ensure_access" do it "should make sure that the requested access is granted" do expect({"a" => "b"}.ensure_access(:strings)).to eq({"a" => "b"}) expect({"a" => "b"}.ensure_access(:symbols)).to eq({a: "b"}) expect({"a" => "b"}.ensure_access(:indifferent)).to be_a(::HashWithIndifferentAccess) expect({"a" => "b"}.ensure_access(:other)).to eq({"a" => "b"}) expect({a: "b"}.ensure_access(:strings)).to eq({"a" => "b"}) expect({a: "b"}.ensure_access(:symbols)).to eq({a: "b"}) expect({a: "b"}.ensure_access(:indifferent)).to be_a(::HashWithIndifferentAccess) expect({a: "b"}.ensure_access(:other)).to eq({a: "b"}) + + reference.ensure_access(:dotted) + expect(reference.a).to eq(1) + expect(reference.b.c).to eq(2) + end + end + + describe "#with_dotted_access" do + it "should recursively enable dotted access on a hash" do + reference = {a: 1, b: {c: 3}, c: [1, {f: {g: 1}}]} + + reference.enable_dotted_access + expect(reference.b.c).to eq(3) + expect(reference.c[1].f.g).to eq(1) + end + + it "should also provide write access if asked" do + reference.enable_dotted_access(false) + expect { reference.b.f = 4 }.not_to raise_error + expect(reference["b"]["f"]).to eq(4) end end end \ No newline at end of file