2 Unit tests for the osrf.json module
6 sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
8 import osrf.json, osrf.net_obj, unittest
10 class TestObject(object):
11 """Test object with basic JSON structures"""
15 self.array = [1,2,3,4]
16 self.dict = {'foo': 'bar', 'key': 'value'}
21 class CheckObjectToJSON(unittest.TestCase):
22 """Tests the osrf.json.to_json() method that converts Python objects into JSON"""
24 self.testo = TestObject()
27 test_json = osrf.json.to_json(self.testo.int)
28 self.assertEqual(test_json, '1')
30 def test_string(self):
31 test_json = osrf.json.to_json(self.testo.string)
32 self.assertEqual(test_json, '"two"')
35 test_json = osrf.json.to_json(self.testo.array)
36 self.assertEqual(test_json, '[1, 2, 3, 4]')
39 test_json = osrf.json.to_json(self.testo.dict)
40 self.assertEqual(test_json, '{"foo": "bar", "key": "value"}')
43 test_json = osrf.json.to_json(self.testo.true)
44 self.assertEqual(test_json, 'true')
47 test_json = osrf.json.to_json(self.testo.false)
48 self.assertEqual(test_json, 'false')
51 test_json = osrf.json.to_json(self.testo.null)
52 self.assertEqual(test_json, 'null')
54 class CheckJSONToObject(unittest.TestCase):
55 """Tests that the osrf.json.to_object() method converts JSON into Python objects"""
58 self.testo = TestObject()
61 test_json = osrf.json.to_object('1')
62 self.assertEqual(test_json, self.testo.int)
64 def test_string(self):
65 test_json = osrf.json.to_object('"two"')
66 self.assertEqual(test_json, self.testo.string)
69 test_json = osrf.json.to_object('[1, 2, 3, 4]')
70 self.assertEqual(test_json, self.testo.array)
73 test_json = osrf.json.to_object('{"foo": "bar", "key": "value"}')
74 self.assertEqual(test_json, self.testo.dict)
77 test_json = osrf.json.to_object('true')
78 self.assertEqual(test_json, self.testo.true)
81 test_json = osrf.json.to_object('false')
82 self.assertEqual(test_json, self.testo.false)
85 test_json = osrf.json.to_object('null')
86 self.assertEqual(test_json, self.testo.null)
88 if __name__ == '__main__':