def test_extract_abbreviation(self):
        """Abbreviations should be able to be extracted"""

        raw_string = """<abbr title="Bit error rate" data-context="Communication" class="h-fcard">BER</abbr>"""
        tag = "abbr"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        data = abbreviation.extract(root)

        self.assertEqual(
            data, {
                "Full": "Bit error rate",
                "Context": "Communication",
                "Abbreviation": "BER"
            })

        raw_string = """<abbr title="Symbol error rate" data-context="Communication" class="h-fcard">SER</abbr>"""
        tag = "abbr"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        data = abbreviation.extract(root)

        self.assertEqual(
            data, {
                "Full": "Symbol error rate",
                "Context": "Communication",
                "Abbreviation": "SER"
            })
    def test_fragments(self):

        raw_string = """<abbr title="Bit error rate" data-context="Communication" class="h-fcard">BER</abbr>"""
        tag = "abbr"
        fragments = f2f.find_fragments(raw_string, tag)

        self.assertEqual(fragments, ["""<abbr title="Bit error rate" data-context="Communication" class="h-fcard">BER</abbr>"""])

        raw_string = """It is <abbr title="Bit error rate" data-context="Communication" class="h-fcard">BER</abbr>"""
        tag = "abbr"
        fragments = f2f.find_fragments(raw_string, tag)

        self.assertEqual(fragments, ["""<abbr title="Bit error rate" data-context="Communication" class="h-fcard">BER</abbr>"""])
    def test_process_file(self):
        """Register formats and process a file"""

        f2f.AnkiConnectWrapper.add_note = MagicMock()
        f2f.AnkiConnectWrapper.add_note.return_value = "1234"
        f2f.AnkiConnectWrapper.update_note = MagicMock()

        f2f.add_format(**abbreviation.definition)

        tmp_dir_o = tempfile.TemporaryDirectory()
        tmp_dir = tmp_dir_o.name
        shutil.copyfile("tests/test.tid", tmp_dir + "/" + "test.tid")

        f2f.process_file(tmp_dir + "/" + "test.tid")

        with open(tmp_dir + "/" + "test.tid", encoding='utf-8') as f:
            content = f.read()

            fragments = f2f.find_fragments(content, "abbr")

            root = ET.fromstring(fragments[0])

            data = abbreviation.extract(root)

            self.assertEqual(data, {"Full": "Bit error rate", "Context": "Communication", "Abbreviation": "BER"})
    def test_fragments_multiple(self):
        """Ensure that multiple fragments can be extracted from a single file"""

        raw_string = """<abbr title="Bit error rate" data-context="Communication" class="h-fcard">BER</abbr> and another <abbr title="Symbol error rate" data-context="Communication" class="h-fcard">SER</abbr>"""
        tag = "abbr"
        fragments = f2f.find_fragments(raw_string, tag)

        self.assertEqual(fragments, ["""<abbr title="Bit error rate" data-context="Communication" class="h-fcard">BER</abbr>""", """<abbr title="Symbol error rate" data-context="Communication" class="h-fcard">SER</abbr>"""])
    def test_extract_cloze_new_cloze(self):
        """Cloze deletions should handle new deletions in known fragments"""

        raw_string = """<span class="h-fcard e-cloze">A <em data-id="1">B</em> <em>C</em> <em data-id="2">D</em> E <em>F</em></span>"""
        tag = "span"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        data = cloze.extract(root)

        self.assertEqual(data, {"Text": "A {{c1::B}} {{c3::C}} {{c2::D}} E {{c4::F}}", "Extra": ""})
    def test_extract_cloze_reuse_id(self):
        """Cloze deletions should reuse the IDs from the fragment"""

        raw_string = """<span class="h-fcard e-cloze">This <em data-id="2">is</em> a <em data-id="1">cloze</em> test</span>"""
        tag = "span"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        data = cloze.extract(root)

        self.assertEqual(data, {"Text": "This {{c2::is}} a {{c1::cloze}} test", "Extra": ""})
    def test_extract_cloze_data_advanced(self):
        """Cloze deletion data should be able to be extracted"""

        raw_string = """<span class="h-fcard e-cloze">This <em>is</em> a <em>cloze</em> test</span>"""
        tag = "span"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        data = cloze.extract(root)

        self.assertEqual(data, {"Text": "This {{c1::is}} a {{c2::cloze}} test", "Extra": ""})
    def test_extract_cloze_data_simple(self):
        """Cloze deletion data should be able to be extracted"""

        raw_string = """<span class="h-fcard e-cloze"><em>This</em></span>"""
        tag = "span"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        data = cloze.extract(root)

        self.assertEqual(data, {"Text": "{{c1::This}}", "Extra": ""})

        raw_string = """<span class="h-fcard e-cloze"><em>That</em></span>"""
        tag = "span"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        data = cloze.extract(root)

        self.assertEqual(data, {"Text": "{{c1::That}}", "Extra": ""})
    def test_extract_cloze_insert_id(self):
        """Cloze deletions have IDs to ensure stability"""

        raw_string = """<span class="h-fcard e-cloze">This <em>is</em> a <em>cloze</em> test</span>"""
        tag = "span"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        cloze.extract(root)

        self.assertEqual(root[0].attrib['data-id'], "1")
        self.assertEqual(root[1].attrib['data-id'], "2")
    def test_inject_Anki_ID(self):
        """Ability to inject Anki ID in elements"""

        raw_string = """<abbr title="Bit error rate" data-context="Communication" class="h-fcard">BER</abbr>"""
        tag = "abbr"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        root = f2f.inject_Anki_ID(root, 1234)

        fragment = ET.tostring(root, encoding="unicode")

        root = ET.fromstring(fragment)

        self.assertEqual(root.attrib["data-anki-id"], "1234")

        fragments = f2f.find_fragments(fragment, tag)

        root = ET.fromstring(fragments[0])

        data = f2f.extract_abbreviation_basic(root)

        self.assertEqual(data, {"Back": "Bit error rate", "Front": "BER"})
    def test_fragments_spanning_lines(self):
        """Ensure that fragments can span multiple lines"""

        raw_string = """
<dl class="h-fcard e-programming-syntax"
    data-language="Ada">
    <dt>Addition</dt>
    <dd><pre>2 + 2</pre></dd>
</dl>"""
        tag = "dl"
        fragments = f2f.find_fragments(raw_string, tag)

        self.assertEqual(fragments, ["""<dl class="h-fcard e-programming-syntax"
    data-language="Ada">
    <dt>Addition</dt>
    <dd><pre>2 + 2</pre></dd>
</dl>"""])
Exemplo n.º 12
0
    def test_extract_syntax(self):
        """Programming syntax should be extracted"""

        raw_string = """
<dl class="h-fcard e-programming-syntax"
    data-language="Ada">
    <dt>Addition</dt>
    <dd><pre>2 + 2</pre></dd>
</dl>"""
        tag = "dl"
        fragments = f2f.find_fragments(raw_string, tag)

        root = ET.fromstring(fragments[0])

        data = syntax.extract(root)

        self.assertEqual(data, {
            "Meaning": "Addition",
            "Syntax": "2 + 2",
            "Language": "Ada"
        })