Mercurial > hgrepos > Python > libs > pygments-lexer-pseudocode2
annotate tests/_testhelper.py @ 56:661461fb4dfc
Make the "py-name" rules parameterized: allow to provide the token type.
For this to work the implemting function must be global, because the
access to the class is not yet possible at construction time.
So consistently make some previons LexBase members module globals.
Make some LexBase members module globals consistently.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 27 Apr 2026 12:37:27 +0200 |
| parents | 859ab8abce0a |
| children |
| rev | line source |
|---|---|
|
18
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
2 # :- |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
3 # SPDX-FileCopyrightText: © 2026 Franz Glasner |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
4 # SPDX-License-Identifier: MIT |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
5 # :- |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
6 """Test helper""" |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
7 |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
8 __all__ = ["TokenAssertHelper"] |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
9 |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
10 |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
11 from pygments.token import Token, is_token_subtype, string_to_tokentype |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
12 |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
13 |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
14 class TokenAssertHelper(object): |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
15 """Mixin to test for token stream equality""" |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
16 |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
17 def assertTokenEqual(self, tok_or_str, txt, token): |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
18 if is_token_subtype(tok_or_str, Token): |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
19 t = tok_or_str |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
20 else: |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
21 t = string_to_tokentype(tok_or_str) |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
22 self.assertEqual((t, txt), token) |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
23 |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
24 def assertNextTokenEqual(self, tok_or_str, txt, tokens): |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
25 self.assertTokenEqual(tok_or_str, txt, next(tokens)) |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
26 |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
27 def assertTokenStreamEqual(self, expected_tokens, given_tokens): |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
28 for tok, txt in expected_tokens: |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
29 self.assertNextTokenEqual(tok, txt, given_tokens) |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
30 |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
31 def assertTokenStreamEqualComplete(self, expected_tokens, given_tokens): |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
32 self.assertTokenStreamEqual(expected_tokens, given_tokens) |
|
859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
33 self.assertRaises(StopIteration, next, given_tokens) |
