test_emph.TestEmph.test_merge_emph2()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
from copy import deepcopy
2
from functools import partial
3
from pathlib import Path
4
from unittest import TestCase
5
6
import panflute as pf
7
from panflute.tools import convert_text
8
9
from amsthm.helper import cancel_repeated_type, merge_consecutive_type, to_type
10
11
DIR = Path(__file__).parent
12
13
14
class TestEmph(TestCase):
15
    ElementType = pf.Emph
16
17
    def setUp(self):
18
        self.simple = pf.Para(pf.Str("a"), pf.Space, pf.Str("b"))
19
        self.ast = pf.Para(self.ElementType(pf.Str("a")), self.ElementType(pf.Str("b")))
20
        self.ast2 = pf.Para(self.ElementType(pf.Str("a")), pf.Space, self.ElementType(pf.Str("b")))
21
        self.ast_double_type = pf.Para(
22
            self.ElementType(self.ElementType(pf.Str("a"))), pf.Space, self.ElementType(pf.Str("b"))
23
        )
24
        self.combined = pf.Para(
25
            self.ElementType(pf.Str("a")),
26
            self.ElementType(pf.Str("b")),
27
            pf.Space,
28
            pf.Str("c"),
29
            pf.Str("d"),
30
        )
31
        self.to_type = partial(to_type, ElementType=self.ElementType)
32
        self.cancel_repeated_type = partial(cancel_repeated_type, ElementType=self.ElementType)
33
        self.merge_consecutive_type = partial(merge_consecutive_type, ElementType=self.ElementType)
34
35
    def test_to_emph_simple(self):
36
        ast = deepcopy(self.simple)
37
        ast.walk(self.to_type)
38
        res = convert_text(ast, input_format="panflute", output_format="native")
39
        ref = pf.Para(self.ElementType(pf.Str("a")), pf.Space, self.ElementType(pf.Str("b")))
40
        ref_native = convert_text(ref, input_format="panflute", output_format="native")
41
        assert res == ref_native
42
43
    def test_cancel_emph(self):
44
        ast = deepcopy(self.ast_double_type)
45
        ast.walk(self.cancel_repeated_type)
46
        res = convert_text(ast, input_format="panflute", output_format="native")
47
        ref = pf.Para(pf.Str("a"), pf.Space, self.ElementType(pf.Str("b")))
48
        ref_native = convert_text(ref, input_format="panflute", output_format="native")
49
        assert res == ref_native
50
51
    def test_merge_emph(self):
52
        ast = deepcopy(self.ast)
53
        ast.walk(self.merge_consecutive_type)
54
        res = convert_text(ast, input_format="panflute", output_format="native")
55
        ref = pf.Para(self.ElementType(pf.Str("a"), pf.Str("b")))
56
        ref_native = convert_text(ref, input_format="panflute", output_format="native")
57
        assert res == ref_native
58
59
    def test_merge_emph2(self):
60
        ast = deepcopy(self.ast2)
61
        ast.walk(self.merge_consecutive_type)
62
        res = convert_text(ast, input_format="panflute", output_format="native")
63
        ref = pf.Para(self.ElementType(pf.Str("a"), pf.Space, pf.Str("b")))
64
        ref_native = convert_text(ref, input_format="panflute", output_format="native")
65
        assert res == ref_native
66
67
    def test_all_emph_together(self):
68
        ast = deepcopy(self.combined)
69
        ast.walk(self.to_type)
70
        ast.walk(self.cancel_repeated_type)
71
        ast.walk(self.merge_consecutive_type)
72
        res = convert_text(ast, input_format="panflute", output_format="native")
73
        ref = pf.Para(pf.Str("a"), pf.Str("b"), pf.Space, self.ElementType(pf.Str("c"), pf.Str("d")))
74
        ref_native = convert_text(ref, input_format="panflute", output_format="native")
75
        assert res == ref_native
76
77
78
# works with most but not all of inline elements as well:
79
# https://github.com/jgm/pandoc-types/blob/master/src/Text/Pandoc/Definition.hs
80
81
82
class TestUnderline(TestEmph):
83
    ElementType = pf.Underline
84
85
86
class TestStrong(TestEmph):
87
    ElementType = pf.Strong
88
89
90
class TestStrikeout(TestEmph):
91
    ElementType = pf.Strikeout
92
93
94
class TestSubscript(TestEmph):
95
    ElementType = pf.Superscript
96
97
98
class TestSubscript(TestEmph):
99
    ElementType = pf.Subscript
100
101
102
class TestSmallCaps(TestEmph):
103
    ElementType = pf.SmallCaps
104
105
106
class TestQuoted(TestEmph):
107
    ElementType = pf.Quoted
108
109
110
class TestCite(TestEmph):
111
    ElementType = pf.Cite
112
113
114
class TestLink(TestEmph):
115
    ElementType = pf.Link
116
117
118
class TestImage(TestEmph):
119
    ElementType = pf.Image
120
121
122
class TestSpan(TestEmph):
123
    ElementType = pf.Span
124