|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestEscapeForAnsiWriter(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + input string |
| 11 | + want string |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "plain text unchanged", |
| 15 | + input: "hello world", |
| 16 | + want: "hello world", |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "ANSI reset before literal bracket not corrupted", |
| 20 | + input: "\x1b[0m] after", |
| 21 | + want: "\x1b[0m] after", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "literal tview-like tag is escaped", |
| 25 | + input: "[ButThisWont] visible", |
| 26 | + want: "[ButThisWont[] visible", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "structured log with ANSI colors and brackets", |
| 30 | + input: "\x1b[2m2026-01-01T00:00:00Z\x1b[0m [\x1b[32m\x1b[1minfo \x1b[0m] \x1b[1msome log message\x1b[0m [\x1b[34mmyapp\x1b[0m] \x1b[36mkey\x1b[0m=\x1b[35mvalue\x1b[0m", |
| 31 | + want: "\x1b[2m2026-01-01T00:00:00Z\x1b[0m [\x1b[32m\x1b[1minfo \x1b[0m] \x1b[1msome log message\x1b[0m [\x1b[34mmyapp\x1b[0m] \x1b[36mkey\x1b[0m=\x1b[35mvalue\x1b[0m", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "ANSI sequences preserved", |
| 35 | + input: "\x1b[31mred\x1b[0m", |
| 36 | + want: "\x1b[31mred\x1b[0m", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "multiple literal tags escaped", |
| 40 | + input: "[red] and [blue]", |
| 41 | + want: "[red[] and [blue[]", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "brackets with spaces not escaped", |
| 45 | + input: "[not a tag because of spaces]", |
| 46 | + want: "[not a tag because of spaces]", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "ANSI color followed immediately by literal tag", |
| 50 | + input: "\x1b[0m[SomeTag]", |
| 51 | + want: "\x1b[0m[SomeTag[]", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "hex color tag escaped", |
| 55 | + input: "[#ff0000]text", |
| 56 | + want: "[#ff0000[]text", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "reset tag escaped", |
| 60 | + input: "[-]reset", |
| 61 | + want: "[-[]reset", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "compound style tag escaped", |
| 65 | + input: "[red:-:b]bold red", |
| 66 | + want: "[red:-:b[]bold red", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "brackets with underscores not escaped", |
| 70 | + input: "[some_var]", |
| 71 | + want: "[some_var]", |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, tt := range tests { |
| 76 | + t.Run(tt.name, func(t *testing.T) { |
| 77 | + got := escapeForAnsiWriter(tt.input) |
| 78 | + if got != tt.want { |
| 79 | + t.Errorf("escapeForAnsiWriter(%q)\n got %q\n want %q", tt.input, got, tt.want) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
0 commit comments