File: test_render_chain.py

package info (click to toggle)
mnemosyne 2.7.3%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,804 kB
  • sloc: python: 34,077; ansic: 837; xml: 625; makefile: 249; sh: 63
file content (146 lines) | stat: -rwxr-xr-x 5,352 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#
# test_render_chain.py <Peter.Bienstman@UGent.be>
#

import os

from mnemosyne_test import MnemosyneTest
from mnemosyne.libmnemosyne.filter import Filter
from mnemosyne.libmnemosyne.renderer import Renderer


class TestRenderChain(MnemosyneTest):

    def test_add_filter(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
            grade=-1, tag_names=["default"])[0]

        class MyFilter(Filter):
            def run(self, text, card, fact_key):
                return "666"

        self.render_chain().register_filter(MyFilter)

        assert "666" in card.question()
        assert isinstance(self.render_chain().filter(MyFilter), MyFilter)
        assert self.render_chain().filter(TestRenderChain) is None

        assert type(self.render_chain()._filters[0]) \
               != type(MyFilter(self.mnemosyne.component_manager))

        self.render_chain().unregister_filter(MyFilter)
        assert "666" not in card.question()

        self.render_chain().unregister_filter(type(1))

    def test_add_filter_2(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
            grade=-1, tag_names=["default"])[0]

        class MyFilter(Filter):
            def run(self, text, card, fact_key):
                return "666"

        self.render_chain().register_filter(MyFilter, in_front=True)
        assert "666" in card.question()

        assert type(self.render_chain()._filters[0]) \
               == type(MyFilter(self.mnemosyne.component_manager))

    def test_add_filter_order(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
            grade=-1, tag_names=["default"])[0]

        class MyFilter1(Filter):
            def run(self, text, card, fact_key):
                return "666"

        class MyFilter2(Filter):
            def run(self, text, card, fact_key):
                return "[%s]" % text

        class MyFilter3(Filter):
            def run(self, text, card, fact_key):
                return "(%s)" % text

        class MyFilter4(Filter):
            def run(self, text, card, fact_key):
                return "{%s}" % text

        def equals(filter_class):
            ty = type(filter_class(self.mnemosyne.component_manager))
            def eq(x):
                return type(x) == ty
            return eq

        self.render_chain().register_filter_at_front(MyFilter1)
        assert "666" in card.question()

        self.render_chain().register_filter_at_front(MyFilter2, [MyFilter1.__name__])
        assert "[666]" in card.question()

        self.render_chain().register_filter_at_back(MyFilter3, [MyFilter2.__name__])
        assert "[(666)]" in card.question()

        self.render_chain().register_filter_at_front(MyFilter4, [MyFilter3.__name__])
        assert "[{(666)}]" in card.question()

        assert type(self.render_chain()._filters[0]) \
               == type(MyFilter1(self.mnemosyne.component_manager))

        assert type(self.render_chain()._filters[1]) \
               == type(MyFilter3(self.mnemosyne.component_manager))

        assert type(self.render_chain()._filters[2]) \
               == type(MyFilter4(self.mnemosyne.component_manager))

        assert type(self.render_chain()._filters[3]) \
               == type(MyFilter2(self.mnemosyne.component_manager))

    def test_add_card_type_renderer(self):
        fact_data = {"f": "question",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
            grade=-1, tag_names=["default"])[0]

        class MyRenderer(Renderer):
            used_for = card_type_1
            def render(self, fact_data, fields, card_type, **render_args):
                return "666"

        self.render_chain().register_renderer(MyRenderer)
        assert "666" in card.question()

        card_type_2 = self.card_type_with_id("2")
        card = self.controller().create_new_cards(fact_data, card_type_2,
            grade=-1, tag_names=["default"])[0]
        assert "666" not in card.question()

        self.render_chain().unregister_renderer(MyRenderer)
        assert "666" not in card.question()

        self.render_chain().unregister_renderer(type(1))

    def test_latex(self):
        fact_data = {"f": "<latex>1<2</latex>",
                     "b": "answer"}
        card_type_1 = self.card_type_with_id("1")
        card = self.controller().create_new_cards(fact_data, card_type_1,
            grade=-1, tag_names=["default"])[0]
        card.question()

        filename = os.path.join(os.path.abspath("dot_test"),
            "default.db_media", "_latex", "tmp.tex")
        contents = "".join(open(filename).readlines())
        assert '<' in contents
        assert "&lt;" not in contents