Ejemplo n.º 1
0
 def test_first_line_is_empty(self):
     source = ParseSource('\n' + 'second line')
     self._assert_current_line_and_remaining_part_of_it_is(1, '', source)
     source.consume_current_line()
     self._assert_current_line_and_remaining_part_of_it_is(2, 'second line', source)
     self.assertEqual('second line', source.remaining_source,
                      'remaining source')
     source.consume_current_line()
     self._assert_is_at_eof(source)
Ejemplo n.º 2
0
 def cases_wo_parentheses(expected_int: int) -> List[NSourceCase]:
     return [
         NSourceCase(
             'plain int',
             ParseSource(str(expected_int)),
             asrt_source.is_at_end_of_line(1),
         ),
         NSourceCase(
             'plain int followed by other plain int',
             ParseSource(str(expected_int) + ' 77'),
             asrt_source.is_at_line(1, '77'),
         ),
         NSourceCase(
             'plain int followed by end parenthesis',
             ParseSource(str(expected_int) + ' )'),
             asrt_source.is_at_line(1, ')'),
         ),
     ]
Ejemplo n.º 3
0
 def test_consume_current_line(self):
     test_cases = [
         ('single line',
          assert_source(is_at_eof=asrt.is_true,
                        has_current_line=asrt.is_false,
                        remaining_source=asrt.equals(''))
          ),
         ('',
          assert_source(is_at_eof=asrt.is_true,
                        has_current_line=asrt.is_false)
          ),
         ('first line\nsecond line',
          assert_source(is_at_eof=asrt.is_false,
                        has_current_line=asrt.is_true,
                        is_at_eol=asrt.is_false,
                        is_at_eol__except_for_space=asrt.is_false,
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('second line'),
                        column_index=asrt.equals(0),
                        remaining_source=asrt.equals('second line'))
          ),
         ('single line\n',
          assert_source(is_at_eof=asrt.is_true,
                        has_current_line=asrt.is_true,
                        is_at_eol=asrt.is_true,
                        is_at_eol__except_for_space=asrt.is_true,
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals(''),
                        column_index=asrt.equals(0),
                        remaining_source=asrt.equals(''))
          ),
         ('\n',
          assert_source(is_at_eof=asrt.is_true,
                        has_current_line=asrt.is_true,
                        is_at_eol=asrt.is_true,
                        is_at_eol__except_for_space=asrt.is_true,
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals(''),
                        column_index=asrt.equals(0),
                        remaining_source=asrt.equals(''))
          ),
         ('\nsecond line',
          assert_source(is_at_eof=asrt.is_false,
                        has_current_line=asrt.is_true,
                        is_at_eol=asrt.is_false,
                        is_at_eol__except_for_space=asrt.is_false,
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('second line'),
                        column_index=asrt.equals(0),
                        remaining_source=asrt.equals('second line'))
          ),
     ]
     for source_string, expectation in test_cases:
         with self.subTest(msg='consume current line: ' + repr(source_string)):
             source = ParseSource(source_string)
             source.consume_current_line()
             expectation.apply_with_message(self, source, 'consume_current_line:{}'.format(repr(source_string)))
Ejemplo n.º 4
0
 def apply(self, test_case: TestCaseFileReference,
           test_case_plain_source: str) -> test_case_doc.TestCase:
     file_parser = test_case_parser.new_parser(
         self._test_case_parsing_setup)
     source = ParseSource(test_case_plain_source)
     try:
         return file_parser.apply(test_case, source)
     except exceptions.ParseError as ex:
         ex.accept(_ParseErrorHandler())
Ejemplo n.º 5
0
 def test_consume_part_of_current_line__until_end_of_line_of_last_line(self):
     source = ParseSource('first line')
     source.consume_part_of_current_line(len('first line'))
     self._assert_current_line_is(1, 'first line', source)
     self.assertTrue(source.is_at_eol, 'is_at_eol')
     self.assertTrue(source.is_at_eof, 'is_at_eof')
     self.assertEqual('', source.remaining_part_of_current_line,
                      'Remaining part of current line')
     self.assertEqual('',
                      source.remaining_source,
                      'Remaining source')
