62159
Sy
symb / files
symb / internal / tui / wrap_test.go
2.2 KB
1package tui 2 3import ( 4 "strings" 5 "testing" 6) 7 8func TestWrapANSIPreservesStyles(t *testing.T) { 9 red := "\x1b[31m" 10 bgBlack := "\x1b[40m" 11 reset := "\x1b[0m" 12 13 // A red-on-black string that will wrap across multiple lines. 14 text := red + bgBlack + strings.Repeat("hello ", 20) + reset 15 16 lines := wrapANSI(text, 30) 17 18 if len(lines) < 2 { 19 t.Fatalf("expected multiple lines, got %d", len(lines)) 20 } 21 22 // Every continuation line must start with the active style sequences. 23 for i, line := range lines { 24 if i == 0 { 25 // First line should start with the original sequences. 26 if !strings.HasPrefix(line, red+bgBlack) { 27 t.Errorf("line 0: expected style prefix, got %q", line[:min(len(line), 30)]) 28 } 29 continue 30 } 31 // Continuation lines must have the style re-opened. 32 if !strings.Contains(line, "\x1b[31m") { 33 t.Errorf("line %d: missing red foreground sequence: %q", i, line) 34 } 35 if !strings.Contains(line, "\x1b[40m") { 36 t.Errorf("line %d: missing black background sequence: %q", i, line) 37 } 38 } 39 40 // All lines except the last should end with a reset. 41 for i := 0; i < len(lines)-1; i++ { 42 if !strings.HasSuffix(lines[i], "\x1b[m") { 43 t.Errorf("line %d: should end with reset, got %q", i, lines[i][max(0, len(lines[i])-10):]) 44 } 45 } 46} 47 48func TestWrapANSIResetClearsState(t *testing.T) { 49 red := "\x1b[31m" 50 reset := "\x1b[0m" 51 52 // Style, some text, reset, more text — the continuation after reset 53 // should NOT carry the red style. 54 text := red + "aaaa" + reset + " " + strings.Repeat("b", 30) 55 56 lines := wrapANSI(text, 10) 57 58 if len(lines) < 2 { 59 t.Fatalf("expected multiple lines, got %d", len(lines)) 60 } 61 62 // After the reset, continuation lines should NOT have red. 63 for i := 1; i < len(lines); i++ { 64 if strings.Contains(lines[i], "\x1b[31m") { 65 t.Errorf("line %d: should not have red after reset: %q", i, lines[i]) 66 } 67 } 68} 69 70func TestWrapANSINoStylePassthrough(t *testing.T) { 71 // Plain text should pass through unchanged (no spurious sequences added). 72 text := "hello world this is a test of plain text wrapping" 73 lines := wrapANSI(text, 15) 74 75 for i, line := range lines { 76 if strings.Contains(line, "\x1b") { 77 t.Errorf("line %d: unexpected ANSI sequence in plain text: %q", i, line) 78 } 79 } 80}