def check(s, d): ''' check basic operation of JsonParser''' # load and dump jp = JsonParser() jp.load(s) assert_eq(jp.dumpDict(), d) assert_eq(jp.dump(), s) # load and dump by another JsonParser jp2 = JsonParser() jp2.load(jp.dump()) assert_eq(jp2.dumpDict(), jp.dumpDict()) assert_eq(jp2.dump(), jp.dump()) # check update jp3 = JsonParser() jp3.update(jp.dumpDict()) assert_eq(jp3.dumpDict(), jp.dumpDict()) assert_eq(jp3.dump(), jp.dump()) # check file operation f = 'temp_for_test' jp.dumpJson(f) jp4 = JsonParser() jp4.loadJson(f) os.remove(f) assert_eq(jp4.dumpDict(), jp.dumpDict()) assert_eq(jp4.dump(), jp.dump())
def test_basic(self): a1 = JsonParser() a2 = JsonParser() a3 = JsonParser() a1.load(test_json_str) d1 = a1.dumpDict() a2.loadDict(d1) a2.dumpJson('jsonfile.txt') a3.loadJson('jsonfile.txt') d3 = a3.dumpDict() self.assertEqual(d1, d3)
def test_main(self): file_path = "output1.json" a1 = JsonParser() a2 = JsonParser() a3 = JsonParser() # test_json_str、test_dict a1.load(JSON) d1 = a1.dumpDict() # 粗糙比较test_dict和d1 a2.loadDict(d1) a2.dumpJson(file_path) a3.loadJson(file_path) d3 = a3.dumpDict() # 比较d1和d3是否完全一样 self.assertEqual(d1, d3)
a1 = JsonParser() a2 = JsonParser() a3 = JsonParser() a1.load(test_json_str) print 'a1.dump():\n',a1.dump() d1=a1.dumpDict() print 'd1:\n',d1 print 'test_dict:\n',test_dict assert test_dict==d1 a2.loadDict(d1) assert id(a2["root"])!=id(d1["root"]) a2.dumpJson(file_path) a3.loadJson(file_path) d3 = a3.dumpDict() print 'd3:\n',d3 assert d1==d3 d4={"root":2,"a":3} a3.update(d4) a2["root"]=2 a2["a"]=3 print 'a3.dump():\n',a3.dump() print 'a2.dump():\n',a2.dump() a2["a"]=(1,2,3) print 'a2.dump():\n',a2.dump()
''' Created on Mar 2, 2013 @author: zhumeiqi ''' from JsonParser import JsonParser,JsonError if __name__ == '__main__': paser = JsonParser() paser2 = JsonParser() paser3 = JsonParser() paser4 = JsonParser() paser.loadJson('test.json') d1 = paser.dumpDict() paser2.loadDict(d1) paser2.dumpJson('test_r.json') paser3.loadJson('test_r.json') d3 = paser3.dumpDict() print paser.dump() print d1 print d3 pass
return False else: if l1[i] != l2[i]: print 'element:%s not equal to %s' % (l1[i], l2[i]) return False return result if __name__ == '__main__': a1 = JsonParser() a2 = JsonParser() a3 = JsonParser() a1.loadJson('./test') d1 = a1.dumpDict() print 'd1:', d1 a2.loadDict(d1) a2.dumpJson('./test1') a3.loadJson('./test1') d3 = a3.dumpDict() print 'd3:', d3 if compared(d1, d3): print 'All is OK!' a1[u'root'][8][u'integer'] = 9876543210 print a1[u'root'][8][u'integer'] myd = {u'root':u'root', u'leaf':u'leaf'} a1.update(myd) print a1.dump() print a1.dumpDict()
class TestCases(unittest.TestCase): def setUp(self): self.jp = JsonParser() def test_basic(self): a1 = JsonParser() a2 = JsonParser() a3 = JsonParser() a1.load(test_json_str) d1 = a1.dumpDict() a2.loadDict(d1) a2.dumpJson('jsonfile.txt') a3.loadJson('jsonfile.txt') d3 = a3.dumpDict() self.assertEqual(d1, d3) def test_load(self): self.jp.load(test_json_str) self.assertEqual(self.jp.dict, json.loads(test_json_str)) def test_dump(self): self.assertEqual(self.jp.dump(), json.dumps(self.jp.dict)) def test_loadJson(self): f = 'jsonfile.txt' self.jp.loadJson(f) with open(f, 'r+') as fb: self.assertEqual(self.jp.dict, json.load(fb)) def test_dumpJson(self): f = 'jsonfile1.txt' f2 = 'jsonfile2.txt' self.jp.load(test_json_str) self.jp.dumpJson(f) with open(f2, 'w+') as fb: json.dump(self.jp.dict, fb) self.assertEqual(True, filecmp.cmp(f, f2)) def test_loadDict(self): d1 = {'a': 1, 'b': ['c', 2, 3], 'd': {'e': True}} d2 = {'a': 1, False: 2, 3: 'd'} self.jp.loadDict(d1) self.assertEqual(d1, self.jp.dict) self.assertFalse(d1 is self.jp.dict) self.jp.loadDict(d2) for key in self.jp.dict.keys(): self.assertEqual(True, isinstance(key, str)) def test_dumpDict(self): d = self.jp.dumpDict() self.assertEqual(d, self.jp.dict) self.assertFalse(d is self.jp.dict) def test_getitem(self): d = {'test': 'getitem'} self.jp.loadDict(d) self.assertEqual(d['test'], self.jp['test']) def test_setitem(self): self.jp['test'] = 'getitem' dic = self.jp.dumpDict() self.assertEqual(dic['test'], 'getitem') def test_update(self): d1 = {'a': 1, 'b': 2} d2 = {'b': 3, 'c': 4} self.jp.loadDict(d1) self.assertEqual(self.jp.update(d2), d1.update(d2))
class JsonParserTest(unittest.TestCase): def setUp(self): self.jp = JsonParser() self.maxDiff = None def test_main(self): file_path = "output1.json" a1 = JsonParser() a2 = JsonParser() a3 = JsonParser() # test_json_str、test_dict a1.load(JSON) d1 = a1.dumpDict() # 粗糙比较test_dict和d1 a2.loadDict(d1) a2.dumpJson(file_path) a3.loadJson(file_path) d3 = a3.dumpDict() # 比较d1和d3是否完全一样 self.assertEqual(d1, d3) def test_load(self): slist = [r'''{}''', r'''{"abc":123}''', r'''{"abc":{"abc":{"abc":123}}}''', r'''{"abc":{"abc":{"abc":[123,234,567,null,false,true,[]]}}}'''] for s in slist: self.jp.load(s) self.assertEqual(self.jp.dictcontent, json.loads(s)) def test_dump(self): slist = [r'''{}''', r'''{"abc":123}''', r'''{"abc":{"abc":{"abc":123}}}''', r'''{"abc":{"abc":{"abc":[123,234,567,null,false,true,[]]}}}'''] for s in slist: self.jp.load(s) self.assertEqual(self.jp.dump(), s) def test_loadJson(self): self.jp.loadJson("JSON.txt") with open("JSON.txt") as f: content = f.read() self.assertEqual(self.jp.dictcontent, json.loads(content)) # 比较使用标准库和JsonParser加载相同字符串结果是否相同 def test_dumpJson(self): output = "output2.json" self.jp.load(JSON) self.jp.dumpJson(output) first = copy.deepcopy(self.jp.dictcontent) # 创建一个原字典的拷贝 with open(output) as f: content = f.read() self.jp.load(content) self.assertEqual(first, self.jp.dictcontent) def test_loadDict(self): d1 = {"abc": 123, 123: "abc", False: "test", None: 123} e1 = {"abc": 123} self.jp.loadDict(d1) self.assertEqual(e1, self.jp.dictcontent) def test_dumpDict(self): d1 = r'''{"abc": 123, "bcd": false, "cde": null}''' self.jp.load(d1) # dumpDict返回的字典跟原字典具有相同的键值,但是不是同一个对象 self.assertEqual(self.jp.dictcontent, self.jp.dumpDict()) self.assertFalse(self.jp.dictcontent is self.jp.dumpDict()) def test_getitem(self): d = {"abc": "cde", "cde": "abc"} self.jp.loadDict(d) key = "abc" value = "cde" self.assertEqual(value, self.jp[key]) def test_setitem(self): d = {"abc": "cde", "cde": "abc"} self.jp.loadDict(d) key = "abc" expected = "abcdefg" self.jp[key] = expected self.assertEqual(expected, self.jp[key]) def test_update(self): d = {"abc": "cde", "cde": "abc"} d1 = {"abc": "abc", "cde": "cde"} self.jp.loadDict(d) self.jp.update(d1) self.assertEqual("abc", self.jp["abc"]) self.assertEqual("cde", self.jp["cde"]) def test_deepdump(self): d0 = {"a": 123} d1 = {"b": d0} d2 = {"c": [d1]} d3 = self.jp.deepdump(d2) # 深拷贝对象 d3['c'][0]['b']['a'] = 234 # 修改拷贝后嵌套对象中的值 self.assertEqual(d2['c'][0]['b']['a'], 123) # 断言原本对象的值未改变 def test_getnumber(self): s1 = r'-0.123' e1 = -0.123 s2 = r'0.123456789E1' e2 = 1.23456789 s3 = r'1231235234234234' e3 = 1231235234234234L s4 = r'-0' e4 = 0 s5 = r'-0.0e+1' e5 = -0.0 self.assertEqual(self.jp.getnumber(s1)[0], e1) self.assertEqual(self.jp.getnumber(s2)[0], e2) self.assertEqual(self.jp.getnumber(s3)[0], e3) self.assertEqual(self.jp.getnumber(s4)[0], e4) self.assertEqual(self.jp.getnumber(s5)[0], e5) def test_getchar(self): c = r'\ude1c' expected = unichr(int('de1c', 16)) result, index = self.jp.getchar(c, 2) self.assertEqual(expected, result) def test_getstring(self): s0 = '"very simple"' e0 = 'very simple' s1 = r'''"`1~!@#$%^&*()_+-={':[,]}|;.</>?"''' e1 = r'''`1~!@#$%^&*()_+-={':[,]}|;.</>?''' self.assertEqual(e0, self.jp.getstring(s0[1:])[0]) self.assertEqual(e1, self.jp.getstring(s1[1:])[0]) def test_getarray(self): liststr = r'''{"key":[{"integer": 1234567890, "real": -9876.543210, "e": 0.123456789e-12, "E": 1.234567890E+34, "": 23456789012E6, "zero": 0, "one": 1, "space": " ", "quote": "\"", "backslash": "\\", "unicode":"\uedda", "controls": "\b\f\n\r\t"}]}''' self.jp.load(liststr) expected = json.loads(liststr) self.assertEqual(0, cmp(expected, self.jp.dictcontent)) def test_getobject(self): self.jp.load(JSON) expected = json.loads(JSON) self.assertEqual(0, cmp(expected, self.jp.dictcontent)) def test_final(self): for tub in finaljson: self.jp.load(tub[0]) assert self.jp.dictcontent[tub[1]] == tub[2] print tub[1], tub[2]