Ejemplo n.º 6
0
 def test_consume_part_of_current_line(self):
     source = ParseSource('first line' + '\n' + 'second line')
     source.consume_part_of_current_line(len('first') + 1)
     self._assert_current_line_is(1, 'first line', source)
     self.assertEqual('line', source.remaining_part_of_current_line,
                      'Remaining part of current line')
     self.assertEqual('line' + '\n' + 'second line',
                      source.remaining_source,
                      'Remaining source')
     self.assertFalse(source.is_at_eof, 'is_at_eof')
     self.assertFalse(source.is_at_eol, 'is_at_eol')
Ejemplo n.º 7
0
 def test_two_line_source(self):
     original_source = 'first line' + '\n' + 'second line'
     source = ParseSource(original_source)
     self._assert_current_line_and_remaining_part_of_it_is(1, 'first line', source)
     self.assertEqual(original_source, source.remaining_source,
                      'remaining source')
     source.consume_current_line()
     self._assert_current_line_and_remaining_part_of_it_is(2, 'second line', source)
     self.assertEqual('second line', source.remaining_source,
                      'remaining source')
     source.consume_current_line()
     self._assert_is_at_eof(source)
Ejemplo n.º 8
0
 def check_invalid_syntax__abs_stx(
         self,
         put: unittest.TestCase,
         invalid_source: AbstractSyntax,
         sub_test_identifiers: Mapping[str, Any] = MappingProxyType({}),
 ):
     for layout_case in STANDARD_LAYOUT_SPECS:
         parse_source = ParseSource(invalid_source.tokenization().layout(
             layout_case.value))
         with put.subTest(zz_layout=layout_case.name,
                          **sub_test_identifiers):
             self.check_invalid_arguments(put, parse_source)
Ejemplo n.º 9
0
 def runTest(self):
     # ARRANGE #
     conf = self.configuration
     arguments_str = _exe_file_syntax_str(conf, 'file.exe',
                                          'remaining args')
     source = ParseSource(arguments_str)
     # ACT #
     exe_file = parse_executable_file_path.parser().parse(source)
     # ASSERT #
     with self._tcds_and_test_as_curr_dir(
             File.empty('file.exe')) as environment:
         self._assert_does_not_pass_validation(exe_file, environment)
Ejemplo n.º 10
0
 def test_consume_with_invalid_arguments(self):
     test_cases = [
         ('a', 0, 2),
         ('a', 1, 1),
         ('a\nb', 0, 4),
         ('a\nb', 1, 3),
     ]
     for source, num_chars_on_current_line_to_consume_before_check, num_chars in test_cases:
         with self.subTest(source=source, num_chars=num_chars):
             source = ParseSource(source)
             source.consume_part_of_current_line(num_chars_on_current_line_to_consume_before_check)
             with self.assertRaises(ValueError):
                 source.consume(num_chars)
Ejemplo n.º 11
0
 def runTest(self):
     # ARRANGE #
     conf = self.configuration
     arguments_str = _exe_file_syntax_str(conf, 'file.exe',
                                          'remaining args')
     source = ParseSource(arguments_str)
     # ACT #
     exe_file = parse_executable_file_path.parser().parse(source)
     # ASSERT #
     source_assertion = has_remaining_part_of_first_line('remaining args')
     source_assertion.apply_with_message(self, source, 'source after parse')
     symbols = empty_symbol_table()
     self._check_existence_pre_sds(exe_file, symbols)
     with tcds_with_act_as_curr_dir(symbols=symbols) as environment:
         self._check_file_path('file.exe', exe_file, environment)
         self._assert_does_not_pass_validation(exe_file, environment)
