Esempio n. 1
0
 def parse(self, document, symbol_table=None):
     symbol_table = symbol_table or EMPTY_TABLE
     return parse(parsers.JsonParser(symbol_table), document)
Esempio n. 2
0
    def test_error_presentation(self):
        document = dedent(
            """
            # An example with several Bobs.
        
            # One with hobbies.
              {
                "type_alias": "pants.engine.internals.parsers_test.Bob",
        
                # And internal comment and blank lines.
        
                "hobbies": [1, 2, 3]} {
              # This comment is inside an empty object that started on the prior line!
            }
        
            # Another that is imaginary aged.
            {
              "type_alias": "pants.engine.internals.parsers_test.Bob",
              "age": 42i,
        
              "four": 1,
              "five": 1,
              "six": 1,
              "seven": 1,
              "eight": 1,
              "nine": 1
            }
            """
        ).strip()
        filepath = "/dev/null"
        with self.assertRaises(parser.ParseError) as exc:
            parsers.JsonParser(EMPTY_TABLE).parse(
                filepath, document, BuildFilePreludeSymbols(FrozenDict())
            )

        # Strip trailing whitespace from the message since our expected literal below will have
        # trailing ws stripped via editors and code reviews calling for it.
        actual_lines = [line.rstrip() for line in str(exc.exception).splitlines()]

        # This message from the json stdlib varies between python releases, so fuzz the match a bit.
        self.assertRegex(
            actual_lines[0], r'Expecting (?:,|\',\'|",") delimiter: line 3 column 12 \(char 72\)'
        )

        self.assertEqual(
            dedent(
                """
                In document at {filepath}:
                    # An example with several Bobs.
        
                    # One with hobbies.
                      {{
                        "type_alias": "pants.engine.internals.parsers_test.Bob",
        
                        # And internal comment and blank lines.
        
                        "hobbies": [1, 2, 3]}} {{
                      # This comment is inside an empty object that started on the prior line!
                    }}
        
                    # Another that is imaginary aged.
                 1: {{
                 2:   "type_alias": "pants.engine.internals.parsers_test.Bob",
                 3:   "age": 42i,
        
                 4:   "four": 1,
                 5:   "five": 1,
                 6:   "six": 1,
                 7:   "seven": 1,
                 8:   "eight": 1,
                 9:   "nine": 1
                10: }}
                """.format(
                    filepath=filepath
                )
            ).strip(),
            "\n".join(actual_lines[1:]),
        )