| 1 |
import bz2 |
| 2 |
import gzip |
| 3 |
|
| 4 |
from pygments import highlight |
| 5 |
from pygments.formatters import HtmlFormatter, RawTokenFormatter |
| 6 |
from pygments.lexers import PythonLexer, RawTokenLexer |
| 7 |
|
| 8 |
|
| 9 |
def test_raw_token(): |
| 10 |
code = "2 + α" |
| 11 |
raw = highlight(code, PythonLexer(), RawTokenFormatter()) |
| 12 |
html = highlight(code, PythonLexer(), HtmlFormatter()) |
| 13 |
|
| 14 |
assert highlight(raw, RawTokenLexer(), RawTokenFormatter()) == raw |
| 15 |
assert highlight(raw, RawTokenLexer(), HtmlFormatter()) == html |
| 16 |
assert highlight(raw.decode(), RawTokenLexer(), HtmlFormatter()) == html |
| 17 |
|
| 18 |
raw_gz = highlight(code, PythonLexer(), RawTokenFormatter(compress="gz")) |
| 19 |
assert gzip.decompress(raw_gz) == raw |
| 20 |
assert highlight(raw_gz, RawTokenLexer(compress="gz"), RawTokenFormatter()) == raw |
| 21 |
assert ( |
| 22 |
highlight( |
| 23 |
raw_gz.decode("latin1"), RawTokenLexer(compress="gz"), RawTokenFormatter() |
| 24 |
) |
| 25 |
== raw |
| 26 |
) |
| 27 |
|
| 28 |
raw_bz2 = highlight(code, PythonLexer(), RawTokenFormatter(compress="bz2")) |
| 29 |
assert bz2.decompress(raw_bz2) == raw |
| 30 |
assert highlight(raw_bz2, RawTokenLexer(compress="bz2"), RawTokenFormatter()) == raw |
| 31 |
assert ( |
| 32 |
highlight( |
| 33 |
raw_bz2.decode("latin1"), RawTokenLexer(compress="bz2"), RawTokenFormatter() |
| 34 |
) |
| 35 |
== raw |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def test_invalid_raw_token(): |
| 40 |
# These should not throw exceptions. |
| 41 |
assert ( |
| 42 |
highlight("Tolkien", RawTokenLexer(), RawTokenFormatter()) |
| 43 |
== b"Token.Error\t'Tolkien\\n'\n" |
| 44 |
) |
| 45 |
assert ( |
| 46 |
highlight("Tolkien\t'x'", RawTokenLexer(), RawTokenFormatter()) |
| 47 |
== b"Token\t'x'\n" |
| 48 |
) |
| 49 |
assert ( |
| 50 |
highlight("Token.Text\t42", RawTokenLexer(), RawTokenFormatter()) |
| 51 |
== b"Token.Error\t'Token.Text\\t42\\n'\n" |
| 52 |
) |
| 53 |
assert ( |
| 54 |
highlight("Token.Text\t'", RawTokenLexer(), RawTokenFormatter()) |
| 55 |
== b'Token.Error\t"Token.Text\\t\'\\n"\n' |
| 56 |
) |
| 57 |
assert ( |
| 58 |
highlight("Token.Text\t'α'", RawTokenLexer(), RawTokenFormatter()) |
| 59 |
== b"Token.Text\t'\\u03b1'\n" |
| 60 |
) |
| 61 |
assert ( |
| 62 |
highlight("Token.Text\tu'α'", RawTokenLexer(), RawTokenFormatter()) |
| 63 |
== b"Token.Text\t'\\u03b1'\n" |
| 64 |
) |
| 65 |
assert ( |
| 66 |
highlight(b"Token.Text\t'\xff'", RawTokenLexer(), RawTokenFormatter()) |
| 67 |
== b"Token.Text\t'\\xff'\n" |
| 68 |
) |