From bcb37e62c7f9f4d7b2c387dc48b9a1e53b5a1fe9 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Thu, 6 Jan 2011 10:11:15 -0500 Subject: [PATCH] RUBY-222 BSON::Code must be a string. --- lib/bson/types/code.rb | 4 ++++ test/bson/bson_test.rb | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/lib/bson/types/code.rb b/lib/bson/types/code.rb index f67b5cb..db226ee 100644 --- a/lib/bson/types/code.rb +++ b/lib/bson/types/code.rb @@ -32,6 +32,10 @@ module BSON def initialize(code, scope={}) @code = code @scope = scope + + unless @code.is_a?(String) + raise ArgumentError, "BSON::Code must be in the form of a String; #{@code.class} is not allowed." + end end def length diff --git a/test/bson/bson_test.rb b/test/bson/bson_test.rb index ee24f93..2b3a7a6 100644 --- a/test/bson/bson_test.rb +++ b/test/bson/bson_test.rb @@ -150,6 +150,12 @@ class BSONTest < Test::Unit::TestCase assert_doc_pass(doc) end + def test_code_with_symbol + assert_raise_error ArgumentError, "BSON::Code must be in the form of a String" do + Code.new(:fubar) + end + end + def test_code_with_scope doc = {'$where' => Code.new('this.a.b < this.b', {'foo' => 1})} assert_doc_pass(doc)