| 1 |
package twwidth |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"os/exec" |
| 7 |
"strings" |
| 8 |
"sync" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/mattn/go-runewidth" |
| 12 |
"github.com/olekukonko/tablewriter/pkg/twcache" |
| 13 |
) |
| 14 |
|
| 15 |
func helperProcess() { |
| 16 |
if IsEastAsian() { |
| 17 |
fmt.Fprint(os.Stdout, "true") |
| 18 |
} else { |
| 19 |
fmt.Fprint(os.Stdout, "false") |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func resetGlobalCache() { |
| 24 |
mu.Lock() |
| 25 |
widthCache = twcache.NewLRU[cacheKey, int](cacheCapacity) |
| 26 |
mu.Unlock() |
| 27 |
} |
| 28 |
|
| 29 |
func TestMain(m *testing.M) { |
| 30 |
if os.Getenv("GO_TEST_SUBPROCESS") == "1" { |
| 31 |
helperProcess() |
| 32 |
return |
| 33 |
} |
| 34 |
os.Exit(m.Run()) |
| 35 |
} |
| 36 |
|
| 37 |
func TestInitRespectsEnvironment(t *testing.T) { |
| 38 |
testCases := []struct { |
| 39 |
name string |
| 40 |
envVar string |
| 41 |
wantOutput string |
| 42 |
}{ |
| 43 |
{"RUNEWIDTH_EASTASIAN=1", "1", "true"}, |
| 44 |
{"RUNEWIDTH_EASTASIAN=0", "0", "false"}, |
| 45 |
} |
| 46 |
|
| 47 |
for _, tc := range testCases { |
| 48 |
t.Run(tc.name, func(t *testing.T) { |
| 49 |
cmd := exec.Command(os.Args[0], "-test.run=^TestHelperProcess$") |
| 50 |
|
| 51 |
cmd.Env = append(os.Environ(), "GO_TEST_SUBPROCESS=1", "RUNEWIDTH_EASTASIAN="+tc.envVar) |
| 52 |
|
| 53 |
output, err := cmd.CombinedOutput() |
| 54 |
if err != nil { |
| 55 |
t.Fatalf("command failed: %v\nOutput: %s", err, output) |
| 56 |
} |
| 57 |
|
| 58 |
got := strings.TrimSpace(string(output)) |
| 59 |
if got != tc.wantOutput { |
| 60 |
t.Errorf("with RUNEWIDTH_EASTASIAN=%s, IsEastAsian() was %s, want %s", tc.envVar, got, tc.wantOutput) |
| 61 |
} |
| 62 |
}) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestFilter(t *testing.T) { |
| 67 |
ansi := Filter() |
| 68 |
tests := []struct { |
| 69 |
input string |
| 70 |
expected bool |
| 71 |
}{ |
| 72 |
{"\033[31m", true}, |
| 73 |
{"\033]8;;http://example.com\007", true}, |
| 74 |
{"hello", false}, |
| 75 |
{"\033[m", true}, |
| 76 |
{"\033[1;34;40m", true}, |
| 77 |
{"\033invalid", false}, |
| 78 |
} |
| 79 |
|
| 80 |
for _, tt := range tests { |
| 81 |
t.Run(tt.input, func(t *testing.T) { |
| 82 |
if got := ansi.MatchString(tt.input); got != tt.expected { |
| 83 |
t.Errorf("Filter().MatchString(%q) = %v, want %v", tt.input, got, tt.expected) |
| 84 |
} |
| 85 |
}) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestSetEastAsian(t *testing.T) { |
| 90 |
original := IsEastAsian() |
| 91 |
t.Cleanup(func() { |
| 92 |
SetEastAsian(original) |
| 93 |
}) |
| 94 |
|
| 95 |
SetEastAsian(true) |
| 96 |
if !IsEastAsian() { |
| 97 |
t.Errorf("SetEastAsian(true): IsEastAsian() = false, want true") |
| 98 |
} |
| 99 |
SetEastAsian(false) |
| 100 |
if IsEastAsian() { |
| 101 |
t.Errorf("SetEastAsian(false): IsEastAsian() = true, want false") |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestWidth(t *testing.T) { |
| 106 |
tests := []struct { |
| 107 |
name string |
| 108 |
input string |
| 109 |
eastAsian bool |
| 110 |
expectedWidth int |
| 111 |
}{ |
| 112 |
{ |
| 113 |
name: "ASCII", |
| 114 |
input: "hello", |
| 115 |
eastAsian: false, |
| 116 |
expectedWidth: 5, |
| 117 |
}, |
| 118 |
{ |
| 119 |
name: "Unicode with ANSI", |
| 120 |
input: "\033[31m☆あ\033[0m", |
| 121 |
eastAsian: false, |
| 122 |
expectedWidth: 3, |
| 123 |
}, |
| 124 |
{ |
| 125 |
name: "Unicode with EastAsian", |
| 126 |
input: "\033[31m☆あ\033[0m", |
| 127 |
eastAsian: true, |
| 128 |
expectedWidth: 4, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "Empty string", |
| 132 |
input: "", |
| 133 |
eastAsian: false, |
| 134 |
expectedWidth: 0, |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "Only ANSI", |
| 138 |
input: "\033[31m\033[0m", |
| 139 |
eastAsian: false, |
| 140 |
expectedWidth: 0, |
| 141 |
}, |
| 142 |
} |
| 143 |
|
| 144 |
for _, tt := range tests { |
| 145 |
t.Run(tt.name, func(t *testing.T) { |
| 146 |
SetEastAsian(tt.eastAsian) |
| 147 |
got := Width(tt.input) |
| 148 |
if got != tt.expectedWidth { |
| 149 |
t.Errorf("Width(%q) = %d, want %d (EastAsian=%v)", tt.input, got, tt.expectedWidth, tt.eastAsian) |
| 150 |
} |
| 151 |
}) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func TestWidthControlCharMatchesNoCache(t *testing.T) { |
| 156 |
for _, s := range []string{"\x00", "\x07", "\x1f", "\x7f"} { |
| 157 |
if got, want := Width(s), WidthNoCache(s); got != want { |
| 158 |
t.Errorf("Width(%q) = %d, WidthNoCache(%q) = %d; fast path must match", s, got, s, want) |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func TestDisplay(t *testing.T) { |
| 164 |
tests := []struct { |
| 165 |
name string |
| 166 |
input string |
| 167 |
eastAsian bool |
| 168 |
expectedWidth int |
| 169 |
}{ |
| 170 |
{ |
| 171 |
name: "ASCII", |
| 172 |
input: "hello", |
| 173 |
eastAsian: false, |
| 174 |
expectedWidth: 5, |
| 175 |
}, |
| 176 |
{ |
| 177 |
name: "Unicode with ANSI", |
| 178 |
input: "\033[31m☆あ\033[0m", |
| 179 |
eastAsian: false, |
| 180 |
expectedWidth: 3, |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "Unicode with EastAsian", |
| 184 |
input: "\033[31m☆あ\033[0m", |
| 185 |
eastAsian: true, |
| 186 |
expectedWidth: 4, |
| 187 |
}, |
| 188 |
{ |
| 189 |
name: "Empty string", |
| 190 |
input: "", |
| 191 |
eastAsian: false, |
| 192 |
expectedWidth: 0, |
| 193 |
}, |
| 194 |
{ |
| 195 |
name: "Only ANSI", |
| 196 |
input: "\033[31m\033[0m", |
| 197 |
eastAsian: false, |
| 198 |
expectedWidth: 0, |
| 199 |
}, |
| 200 |
} |
| 201 |
|
| 202 |
for _, tt := range tests { |
| 203 |
t.Run(tt.name, func(t *testing.T) { |
| 204 |
cond := &runewidth.Condition{ |
| 205 |
EastAsianWidth: tt.eastAsian, |
| 206 |
} |
| 207 |
got := Display(cond, tt.input) |
| 208 |
if got != tt.expectedWidth { |
| 209 |
t.Errorf("Display(%q, options) = %d, want %d (EastAsian=%v)", tt.input, got, tt.expectedWidth, tt.eastAsian) |
| 210 |
} |
| 211 |
}) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func TestTruncate(t *testing.T) { |
| 216 |
tests := []struct { |
| 217 |
name string |
| 218 |
input string |
| 219 |
maxWidth int |
| 220 |
suffix []string |
| 221 |
eastAsian bool |
| 222 |
expected string |
| 223 |
}{ |
| 224 |
{ |
| 225 |
name: "ASCII within width", |
| 226 |
input: "hello", |
| 227 |
maxWidth: 5, |
| 228 |
suffix: nil, |
| 229 |
eastAsian: false, |
| 230 |
expected: "hello", |
| 231 |
}, |
| 232 |
{ |
| 233 |
name: "ASCII with suffix", |
| 234 |
input: "hello", |
| 235 |
maxWidth: 8, |
| 236 |
suffix: []string{"..."}, |
| 237 |
eastAsian: false, |
| 238 |
expected: "hello...", |
| 239 |
}, |
| 240 |
{ |
| 241 |
name: "ASCII truncate", |
| 242 |
input: "hello", |
| 243 |
maxWidth: 3, |
| 244 |
suffix: []string{"..."}, |
| 245 |
eastAsian: false, |
| 246 |
expected: "...", |
| 247 |
}, |
| 248 |
{ |
| 249 |
name: "Unicode with ANSI, no truncate", |
| 250 |
input: "\033[31m☆あ\033[0m", |
| 251 |
maxWidth: 3, |
| 252 |
suffix: nil, |
| 253 |
eastAsian: false, |
| 254 |
expected: "\033[31m☆あ\033[0m", |
| 255 |
}, |
| 256 |
{ |
| 257 |
name: "Unicode with ANSI, truncate", |
| 258 |
input: "\033[31m☆あ\033[0m", |
| 259 |
maxWidth: 2, |
| 260 |
suffix: []string{"..."}, |
| 261 |
eastAsian: false, |
| 262 |
expected: "", |
| 263 |
}, |
| 264 |
{ |
| 265 |
name: "Unicode with EastAsian", |
| 266 |
input: "\033[31m☆あ\033[0m", |
| 267 |
maxWidth: 3, |
| 268 |
suffix: []string{"..."}, |
| 269 |
eastAsian: true, |
| 270 |
expected: "...", |
| 271 |
}, |
| 272 |
{ |
| 273 |
name: "Zero maxWidth", |
| 274 |
input: "hello", |
| 275 |
maxWidth: 0, |
| 276 |
suffix: []string{"..."}, |
| 277 |
eastAsian: false, |
| 278 |
expected: "", |
| 279 |
}, |
| 280 |
{ |
| 281 |
name: "Negative maxWidth", |
| 282 |
input: "hello", |
| 283 |
maxWidth: -1, |
| 284 |
suffix: []string{"..."}, |
| 285 |
eastAsian: false, |
| 286 |
expected: "", |
| 287 |
}, |
| 288 |
{ |
| 289 |
name: "Empty string with suffix", |
| 290 |
input: "", |
| 291 |
maxWidth: 3, |
| 292 |
suffix: []string{"..."}, |
| 293 |
eastAsian: false, |
| 294 |
expected: "...", |
| 295 |
}, |
| 296 |
{ |
| 297 |
name: "Only ANSI", |
| 298 |
input: "\033[31m\033[0m", |
| 299 |
maxWidth: 3, |
| 300 |
suffix: []string{"..."}, |
| 301 |
eastAsian: false, |
| 302 |
expected: "...", |
| 303 |
}, |
| 304 |
} |
| 305 |
|
| 306 |
for _, tt := range tests { |
| 307 |
t.Run(tt.name, func(t *testing.T) { |
| 308 |
SetEastAsian(tt.eastAsian) |
| 309 |
got := Truncate(tt.input, tt.maxWidth, tt.suffix...) |
| 310 |
if got != tt.expected { |
| 311 |
t.Errorf("Truncate(%q, %d, %v) (EA=%v) = %q, want %q", tt.input, tt.maxWidth, tt.suffix, tt.eastAsian, got, tt.expected) |
| 312 |
} |
| 313 |
}) |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
func TestConcurrentSetEastAsian(t *testing.T) { |
| 318 |
var wg sync.WaitGroup |
| 319 |
numGoroutines := 10 |
| 320 |
iterations := 100 |
| 321 |
|
| 322 |
for i := 0; i < numGoroutines; i++ { |
| 323 |
wg.Add(1) |
| 324 |
go func(enable bool) { |
| 325 |
defer wg.Done() |
| 326 |
for j := 0; j < iterations; j++ { |
| 327 |
SetEastAsian(enable) |
| 328 |
} |
| 329 |
}(i%2 == 0) |
| 330 |
} |
| 331 |
|
| 332 |
wg.Wait() |
| 333 |
} |
| 334 |
|
| 335 |
func TestWidthWithEnvironment(t *testing.T) { |
| 336 |
original := os.Getenv("RUNEWIDTH_EASTASIAN") |
| 337 |
defer func() { |
| 338 |
if original == "" { |
| 339 |
os.Unsetenv("RUNEWIDTH_EASTASIAN") |
| 340 |
} else { |
| 341 |
os.Setenv("RUNEWIDTH_EASTASIAN", original) |
| 342 |
} |
| 343 |
}() |
| 344 |
|
| 345 |
os.Setenv("RUNEWIDTH_EASTASIAN", "0") |
| 346 |
SetEastAsian(false) |
| 347 |
if got := Width("☆あ"); got != 3 { |
| 348 |
t.Errorf("Width(☆あ) with RUNEWIDTH_EASTASIAN=0 = %d, want 3", got) |
| 349 |
} |
| 350 |
|
| 351 |
os.Setenv("RUNEWIDTH_EASTASIAN", "1") |
| 352 |
SetEastAsian(true) |
| 353 |
if got := Width("☆あ"); got != 4 { |
| 354 |
t.Errorf("Width(☆あ) with RUNEWIDTH_EASTASIAN=1 = %d, want 4", got) |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
func TestCoverageWidth(t *testing.T) { |
| 359 |
t.Run("Direct Width Functions", func(t *testing.T) { |
| 360 |
w := WidthNoCache("abc") |
| 361 |
if w != 3 { |
| 362 |
t.Errorf("WidthNoCache('abc') = %d, want 3", w) |
| 363 |
} |
| 364 |
|
| 365 |
opts := Options{EastAsianWidth: true} |
| 366 |
w2 := WidthWithOptions("abc", opts) |
| 367 |
if w2 != 3 { |
| 368 |
t.Errorf("WidthWithOptions result wrong") |
| 369 |
} |
| 370 |
}) |
| 371 |
|
| 372 |
t.Run("SetCondition", func(t *testing.T) { |
| 373 |
original := IsEastAsian() |
| 374 |
defer SetEastAsian(original) |
| 375 |
|
| 376 |
cond := &runewidth.Condition{EastAsianWidth: !original} |
| 377 |
SetCondition(cond) |
| 378 |
|
| 379 |
if IsEastAsian() == original { |
| 380 |
t.Error("SetCondition failed to update global state") |
| 381 |
} |
| 382 |
}) |
| 383 |
|
| 384 |
t.Run("Truncate ANSI Reset", func(t *testing.T) { |
| 385 |
input := "\x1b[31mHello World" |
| 386 |
got := Truncate(input, 5) |
| 387 |
|
| 388 |
if !strings.HasPrefix(got, "\x1b[31m") { |
| 389 |
t.Error("Lost initial color code") |
| 390 |
} |
| 391 |
if !strings.HasSuffix(got, "\x1b[0m") { |
| 392 |
t.Errorf("Expected ANSI reset at end, got: %q", got) |
| 393 |
} |
| 394 |
|
| 395 |
got = Truncate("\x1b[31mHi", 2) |
| 396 |
if !strings.HasSuffix(got, "\x1b[0m") { |
| 397 |
t.Error("Should append reset even on exact fit if color was active") |
| 398 |
} |
| 399 |
}) |
| 400 |
|
| 401 |
t.Run("Truncate Suffix Logic", func(t *testing.T) { |
| 402 |
got := Truncate("Hello", 2, "...") |
| 403 |
if got != "" { |
| 404 |
t.Errorf("Expected empty string when suffix doesn't fit, got %q", got) |
| 405 |
} |
| 406 |
|
| 407 |
got = Truncate("Hello", 3, "...") |
| 408 |
if got != "..." { |
| 409 |
t.Errorf("Expected only suffix, got %q", got) |
| 410 |
} |
| 411 |
}) |
| 412 |
|
| 413 |
t.Run("Global Cache Management", func(t *testing.T) { |
| 414 |
mu.Lock() |
| 415 |
origCache := widthCache |
| 416 |
mu.Unlock() |
| 417 |
defer func() { |
| 418 |
mu.Lock() |
| 419 |
widthCache = origCache |
| 420 |
mu.Unlock() |
| 421 |
}() |
| 422 |
|
| 423 |
SetCacheCapacity(0) |
| 424 |
size, cap, _ := GetCacheStats() |
| 425 |
if size != 0 || cap != 0 { |
| 426 |
t.Error("Cache should be disabled (stats 0,0)") |
| 427 |
} |
| 428 |
|
| 429 |
if w := Width("abc"); w != 3 { |
| 430 |
t.Errorf("Width('abc') = %d, want 3", w) |
| 431 |
} |
| 432 |
|
| 433 |
SetCacheCapacity(10) |
| 434 |
Width("ab") |
| 435 |
size, cap, _ = GetCacheStats() |
| 436 |
if size != 1 || cap != 10 { |
| 437 |
t.Errorf("Stats mismatch: size=%d, cap=%d", size, cap) |
| 438 |
} |
| 439 |
}) |
| 440 |
|
| 441 |
} |
| 442 |
|
| 443 |
func TestBug_ForceNarrow_MultiChar(t *testing.T) { |
| 444 |
// Setup the specific condition causing the bug: |
| 445 |
// EastAsianWidth is ON (simulating RUNEWIDTH_EASTASIAN=1) |
| 446 |
// ForceNarrowBorders is ON (simulating Modern Terminal detection) |
| 447 |
buggyOptions := Options{ |
| 448 |
EastAsianWidth: true, |
| 449 |
ForceNarrowBorders: true, |
| 450 |
} |
| 451 |
SetOptions(buggyOptions) |
| 452 |
|
| 453 |
// Define a border string commonly used by the renderer (length > 1) |
| 454 |
// The current bug fails because it only checks len(r) == 1 |
| 455 |
input := "─────" // 5 chars |
| 456 |
want := 5 // Should be 5 width (1 per char) |
| 457 |
|
| 458 |
// 3. Measure |
| 459 |
got := Width(input) |
| 460 |
|
| 461 |
// 4. Assert |
| 462 |
if got != want { |
| 463 |
t.Fatalf("Regression found: ForceNarrowBorders failed on multi-char string.\nInput: %q\nGot Width: %d (Likely Double Width)\nWant Width: %d", input, got, want) |
| 464 |
} |
| 465 |
} |