From 76730d4a7cfc21ad54a99b6d4f985cd2bb19a272 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Fri, 4 Feb 2011 12:07:28 -0500 Subject: [PATCH] RUBY-233 show invalid key on invalid key exception --- ext/cbson/cbson.c | 4 ++-- test/bson/bson_test.rb | 43 ++++++++++++++++++++++++++++++++++++++++++ test/db_api_test.rb | 37 ------------------------------------ 3 files changed, 45 insertions(+), 39 deletions(-) diff --git a/ext/cbson/cbson.c b/ext/cbson/cbson.c index c89868fc97..9791d7afac 100644 --- a/ext/cbson/cbson.c +++ b/ext/cbson/cbson.c @@ -231,12 +231,12 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) { int i; if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') { buffer_free(buffer); - rb_raise(InvalidKeyName, "key must not start with '$'"); + rb_raise(InvalidKeyName, "%s - key must not start with '$'", RSTRING_PTR(key)); } for (i = 0; i < RSTRING_LEN(key); i++) { if (RSTRING_PTR(key)[i] == '.') { buffer_free(buffer); - rb_raise(InvalidKeyName, "key must not contain '.'"); + rb_raise(InvalidKeyName, "%s - key must not contain '.'", RSTRING_PTR(key)); } } } diff --git a/test/bson/bson_test.rb b/test/bson/bson_test.rb index 2b3a7a674a..bf652d7417 100644 --- a/test/bson/bson_test.rb +++ b/test/bson/bson_test.rb @@ -562,4 +562,47 @@ def test_move_id_with_nested_doc end end + def test_invalid_key_names + assert @encoder.serialize({"hello" => "world"}, true) + assert @encoder.serialize({"hello" => {"hello" => "world"}}, true) + + assert @encoder.serialize({"he$llo" => "world"}, true) + assert @encoder.serialize({"hello" => {"hell$o" => "world"}}, true) + + assert_raise BSON::InvalidDocument do + @encoder.serialize({"he\0llo" => "world"}, true) + end + + assert_raise_error BSON::InvalidKeyName, "$hello" do + @encoder.serialize({"$hello" => "world"}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello" => {"$hello" => "world"}}, true) + end + + assert_raise_error BSON::InvalidKeyName, ".hello" do + @encoder.serialize({".hello" => "world"}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello" => {".hello" => "world"}}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello." => "world"}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello" => {"hello." => "world"}}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hel.lo" => "world"}, true) + end + + assert_raise BSON::InvalidKeyName do + @encoder.serialize({"hello" => {"hel.lo" => "world"}}, true) + end + end end diff --git a/test/db_api_test.rb b/test/db_api_test.rb index ecb5fb5287..d1688a37f1 100644 --- a/test/db_api_test.rb +++ b/test/db_api_test.rb @@ -621,43 +621,6 @@ def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection assert_equal("mike", @@coll.find_one()["hello"]) end - def test_invalid_key_names - @@coll.remove - - @@coll.insert({"hello" => "world"}) - @@coll.insert({"hello" => {"hello" => "world"}}) - - assert_raise BSON::InvalidKeyName do - @@coll.insert({"$hello" => "world"}) - end - - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello" => {"$hello" => "world"}}) - end - - @@coll.insert({"he$llo" => "world"}) - @@coll.insert({"hello" => {"hell$o" => "world"}}) - - assert_raise BSON::InvalidKeyName do - @@coll.insert({".hello" => "world"}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello" => {".hello" => "world"}}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello." => "world"}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello" => {"hello." => "world"}}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hel.lo" => "world"}) - end - assert_raise BSON::InvalidKeyName do - @@coll.insert({"hello" => {"hel.lo" => "world"}}) - end - end - def test_collection_names assert_raise TypeError do @@db.collection(5)