Mercurial > hgrepos > Python > libs > pygments-lexer-pseudocode2
annotate tests/_testhelper.py @ 18:859ab8abce0a
The first real tests about lexing with the Pygments API for the tests.
Also implemented a simple helper to compare token streams with shorter code.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 20 Apr 2026 14:12:35 +0200 |
| parents | |
| 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) |
