def test_parsing_boolean(self): x = JsonParser.parse("true") self.assertEquals(x, True) x = JsonParser.parse("false") self.assertEquals(x, False) self.assertRaises(JsonParseException, JsonParser.parse, "tuto") self.assertRaises(JsonParseException, JsonParser.parse, "foo")
def test_parsing_comment(self): x = JsonParser.parse(" // Hi there\n 45") self.assertEquals(x, 45) x = JsonParser.parse(" /* Hi there */ 45") self.assertEquals(x, 45) self.assertRaises(JsonParseException, JsonParser.parse, "/* Unterminated comment") self.assertRaises(JsonParseException, JsonParser.parse, "/ Hi there")
def test_parsing_array(self): x = JsonParser.parse("[1, 2, 3]") self.assertEquals(len(x), 3) self.assertEquals(x[0], 1) self.assertEquals(x[1], 2) self.assertEquals(x[2], 3) self.assertRaises(JsonParseException, JsonParser.parse, "[ 1, 2,") self.assertRaises(JsonParseException, JsonParser.parse, "[ 1, 2 3]") x = JsonParser.parse("[1, 2\n 3]") self.assertEquals(len(x), 3)
def test_parsing_numbers(self): x = JsonParser.parse("123") self.assertEquals(x, 123) x = JsonParser.parse("-10") self.assertEquals(x, -10) x = JsonParser.parse("-10.5") self.assertEquals(x, -10.5) x = JsonParser.parse("12345678901234567890") self.assertEquals(x, 12345678901234567890L) self.assertRaises(JsonParseException, JsonParser.parse, "1..e")
def test_inferring_missing_comma(self): x = JsonParser.parse("""{ a: 5 b: 7 }""") self.assertEquals(len(x), 2) self.assertEquals(x.get("a"), 5) self.assertEquals(x.get("b"), 7)
def test_parsing_object(self): x = JsonParser.parse("{ a: 5}") self.assertEquals(len(x), 1) self.assertEquals(x.get("a"), 5) x = JsonParser.parse("{ a: 5, b:3, c:true}") self.assertEquals(len(x), 3) self.assertEquals(x.get("a"), 5) self.assertEquals(x.get("b"), 3) self.assertEquals(x.get("c"), True) x = JsonParser.parse('{ "a": 5}') self.assertEquals(len(x), 1) self.assertEquals(x.get("a"), 5) self.assertRaises(JsonParseException, JsonParser.parse, "{ a 5}") self.assertRaises(JsonParseException, JsonParser.parse, "{ a: 5") self.assertRaises(JsonParseException, JsonParser.parse, "{ a")
def test_parsing_strings(self): x = JsonParser.parse('"Hi there"') self.assertEquals(x, "Hi there") x = JsonParser.parse('"Hi there" \n + " Bye there"') self.assertEquals(x, "Hi there Bye there") x = JsonParser.parse('"Hi there\\n"') self.assertEquals(x, "Hi there\n") x = JsonParser.parse('"Hi there\\ua000"') self.assertEquals(x, u"Hi there\ua000") self.assertRaises(JsonParseException, JsonParser.parse, '"Hi there') self.assertRaises(JsonParseException, JsonParser.parse, '"Hi there \n ok bye "') self.assertRaises(JsonParseException, JsonParser.parse, '"Hi there \\') self.assertRaises(JsonParseException, JsonParser.parse, '"Hi there" + Hi')
def test_parsing_strings(self): x = JsonParser.parse("\"Hi there\"") self.assertEquals(x, "Hi there") x = JsonParser.parse("\"Hi there\" \n + \" Bye there\"") self.assertEquals(x, "Hi there Bye there") x = JsonParser.parse("\"Hi there\\n\"") self.assertEquals(x, "Hi there\n") x = JsonParser.parse("\"Hi there\\ua000\"") self.assertEquals(x, u"Hi there\ua000") self.assertRaises(JsonParseException, JsonParser.parse, "\"Hi there") self.assertRaises(JsonParseException, JsonParser.parse, "\"Hi there \n ok bye \"") self.assertRaises(JsonParseException, JsonParser.parse, "\"Hi there \\") self.assertRaises(JsonParseException, JsonParser.parse, "\"Hi there\" + Hi")
def test_parsing_object(self): x = JsonParser.parse("{ a: 5}") self.assertEquals(len(x), 1) self.assertEquals(x.get("a"), 5) x = JsonParser.parse("{ a: 5, b:3, c:true}") self.assertEquals(len(x), 3) self.assertEquals(x.get("a"), 5) self.assertEquals(x.get("b"), 3) self.assertEquals(x.get("c"), True) x = JsonParser.parse("{ \"a\": 5}") self.assertEquals(len(x), 1) self.assertEquals(x.get("a"), 5) self.assertRaises(JsonParseException, JsonParser.parse, "{ a 5}") self.assertRaises(JsonParseException, JsonParser.parse, "{ a: 5") self.assertRaises(JsonParseException, JsonParser.parse, "{ a")
def test_parse_from_bytes(self): self.assertEqual(JsonParser.parse(b"123"), 123) self.assertEqual(JsonParser.parse(b"-10.5"), -10.5) self.assertEqual( JsonParser.parse(b'"""Howdy\n"folks"!"""'), """Howdy\n"folks"!""" ) self.assertEqual(JsonParser.parse(b"true"), True) self.assertEqual(JsonParser.parse(b"null"), None) self.assertEqual(JsonParser.parse(b" // Hi there\n 45"), 45) x = JsonParser.parse(b"[1, 2, 3]") self.assertEquals(len(x), 3) self.assertEquals(x[0], 1) self.assertEquals(x[1], 2) self.assertEquals(x[2], 3) x = JsonParser.parse(b'{ a: 5, b:3, c:true, d: "Hello"}') self.assertEquals(len(x), 4) self.assertEquals(x.get("a"), 5) self.assertEquals(x.get("b"), 3) self.assertEquals(x.get("c"), True) self.assertEquals(x.get("d"), "Hello")
def test_parsing_length_prefixed_strings(self): x = JsonParser.parse('`s\x00\x00\x00\x0cHowdy folks!') self.assertEquals('Howdy folks!', x)
def test_parsing_null(self): x = JsonParser.parse("null") self.assertEquals(x, None) self.assertRaises(JsonParseException, JsonParser.parse, "nill")
def test_triple_quoted_strings(self): x = JsonParser.parse('"""Howdy\n"folks"!"""') self.assertEquals('Howdy\n"folks"!', x)
def test_parsing_length_prefixed_strings(self): x = JsonParser.parse("`s\x00\x00\x00\x0cHowdy folks!") self.assertEquals("Howdy folks!", x)