from unittest import TestCase, TestSuite, main, makeSuite import minjson as json #import jsonnew as json ## jsontest.py implements tests for the json.py JSON ## (http://json.org) reader and writer. ## Copyright (C) 2005 Patrick D. Logan ## Contact mailto:patrickdlogan@stardecisions.com ## ## This library is free software; you can redistribute it and/or ## modify it under the terms of the GNU Lesser General Public ## License as published by the Free Software Foundation; either ## version 2.1 of the License, or (at your option) any later version. ## ## This library is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## Lesser General Public License for more details.= ## ## You should have received a copy of the GNU Lesser General Public ## License along with this library; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # The object tests should be order-independent. They're not. # i.e. they should test for existence of keys and values # with read/write invariance. class JsonTest(TestCase): def testReadEmptyObject(self): obj = json.read("{}") self.assertEqual({}, obj) def testWriteEmptyObject(self): s = json.write({}) self.assertEqual("{}", s) def testReadStringValue(self): obj = json.read('{ "name" : "Patrick" }') self.assertEqual({ "name" : "Patrick" }, obj) def testReadEscapedQuotationMark(self): obj = json.read(r'"\""') #self.assertEqual(r'"', obj) def testReadEscapedSolidus(self): obj = json.read(r'"\/"') #self.assertEqual(r'/', obj) def testReadEscapedReverseSolidus(self): obj = json.read(r'"\\"') self.assertEqual("\\", obj) def testReadEscapedBackspace(self): obj = json.read(r'"\b"') self.assertEqual("\b", obj) def testReadEscapedFormfeed(self): obj = json.read(r'"\f"') self.assertEqual("\f", obj) def testReadEscapedNewline(self): obj = json.read(r'"\n"') self.assertEqual("\n", obj) def testReadEscapedCarriageReturn(self): obj = json.read(r'"\r"') self.assertEqual("\r", obj) def testReadEscapedHorizontalTab(self): obj = json.read(r'"\t"') self.assertEqual("\t", obj) def testReadEscapedHexCharacter(self): obj = json.read(r'"\u000A"') #self.assertEqual("\n", obj) obj = json.read(r'"\u1001"') #self.assertEqual(u'\u1001', obj) #def testReadBadEscapedHexCharacter(self): # self.assertRaises(UnicodeDecodeError, self.doReadBadEscapedHexCharacter) #self.doReadBadEscapedHexCharacter() #do we really care about this? #def doReadBadEscapedHexCharacter(self): # json.read('"\u10K5"') def testReadBadObjectKey(self): self.assertRaises(SyntaxError, self.doReadBadObjectKey) def doReadBadObjectKey(self): json.read('{ "44 : "age" }') def testReadDoubleSolidusComment(self): obj = json.read("[1, 2, // This is a comment.\n 3]") self.assertEqual([1, 2, 3], obj) obj = json.read('[1, 2, // This is a comment.\n{"last":3}]') self.assertEqual([1, 2, {"last":3}], obj) def testReadBadDoubleSolidusComment(self): self.assertRaises(SyntaxError, self.doReadBadDoubleSolidusComment) def doReadBadDoubleSolidusComment(self): json.read("[1, 2, / This is not a comment.\n 3]") def testReadCStyleComment(self): obj = json.read("[1, 2, /* This is a comment. \n */ 3]") self.assertEqual([1, 2, 3], obj) obj = json.read('[1, 2, /* This is a comment. */{"last":3}]') self.assertEqual([1, 2, {"last":3}], obj) def testReadCStyleCommentWithoutEnd(self): self.assertRaises(SyntaxError, self.doReadCStyleCommentWithoutEnd) def testReadCStyleCommentWithSlashStar(self): obj = self.doReadCStyleCommentWithSlashStar() self.assertEqual([1, 2, 3], obj) def doReadCStyleCommentWithoutEnd(self): json.read("[1, 2, /* This is not a comment./ 3]") def doReadCStyleCommentWithSlashStar(self): return json.read("[1, 2, /* This is not a comment./* */ 3]") def testReadBadObjectSyntax(self): self.assertRaises(SyntaxError, self.doReadBadObjectSyntax) def doReadBadObjectSyntax(self): json.read('{"age", 44}') def testWriteStringValue(self): s = json.write({ 'name' : 'Patrick' }) self.assertEqual("{'name': 'Patrick'}", s) def testReadIntegerValue(self): obj = json.read('{ "age" : 44 }') self.assertEqual({ "age" : 44 }, obj) def testReadNegativeIntegerValue(self): obj = json.read('{ "key" : -44 }') self.assertEqual({ "key" : -44 }, obj) def testReadFloatValue(self): obj = json.read('{ "age" : 44.5 }') self.assertEqual({ "age" : 44.5 }, obj) def testReadNegativeFloatValue(self): obj = json.read(' { "key" : -44.5 } ') self.assertEqual({ "key" : -44.5 }, obj) def testReadBadNumber(self): self.assertRaises(SyntaxError, self.doReadBadNumber) def doReadBadNumber(self): json.read('-44.4.4') def testReadSmallObject(self): obj = json.read('{ "name" : \'Patrick\', "age":44} ') self.assertEqual({ "age" : 44, "name" : "Patrick" }, obj) def testReadEmptyArray(self): obj = json.read('[]') self.assertEqual([], obj) def testWriteEmptyArray(self): self.assertEqual("[]", json.write([])) def testReadSmallArray(self): obj = json.read(' [ "a" , "b", "c" ] ') self.assertEqual(["a", "b", "c"], obj) def testWriteSmallArray(self): self.assertEqual('[1, 2, 3, 4]', json.write([1, 2, 3, 4])) def testWriteSmallObject(self): s = json.write({ "name" : "Patrick", "age": 44 }) self.assertEqual("{'age': 44, 'name': 'Patrick'}", s) def testWriteFloat(self): self.assertEqual("3.445567", json.write(3.445567)) def testWriteLong(self): self.assertEqual("12345678901234567890",json.write(12345678901234567890)) def testReadNegativeLongValue(self): obj = json.read('{ "key" : -44556677889900112233 }') self.assertEqual({ "key" : -44556677889900112233 }, obj) def testReadTrue(self): self.assertEqual(json.read("true"),True) def testReadFalse(self): self.assertEqual(json.read("false"),False) def testReadNull(self): self.assertEqual(None, json.read("null")) def testWriteTrue(self): self.assertEqual("true", json.write(True)) def testWriteFalse(self): self.assertEqual("false", json.write(False)) def testReadEmptyArrayAtEndOfObject(self): self.assertEqual({"a":"a","b":"b","c":[]}, json.read('{"a":"a","b":"b","c":[]}')) def testReadObjectAtEndOfArray(self): self.assertEqual(["a","b","c",{"a":"a","b":"b"}], json.read('["a","b","c",{"a":"a","b":"b"} ]')) def testReadEmptyObjectAtEndOfArray(self): self.assertEqual(["a","b","c",{}], json.read('["a","b","c",{}]')) def testReadEmptyArrayAtEndOfArray(self): self.assertEqual(["a","b",[]], json.read('["a","b",[]]')) def testReadEmptyObjectAtEndOfObject(self): self.assertEqual({"a":"a","b":"b","c":{}}, json.read('{"a":"a","b":"b","c":{}}')) #The following 3 yield a syntax error in minjson #def testWriteAndReadComplexString(self): # obj = 'she said, "isn\'t this great?"' # print # print obj # obj = str(obj) # w = json.write(obj) # print w # r = json.read(w) # self.assertEqual(obj,str(r)) #def testWriteAndReadTripleQuotedString(self): # obj = """ Mary said, "Hi, show me a 'good time!'" # To which Bob replied, "Sure!" # """ # w = json.write(obj) # print str(w) # r = json.read(obj) # self.assertEqual(r , obj) #def testWriteAndReadUnicode(self): # obj = u'La Pe\xf1a' # w = json.write(obj) # print # print w # print '_________________________' # r = json.read(w) # print r # self.assertEqual(r,obj) def testWriteNull(self): self.assertEqual("null", json.write(None)) def testWriteUnicode(self): self.assertEqual(u'test', json.write(u'test')) def testReadArrayOfSymbols(self): self.assertEqual([True, False, None], json.read(" [ true, false,null] ")) def testWriteArrayOfSymbols(self): self.assertEqual("[true, false, null]", json.write([True, False, None])) def testReadArray2(self): self.assertEqual(json.read('{"a":[1,2,3]}'),{'a':[1,2,3]}) def testReadComplexObject(self): src = ''' { "name": "Patrick", "age" : 44, "Employed?" : true, "Female?" : false, "grandchildren":null } ''' obj = json.read(src) self.assertEqual({"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None}, obj) def testReadSingleQuotedDictKeys(self): src = "{'id':'HttpReq','params':[1],'method':'action'}" obj = json.read(src) def testReadSingleQuotedStrings(self): src = "'hello'" obj = json.read(src) def testReadLongArray(self): src = '''[ "used", "abused", "confused", true, false, null, 1, 2, [3, 4, 5] ] ''' obj = json.read(src) self.assertEqual(["used","abused","confused", True, False, None, 1,2,[3,4,5]], obj) def testReadComplexArray(self): src = ''' [ { "name": "Patrick", "age" : 44, "Employed?" : true, "Female?" : false, "grandchildren":null }, "used", "abused", "confused", 1, 2, [3, 4, 5] ] ''' obj = json.read(src) self.assertEqual([{"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None}, "used","abused","confused", 1,2,[3,4,5]], obj) def testWriteComplexArray(self): obj = [{"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None}, "used","abused","confused", 1,2,[3,4,5]] self.assertEqual("[{'Female?': false, 'age': 44, 'name': 'Patrick', 'grandchildren': null, 'Employed?': true}, 'used', 'abused', 'confused', 1, 2, [3, 4, 5]]", json.write(obj)) def test_suite(): return TestSuite(( makeSuite(JsonTest), )) if __name__ == '__main__': main(defaultTest = 'test_suite')