File: test_filter.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 (102 lines) | stat: -rwxr-xr-x 3,594 bytes parent folder | download | duplicates (3)
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
#
# test_filter.py <Peter.Bienstman@UGent.be>
#

import subprocess as sp

from nose.tools import raises
from unittest import mock

from mnemosyne_test import MnemosyneTest
from mnemosyne.libmnemosyne.filter import Filter
from mnemosyne.libmnemosyne.filters.html5_video import Html5Video
from mnemosyne.libmnemosyne.filters.html5_audio import Html5Audio
from mnemosyne.libmnemosyne.filters.RTL_handler import RTLHandler
from mnemosyne.libmnemosyne.filters.expand_paths import ExpandPaths
from mnemosyne.libmnemosyne.filters.escape_to_html import EscapeToHtml
from mnemosyne.libmnemosyne.filters.latex import CheckForUpdatedLatexFiles, Latex

side_effects = [FileNotFoundError, sp.TimeoutExpired(cmd='foo', timeout=5),
                sp.CalledProcessError(cmd='foo', returncode=1)]
check_output_mock = mock.Mock(side_effect=side_effects)
check_call_mock = mock.Mock(side_effect=side_effects)

class TestFilter(MnemosyneTest):

    @raises(NotImplementedError)
    def test(self):
        f = Filter(None)
        f.run("", None, None)

    def test_html5_audio(self):

        f = Html5Audio(self.mnemosyne.component_manager)

        self.config()["media_autoplay"] = True
        self.config()["media_controls"] = True

        f.run("""<audio src="b">""", None, None)

        self.config()["media_autoplay"] = False
        self.config()["media_controls"] = True

        f.run("""<audio src="b">""", None, None)

    def test_html5_video(self):

        f = Html5Video(self.mnemosyne.component_manager)

        self.config()["media_autoplay"] = True
        self.config()["media_controls"] = True

        assert f.run("""<video src="b">""", None, None) == \
              """<video src="b" autoplay=1 controls=1>"""

        self.config()["media_autoplay"] = False
        self.config()["media_controls"] = True

        assert f.run("""<video src="b">""", None, None) == \
              """<video src="b" controls=1>"""

    def test_escape_to_html(self):

         f = EscapeToHtml(self.mnemosyne.component_manager)

         assert f.run("a\nb", None, None) == "a<br>b"
         assert f.run("<latex>a\nb<\latex>", None, None) == "<latex>a\nb<\latex>"
         
    def test_expand_paths(self):

        f = ExpandPaths(self.mnemosyne.component_manager)
         
        assert "media" not in \
               f.run("""data=trainingData, method=\"rpart\"""", None, None)
        assert "media" in \
               f.run("""data=\"rpart\"""", None, None)
        assert "media" in \
               f.run("""data= \"rpart\"""", None, None)        
        assert "media" not in \
               f.run("""Application data = \"rpart\"""", None, None) 
        
    def test_RTL_handler(self):

        f = RTLHandler(self.mnemosyne.component_manager)

        f.run(chr(0x0591), None, None)
        f.run(chr(0x0491), None, None)
        f.run("[a]" + chr(0x0491), None, None)


    @mock.patch('mnemosyne.libmnemosyne.filters.latex.sp.check_output',
                check_output_mock)
    @mock.patch('mnemosyne.libmnemosyne.filters.latex.sp.check_call',
                check_call_mock)
    def test_latex_exceptions(self):
        for _ in side_effects:
            f = CheckForUpdatedLatexFiles(self.mnemosyne.component_manager)
            assert f.is_working() is False

            f = Latex(self.mnemosyne.component_manager)
            # Should not raise an exception
            f._call_cmd(['dummy', 'cmd'],
                        'dot_test/default.db_media/latex_out.txt')