Ejemplo n.º 12
0
 def test_successful_parse(self):
     # ARRANGE #
     symbol_ref = SymbolWithReferenceSyntax('validSymbol_name_1')
     single_symbol = [symbol(symbol_ref.name)]
     parse_source = ParseSource(
         _src('{soft_quote}{symbol_reference}{soft_quote} rest',
              soft_quote=SOFT_QUOTE_CHAR,
              symbol_reference=symbol_ref))
     # ACT #
     actual = sut.parse_string_sdv_from_parse_source(parse_source)
     # ASSERT #
     assertion_on_result = assert_equals_string_sdv(single_symbol)
     assertion_on_result.apply_with_message(self, actual, 'result')
     assertion_on_parse_source = assert_source(
         remaining_part_of_current_line=asrt.equals('rest'))
     assertion_on_parse_source.apply_with_message(self, parse_source,
                                                  'parse_source')
Ejemplo n.º 13
0
 def test_all_fail_recognized_element_SHOULD_be_raised_WHEN_a_parser_raises_RE(
         self):
     # ARRANGE #
     returned_element = ParsedNonInstructionElement(
         single_line_sequence(1, 'parsed by successful parser'),
         ElementType.EMPTY)
     source_text = 'first line'
     err_msg = 'error message in RE exception'
     cases = [
         NameAndValue('single parser raises RE', [
             SectionElementParserThatRaisesRecognizedSectionElementSourceError(
                 err_msg)
         ]),
         NameAndValue(
             '1:st parser fails by returning None, 2nd=last parsers - last parser raises RE',
             [
                 SectionElementParserThatReturnsNone(),
                 SectionElementParserThatRaisesRecognizedSectionElementSourceError(
                     err_msg)
             ]),
         NameAndValue(
             '1:st parser fails by raising UnrecognizedError, 2nd=last parsers - last parser raises RE',
             [
                 SectionElementParserThatRaisesUnrecognizedSectionElementSourceError(
                     'err msg of UE'),
                 SectionElementParserThatRaisesRecognizedSectionElementSourceError(
                     err_msg)
             ]),
         NameAndValue('first parser raises RE, following parser succeeds', [
             SectionElementParserThatRaisesRecognizedSectionElementSourceError(
                 err_msg),
             SectionElementParserThatReturnsConstantAndConsumesCurrentLine(
                 returned_element),
         ]),
     ]
     for case in cases:
         with self.subTest(case.name):
             source = ParseSource(source_text)
             parser = sut.ParserFromSequenceOfParsers(case.value)
             # ACT #
             with self.assertRaises(
                     RecognizedSectionElementSourceError) as cm:
                 parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
             self.assertEqual(err_msg, cm.exception.message,
                              'error message in exception')
Ejemplo n.º 14
0
 def _check(self, arguments_str: str,
            expected_source_after_parse: Assertion[ParseSource],
            expectation_on_exe_file: ExpectationOnExeFile,
            validator_expectation: validation.Expectation):
     # ARRANGE #
     source = ParseSource(arguments_str)
     # ACT #
     exe_file = sut.parser().parse(source)
     # ASSERT #
     utils.check_exe_file(self, expectation_on_exe_file, exe_file)
     expected_source_after_parse.apply_with_message(self, source,
                                                    'parse source')
     exe_file_command = command_sdvs.for_executable_file(exe_file)
     with tcds_with_act_as_curr_dir() as environment:
         actual_validator = ddv_validators.all_of(
             exe_file_command.resolve(environment.symbols).validators)
         assertion = ddv_assertions.DdvValidationAssertion.of_expectation(
             validator_expectation, environment.tcds)
         assertion.apply_with_message(self, actual_validator, 'validation')
Ejemplo n.º 15
0
def check(put: unittest.TestCase, instruction_argument_string: str,
          arrangement: Arrangement, expectation: Expectation):
    # ARRANGE #
    source = ParseSource(instruction_argument_string)
    # ACT #
    actual_exe_file = parse_executable_file_path.parser().parse(source)
    # ASSERT #
    exe_file_as_command = command_sdvs.for_executable_file(actual_exe_file)

    expectation.source.apply_with_message(put, source, 'parse source')
    check_exe_file(put, expectation.expectation_on_exe_file, actual_exe_file)
    with tcds_with_act_as_curr_dir(
            tcds_contents=arrangement.tcds_populator) as environment:
        os.mkdir('act-cwd')
        os.chdir('act-cwd')
        actual_validator = ddv_validators.all_of(
            exe_file_as_command.resolve(environment.symbols).validators)

        assertion = ddv_assertions.DdvValidationAssertion.of_expectation(
            expectation.validation_result, environment.tcds)
        assertion.apply_with_message(put, actual_validator, 'validation')
