|
| 1 | +package run.halo.shiki; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.*; |
| 8 | + |
| 9 | +/** |
| 10 | + * Test for {@link Constants} |
| 11 | + */ |
| 12 | +class ConstantsTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + void shouldGeneratePreStyleWithoutExcludedLanguages() { |
| 16 | + String style = Constants.generatePreStyle(null); |
| 17 | + |
| 18 | + // Should contain pre:has(code) selector that applies to all code blocks |
| 19 | + assertTrue(style.contains("pre:has(code)"), |
| 20 | + "Style should contain 'pre:has(code)' selector"); |
| 21 | + |
| 22 | + // Should contain the blur filter |
| 23 | + assertTrue(style.contains("filter: blur(10px)"), |
| 24 | + "Style should contain blur filter"); |
| 25 | + |
| 26 | + // Should be wrapped in style tags |
| 27 | + assertTrue(style.contains("<style") && style.contains("</style>"), |
| 28 | + "Style should be wrapped in style tags"); |
| 29 | + |
| 30 | + // Should not contain any language-specific exclusions |
| 31 | + assertFalse(style.contains("language-mermaid"), |
| 32 | + "Should not contain language-specific rules when no exclusions"); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void shouldGeneratePreStyleWithExcludedLanguages() { |
| 37 | + List<String> excludedLanguages = List.of("mermaid", "plantuml"); |
| 38 | + String style = Constants.generatePreStyle(excludedLanguages); |
| 39 | + |
| 40 | + // Should contain base blur rule |
| 41 | + assertTrue(style.contains("pre:has(code)"), |
| 42 | + "Style should contain base pre:has(code) selector"); |
| 43 | + assertTrue(style.contains("filter: blur(10px)"), |
| 44 | + "Style should contain blur filter"); |
| 45 | + |
| 46 | + // Should contain exclusion rules for mermaid |
| 47 | + assertTrue(style.contains("language-mermaid"), |
| 48 | + "Style should contain language-mermaid selector"); |
| 49 | + assertTrue(style.contains("lang-mermaid"), |
| 50 | + "Style should contain lang-mermaid selector"); |
| 51 | + |
| 52 | + // Should contain exclusion rules for plantuml |
| 53 | + assertTrue(style.contains("language-plantuml"), |
| 54 | + "Style should contain language-plantuml selector"); |
| 55 | + assertTrue(style.contains("lang-plantuml"), |
| 56 | + "Style should contain lang-plantuml selector"); |
| 57 | + |
| 58 | + // Exclusion rules should set filter to none (1 per language, not per prefix) |
| 59 | + int filterNoneCount = countOccurrences(style, "filter: none"); |
| 60 | + assertEquals(2, filterNoneCount, |
| 61 | + "Should have 2 'filter: none' rules (1 per language, covering both prefixes)"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void shouldGeneratePreStyleWithSingleExcludedLanguage() { |
| 66 | + List<String> excludedLanguages = List.of("mermaid"); |
| 67 | + String style = Constants.generatePreStyle(excludedLanguages); |
| 68 | + |
| 69 | + // Should contain exclusion rules for mermaid only |
| 70 | + assertTrue(style.contains("language-mermaid"), |
| 71 | + "Style should contain language-mermaid selector"); |
| 72 | + assertTrue(style.contains("lang-mermaid"), |
| 73 | + "Style should contain lang-mermaid selector"); |
| 74 | + |
| 75 | + // Should not contain plantuml |
| 76 | + assertFalse(style.contains("plantuml"), |
| 77 | + "Style should not contain plantuml when not excluded"); |
| 78 | + |
| 79 | + // Should have 1 filter:none rule (covering both prefixes) |
| 80 | + int filterNoneCount = countOccurrences(style, "filter: none"); |
| 81 | + assertEquals(1, filterNoneCount, |
| 82 | + "Should have 1 'filter: none' rule for single language"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void shouldGeneratePreStyleWithEmptyExcludedList() { |
| 87 | + String style = Constants.generatePreStyle(List.of()); |
| 88 | + |
| 89 | + // Should work same as null |
| 90 | + assertTrue(style.contains("pre:has(code)")); |
| 91 | + assertTrue(style.contains("filter: blur(10px)")); |
| 92 | + assertFalse(style.contains("filter: none"), |
| 93 | + "Should not contain filter:none when exclusion list is empty"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void preStyleShouldHaveCorrectStructure() { |
| 98 | + String style = Constants.generatePreStyle(null); |
| 99 | + |
| 100 | + // Verify it's a valid style tag with pjax class |
| 101 | + assertTrue(style.contains("class=\"pjax\""), |
| 102 | + "Style should have pjax class for compatibility"); |
| 103 | + assertTrue(style.contains("data-pjax"), |
| 104 | + "Style should have data-pjax attribute"); |
| 105 | + } |
| 106 | + |
| 107 | + private int countOccurrences(String str, String substring) { |
| 108 | + int count = 0; |
| 109 | + int index = 0; |
| 110 | + while ((index = str.indexOf(substring, index)) != -1) { |
| 111 | + count++; |
| 112 | + index += substring.length(); |
| 113 | + } |
| 114 | + return count; |
| 115 | + } |
| 116 | +} |
0 commit comments