summaryrefslogtreecommitdiffstats
path: root/lang/python/python-pygments/test.sh
blob: 45aedb62aa0a82c263f00d94bb914cbe0e6e6419 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/sh

[ "$1" = "python3-pygments" ] || exit 0

python3 - << EOF
import sys
import pygments
from pygments import highlight
from pygments.lexers import PythonLexer, get_lexer_by_name
from pygments.formatters import HtmlFormatter, NullFormatter

if pygments.__version__ != "$2":
    print("Wrong version: " + pygments.__version__)
    sys.exit(1)

code = "def hello(name):\n    print('Hello, ' + name)\n"

# Test basic highlighting to HTML
formatter = HtmlFormatter()
result = highlight(code, PythonLexer(), formatter)
assert '<span' in result, "Expected HTML span tags in output"
assert 'hello' in result, "Expected function name in output"

# Test getting lexer by name
lexer = get_lexer_by_name("python")
assert lexer is not None, "Expected to get Python lexer by name"

# Test highlighting to plain text (NullFormatter strips markup)
plain = highlight(code, PythonLexer(), NullFormatter())
assert 'hello' in plain, "Expected function name in plain output"

# Test CSS generation
css = formatter.get_style_defs('.highlight')
assert '.highlight' in css, "Expected CSS class in style defs"

sys.exit(0)
EOF