"""Tests for color markup engine.""" from mudlib.render.colors import colorize class TestColorize: """Test the colorize markup processor.""" def test_basic_color_16(self): """Test basic color tag in 16-color mode.""" result = colorize("{red}hello{/}", "16") assert result == "\033[31mhello\033[0m" def test_bold_16(self): """Test bold tag in 16-color mode.""" result = colorize("{bold}test{/}", "16") assert result == "\033[1mtest\033[0m" def test_multiple_colors_16(self): """Test multiple color tags in sequence.""" result = colorize("{green}hp{/} {red}sp{/}", "16") assert result == "\033[32mhp\033[0m \033[31msp\033[0m" def test_no_color_support(self): """Test stripping tags when color_depth is None.""" result = colorize("{red}danger{/}", None) assert result == "danger" def test_nested_tags(self): """Test nested tags - bold and red together.""" result = colorize("{bold}{red}critical{/}", "16") assert result == "\033[1m\033[31mcritical\033[0m" def test_plain_text_passthrough(self): """Test plain text without tags passes through unchanged.""" result = colorize("plain text", "16") assert result == "plain text" def test_unknown_tags_preserved(self): """Test unknown tags are preserved for template substitution.""" result = colorize("{fake}text{/}", "16") assert result == "{fake}text\033[0m" def test_multiple_resets(self): """Test multiple reset tags work correctly.""" result = colorize("{red}one{/}{green}two{/}", "16") assert result == "\033[31mone\033[0m\033[32mtwo\033[0m" def test_reset_always_ansi_reset(self): """Test {/} always maps to ANSI reset code.""" result = colorize("{/}", "16") assert result == "\033[0m" def test_256_color_mode(self): """Test color tag works in 256-color mode.""" result = colorize("{green}hp{/}", "256") # Should contain ANSI code (exact code may vary by implementation) assert "\033[" in result assert "hp" in result assert "\033[0m" in result def test_template_variables_preserved(self): """Test that non-color tags like {stamina_pct} are preserved.""" result = colorize("{red}HP:{/} {hp_current}/{hp_max}", "16") assert result == "\033[31mHP:\033[0m {hp_current}/{hp_max}" def test_all_basic_colors(self): """Test all basic color tags work.""" colors = ["red", "green", "yellow", "blue", "magenta", "cyan", "white", "black"] for color in colors: result = colorize(f"{{{color}}}text{{/}}", "16") assert "\033[" in result assert "text" in result assert "\033[0m" in result def test_dim_style(self): """Test dim style tag.""" result = colorize("{dim}faded{/}", "16") assert "\033[2m" in result assert "faded" in result def test_no_closing_tag(self): """Test unclosed tag is handled gracefully.""" result = colorize("{red}no close", "16") # Should process the opening tag even without close assert "\033[31m" in result assert "no close" in result def test_color_depth_empty_string(self): """Test empty string color_depth strips tags like None.""" result = colorize("{red}danger{/}", "") assert result == "danger" def test_reset_alias(self): """Test {reset} works the same as {/}.""" result = colorize("{reset}", "16") assert result == "\033[0m" # Test in context result2 = colorize("{red}text{reset}", "16") assert result2 == "\033[31mtext\033[0m" def test_bright_red_16(self): """Test bright red in 16-color mode.""" result = colorize("{RED}bright{/}", "16") assert result == "\033[91mbright\033[0m" def test_all_bright_colors_16(self): """Test all bright color variants work.""" bright_colors = [ "RED", "GREEN", "YELLOW", "BLUE", "MAGENTA", "CYAN", "WHITE", "BLACK", ] for color in bright_colors: result = colorize(f"{{{color}}}text{{/}}", "16") assert "\033[" in result assert "text" in result assert "\033[0m" in result def test_bright_colors_no_support(self): """Test bright colors get stripped when no color support.""" result = colorize("{RED}bright{/} {GREEN}text{/}", None) assert result == "bright text"