File: test_markup.py

package info (click to toggle)
manimpango 0.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,804 kB
  • sloc: python: 1,252; sh: 164; makefile: 18
file content (121 lines) | stat: -rw-r--r-- 3,546 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
# -*- coding: utf-8 -*-
from pathlib import Path

import pytest

import manimpango

from . import CASES_DIR
from ._manim import MarkupText
from .svg_tester import SVGStyleTester

ipsum_text = (
    "<b>Lorem ipsum dolor</b> sit amet, <i>consectetur</i> adipiscing elit,"
    "sed do eiusmod tempor incididunt ut labore et dolore"
    "magna aliqua. Ut enim <b>ad</b> minim veniam, quis nostrud"
    "exercitation ullamco laboris nisi ut aliquip"
    "ex ea commodo consequat. Duis aute irure dolor"
    "in reprehenderit in voluptate velit esse cillum"
    "dolore eu fugiat nulla pariatur. Excepteur sint"
    "occaecat cupidatat non proident, sunt in culpa qui"
    "officia deserunt mollit anim id est laborum."
)


@pytest.mark.parametrize("text", ["foo", "<b>bar</b>", "வணக்கம்"])
def test_good_markup(text):
    assert not manimpango.MarkupUtils.validate(
        text,
    ), f"{text} should not fail validation"


@pytest.mark.parametrize("text", ["<b>foo", "<xyz>foo</xyz>"])
def test_bad_markup(text):
    assert manimpango.MarkupUtils.validate(
        text
    ), f"{text} should fail validation (unbalanced tags)"


@pytest.mark.parametrize(
    "text,error",
    [
        (
            "<b>foo",
            "Error on line 1 char 23: Element “markup” was closed, "
            "but the currently open element is “b”",
        ),
        (
            "<xyz>foo</xyz>",
            "Unknown tag 'xyz' on line 1 char 14",
        ),
    ],
)
def test_bad_markup_error_message(text, error):
    assert manimpango.MarkupUtils.validate(text) == error


def test_markup_text(tmpdir):
    loc = Path(tmpdir, "test.svg")
    assert not loc.exists()
    MarkupText(
        '<span underline="error"><b><i>Hello Manim</i></b></span>', filename=str(loc)
    )
    assert loc.exists()


def test_markup_justify(tmpdir):
    # don't know how to verify this correctly
    # it varies upon diffent system so, we are
    # just check whether it runs
    loc = Path(tmpdir, "test.svg")
    assert not loc.exists()
    MarkupText(ipsum_text, justify=True, filename=str(loc))
    assert loc.exists()


def test_markup_indent(tmpdir):
    # don't know how to verify this correctly
    # it varies upon diffent system so, we are
    # just check whether it runs
    loc = Path(tmpdir, "test.svg")
    assert not loc.exists()
    MarkupText(ipsum_text, indent=10, filename=str(loc))
    assert loc.exists()


def test_markup_alignment(tmpdir):
    # don't know how to verify this correctly
    # it varies upon diffent system so, we are
    # just check whether it runs
    loc = Path(tmpdir, "test.svg")
    assert not loc.exists()
    MarkupText(
        ipsum_text,
        alignment=manimpango.Alignment.CENTER,
        filename=str(loc),
    )
    assert loc.exists()


def test_markup_style(tmpdir):
    test_case = CASES_DIR / "hello_blue_world_green.svg"
    expected = tmpdir / "expected.svg"
    text = "<span foreground='BLUE'>Hello</span>\n<span foreground='GREEN'>World</span>"
    MarkupText(
        text,
        filename=str(expected),
    )
    s = SVGStyleTester(gotSVG=expected, expectedSVG=test_case)
    assert len(s.got_svg_style) == len(s.expected_svg_style)
    assert s.got_svg_style == s.expected_svg_style


def test_wrap_text(tmpdir):
    tmpdir = Path(tmpdir)
    wrapped = tmpdir / "wrap.svg"
    nowrap = tmpdir / "nowarap.svg"

    MarkupText(ipsum_text, wrap_text=False, filename=str(nowrap))
    MarkupText(ipsum_text, filename=str(wrapped))

    assert wrapped.read_text() != nowrap.read_text()