Ejemplo n.º 16
0
 def test_consume_initial_space(self):
     test_cases = [
         (['', 'second line'], '', True),
         (['non-space', 'second line'], 'non-space', False),
         (['   ', 'second line'], '', True),
         (['   non-space', 'second line'], 'non-space', False),
     ]
     for lines, remaining_part, is_at_eol in test_cases:
         with self.subTest():
             first_line = lines[0]
             source_lines = '\n'.join(lines)
             # ACT #
             source = ParseSource(source_lines)
             # ASSERT #
             source.consume_initial_space_on_current_line()
             assertion = assert_source(is_at_eol=asrt.equals(is_at_eol),
                                       remaining_part_of_current_line=asrt.equals(remaining_part),
                                       current_line_number=asrt.equals(1),
                                       current_line_text=asrt.equals(lines[0]),
                                       column_index=asrt.equals(len(first_line) - len(first_line.lstrip()))
                                       )
             assertion.apply(self, source)
Ejemplo n.º 17
0
 def test_all_fail_None_SHOULD_be_returned_WHEN_all_parsers_return_None(
         self):
     # ARRANGE #
     source_text = 'first line'
     cases = [
         NameAndValue('no parsers', []),
         NameAndValue('one parser',
                      [SectionElementParserThatReturnsNone()]),
         NameAndValue('more than one parser', [
             SectionElementParserThatReturnsNone(),
             SectionElementParserThatReturnsNone()
         ]),
     ]
     for case in cases:
         with self.subTest(case.name):
             source = ParseSource(source_text)
             parser = sut.ParserFromSequenceOfParsers(case.value)
             # ACT #
             actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
             # ASSERT #
             self.assertIsNone(actual, 'return value from parser')
             expected_source = asrt_source.source_is_not_at_end(
                 remaining_source=asrt.equals(source_text))
             expected_source.apply_with_message(self, source, 'source')
Ejemplo n.º 18
0
 def test_raise_exception_when_too_many_characters_are_consumed(self):
     source = ParseSource('first line' + '\n' + 'second line')
     with self.assertRaises(ValueError):
         source.consume_part_of_current_line(len('first line') + 1)
         self.assertEqual('first line', source.remaining_part_of_current_line,
                          'remaining_part_of_current_line should not have been updated')
Ejemplo n.º 19
0
 def test_invalid_option(self):
     with self.assertRaises(SingleInstructionInvalidArgumentException):
         sut.parser().parse(
             ParseSource('{} FILE'.format(
                 CustomOptionArgument('invalid-option'))))
Ejemplo n.º 20
0
 def test_missing_file_argument(self):
     with self.assertRaises(SingleInstructionInvalidArgumentException):
         sut.parser().parse(ParseSource(path_texts.REL_HDS_CASE_OPTION))
Ejemplo n.º 21
0
def new_for_file(path: pathlib.Path) -> ParseSource:
    with path.open() as fo:
        contents = fo.read()
    return ParseSource(contents)
