Exemplo n.º 1
0
    def test_parse_string(self):
        def first(inp):
            return next(JSONLexer(inp))

        self.assertEqual(first('""').content, [])
        self.assertEqual(first('"abc"').content, ['a', 'b', 'c'])
        self.assertEqual(
            first('" as  \tdsg  ;  s"').content, [
                ' ', 'a', 's', ' ', ' ', '\t', 'd', 's', 'g', ' ', ' ', ';',
                ' ', ' ', 's'
            ])
        self.assertEqual(
            first('"\\t\\n\\r \t \\u270A\\u0101"').content,
            ['\\t', '\\n', '\\r', ' ', '\t', ' ', '\\u270a', '\\u0101'])
        self.assertEqual(
            first('"[](){}\\"\\\\"').content,
            ['[', ']', '(', ')', '{', '}', '\\"', '\\\\'])
        with self.assertRaises(ValueError):
            next(JSONLexer('"'))
        with self.assertRaises(ValueError):
            next(JSONLexer('     " sagfjsklh hgkdsgh  shuelighkshg'))
        with self.assertRaises(LexingError):
            next(JSONLexer(' "\\somevalue"  '))
        with self.assertRaises(LexingError):
            next(JSONLexer('   "\\ux"'))
Exemplo n.º 2
0
    def test_parse_signs(self):
        def first(inp):
            return next(JSONLexer(inp))

        def assertIsInstanceList(ins, cls):
            nonlocal self
            for i, (u, v) in enumerate(zip_longest(ins, cls), 1):
                self.assertIsInstance(u, v, "Case {}".format(i))

        self.assertIsInstance(first("  [   "), OpenSquareBracketToken)
        self.assertIsInstance(first("  \n  ] \t  "), CloseSquareBracketToken)
        self.assertIsInstance(first("  { "), OpenCurlyBracketToken)
        self.assertIsInstance(first("  }  "), CloseCurlyBracketToken)
        self.assertIsInstance(first(" , "), CommaToken)
        self.assertIsInstance(first("  :  "), ColonToken)

        assertIsInstanceList(
            JSONLexer(
                " [][]][]  \t {} {{}{} \n ,,,:::  \t[,]],[\t\t {:}}}{{{:,  \n\n\n"
            ), [
                OpenSquareBracketToken,
                CloseSquareBracketToken,
                OpenSquareBracketToken,
                CloseSquareBracketToken,
                CloseSquareBracketToken,
                OpenSquareBracketToken,
                CloseSquareBracketToken,
                OpenCurlyBracketToken,
                CloseCurlyBracketToken,
                OpenCurlyBracketToken,
                OpenCurlyBracketToken,
                CloseCurlyBracketToken,
                OpenCurlyBracketToken,
                CloseCurlyBracketToken,
                CommaToken,
                CommaToken,
                CommaToken,
                ColonToken,
                ColonToken,
                ColonToken,
                OpenSquareBracketToken,
                CommaToken,
                CloseSquareBracketToken,
                CloseSquareBracketToken,
                CommaToken,
                OpenSquareBracketToken,
                OpenCurlyBracketToken,
                ColonToken,
                CloseCurlyBracketToken,
                CloseCurlyBracketToken,
                CloseCurlyBracketToken,
                OpenCurlyBracketToken,
                OpenCurlyBracketToken,
                OpenCurlyBracketToken,
                ColonToken,
                CommaToken,
            ])
Exemplo n.º 3
0
    def test_parse_literals(self):
        def first(inp):
            return next(JSONLexer(inp))

        self.assertIsInstance(first("null"), NullToken)
        self.assertIsInstance(first("false"), FalseToken)
        self.assertIsInstance(first("true"), TrueToken)

        def assertIsInstanceList(ins, cls):
            nonlocal self
            for i, (u, v) in enumerate(zip_longest(ins, cls), 1):
                self.assertIsInstance(u, v, "Case {}".format(i))

        assertIsInstanceList(
            JSONLexer("\n\ntrue\tfalse\ntrue\ntrue null null false\t true"), [
                TrueToken,
                FalseToken,
                TrueToken,
                TrueToken,
                NullToken,
                NullToken,
                FalseToken,
                TrueToken,
            ])
Exemplo n.º 4
0
 def first(inp):
     return next(JSONLexer(inp))
Exemplo n.º 5
0
import unittest
from itertools import zip_longest
from lib.JSONParser.JSONLexer import createJSONLexer as JSONLexer
from lib.JSONParser import parseJSON
from lib.JSONParser.JSONLexer import *
import json  # ironically, using json to test json

helper = lambda exp: list(map(str, JSONLexer(exp)))


class HelperAssert:
    def assertEqualJSONString(self, a, b):
        self.assertEqual(json.dumps(a, sort_keys=True),
                         json.dumps(b, sort_keys=True))


class TestJSONLexer(unittest.TestCase, HelperAssert):
    #@unittest.skip("")
    def test_skip_spaces(self):
        self.assertEqual(helper(""), [])
        self.assertEqual(helper(" "), [])
        self.assertEqual(helper("    "), [])
        self.assertEqual(helper("\n"), [])
        self.assertEqual(helper("\n\n"), [])
        self.assertEqual(helper(" \n \n"), [])
        self.assertEqual(helper("\t \t \n \t\n \t\r\t"), [])

    def test_parse_number(self):
        self.assertEqual(helper("0"), ["+0.0e+0"])
        self.assertEqual(helper("1 "), ["+1.0e+0"])
        self.assertEqual(helper("2"), ["+2.0e+0"])