Ejemplo n.º 1
0
 def fmt(data: Data) -> BaseText:
     return Text(
         list_join([child(data) for child in children],
                   sep=sep,
                   sep2=sep2,
                   last_sep=last_sep,
                   other=other))
Ejemplo n.º 2
0
def test_hello_brave_world() -> None:
    s = Text([
        String('hello '),
        Tag(TagType.STRONG, String('brave')),
        String(' world!')
    ])
    assert ''.join(render_plaintext(s)) == 'hello brave world!'
    assert ''.join(render_html(s)) == 'hello <strong>brave</strong> world!'
    assert ''.join(render_markdown(s)) == r'hello **brave** world\!'
    assert ''.join(render_rst(s)) == r'hello **brave** world!'
Ejemplo n.º 3
0
def test_escape() -> None:
    s = Text([
        String('hello '),
        Tag(TagType.STRONG, String('"<[*]>"')),
        String(' world!')
    ])
    assert ''.join(render_plaintext(s)) == 'hello "<[*]>" world!'
    assert ''.join(render_html(s)) == \
           'hello <strong>&quot;&lt;[*]&gt;&quot;</strong> world!'
    assert ''.join(render_markdown(s)) == r'hello **"<\[\*\]>"** world\!'
    assert ''.join(render_rst(s)) == r'hello **"<[\*]>"** world!'
Ejemplo n.º 4
0
def test_protocol() -> None:
    template: Node[PersonProtocol] = join([
        tag(TagType.EMPHASIS, name()),
        str_(': '),
        tag(TagType.STRONG, birthday("%b %d, %Y"))])
    person1 = Person(
        name='John', birthday=datetime.date(year=1998, month=3, day=7))
    person2 = PersonWithGender(
        name='Mary', birthday=datetime.date(year=1997, month=6, day=22),
        female=True)
    text1 = template(person1)
    text2 = template(person2)
    assert text1 == Text([
        Tag(TagType.EMPHASIS, _s('John')),
        _s(': '),
        Tag(TagType.STRONG, _s('Mar 07, 1998'))])
    assert text2 == Text([
        Tag(TagType.EMPHASIS, _s('Mary')),
        _s(': '),
        Tag(TagType.STRONG, _s('Jun 22, 1997'))])
Ejemplo n.º 5
0

# helper function for constructing test cases
def _s(value: str) -> String:
    return String(value)


# helper function for constructing test cases
def _t(value: str) -> Tag:
    return Tag(TagType.CODE, String(value))


@pytest.mark.parametrize(
    "text,func,result_map,result_functor_map,result_raw", [
        (
            Text([_s("one "), _t("two"), _s(" three")]),
            lambda x: "*" + x + "*",
            ['*one *', '*two*', '* three*'],
            Text([_s("*one *"), _t("*two*"),
                  _s("* three*")]),
            'one two three',
        ),
    ])
def test_text_map(text: BaseText, func: Callable[[str],
                                                 str], result_map: List[str],
                  result_functor_map: BaseText, result_raw: str):
    assert list(text_map(text, func)) == result_map
    assert text_functor_map(text, func) == result_functor_map
    assert text_raw(text) == result_raw

Ejemplo n.º 6
0
def test_capfirst():
    template = capfirst(join([str_(''), str_data(), str_(' world')]))
    assert template('hello') == Text([_s(''), _t('Hello'), _s(' world')])
Ejemplo n.º 7
0
def test_template() -> None:
    template: Node[str] = join([str_data(), str_(' world')])
    assert template('hello') == Text([_t('hello'), _s(' world')])
Ejemplo n.º 8
0
def test_upper():
    template = upper(join([str_(''), str_data(), str_(' WORLD')]))
    assert template('heLLo') == Text([_s(''), _t('HELLO'), _s(' WORLD')])
Ejemplo n.º 9
0
def test_lower():
    template = lower(join([str_(''), str_data(), str_(' WORLD')]))
    assert template('heLLo') == Text([_s(''), _t('hello'), _s(' world')])
Ejemplo n.º 10
0
def test_capitalize():
    template = capitalize(join([str_(''), str_data(), str_(' WORLD')]))
    assert template('heLLo') == Text([_s(''), _t('Hello'), _s(' world')])