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_format_roam_curly_command( format_roam_curly_command, mock_html_formatter, ): command = RoamCurlyCommand('command') when(mock_html_formatter).called_with('command').then_return( 'html command') assert format_roam_curly_command(command) == '{{html command}}'
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_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_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_format_roam_colon_command( format_roam_colon_command, mock_html_formatter, ): when(mock_html_formatter).called_with('command').then_return( 'html command') when(mock_html_formatter).called_with('content').then_return( 'html content') command = RoamColonCommand('command', 'content') assert format_roam_colon_command(command) == ':html commandhtml content'
def test_cloze_formatter_formats_hint( format_cloze, mock_cloze_part_formatter, mock_html_formatter, ): cloze = Cloze(['part'], 'hint', number=1) (when(mock_cloze_part_formatter).called_with('part').then_return( 'formatted part')) (when(mock_html_formatter).called_with('hint').then_return('html hint')) assert format_cloze(cloze) == '{{c1::formatted part::html hint}}'
def test_format_source(source_formatter, mock_time_formatter): create_time = 1337 edit_time = 31337 page_json = page(title='title') block_json = block('note', create_time=create_time, edit_time=edit_time) (when(mock_time_formatter).called_with(create_time).then_return( '[create time]')) (when(mock_time_formatter).called_with(edit_time).then_return( '[edit time]')) formatted_source = source_formatter(block_json, '[source]', page_json) assert formatted_source == "[source]\nNote from Roam page 'title', created at [create time], edited at [edit time]."
def test_cloze_formatter_joins_cloze_parts( format_cloze, mock_cloze_part_formatter, ): (when(mock_cloze_part_formatter).called_with('part 1').then_return( 'formatted part 1')) (when(mock_cloze_part_formatter).called_with('part 2').then_return( 'formatted part 2')) cloze = Cloze(['part 1', 'part 2'], number=1) formatted_cloze = format_cloze(cloze) assert formatted_cloze == '{{c1::formatted part 1formatted part 2}}'
def test_return_note_when_cloze_part( roam_note_builder, mock_roam_parser, mock_source_builder, ): block_json = block('{block text}') parent_json = page(block_json) note_parts = [Cloze(['cloze content'])] when(mock_roam_parser).called_with('{block text}').then_return(note_parts) (when(mock_source_builder).called_with( block_json, [parent_json]).then_return('source')) roam_note = roam_note_builder(block_json, [parent_json]) assert roam_note == RoamBlock(note_parts, 'source')
def test_source_builder(source_builder, mock_source_finder, mock_source_formatter): child_block_json = block('child') parent_block_json = block('parent', child_block_json) page_json = page(parent_block_json) (when(mock_source_finder).called_with( child_block_json, [page_json, parent_block_json]).then_return('found source')) (when(mock_source_formatter).called_with( child_block_json, 'found source', page_json).then_return('formatted source')) formatted_source = source_builder(child_block_json, [page_json, parent_block_json]) assert formatted_source == 'formatted source'
def test_extract_without_note(note_extractor, mock_roam_note_builder): content = 'no note' block_json = block(content) page_json = page(block_json) (when(mock_roam_note_builder).called_with(block_json, [page_json]).then_return(None)) assert list(note_extractor([page_json])) == []
def test_extract_from_nested_block(note_extractor, mock_roam_note_builder): child_block_json = block('{child}') parent_block_json = block('{parent}', child_block_json) page_json = page(parent_block_json) child_roam_note = RoamBlock([Cloze(['child cloze content'])], 'child source') parent_roam_note = RoamBlock([Cloze(['parent cloze content'])], 'parent source') (when(mock_roam_note_builder).called_with( parent_block_json, [page_json]).then_return(parent_roam_note)) (when(mock_roam_note_builder).called_with( child_block_json, [page_json, parent_block_json]).then_return(child_roam_note)) assert list(note_extractor([page_json ])) == [parent_roam_note, child_roam_note]
def test_cloze_formatter_formats_cloze_number( format_cloze, mock_cloze_part_formatter, ): cloze = Cloze(['part'], number=2) (when(mock_cloze_part_formatter).called_with('part').then_return( 'formatted part')) formatted_cloze = format_cloze(cloze) assert formatted_cloze == '{{c2::formatted part}}'
def test_extract_from_single_block(note_extractor, mock_roam_note_builder): content = '{note}' block_json = block(content) page_json = page(block_json) roam_note = RoamBlock([Cloze(['cloze content'])], 'source') (when(mock_roam_note_builder).called_with( block_json, [page_json]).then_return(roam_note)) assert list(note_extractor([page_json])) == [roam_note]
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_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 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 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
def test_return_none_when_no_cloze_part(roam_note_builder, mock_roam_parser): block_json = block('no note') parent_json = page(block_json) when(mock_roam_parser).called_with('no note').then_return(['no note']) assert roam_note_builder(block_json, [parent_json]) is None
def test_format_string_returns_html_formatted_string( format_string, mock_html_formatter, ): when(mock_html_formatter).called_with('string').then_return('html string') assert format_string('string') == 'html string'
def test_format_math_formats_math_object(format_math, mock_html_formatter): when(mock_html_formatter).called_with('math').then_return('html math') assert format_math(Math('math')) == r'\(html math\)'