def _test_case__sym_ref_or_string_parser(self, parser: ParserFromTokens, tc: TC): # ARRANGE # token_stream = TokenStream(tc.source_string) token_parser = TokenParser(token_stream) expected_fragments = tc.expectation.fragments head_is_plain = TokenStream(tc.source_string).head.is_plain assertion_on_value = (_EqualsEitherOfSymbolName( expected_fragments[0].value) if head_is_plain and len(expected_fragments) == 1 and expected_fragments[0].is_symbol else _EqualsEitherOfStringSdv(expected_fragments)) # ACT # actual = parser.parse(token_parser) # ASSERT # tc.expectation.token_stream.apply_with_message(self, token_stream, 'token_stream') assertion_on_value.apply_with_message(self, actual, 'fragment')
def test_head_token_pass(self): # ARRANGE # token_stream = TokenStream('a_token') passing_token_assertion = asrt.sub_component('token_type', Token.is_quoted.fget, asrt.is_false) assertion = sut.assert_token_stream(head_token=passing_token_assertion) # ACT & ASSERT # assertion.apply_without_message(self, token_stream)
def test_look_ahead_state_pass(self): # ARRANGE # assertion = sut.assert_token_stream(look_ahead_state=asrt.is_(LookAheadState.NULL)) # ACT & ASSERT # token_stream = TokenStream('') self.assertTrue(token_stream.look_ahead_state is LookAheadState.NULL, 'sanity check: this test assumes that the constructor of the token parser ' 'sets correct look-ahead state') assertion.apply_without_message(self, token_stream)
def test_fail(self): # ARRANGE # assertion = sut.assert_token_stream(look_ahead_state=asrt.is_(LookAheadState.NULL)) # ACT & ASSERT # token_stream = TokenStream('a_token') self.assertTrue(token_stream.look_ahead_state is LookAheadState.HAS_TOKEN, 'sanity check: this test assumes that the constructor of the token parser ' 'sets correct look-ahead state') assert_that_assertion_fails(assertion, token_stream)
def remaining_source(remaining_contents_of_first_line: str, following_lines: Sequence[str] = ()) -> TokenParser: """ :param remaining_contents_of_first_line: Part of the first line that has not been consumed. :return: Source with some initial content of the first line that has been consumed. """ content = '\n'.join([remaining_contents_of_first_line] + list(following_lines)) token_stream = TokenStream(content) return TokenParser(token_stream)
def test_head_token_fail(self): # ARRANGE # token_stream = TokenStream('a_token') failing_token_assertion = asrt.sub_component('token_type', Token.is_quoted.fget, asrt.is_true) assertion = sut.assert_token_stream(head_token=failing_token_assertion) # ACT & ASSERT # assert_that_assertion_fails(assertion, token_stream)
def _test_case(self, tc: TC): with self.subTest(token_stream=tc.source_string): # ARRANGE # token_stream = TokenStream(tc.source_string) # ACT # actual = sut.parse_fragments_from_tokens(token_stream, CONFIGURATION) # ASSERT # self.assertEqual(tc.expectation.fragments, actual, 'fragment') tc.expectation.token_stream.apply_with_message( self, token_stream, 'token_stream')
def _test_case__parse_string_sdv(self, tc: TC): # ARRANGE # token_stream = TokenStream(tc.source_string) # ACT # actual = sut.parse_string_sdv(token_stream, CONFIGURATION) # ASSERT # tc.expectation.token_stream.apply_with_message(self, token_stream, 'token_stream') assertion_on_result = assert_equals_string_sdv( tc.expectation.fragments) assertion_on_result.apply_with_message(self, actual, 'fragment')
def new_token_stream(source: str) -> TokenStream: """ Constructs a :class:`TokenStream` :rtype: :class:`TokenStream` :raises :class:`SingleInstructionInvalidArgumentException` Invalid syntax """ try: return TokenStream(source) except TokenSyntaxError as ex: raise SingleInstructionInvalidArgumentException( std_error_message_text_for_token_syntax_error_from_exception(ex))
def check(self, put: unittest.TestCase, arrangement: Arrangement, expectation: Expectation): # ARRANGE # ts = TokenStream(arrangement.source) # ACT # actual = sut.parse_path(ts, arrangement.rel_option_argument_configuration, arrangement.source_file_path) # ASSERT # equals_path_sdv(expectation.path_sdv).apply_with_message( put, actual, 'path sdv') expectation.token_stream.apply_with_message(put, ts, 'token-stream')
def test_pass_when_no_explicit_component_assertion(self): test_cases = [ NameAndValue('Empty source', ''), NameAndValue('Single token source', 'a_token'), NameAndValue('Two token source', 'first_token second'), ] for test_case in test_cases: with self.subTest(test_case.name): # ARRANGE # token_stream = TokenStream(test_case.value) assertion = sut.assert_token_stream() # ACT & ASSERT # assertion.apply_without_message(self, token_stream)
def test_return_value_from_parser_WHEN_command_is_found(self): command_name = 'command' return_value_from_parser = 'parser return value' command_parser = do_return(return_value_from_parser) command_name_2_parser = { command_name: command_parser, } # ARRANGE # token_stream = TokenStream(command_name) parser = sut.TokenParser(token_stream) # ACT # actual = parser.parse_optional_command(command_name_2_parser) # ASSERT # self.assertEqual(return_value_from_parser, actual)
def check2(self, put: unittest.TestCase, arrangement: Arrangement, expectation: Expectation2): # ARRANGE # ts = TokenStream(arrangement.source) # ACT # actual = sut.parse_path(ts, arrangement.rel_option_argument_configuration, arrangement.source_file_path) # ASSERT # self.__assertions_on_reference_restrictions( put, actual, expectation. symbol_table_in_with_all_ref_restrictions_are_satisfied) expectation.path_sdv.apply_with_message(put, actual, 'path-sdv') expectation.token_stream.apply_with_message(put, ts, 'token-stream') self.__assertions_on_hypothetical_reference_to_sdv( put, actual, expectation. symbol_table_in_with_all_ref_restrictions_are_satisfied)
def assert_raises_invalid_argument_exception( self, put: unittest.TestCase, source_string: str, test_name: str = '', path_suffix_is_required_cases: Sequence[bool] = (False, True), ): for path_suffix_is_required in path_suffix_is_required_cases: rel_opt_arg_conf = ARBITRARY_REL_OPT_ARG_CONF.config_for( path_suffix_is_required) for source_file_location in [None, Path('/source/file/location')]: with put.subTest( test_name=test_name, path_suffix_is_required=path_suffix_is_required, source_file_location=source_file_location): token_stream = TokenStream(source_string) with put.assertRaises( SingleInstructionInvalidArgumentException): sut.parse_path( token_stream, rel_opt_arg_conf, source_file_location=source_file_location)
def _token_parser_of(source: str) -> TokenParser: return TokenParser(TokenStream(source))
def test_source_pass(self): # ARRANGE # token_stream = TokenStream('a_token') assertion = sut.assert_token_stream(source=asrt.equals('a_token')) # ACT & ASSERT # assertion.apply_without_message(self, token_stream)
def test_position_fail(self): # ARRANGE # token_stream = TokenStream('a_token') assertion = sut.assert_token_stream(position=asrt.equals(2)) # ACT & ASSERT # assert_that_assertion_fails(assertion, token_stream)
def test_remaining_part_of_current_line_fail(self): # ARRANGE # token_stream = TokenStream('first line\nsecond line') assertion = sut.assert_token_stream(remaining_part_of_current_line=asrt.equals('second line')) # ACT & ASSERT # assert_that_assertion_fails(assertion, token_stream)
def test_remaining_source_fail(self): # ARRANGE # token_stream = TokenStream('a_token') assertion = sut.assert_token_stream(remaining_source=asrt.equals('not remaining source')) # ACT & ASSERT # assert_that_assertion_fails(assertion, token_stream)
def test_missing_argument__parse_string_sdv(self): with self.assertRaises(SingleInstructionInvalidArgumentException): sut.parse_string_sdv(TokenStream(''))
def test_parse_string_sdv(self): for reserved in reserved_words.RESERVED_TOKENS: with self.subTest(reserved): with self.assertRaises( SingleInstructionInvalidArgumentException): sut.parse_string_sdv(TokenStream(reserved))
def _multi_line_source(lines: List[str], **kwargs) -> TokenStream: all_lines = '\n'.join([_src(line, **kwargs) for line in lines]) return TokenStream(all_lines)
def test_missing_argument__parse_fragments_from_token(self): with self.assertRaises(SingleInstructionInvalidArgumentException): sut.parse_fragments_from_tokens(TokenStream(''))
def test_missing_argument__parse_sym_ref_or_string_sdv(self): parser = sut.SymbolReferenceOrStringParser(CONFIGURATION) with self.assertRaises(SingleInstructionInvalidArgumentException): parser.parse(TokenParser(TokenStream('')))
def _single_line_source(s: str, **kwargs) -> TokenStream: return TokenStream(_src(s, **kwargs))
def test_remaining_part_of_current_line_pass(self): # ARRANGE # token_stream = TokenStream('first line\nsecond line') assertion = sut.assert_token_stream(remaining_part_of_current_line=asrt.equals('first line')) # ACT & ASSERT # assertion.apply_without_message(self, token_stream)
def test_remaining_source_after_head_fail(self): # ARRANGE # token_stream = TokenStream('fst_token snd_token') assertion = sut.assert_token_stream(remaining_source_after_head=asrt.equals('fst_token')) # ACT & ASSERT # assert_that_assertion_fails(assertion, token_stream)
def test_remaining_source_after_head_pass(self): # ARRANGE # token_stream = TokenStream('fst_token snd_token') assertion = sut.assert_token_stream(remaining_source_after_head=asrt.equals('snd_token')) # ACT & ASSERT # assertion.apply_without_message(self, token_stream)
def test_is_null_fail(self): # ARRANGE # token_stream = TokenStream('a_token') assertion = sut.assert_token_stream(is_null=asrt.is_true) # ACT & ASSERT # assert_that_assertion_fails(assertion, token_stream)
def test_is_null_pass(self): # ARRANGE # token_stream = TokenStream('') assertion = sut.assert_token_stream(is_null=asrt.is_true) # ACT & ASSERT # assertion.apply_without_message(self, token_stream)