mud/tests/test_highlight.py
Jared Miller b0fcb080d3
Wire client capabilities into Player & terrain
Parse MTTS from telnetlib3 writer during connection and store capabilities
on Player.caps field. Add convenience property Player.color_depth that
delegates to caps.color_depth for easy access by rendering code.

Changes:
- Add caps field to Player with default 16-color ANSI capabilities
- Parse MTTS in server shell after Player creation using parse_mtts()
- Add Player.color_depth property for quick capability checks
- Add tests verifying Player caps integration and color_depth property
2026-02-07 22:44:45 -05:00

125 lines
4.4 KiB
Python

"""Tests for syntax highlighting module."""
from mudlib.render.highlight import highlight
def test_highlight_python_returns_ansi_codes():
"""Test that Python code returns string with ANSI escape codes."""
code = "def foo():\n return 42"
result = highlight(code, language="python")
# Should contain ANSI escape codes
assert "\033[" in result
# Should contain the original code somewhere
assert "def" in result
assert "foo" in result
def test_highlight_toml_returns_ansi_codes():
"""Test that TOML code returns string with ANSI escape codes."""
code = '[section]\nkey = "value"'
result = highlight(code, language="toml")
# Should contain ANSI escape codes
assert "\033[" in result
# Should contain the original code somewhere
assert "section" in result
assert "key" in result
def test_highlight_truecolor_depth():
"""Test with color_depth=truecolor uses appropriate formatter."""
code = "x = 1"
result = highlight(code, language="python", color_depth="truecolor")
# Should contain ANSI codes (either 256-color or truecolor format)
assert "\033[" in result
# Should contain the code
assert "x" in result
def test_highlight_256_depth():
"""Test with color_depth=256 uses 256-color formatter."""
code = "x = 1"
result = highlight(code, language="python", color_depth="256")
# Should contain ANSI codes
assert "\033[" in result
# Should contain the code
assert "x" in result
def test_highlight_16_depth():
"""Test with color_depth=16 uses basic terminal formatter."""
code = "x = 1"
result = highlight(code, language="python", color_depth="16")
# Should contain ANSI codes
assert "\033[" in result
# Should contain the code
assert "x" in result
def test_highlight_unknown_language_returns_original():
"""Test unknown language returns original text unmodified (no crash)."""
code = "some random text"
result = highlight(code, language="unknown_language_xyz")
# Should return original text unchanged
assert result == code
# Should not contain ANSI codes
assert "\033[" not in result
def test_highlight_empty_string():
"""Test empty string input returns empty string."""
result = highlight("", language="python")
assert result == ""
def test_highlight_no_extra_trailing_newlines():
"""Test that output doesn't end with extra newlines."""
code = "x = 1"
result = highlight(code, language="python")
# Pygments tends to add a trailing newline, we should strip it
# The original code has no trailing newline, so neither should the result
assert not result.endswith("\n\n")
# A single trailing newline might be ok if original had one,
# but our test input doesn't, so result shouldn't either
assert not result.endswith("\n")
def test_highlight_with_line_numbers():
"""Test highlighting with line numbers enabled."""
code = "def foo():\n return 42\n return 99"
result = highlight(code, language="python", line_numbers=True)
# Should contain line numbers
assert "1" in result
assert "2" in result
assert "3" in result
# Should still contain ANSI codes
assert "\033[" in result
# Should contain the code (split by ANSI codes, so check separately)
assert "def" in result
assert "foo" in result
assert "return" in result
assert "42" in result
def test_highlight_line_numbers_use_ansi_dim():
"""Test that line numbers use ANSI dim/gray styling."""
code = "x = 1\ny = 2"
result = highlight(code, language="python", line_numbers=True)
# Line numbers should have some ANSI styling
# We'll check that there are escape codes before the digits
assert "\033[" in result
# This is a bit fragile, but we can check that the result
# starts with an escape code (for line number 1)
lines = result.split("\n")
# First line should start with escape code or digit
assert lines[0][0] in ("\033", "1", " ")
def test_highlight_preserves_code_with_trailing_newline():
"""Test code with trailing newline is handled correctly."""
code = "x = 1\n"
result = highlight(code, language="python")
# Should contain the code
assert "x" in result
# Should not accumulate extra newlines beyond what Pygments naturally adds
# We strip the trailing newline, so even input with \n shouldn't get extra
assert not result.endswith("\n\n")