# -*- coding: utf-8 -*-
# :-
# SPDX-FileCopyrightText: © 2026 Franz Glasner
# SPDX-License-Identifier: MIT
# :-

from _tsetup import ALGLEXERFILENAME, ALGLEXERCLASS

import unittest

import pygments
import pygments.lexers
import pygments.formatters

import _testhelper


class TestSnippets(unittest.TestCase, _testhelper.TokenAssertHelper):

    def setUp(self):
        self.lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, ALGLEXERCLASS)

    def test_lf(self):
        self.assertTokenStreamEqualComplete(
            [("Text.Whitespace", "\n")],
            pygments.lex("\n", self.lexer))

    def test_protected_lf(self):
        self.assertTokenStreamEqualComplete(
            [("Text", "\\\n")],
            pygments.lex("\\\n", self.lexer))

    def test_number_int(self):
        self.assertTokenStreamEqualComplete(
            [("Number.Integer", "10"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("10", self.lexer))

    def test_number_float_1(self):
        self.assertTokenStreamEqualComplete(
            [("Number.Float", "3.1415926"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("3.1415926", self.lexer))

    def test_number_float_2(self):
        self.assertTokenStreamEqualComplete(
            [("Number.Float", "3.14e-12"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("3.14e-12", self.lexer))

    def test_string_s_1(self):
        self.assertTokenStreamEqualComplete(
            [("String.Single", "'"),
             ("String.Single", "HU"),
             ("String.Single", '"'),
             ("String.Single", "HE HA"),
             ("String.Escape", "\\'"),
             ("String.Single", "HO"),
             ("String.Single", "'"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("""'HU"HE HA\\'HO'""", self.lexer))

    def test_string_s_2(self):
        self.assertTokenStreamEqual(
            [("String.Single", "'"),
             ("String.Single", "HUHU"),
             ("Error", "\n"),
             ],
            pygments.lex("'HUHU\nHEHE'", self.lexer))

    def test_string_ts_1(self):
        self.assertTokenStreamEqualComplete(
            [("String.Single", "'''"),
             ("String.Single", "HUHU HEHE"),
             ("String.Single", "'''"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("'''HUHU HEHE'''", self.lexer))

    def test_string_ts_2(self):
        self.assertTokenStreamEqualComplete(
            [("String.Single", "'''"),
             ("String.Single", "HI"),
             ("String.Single", "'"),
             ("String.Single", "HU"),
             ("String.Single", "\n"),
             ("String.Single", "HE"),
             ("String.Single", '"'),
             ("String.Single", "HA"),
             ("String.Single", "'''"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("""'''HI'HU\nHE"HA'''""", self.lexer))

    def test_string_d_1(self):
        self.assertTokenStreamEqualComplete(
            [("String.Double", '"'),
             ("String.Double", 'HU'),
             ("String.Double", "'"),
             ("String.Double", 'HE HA'),
             ("String.Escape", '\\"'),
             ("String.Double", 'HO'),
             ("String.Double", '"'),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex('''"HU'HE HA\\"HO"''', self.lexer))

    def test_string_d_2(self):
        self.assertTokenStreamEqual(
            [("String.Double", '"'),
             ("String.Double", "HUHU"),
             ("Error", "\n"),
             ],
            pygments.lex('"HUHU\nHEHE"', self.lexer))

    def test_string_td_1(self):
        self.assertTokenStreamEqualComplete(
            [("String.Double", '"""'),
             ("String.Double", 'HUHU HAHA'),
             ("String.Double", '"""'),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex('"""HUHU HAHA"""', self.lexer))

    def test_string_td_2(self):
        self.assertTokenStreamEqualComplete(
            [("String.Double", '"""'),
             ("String.Double", 'HU'),
             ("String.Double", '"'),
             ("String.Double", "HO"),
             ("String.Double", "\n"),
             ("String.Double", "HE"),
             ("String.Double", "'"),
             ("String.Double", "HA"),
             ("String.Double", '"""'),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex('''"""HU"HO\nHE'HA"""''', self.lexer))

    def test_proc(self):
        self.assertTokenStreamEqualComplete(
            [("Keyword", "PROCEDURE"),
             ("Text.Whitespace", " "),
             ("Name.Entity", "the name"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\PROC {the name}", self.lexer))

    def test_endproc(self):
        self.assertTokenStreamEqualComplete(
            [("Keyword", "END OF PROCEDURE"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\END-PROCEDURE", self.lexer))

    def test_endproc_with_entityname(self):
        self.assertTokenStreamEqualComplete(
            [("Keyword", "END OF PROCEDURE"),
             ("Text.Whitespace", " "),
             ("Name.Entity", "the procedure name"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\ENDPROCEDURE {the procedure name}", self.lexer))

    def test_proc_de(self):
        lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, "AlgPseudocodeLexer_DE")
        self.assertTokenStreamEqualComplete(
            [("Keyword", "PROZEDUR"),
             ("Text.Whitespace", " "),
             ("Name.Entity", " also {nichtxs"),
             ("Name.Entity", "\\}"),
             ("Name.Entity", " hier"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\PROC  { also {nichtxs\\} hier}", lexer))

    def test_function_1(self):
        self.assertTokenStreamEqualComplete(
            [("Keyword", "FUNCTION"),
             ("Text.Whitespace", " "),
             ("Name.Entity", "1"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\FUNC{1}", self.lexer))

    def test_function_2(self):
        self.assertTokenStreamEqualComplete(
            [("Keyword", "FUNCTION"),
             ("Text.Whitespace", " "),
             ("Name.Entity", "line 1\nline 2\n"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\FUNCTION{line 1\nline 2\n}", self.lexer))

    def test_function_3(self):
        self.assertTokenStreamEqualComplete(
            [("Keyword", "FUNCTION"),
             ("Text.Whitespace", " "),
             ("Name.Entity", "\\"),
             ("Name.Entity", "n"),
             ("Name.Entity", "\\}"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\FN {\\n\\}}", self.lexer))

    def test_class(self):
        self.assertTokenStreamEqualComplete(
            [("Keyword", "CLASS"),
             ("Text.Whitespace", " "),
             ("Name.Entity", "\\"),
             ("Name.Entity", "n"),
             ("Name.Entity", "\\}"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\CLASS {\\n\\}}", self.lexer))

    def test_class_fr(self):
        lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, "AlgPseudocodeLexer_FR")
        self.assertTokenStreamEqualComplete(
            [("Keyword", "CLASSE"),
             ("Text.Whitespace", " "),
             ("Name.Entity", "\\"),
             ("Name.Entity", "n"),
             ("Name.Entity", "\\}"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\CLASS {\\n\\}}", lexer))

    def test_class_de_with_noend_option(self):
        lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, "AlgPseudocodeLexer_DE", no_end="True")
        self.assertTokenStreamEqualComplete(
            [("Keyword", "KLASSE"),
             ("Text.Whitespace", " "),
             ("Name.Entity", "\\"),
             ("Name.Entity", "n"),
             ("Name.Entity", "\\}"),
             ("Text.Whitespace", "\n"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\CLASS {\\n\\}}\n\\ENDCLASS", lexer))

    def test_class_de_with_noend_option_and_name(self):
        lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, "AlgPseudocodeLexer_DE", no_end="True")
        self.assertTokenStreamEqualComplete(
            [("Keyword", "KLASSE"),
             ("Text.Whitespace", " "),
             ("Name.Entity", "\\"),
             ("Name.Entity", "n"),
             ("Name.Entity", "\\}"),
             ("Text.Whitespace", "\n"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\CLASS {\\n\\}}\n\\ENDCLASS {end class}", lexer))

    def test_remark_1(self):
        self.assertTokenStreamEqualComplete(
            [("Comment.Single", "▷"),
             ("Comment.Single", "  the remark"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex("\\REMARK  the remark\n", self.lexer))

    def test_remark_2(self):
        self.assertTokenStreamEqualComplete(
            [("Comment.Single", "▷"),
             ("Comment.Single", "  the remark 1"),
             ("Text.Whitespace", "\n"),
             ("Comment.Single", "▷"),
             ("Comment.Single", "  the remark 2"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex(
                """\\remArk  the remark 1
\\Rem  the remark 2
""", self.lexer))

    def test_expr_and_text(self):
        self.assertTokenStreamEqualComplete(
            [("Name.Entity", "a"),
             ("Text", " "),
             ("Text", "multiplied by"),
             ("Text", " "),
             ("Name.Entity", "b"),
             ("Text", " "),
             ("Text", "is"),
             ("Text", " "),
             ("Number.Integer", "0"),
             ("Text", "  "),
             ("Text", "mod"),
             ("Text", " "),
             ("Name.Entity", "p"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex(
                r"""a \TEXT{multiplied by} b \text{is} 0  \text{mod} p""",
                self.lexer))

    def test_expr_and_text_with_escaped_characters(self):
        self.assertTokenStreamEqualComplete(
            [("Name.Entity", "a"),
             ("Text", " "),
             ("Text", "multiplied by {escaped"),
             ("Text", "}"),
             ("Text", " "),
             ("Text", " "),
             ("Name.Entity", "b"),
             ("Text", " "),
             ("Text", "is "),
             ("Text", "\\"),
             ("Text", " not"),
             ("Text", " "),
             ("Number.Integer", "0"),
             ("Text", "  "),
             ("Text", "mod"),
             ("Text", " "),
             ("Name.Entity", "p"),
             ("Text.Whitespace", "\n"),
             ],
            pygments.lex(
                r"""a \TEXT{multiplied by {escaped\} } b \text{is \ not} 0"""
                r"  \text{mod} p""",
                self.lexer))


class PygmentizeCompletely(unittest.TestCase):

    def setUp(self):
        self.lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, ALGLEXERCLASS)

    def test_pygmentize_html(self):
        html_formatter = pygments.formatters.get_formatter_by_name("html")
        highlighted = pygments.highlight(
            '"""FOO"""',
            self.lexer,
            html_formatter,
            outfile=None)
        self.assertTrue(highlighted.startswith("""<div class="highlight">"""))

    def test_pygmentize_latex(self):
        latex_formatter = pygments.formatters.get_formatter_by_name("latex")
        highlighted = pygments.highlight(
            '"""FOO"""',
            self.lexer,
            latex_formatter,
            outfile=None)
        self.assertTrue(highlighted.startswith(r"""\begin{Verbatim}"""))


if __name__ == "__main__":
    unittest.main()
