# -*- 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 _testhelper
import pygments_lexer_pseudocode2.filters


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

    def test_prohibit_raiseonerror_filter(self):
        lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, ALGLEXERCLASS,
            prohibit_raiseonerror_filter=True)
        lexer.add_filter("raiseonerror")
        self.assertFalse(lexer.filters)

    def test_sphinx_default(self):
        lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, ALGLEXERCLASS)
        lexer.add_filter("raiseonerror")
        self.assertTrue(lexer.filters)

    def test_error(self):
        lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, ALGLEXERCLASS)
        self.assertTokenStreamEqual(
            [("Error", "\\"),
             ],
            pygments.lex(r"\EXPR{", lexer))

    def test_error_to_genericerror_filter(self):
        #
        # Simulate the call sequence as done in Sphinx.
        # Cannot use the filter name here because tests are run without
        # the package to be "installed".
        #
        filter = (pygments_lexer_pseudocode2.filters
                  .ErrorToGenericErrorTokenFilter())
        lexer = pygments.lexers.load_lexer_from_file(
            ALGLEXERFILENAME, ALGLEXERCLASS,
            filters=(filter,))
        lexer.add_filter("raiseonerror")
        self.assertEqual(2, len(lexer.filters))
        self.assertTokenStreamEqual(
            [("Generic.Error", "\\"),
             ],
            pygments.lex(r"\EXPR{", lexer))


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