Beispiel #1
0
  def test_no_symbol_table(self):
    document = dedent("""
    from pants_test.engine.test_parsers import Bob

    nancy = Bob(
      hobbies=[1, 2, 3]
    )
    """)
    results = parse(parsers.PythonAssignmentsParser(parser.EmptyTable()), document)
    self.assertEqual([Bob(name='nancy', hobbies=[1, 2, 3])], results)

    # No symbol table was used so no `type_alias` plumbing can be expected.
    self.assertNotIn('type_alias', results[0]._asdict())
Beispiel #2
0
 def parse(self, document, symbol_table=None, **kwargs):
     symbol_table = symbol_table or parser.EmptyTable()
     return parse(parsers.JsonParser(symbol_table), document, **kwargs)
Beispiel #3
0
    def test_error_presentation(self):
        document = dedent("""
    # An example with several Bobs.

    # One with hobbies.
      {
        "type_alias": "pants_test.engine.test_parsers.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_test.engine.test_parsers.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(parser.EmptyTable()).parse(filepath, document)

        # 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.
        assertRegex(
            self, actual_lines[0],
            r'Expecting (?:,|\',\'|",") delimiter: line 3 column 12 \(char 67\)'
        )

        self.assertEqual(
            dedent("""
      In document at {filepath}:
          # An example with several Bobs.

          # One with hobbies.
            {{
              "type_alias": "pants_test.engine.test_parsers.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_test.engine.test_parsers.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:]))