def test_return_first_matching_choice(): second_choice = mock(Parser) parser = choose(error_parser, second_choice) parsed_value = ParsedValue('s', 1) when(second_choice).called_with('string', 0).then_return(parsed_value) assert parser('string', 0) == parsed_value
def test_code_inline_formatter(): code_formatter = mock(Formatter[str]) when(code_formatter).called_with('code').then_return('<code>code</code>') format_code_inline = code_inline_formatter(code_formatter) formatted_code = format_code_inline(CodeInline('code')) assert formatted_code == '<code>code</code>'
def test_code_block_formatter(): code_formatter = mock(Formatter[str]) when(code_formatter).called_with('code').then_return('<code>code</code>') format_code_block = code_block_formatter(code_formatter) formatted_code = format_code_block(CodeBlock('code')) assert formatted_code == '<pre><code>code</code></pre>'
def test_parser_generator_raised_value_error_if_start_offset_too_large(): parser = mock(Parser) @parser_generator def pass_through_parser() -> ParserGenerator[str]: return (yield parser) with pytest.raises(ValueError): pass_through_parser('string', len('string') + 1)
def test_return_list_containing_each_match(): sub_parser = mock(Parser) when(sub_parser).called_with('aab', 0).then_return(ParsedValue('A', 2)) when(sub_parser).called_with('aab', 2).then_return(ParsedValue('B', 1)) when(sub_parser).called_with('aab', 3).then_raise(ParseError) parser = zero_or_more(sub_parser) assert parser('aab', 0) == ParsedValue(['A', 'B'], 3)
def test_parser_generator_passes_through_parsed_value(): parsed_value = ParsedValue('value', 3) parser = mock(Parser) when(parser).called_with('string', 2).then_return(parsed_value) @parser_generator def pass_through_parser() -> ParserGenerator[str]: return (yield parser) assert pass_through_parser('string', 2) == parsed_value
def test_parser_generator_allows_start_offset_at_end_of_string(): parsed_value = ParsedValue('value', 0) parser = mock(Parser) when(parser).called_with('string', len('string')).then_return(parsed_value) @parser_generator def pass_through_parser() -> ParserGenerator[str]: return (yield parser) assert pass_through_parser('string', len('string')) == parsed_value
def test_parser_generator_raises_value_error_if_num_characters_negative(): parser = mock(Parser) parser.return_value = ParsedValue('value', -1) @parser_generator def pass_through_parser() -> ParserGenerator[str]: return (yield parser) with pytest.raises(ValueError): pass_through_parser('string', 0)
def test_parser_generator_raises_value_error_when_num_characters_too_large(): parser = mock(Parser) parser.return_value = ParsedValue('value', len('string')) @parser_generator def pass_through_parser() -> ParserGenerator[str]: return (yield parser) with pytest.raises(ValueError): pass_through_parser('string', 1)
def test_parser_generator_iterates_through_parsers(): first_parser = mock(Parser) (when(first_parser) .called_with('string', 0) .then_return(ParsedValue('first value', 1))) second_parser = mock(Parser) (when(second_parser) .called_with('string', 1) .then_return(ParsedValue('second value', 2))) @parser_generator def parser() -> ParserGenerator[str]: first_value = yield first_parser second_value = yield second_parser return first_value, second_value parsed_value = parser('string', 0) assert parsed_value == ParsedValue(('first value', 'second value'), 3)
def test_parser_generator_send_parsed_value_back(): parser = mock(Parser) parser.return_value = ParsedValue('value', 1) @parser_generator def pass_through_parser() -> ParserGenerator[str]: value = yield parser if value == 'value': return 'success' assert pass_through_parser('string', 2) == ParsedValue('success', 1)
def test_make_note(mock_html_formatter): cloze = Cloze(['content']) roam_block = RoamBlock([cloze], 'source') cloze_enumerator = mock(ClozeEnumerator) numbered_cloze = replace(cloze, number=1) when(cloze_enumerator).called_with([cloze]).then_return([numbered_cloze]) roam_parts_formatter = mock(Formatter[Iterable[RoamPart]]) formatted_note = '{{c1::content}}' (when(roam_parts_formatter).called_with([numbered_cloze ]).then_return(formatted_note)) when(mock_html_formatter).called_with('source').then_return('html source') note_maker = AnkiNoteMaker( cloze_enumerator, roam_parts_formatter, mock_html_formatter, ) assert note_maker(roam_block) == AnkiNote(formatted_note, 'html source')
def addon_data(tmp_path_factory) -> AnkiAddonData: anki_addon_data = mock(AnkiAddonData) anki_addon_data.read_config.return_value = { 'model_name': MODEL_NAME, 'content_field': CONTENT_FIELD, 'source_field': SOURCE_FIELD, 'deck_name': DECK_NAME, } user_files_path = tmp_path_factory.mktemp('user_files') anki_addon_data.user_files_path.return_value = str(user_files_path) return anki_addon_data
def mock_source_formatter() -> SourceFormatter: return mock(SourceFormatter)
def mock_time_formatter() -> TimeFormatter: return mock(TimeFormatter)
def mock_source_finder() -> SourceFinder: return mock(SourceFinder)
def mock_roam_parser() -> Callable[[str], List[RoamPart]]: return mock(Callable[[str], List[RoamPart]])
def mock_source_builder() -> SourceBuilder: return mock(SourceBuilder)
def anki_model_notes() -> AnkiModelNotes: return mock(AnkiModelNotes)
def mock_roam_note_builder() -> RoamBlockBuilder: return mock(RoamBlockBuilder)
def test_code_block_formatter_returns_none_if_not_given_code_block_object(): code_formatter = mock(Formatter[str]) format_code_block = code_block_formatter(code_formatter) assert format_code_block('') is None
def test_code_inline_formatter_returns_none_when_not_given_code_inline(): code_formatter = mock(Formatter[str]) format_code_inline = code_inline_formatter(code_formatter) assert format_code_inline('') is None
def mock_cloze_part_formatter() -> Formatter[ClozePart]: return mock(Formatter[ClozePart])
def mock_html_formatter() -> Formatter[str]: return mock(Formatter[str])
def anki_collection(anki_model_notes) -> AnkiCollection: collection = mock(AnkiCollection) (when(collection.get_model_notes) .called_with(MODEL_NAME, CONTENT_FIELD, SOURCE_FIELD, DECK_NAME) .then_return(anki_model_notes)) return collection