spec/hash_spec.rb in nydp-0.1.0 vs spec/hash_spec.rb in nydp-0.1.1
- old
+ new
@@ -22,26 +22,29 @@
hash = Nydp::Hash.new
hash[sym "boo"] = 42
rhash = hash.to_ruby
expect(rhash[:boo]).to eq 42
+ expect(rhash.keys) .to eq [:boo]
end
it "converts ruby string key to nydp string key" do
hash = Nydp::Hash.new
hash[Nydp::StringAtom.new "boo"] = 42
rhash = hash.to_ruby
expect(rhash["boo"]).to eq 42
+ expect(rhash.keys) .to eq ["boo"]
end
it "uses integer keys unconverted" do
hash = Nydp::Hash.new
hash[21] = 42
rhash = hash.to_ruby
expect(rhash[21]).to eq 42
+ expect(rhash.keys) .to eq [21]
end
end
describe "hash merge" do
it "merges two hashes" do
@@ -90,10 +93,32 @@
Nydp::Builtin::HashGet.new(ns).invoke vm, args
expect(vm.pop_arg).to eq v
end
end
+ describe "key?" do
+ it "returns t when key is present" do
+ h = Nydp::Hash.new
+ k = sym "jerry"
+ v = 42
+ h[k] = v
+
+ Nydp::Builtin::HashKeyPresent.new(ns).invoke vm, pair_list([h, k])
+
+ expect(vm.pop_arg).to eq Nydp.T
+ end
+
+ it "returns nil when key is absent" do
+ h = Nydp::Hash.new
+ k = sym "benjamin"
+
+ Nydp::Builtin::HashKeyPresent.new(ns).invoke vm, pair_list([h, k])
+
+ expect(vm.pop_arg).to eq Nydp.NIL
+ end
+ end
+
describe "hash keys" do
it "returns a list of keys" do
h = Nydp::Hash.new
h[sym "k0"] = 42
h[sym "k1"] = 84
@@ -149,9 +174,30 @@
args = [ ahash, k ]
Nydp::Builtin::HashGet.new(ns).invoke vm, pair_list(args)
expect(vm.pop_arg).to eq Nydp.T
+ end
+ end
+
+ describe "key?" do
+ it "returns t when key is present" do
+ ahash[:simon] = 24
+ k = sym("simon")
+ args = [ ahash, k ]
+
+ Nydp::Builtin::HashKeyPresent.new(ns).invoke vm, pair_list(args)
+
+ expect(vm.pop_arg).to eq Nydp.T
+ end
+
+ it "returns nil when key is absent" do
+ k = sym("simon")
+ args = [ ahash, k ]
+
+ Nydp::Builtin::HashKeyPresent.new(ns).invoke vm, pair_list(args)
+
+ expect(vm.pop_arg).to eq Nydp.NIL
end
end
describe "hash keys" do
it "returns a list of keys" do