Ejemplo n.º 22
0
 def test_consume_with_valid_arguments(self):
     test_cases = [
         (TestSetupForConsume('one whole line and part of next, with one line remaining',
                              source_lines=['123',
                                            '45',
                                            '6'],
                              number_of_characters_to_consume_from_current_line_before_test=0,
                              number_of_characters_to_consume=4 + 1),
          assert_source(is_at_eof=asrt.equals(False),
                        is_at_eol=asrt.equals(False),
                        is_at_eol__except_for_space=asrt.equals(False),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(1),
                        remaining_part_of_current_line=asrt.equals('5'),
                        remaining_source=asrt.equals('5\n6'))),
         (TestSetupForConsume('one whole line and part of next, with one line remaining, on column index 1',
                              source_lines=['123',
                                            '45',
                                            '6'],
                              number_of_characters_to_consume_from_current_line_before_test=1,
                              number_of_characters_to_consume=4 + 1 - 1),
          assert_source(is_at_eof=asrt.equals(False),
                        is_at_eol=asrt.equals(False),
                        is_at_eol__except_for_space=asrt.equals(False),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(1),
                        remaining_part_of_current_line=asrt.equals('5'),
                        remaining_source=asrt.equals('5\n6'))),
         (TestSetupForConsume('two whole lines, with one line remaining',
                              source_lines=['123',
                                            '45',
                                            '6'],
                              number_of_characters_to_consume_from_current_line_before_test=0,
                              number_of_characters_to_consume=5 + 1),
          assert_source(is_at_eof=asrt.equals(False),
                        is_at_eol=asrt.equals(True),
                        is_at_eol__except_for_space=asrt.equals(True),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(2),
                        remaining_part_of_current_line=asrt.equals(''),
                        remaining_source=asrt.equals('\n6'))),
         (TestSetupForConsume('two whole lines, with one line remaining, on column index 2',
                              source_lines=['123',
                                            '45',
                                            '6'],
                              number_of_characters_to_consume_from_current_line_before_test=2,
                              number_of_characters_to_consume=5 + 1 - 2),
          assert_source(is_at_eof=asrt.equals(False),
                        is_at_eol=asrt.equals(True),
                        is_at_eol__except_for_space=asrt.equals(True),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(2),
                        remaining_part_of_current_line=asrt.equals(''),
                        remaining_source=asrt.equals('\n6'))),
         (TestSetupForConsume('two whole lines, with no line after',
                              source_lines=['123',
                                            '45'],
                              number_of_characters_to_consume_from_current_line_before_test=0,
                              number_of_characters_to_consume=5 + 1),
          assert_source(is_at_eof=asrt.equals(True),
                        is_at_eol=asrt.equals(True),
                        is_at_eol__except_for_space=asrt.equals(True),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        remaining_part_of_current_line=asrt.equals(''),
                        remaining_source=asrt.equals(''))),
         (TestSetupForConsume('two whole lines, with no line after, on column index 3',
                              source_lines=['123',
                                            '45'],
                              number_of_characters_to_consume_from_current_line_before_test=3,
                              number_of_characters_to_consume=5 + 1 - 3),
          assert_source(is_at_eof=asrt.equals(True),
                        is_at_eol=asrt.equals(True),
                        is_at_eol__except_for_space=asrt.equals(True),
                        has_current_line=asrt.equals(True),
                        current_line_number=asrt.equals(2),
                        current_line_text=asrt.equals('45'),
                        column_index=asrt.equals(2),
                        remaining_part_of_current_line=asrt.equals(''),
                        remaining_source=asrt.equals(''))),
     ]
     for setup, assertion in test_cases:
         with self.subTest(case_name=setup.name):
             source_lines = '\n'.join(setup.source_lines)
             source = ParseSource(source_lines)
             source.consume_part_of_current_line(setup.number_of_characters_to_consume_from_current_line_before_test)
             source.consume(setup.number_of_characters_to_consume)
             assertion.apply(self, source, asrt.MessageBuilder(setup.name))
Ejemplo n.º 23
0
def _source_for_lines(source_lines: list) -> ParseSource:
    source_string = '\n'.join(source_lines)
    return ParseSource(source_string)
Ejemplo n.º 24
0
 def test_consume__consume_all_characters_of_current_line(self):
     for source_string, expectation in self.test_cases_for_consume_all_characters_of_current_line:
         with self.subTest(msg='consume: ' + repr(source_string)):
             source = ParseSource(source_string)
             source.consume(len(source.current_line_text))
             expectation.apply_with_message(self, source, 'consume:{}'.format(repr(source_string)))