00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """Tests for the Code wrapper."""
00016
00017 import unittest
00018 import sys
00019 sys.path[0:0] = [""]
00020
00021 from bson.code import Code
00022
00023
00024 class TestCode(unittest.TestCase):
00025
00026 def setUp(self):
00027 pass
00028
00029 def test_types(self):
00030 self.assertRaises(TypeError, Code, 5)
00031 self.assertRaises(TypeError, Code, None)
00032 self.assertRaises(TypeError, Code, "aoeu", 5)
00033 self.assertRaises(TypeError, Code, u"aoeu", 5)
00034 self.assert_(Code("aoeu"))
00035 self.assert_(Code(u"aoeu"))
00036 self.assert_(Code("aoeu", {}))
00037 self.assert_(Code(u"aoeu", {}))
00038
00039 def test_read_only(self):
00040 c = Code("blah")
00041
00042 def set_c():
00043 c.scope = 5
00044 self.assertRaises(AttributeError, set_c)
00045
00046 def test_code(self):
00047 a_string = "hello world"
00048 a_code = Code("hello world")
00049 self.assert_(a_code.startswith("hello"))
00050 self.assert_(a_code.endswith("world"))
00051 self.assert_(isinstance(a_code, Code))
00052 self.assertFalse(isinstance(a_string, Code))
00053 self.assertEqual(a_code.scope, {})
00054 a_code.scope["my_var"] = 5
00055 self.assertEqual(a_code.scope, {"my_var": 5})
00056
00057 def test_repr(self):
00058 c = Code("hello world")
00059 self.assertEqual(repr(c), "Code('hello world', {})")
00060 c.scope["foo"] = "bar"
00061 self.assertEqual(repr(c), "Code('hello world', {'foo': 'bar'})")
00062 c = Code("hello world", {"blah": 3})
00063 self.assertEqual(repr(c), "Code('hello world', {'blah': 3})")
00064 c = Code("\x08\xFF")
00065 self.assertEqual(repr(c), "Code('\\x08\\xff', {})")
00066
00067 def test_equality(self):
00068 b = Code("hello")
00069 c = Code("hello", {"foo": 5})
00070 self.assertNotEqual(b, c)
00071 self.assertEqual(c, Code("hello", {"foo": 5}))
00072 self.assertNotEqual(c, Code("hello", {"foo": 6}))
00073 self.assertEqual(b, Code("hello"))
00074 self.assertEqual(b, Code("hello", {}))
00075 self.assertNotEqual(b, Code("hello "))
00076 self.assertNotEqual("hello", Code("hello"))
00077
00078 def test_scope_preserved(self):
00079 a = Code("hello")
00080 b = Code("hello", {"foo": 5})
00081
00082 self.assertEqual(a, Code(a))
00083 self.assertEqual(b, Code(b))
00084 self.assertNotEqual(a, Code(b))
00085 self.assertNotEqual(b, Code(a))
00086
00087 def test_scope_kwargs(self):
00088 self.assertEqual({"a": 1}, Code("", a=1).scope)
00089 self.assertEqual({"a": 1}, Code("", {"a": 2}, a=1).scope)
00090 self.assertEqual({"a": 1, "b": 2, "c": 3},
00091 Code("", {"b": 2}, a=1, c=3).scope)
00092
00093
00094 if __name__ == "__main__":
00095 unittest.main()