2
0
Fork 0
textout/tests/test_lexer.py

127 lines
3.7 KiB
Python

#!/usr/bin/env python
# *****************************************************************************
# Copyright (C) 2023 Thomas Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
# *****************************************************************************
"""Lexer tests for textoutpc."""
from __future__ import annotations
from collections.abc import Sequence
from io import StringIO
import pytest
from textoutpc.lexer import (
CloseTagEntity,
Entity,
NewlineEntity,
OpenTagEntity,
SpecialEntity,
TextEntity,
iter_textout_entities,
)
@pytest.mark.parametrize(
"inp,expected",
(
("abc", (TextEntity(content="abc"),)),
("a" * 1500, (TextEntity(content="a" * 1500),)),
("[]", (TextEntity(content="[]"),)),
("[ /hello]", (CloseTagEntity(name="hello"),)),
(
"[b][i]hello[/b]omg",
(
OpenTagEntity(name="b"),
OpenTagEntity(name="i"),
TextEntity(content="hello"),
CloseTagEntity(name="b"),
TextEntity(content="omg"),
),
),
("[mytag]", (OpenTagEntity(name="mytag"),)),
("[mytag=value]", (OpenTagEntity(name="mytag", value="value"),)),
(
"[mytag=value=other]",
(OpenTagEntity(name="mytag", value="value=other"),),
),
("[hello[]]", (OpenTagEntity(name="hello[]"),)),
(
"[hello[][]=world[][ohno]]what",
(
OpenTagEntity(name="hello[][]", value="world[][ohno]"),
TextEntity(content="what"),
),
),
(
"[hello=]",
(OpenTagEntity(name="hello", value=""),),
),
(
"``k",
(
SpecialEntity(value="`"),
SpecialEntity(value="`"),
TextEntity(content="k"),
),
),
(
"a[mytag]b[/myothertag]",
(
TextEntity(content="a"),
OpenTagEntity(name="mytag"),
TextEntity(content="b"),
CloseTagEntity(name="myothertag"),
),
),
("\n\r\n", (NewlineEntity(), NewlineEntity())),
(
"[" + "w" * 33 + "]",
(TextEntity(content="[" + "w" * 33 + "]"),),
),
(
# Partial result cannot be of the maximum entity size.
"w" * 1000 + "[" + "w" * 512,
(TextEntity(content="w" * 1000 + "[" + "w" * 512),),
),
(
"w" * 1000 + "[" + "a" * 50,
(TextEntity(content="w" * 1000 + "[" + "a" * 50),),
),
(
"[hello=" + "w" * 256 + "]",
(OpenTagEntity(name="hello", value="w" * 256),),
),
(
"[hello=" + "w" * 257 + "]",
(TextEntity(content="[hello=" + "w" * 257 + "]"),),
),
(
"[" + "w" * 33 + "]",
(TextEntity(content="[" + "w" * 33 + "]"),),
),
(
"[/" + "w" * 33 + "]",
(TextEntity(content="[/" + "w" * 33 + "]"),),
),
(
"[" * 19 + "]" * 18,
(
TextEntity(content="[["),
OpenTagEntity(name="[" * 16 + "]" * 16),
TextEntity(content="]"),
),
),
),
)
def test_lex(inp: str, expected: Sequence[Entity]):
"""Test lexing an input string to get a sequence."""
assert tuple(iter_textout_entities(inp)) == tuple(expected)
def test_stringio_lex():
"""Test lexing from a string input string."""
stream = StringIO("[abc]")
assert tuple(iter_textout_entities(stream)) == (OpenTagEntity(name="abc"),)