Example #1
0
 def test_basic_lexer(self):
     self.maxDiff = None
     code = '{2: [], "lol": {"omg": {"so": {"deep": []}}}}'
     tokens = Lexer(code).all_tokens()
     expected = [
         Token(Tokens.L_CURLY_BRACE, '{'),
         Token(Tokens.NUMBER, 2),
         Token(Tokens.COLON, ':'),
         Token(Tokens.L_SQUARE_BRACKET, '['),
         Token(Tokens.R_SQUARE_BRACKET, ']'),
         Token(Tokens.COMMA, ','),
         Token(Tokens.STRING, 'lol'),
         Token(Tokens.COLON, ':'),
         Token(Tokens.L_CURLY_BRACE, '{'),
         Token(Tokens.STRING, 'omg'),
         Token(Tokens.COLON, ':'),
         Token(Tokens.L_CURLY_BRACE, '{'),
         Token(Tokens.STRING, 'so'),
         Token(Tokens.COLON, ':'),
         Token(Tokens.L_CURLY_BRACE, '{'),
         Token(Tokens.STRING, 'deep'),
         Token(Tokens.COLON, ':'),
         Token(Tokens.L_SQUARE_BRACKET, '['),
         Token(Tokens.R_SQUARE_BRACKET, ']'),
         Token(Tokens.R_CURLY_BRACE, '}'),
         Token(Tokens.R_CURLY_BRACE, '}'),
         Token(Tokens.R_CURLY_BRACE, '}'),
         Token(Tokens.R_CURLY_BRACE, '}'),
     ]
     self.assertEqual(tokens, expected)
Example #2
0
 def test_empty_array(self):
     code = '[]'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, [])
Example #3
0
 def test_object_multiple_pairs(self):
     code = '{1:2, 3:4}'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, {1: 2, 3: 4})
Example #4
0
 def test_object_one_pair(self):
     code = '{1: 2}'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, {1: 2})
Example #5
0
 def test_empty_object(self):
     code = '{}'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, {})
Example #6
0
 def test_positive_float(self):
     code = '3.14'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, 3.14)
Example #7
0
 def test_null(self):
     code = 'null'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, None)
Example #8
0
 def test_true(self):
     code = 'true'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, True)
Example #9
0
 def test_invalid_syntax_solo_comma(self):
     with self.assertRaises(ParserError):
         result = Parser(Lexer(',')).parse()
Example #10
0
 def test_complex_object(self):
     code = '{2: [], "lol": {"omg": {"so": {"deep": []}}}}'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, {2: [], 'lol': {'omg': {'so': {'deep': []}}}})
Example #11
0
 def test_complex_array(self):
     code = '[true, false, null, "lol", {"key": [{}, []]}]'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, [True, False, None, 'lol', {'key': [{}, []]}])
Example #12
0
 def test_string(self):
     code = '"omg_kittens"'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, "omg_kittens")
Example #13
0
 def test_negative_float(self):
     code = '-3.14'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, -3.14)
Example #14
0
 def test_array_one_element(self):
     code = '[1]'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, [1])
Example #15
0
 def test_invalid_syntax_array_key(self):
     with self.assertRaises(ParserError):
         result = Parser(Lexer('{[]: "no"}')).parse()
Example #16
0
 def test_array_multiple_elements(self):
     code = '[1, 2]'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, [1, 2])
Example #17
0
 def test_invalid_syntax_array_consume_eof(self):
     with self.assertRaises(ParserError):
         result = Parser(Lexer('[][]')).parse()
Example #18
0
 def test_false(self):
     code = 'false'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, False)
Example #19
0
 def test_invalid_keyword(self):
     with self.assertRaises(KeywordError):
         result = Parser(Lexer('falsey')).parse()
Example #20
0
 def test_positive_integer(self):
     code = '314'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, 314)
Example #21
0
 def test_negative_integer(self):
     code = '-314'
     result = Parser(Lexer(code)).parse()
     self.assertEqual(result, -314)