| 1 |
package color_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"os" |
| 6 |
"sync" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/color" |
| 13 |
) |
| 14 |
|
| 15 |
// newTestConfig creates a test config with color explicitly enabled. |
| 16 |
func newTestConfig(buf *bytes.Buffer) *color.Config { |
| 17 |
return &color.Config{ |
| 18 |
NoColor: false, |
| 19 |
Output: buf, |
| 20 |
Error: buf, |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
// TestIntegration_HelpersComprehensive tests the color helper functions comprehensively. |
| 25 |
func TestIntegration_HelpersComprehensive(t *testing.T) { |
| 26 |
t.Parallel() |
| 27 |
|
| 28 |
tests := []struct { |
| 29 |
name string |
| 30 |
noColor bool |
| 31 |
testFn func() string |
| 32 |
checkEscape bool |
| 33 |
checkText string |
| 34 |
}{ |
| 35 |
{ |
| 36 |
name: "RedString with color enabled via environment bypass", |
| 37 |
noColor: false, |
| 38 |
testFn: func() string { |
| 39 |
// Use NewWithConfig to bypass environment detection |
| 40 |
buf := &bytes.Buffer{} |
| 41 |
cfg := newTestConfig(buf) |
| 42 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 43 |
|
| 44 |
return c.Sprint("test") |
| 45 |
}, |
| 46 |
checkEscape: true, |
| 47 |
checkText: "test", |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "RedString with NoColor via config", |
| 51 |
noColor: true, |
| 52 |
testFn: func() string { |
| 53 |
buf := &bytes.Buffer{} |
| 54 |
cfg := &color.Config{NoColor: true, Output: buf, Error: buf} |
| 55 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 56 |
|
| 57 |
return c.Sprint("test") |
| 58 |
}, |
| 59 |
checkEscape: false, |
| 60 |
checkText: "test", |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "GreenString with color", |
| 64 |
noColor: false, |
| 65 |
testFn: func() string { |
| 66 |
buf := &bytes.Buffer{} |
| 67 |
cfg := newTestConfig(buf) |
| 68 |
c := color.NewWithConfig(cfg, color.FgGreen) |
| 69 |
|
| 70 |
return c.Sprint("hello") |
| 71 |
}, |
| 72 |
checkEscape: true, |
| 73 |
checkText: "hello", |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "BlueString with NoColor", |
| 77 |
noColor: true, |
| 78 |
testFn: func() string { |
| 79 |
buf := &bytes.Buffer{} |
| 80 |
cfg := &color.Config{NoColor: true, Output: buf, Error: buf} |
| 81 |
c := color.NewWithConfig(cfg, color.FgBlue) |
| 82 |
|
| 83 |
return c.Sprint("world") |
| 84 |
}, |
| 85 |
checkEscape: false, |
| 86 |
checkText: "world", |
| 87 |
}, |
| 88 |
{ |
| 89 |
name: "YellowString formatted", |
| 90 |
noColor: false, |
| 91 |
testFn: func() string { |
| 92 |
buf := &bytes.Buffer{} |
| 93 |
cfg := newTestConfig(buf) |
| 94 |
c := color.NewWithConfig(cfg, color.FgYellow) |
| 95 |
|
| 96 |
return c.Sprintf("value: %d", 42) |
| 97 |
}, |
| 98 |
checkEscape: true, |
| 99 |
checkText: "value: 42", |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "CyanString empty", |
| 103 |
noColor: false, |
| 104 |
testFn: func() string { |
| 105 |
buf := &bytes.Buffer{} |
| 106 |
cfg := newTestConfig(buf) |
| 107 |
c := color.NewWithConfig(cfg, color.FgCyan) |
| 108 |
|
| 109 |
return c.Sprint("") |
| 110 |
}, |
| 111 |
checkEscape: true, |
| 112 |
checkText: "", |
| 113 |
}, |
| 114 |
} |
| 115 |
|
| 116 |
for _, tt := range tests { |
| 117 |
t.Run(tt.name, func(t *testing.T) { |
| 118 |
t.Parallel() |
| 119 |
|
| 120 |
result := tt.testFn() |
| 121 |
|
| 122 |
if tt.checkEscape { |
| 123 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 124 |
} else { |
| 125 |
assert.NotContains(t, result, "\x1b[", "should not contain ANSI escape code") |
| 126 |
} |
| 127 |
|
| 128 |
assert.Contains(t, result, tt.checkText, "should contain expected text") |
| 129 |
}) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// TestIntegration_ColorChainingIntegration tests color chaining functionality. |
| 134 |
func TestIntegration_ColorChainingIntegration(t *testing.T) { |
| 135 |
t.Parallel() |
| 136 |
|
| 137 |
tests := []struct { |
| 138 |
name string |
| 139 |
colorFn func() *color.Color |
| 140 |
noColor bool |
| 141 |
check func(t *testing.T, c *color.Color) |
| 142 |
}{ |
| 143 |
{ |
| 144 |
name: "chained foreground colors", |
| 145 |
colorFn: func() *color.Color { |
| 146 |
buf := &bytes.Buffer{} |
| 147 |
cfg := newTestConfig(buf) |
| 148 |
|
| 149 |
return color.NewWithConfig(cfg, color.FgRed).Add(color.Bold) |
| 150 |
}, |
| 151 |
noColor: false, |
| 152 |
check: func(t *testing.T, c *color.Color) { |
| 153 |
t.Helper() |
| 154 |
|
| 155 |
result := c.Sprint("test") |
| 156 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 157 |
assert.Contains(t, result, "test", "should contain text") |
| 158 |
}, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "combined color with multiple attributes", |
| 162 |
colorFn: func() *color.Color { |
| 163 |
// Using chained Add calls to combine red, bold, and underline |
| 164 |
buf := &bytes.Buffer{} |
| 165 |
cfg := newTestConfig(buf) |
| 166 |
|
| 167 |
return color.NewWithConfig(cfg, color.FgRed).Add(color.Bold).Add(color.Underline) |
| 168 |
}, |
| 169 |
noColor: false, |
| 170 |
check: func(t *testing.T, c *color.Color) { |
| 171 |
t.Helper() |
| 172 |
|
| 173 |
result := c.Sprint("important") |
| 174 |
// Combined attributes produce combined ANSI codes |
| 175 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 176 |
assert.Contains(t, result, "important", "should contain text") |
| 177 |
// The format should be combined: 31;1;4 |
| 178 |
assert.Contains(t, result, "31", "should have red code") |
| 179 |
}, |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "NoColor disables all colors", |
| 183 |
colorFn: func() *color.Color { |
| 184 |
// Create color with NoColor enabled |
| 185 |
cfg := &color.Config{NoColor: true} |
| 186 |
|
| 187 |
return color.NewWithConfig(cfg, color.FgRed) |
| 188 |
}, |
| 189 |
noColor: true, |
| 190 |
check: func(t *testing.T, c *color.Color) { |
| 191 |
t.Helper() |
| 192 |
|
| 193 |
result := c.Sprint("test") |
| 194 |
assert.Equal(t, "test", result, "should be plain text when NoColor is set") |
| 195 |
}, |
| 196 |
}, |
| 197 |
} |
| 198 |
|
| 199 |
for _, tt := range tests { |
| 200 |
t.Run(tt.name, func(t *testing.T) { |
| 201 |
t.Parallel() |
| 202 |
|
| 203 |
tt.check(t, tt.colorFn()) |
| 204 |
}) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// TestIntegration_RGBFunctionsIntegration tests RGB color functions. |
| 209 |
func TestIntegration_RGBFunctionsIntegration(t *testing.T) { |
| 210 |
t.Parallel() |
| 211 |
|
| 212 |
tests := []struct { |
| 213 |
name string |
| 214 |
noColor bool |
| 215 |
testFn func() string |
| 216 |
}{ |
| 217 |
{ |
| 218 |
name: "RGB foreground with color", |
| 219 |
noColor: false, |
| 220 |
testFn: func() string { |
| 221 |
buf := &bytes.Buffer{} |
| 222 |
cfg := newTestConfig(buf) |
| 223 |
c := color.NewWithConfig(cfg).AddRGB(255, 128, 64) |
| 224 |
|
| 225 |
return c.Sprint("test") |
| 226 |
}, |
| 227 |
}, |
| 228 |
{ |
| 229 |
name: "RGB foreground without color", |
| 230 |
noColor: true, |
| 231 |
testFn: func() string { |
| 232 |
cfg := &color.Config{NoColor: true} |
| 233 |
|
| 234 |
return color.NewWithConfig(cfg, color.FgRed).Sprint("test") |
| 235 |
}, |
| 236 |
}, |
| 237 |
{ |
| 238 |
name: "BgRGB background with color", |
| 239 |
noColor: false, |
| 240 |
testFn: func() string { |
| 241 |
buf := &bytes.Buffer{} |
| 242 |
cfg := newTestConfig(buf) |
| 243 |
c := color.NewWithConfig(cfg).AddBgRGB(128, 64, 32) |
| 244 |
|
| 245 |
return c.Sprint("test") |
| 246 |
}, |
| 247 |
}, |
| 248 |
{ |
| 249 |
name: "AddRGB chained with color", |
| 250 |
noColor: false, |
| 251 |
testFn: func() string { |
| 252 |
buf := &bytes.Buffer{} |
| 253 |
cfg := newTestConfig(buf) |
| 254 |
c := color.NewWithConfig(cfg).AddRGB(255, 128, 64) |
| 255 |
|
| 256 |
return c.Sprint("test") |
| 257 |
}, |
| 258 |
}, |
| 259 |
} |
| 260 |
|
| 261 |
for _, tt := range tests { |
| 262 |
t.Run(tt.name, func(t *testing.T) { |
| 263 |
t.Parallel() |
| 264 |
|
| 265 |
result := tt.testFn() |
| 266 |
|
| 267 |
if tt.noColor { |
| 268 |
assert.Equal(t, "test", result, "should be plain text") |
| 269 |
} else { |
| 270 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 271 |
assert.Contains(t, result, "test", "should contain text") |
| 272 |
} |
| 273 |
}) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// TestIntegration_NewWithConfigIntegration tests the NewWithConfig function. |
| 278 |
func TestIntegration_NewWithConfigIntegration(t *testing.T) { |
| 279 |
t.Parallel() |
| 280 |
|
| 281 |
tests := []struct { |
| 282 |
name string |
| 283 |
cfg *color.Config |
| 284 |
attrs []color.Attribute |
| 285 |
input string |
| 286 |
noColor bool |
| 287 |
}{ |
| 288 |
{ |
| 289 |
name: "custom config with NoColor=true", |
| 290 |
cfg: &color.Config{NoColor: true, Output: &bytes.Buffer{}, Error: &bytes.Buffer{}}, |
| 291 |
attrs: []color.Attribute{color.FgRed}, |
| 292 |
input: "test", |
| 293 |
noColor: true, |
| 294 |
}, |
| 295 |
{ |
| 296 |
name: "custom config with NoColor=false", |
| 297 |
cfg: &color.Config{NoColor: false, Output: &bytes.Buffer{}, Error: &bytes.Buffer{}}, |
| 298 |
attrs: []color.Attribute{color.FgGreen}, |
| 299 |
input: "test", |
| 300 |
noColor: false, |
| 301 |
}, |
| 302 |
{ |
| 303 |
name: "custom config with multiple attributes", |
| 304 |
cfg: &color.Config{NoColor: false, Output: &bytes.Buffer{}, Error: &bytes.Buffer{}}, |
| 305 |
attrs: []color.Attribute{color.FgBlue, color.Bold}, |
| 306 |
input: "test", |
| 307 |
noColor: false, |
| 308 |
}, |
| 309 |
{ |
| 310 |
name: "empty config with explicit NoColor=false", |
| 311 |
cfg: &color.Config{NoColor: false, Output: &bytes.Buffer{}, Error: &bytes.Buffer{}}, |
| 312 |
attrs: []color.Attribute{color.FgRed}, |
| 313 |
input: "test", |
| 314 |
noColor: false, |
| 315 |
}, |
| 316 |
} |
| 317 |
|
| 318 |
for _, tt := range tests { |
| 319 |
t.Run(tt.name, func(t *testing.T) { |
| 320 |
t.Parallel() |
| 321 |
|
| 322 |
c := color.NewWithConfig(tt.cfg, tt.attrs...) |
| 323 |
result := c.Sprint(tt.input) |
| 324 |
|
| 325 |
if tt.noColor { |
| 326 |
assert.Equal(t, tt.input, result, "should be plain text when NoColor is set") |
| 327 |
} else { |
| 328 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 329 |
assert.Contains(t, result, tt.input, "should contain text") |
| 330 |
} |
| 331 |
}) |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
// TestIntegration_DefaultConfigIntegration tests the DefaultConfig function. |
| 336 |
func TestIntegration_DefaultConfigIntegration(t *testing.T) { |
| 337 |
t.Parallel() |
| 338 |
|
| 339 |
tests := []struct { |
| 340 |
name string |
| 341 |
test func(t *testing.T) |
| 342 |
}{ |
| 343 |
{ |
| 344 |
name: "DefaultConfig returns valid config", |
| 345 |
test: func(t *testing.T) { |
| 346 |
t.Helper() |
| 347 |
|
| 348 |
cfg := color.DefaultConfig() |
| 349 |
require.NotNil(t, cfg) |
| 350 |
assert.NotNil(t, cfg.Output) |
| 351 |
assert.NotNil(t, cfg.Error) |
| 352 |
}, |
| 353 |
}, |
| 354 |
{ |
| 355 |
name: "New with explicit color config works", |
| 356 |
test: func(t *testing.T) { |
| 357 |
t.Helper() |
| 358 |
|
| 359 |
buf := &bytes.Buffer{} |
| 360 |
cfg := newTestConfig(buf) |
| 361 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 362 |
assert.NotNil(t, c) |
| 363 |
result := c.Sprint("test") |
| 364 |
assert.NotEmpty(t, result) |
| 365 |
assert.Contains(t, result, "test") |
| 366 |
}, |
| 367 |
}, |
| 368 |
{ |
| 369 |
name: "NewWithConfig with explicit config works", |
| 370 |
test: func(t *testing.T) { |
| 371 |
t.Helper() |
| 372 |
|
| 373 |
buf := &bytes.Buffer{} |
| 374 |
cfg := newTestConfig(buf) |
| 375 |
c := color.NewWithConfig(cfg, color.FgGreen) |
| 376 |
assert.NotNil(t, c) |
| 377 |
result := c.Sprint("test") |
| 378 |
assert.NotEmpty(t, result) |
| 379 |
assert.Contains(t, result, "test") |
| 380 |
}, |
| 381 |
}, |
| 382 |
} |
| 383 |
|
| 384 |
for _, tt := range tests { |
| 385 |
t.Run(tt.name, func(t *testing.T) { |
| 386 |
t.Parallel() |
| 387 |
tt.test(t) |
| 388 |
}) |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
// TestIntegration_InstanceConfigIntegration tests instance-based configuration. |
| 393 |
func TestIntegration_InstanceConfigIntegration(t *testing.T) { |
| 394 |
t.Parallel() |
| 395 |
|
| 396 |
tests := []struct { |
| 397 |
name string |
| 398 |
test func(t *testing.T) |
| 399 |
}{ |
| 400 |
{ |
| 401 |
name: "separate instances have independent configs", |
| 402 |
test: func(t *testing.T) { |
| 403 |
t.Helper() |
| 404 |
|
| 405 |
// Create two independent color instances with different configs |
| 406 |
buf1 := &bytes.Buffer{} |
| 407 |
cfg1 := &color.Config{NoColor: false, Output: buf1, Error: buf1} |
| 408 |
c1 := color.NewWithConfig(cfg1, color.FgRed) |
| 409 |
|
| 410 |
buf2 := &bytes.Buffer{} |
| 411 |
cfg2 := &color.Config{NoColor: true, Output: buf2, Error: buf2} |
| 412 |
c2 := color.NewWithConfig(cfg2, color.FgGreen) |
| 413 |
|
| 414 |
// c1 should produce colored output |
| 415 |
result1 := c1.Sprint("test1") |
| 416 |
assert.Contains(t, result1, "\x1b[", "c1 should have color") |
| 417 |
|
| 418 |
// c2 should produce plain output |
| 419 |
result2 := c2.Sprint("test2") |
| 420 |
assert.Equal(t, "test2", result2, "c2 should be plain") |
| 421 |
}, |
| 422 |
}, |
| 423 |
{ |
| 424 |
name: "shared config affects all instances", |
| 425 |
test: func(t *testing.T) { |
| 426 |
t.Helper() |
| 427 |
|
| 428 |
// Create a shared config |
| 429 |
buf := &bytes.Buffer{} |
| 430 |
sharedCfg := &color.Config{NoColor: false, Output: buf, Error: buf} |
| 431 |
|
| 432 |
// Create multiple colors sharing the same config |
| 433 |
c1 := color.NewWithConfig(sharedCfg, color.FgRed) |
| 434 |
c2 := color.NewWithConfig(sharedCfg, color.FgGreen) |
| 435 |
|
| 436 |
// Both should work |
| 437 |
result1 := c1.Sprint("red") |
| 438 |
result2 := c2.Sprint("green") |
| 439 |
|
| 440 |
assert.Contains(t, result1, "\x1b[", "c1 should have color") |
| 441 |
assert.Contains(t, result2, "\x1b[", "c2 should have color") |
| 442 |
assert.Contains(t, result1, "red") |
| 443 |
assert.Contains(t, result2, "green") |
| 444 |
}, |
| 445 |
}, |
| 446 |
{ |
| 447 |
name: "instance-level override takes precedence", |
| 448 |
test: func(t *testing.T) { |
| 449 |
t.Helper() |
| 450 |
|
| 451 |
buf := &bytes.Buffer{} |
| 452 |
cfg := &color.Config{NoColor: false, Output: buf, Error: buf} |
| 453 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 454 |
|
| 455 |
// Initially should have color |
| 456 |
result1 := c.Sprint("test") |
| 457 |
assert.Contains(t, result1, "\x1b[", "should have color initially") |
| 458 |
|
| 459 |
// Disable color at instance level |
| 460 |
c.DisableColor() |
| 461 |
result2 := c.Sprint("test") |
| 462 |
assert.Equal(t, "test", result2, "should be plain after DisableColor") |
| 463 |
|
| 464 |
// Re-enable color at instance level |
| 465 |
c.EnableColor() |
| 466 |
result3 := c.Sprint("test") |
| 467 |
assert.Contains(t, result3, "\x1b[", "should have color after EnableColor") |
| 468 |
}, |
| 469 |
}, |
| 470 |
} |
| 471 |
|
| 472 |
for _, tt := range tests { |
| 473 |
t.Run(tt.name, func(t *testing.T) { |
| 474 |
t.Parallel() |
| 475 |
tt.test(t) |
| 476 |
}) |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
// TestIntegration_PrintFunctionsIntegration tests print functions integration. |
| 481 |
func TestIntegration_PrintFunctionsIntegration(t *testing.T) { |
| 482 |
t.Parallel() |
| 483 |
|
| 484 |
tests := []struct { |
| 485 |
name string |
| 486 |
testFn func(t *testing.T) (string, error) |
| 487 |
}{ |
| 488 |
{ |
| 489 |
name: "Print prints to Output", |
| 490 |
testFn: func(t *testing.T) (string, error) { |
| 491 |
t.Helper() |
| 492 |
|
| 493 |
buf := &bytes.Buffer{} |
| 494 |
cfg := newTestConfig(buf) |
| 495 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 496 |
|
| 497 |
_, err := c.Print("test") |
| 498 |
|
| 499 |
return buf.String(), err |
| 500 |
}, |
| 501 |
}, |
| 502 |
{ |
| 503 |
name: "Printf formats and prints", |
| 504 |
testFn: func(t *testing.T) (string, error) { |
| 505 |
t.Helper() |
| 506 |
|
| 507 |
buf := &bytes.Buffer{} |
| 508 |
cfg := newTestConfig(buf) |
| 509 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 510 |
|
| 511 |
_, err := c.Printf("value: %d", 42) |
| 512 |
|
| 513 |
return buf.String(), err |
| 514 |
}, |
| 515 |
}, |
| 516 |
{ |
| 517 |
name: "Println prints with newline", |
| 518 |
testFn: func(t *testing.T) (string, error) { |
| 519 |
t.Helper() |
| 520 |
|
| 521 |
buf := &bytes.Buffer{} |
| 522 |
cfg := newTestConfig(buf) |
| 523 |
c := color.NewWithConfig(cfg, color.FgBlue) |
| 524 |
|
| 525 |
_, err := c.Println("test") |
| 526 |
result := buf.String() |
| 527 |
// Contains "test" and "\n", escape codes are between them |
| 528 |
assert.Contains(t, result, "test", "should contain text") |
| 529 |
assert.Contains(t, result, "\n", "should contain newline") |
| 530 |
|
| 531 |
return result, err |
| 532 |
}, |
| 533 |
}, |
| 534 |
{ |
| 535 |
name: "Fprint writes to custom writer", |
| 536 |
testFn: func(t *testing.T) (string, error) { |
| 537 |
t.Helper() |
| 538 |
|
| 539 |
buf := &bytes.Buffer{} |
| 540 |
cfg := newTestConfig(buf) |
| 541 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 542 |
|
| 543 |
_, err := c.Fprint(buf, "test") |
| 544 |
|
| 545 |
return buf.String(), err |
| 546 |
}, |
| 547 |
}, |
| 548 |
{ |
| 549 |
name: "Fprintf formats and writes to custom writer", |
| 550 |
testFn: func(t *testing.T) (string, error) { |
| 551 |
t.Helper() |
| 552 |
|
| 553 |
buf := &bytes.Buffer{} |
| 554 |
cfg := newTestConfig(buf) |
| 555 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 556 |
|
| 557 |
_, err := c.Fprintf(buf, "value: %d", 42) |
| 558 |
|
| 559 |
return buf.String(), err |
| 560 |
}, |
| 561 |
}, |
| 562 |
{ |
| 563 |
name: "Fprintln writes with newline to custom writer", |
| 564 |
testFn: func(t *testing.T) (string, error) { |
| 565 |
t.Helper() |
| 566 |
|
| 567 |
buf := &bytes.Buffer{} |
| 568 |
cfg := newTestConfig(buf) |
| 569 |
c := color.NewWithConfig(cfg, color.FgMagenta) |
| 570 |
|
| 571 |
_, err := c.Fprintln(buf, "test") |
| 572 |
result := buf.String() |
| 573 |
// Contains "test" and "\n", escape codes are between them |
| 574 |
assert.Contains(t, result, "test", "should contain text") |
| 575 |
assert.Contains(t, result, "\n", "should contain newline") |
| 576 |
|
| 577 |
return result, err |
| 578 |
}, |
| 579 |
}, |
| 580 |
{ |
| 581 |
name: "Sprint returns string with color codes", |
| 582 |
testFn: func(t *testing.T) (string, error) { |
| 583 |
t.Helper() |
| 584 |
|
| 585 |
buf := &bytes.Buffer{} |
| 586 |
cfg := newTestConfig(buf) |
| 587 |
result := color.NewWithConfig(cfg, color.FgRed).Sprint("test") |
| 588 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 589 |
assert.Contains(t, result, "test", "should contain text") |
| 590 |
|
| 591 |
return result, nil |
| 592 |
}, |
| 593 |
}, |
| 594 |
{ |
| 595 |
name: "Sprintf returns formatted string with color codes", |
| 596 |
testFn: func(t *testing.T) (string, error) { |
| 597 |
t.Helper() |
| 598 |
|
| 599 |
buf := &bytes.Buffer{} |
| 600 |
cfg := newTestConfig(buf) |
| 601 |
result := color.NewWithConfig(cfg, color.FgRed).Sprintf("value: %d", 42) |
| 602 |
assert.Contains(t, result, "value: 42", "should contain formatted text") |
| 603 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 604 |
|
| 605 |
return result, nil |
| 606 |
}, |
| 607 |
}, |
| 608 |
{ |
| 609 |
name: "Sprintln returns string with newline and color codes", |
| 610 |
testFn: func(t *testing.T) (string, error) { |
| 611 |
t.Helper() |
| 612 |
|
| 613 |
buf := &bytes.Buffer{} |
| 614 |
cfg := newTestConfig(buf) |
| 615 |
result := color.NewWithConfig(cfg, color.FgGreen).Sprintln("test") |
| 616 |
// Contains "test" and "\n", escape codes are between them |
| 617 |
assert.Contains(t, result, "test", "should contain text") |
| 618 |
assert.Contains(t, result, "\n", "should contain newline") |
| 619 |
|
| 620 |
return result, nil |
| 621 |
}, |
| 622 |
}, |
| 623 |
} |
| 624 |
|
| 625 |
for _, tt := range tests { |
| 626 |
t.Run(tt.name, func(t *testing.T) { |
| 627 |
t.Parallel() |
| 628 |
_, err := tt.testFn(t) |
| 629 |
require.NoError(t, err) |
| 630 |
}) |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
// TestIntegration_ColorEqualityIntegration tests color equality functionality. |
| 635 |
func TestIntegration_ColorEqualityIntegration(t *testing.T) { |
| 636 |
t.Parallel() |
| 637 |
tests := []struct { |
| 638 |
name string |
| 639 |
testFn func(t *testing.T) bool |
| 640 |
}{ |
| 641 |
{ |
| 642 |
name: "identical colors are equal", |
| 643 |
testFn: func(t *testing.T) bool { |
| 644 |
t.Helper() |
| 645 |
|
| 646 |
buf := &bytes.Buffer{} |
| 647 |
cfg := newTestConfig(buf) |
| 648 |
c1 := color.NewWithConfig(cfg, color.FgRed, color.Bold) |
| 649 |
c2 := color.NewWithConfig(cfg, color.FgRed, color.Bold) |
| 650 |
|
| 651 |
return c1.Equals(c2) |
| 652 |
}, |
| 653 |
}, |
| 654 |
{ |
| 655 |
name: "different colors are not equal", |
| 656 |
testFn: func(t *testing.T) bool { |
| 657 |
t.Helper() |
| 658 |
|
| 659 |
buf := &bytes.Buffer{} |
| 660 |
cfg := newTestConfig(buf) |
| 661 |
c1 := color.NewWithConfig(cfg, color.FgRed) |
| 662 |
c2 := color.NewWithConfig(cfg, color.FgGreen) |
| 663 |
|
| 664 |
return !c1.Equals(c2) |
| 665 |
}, |
| 666 |
}, |
| 667 |
{ |
| 668 |
name: "colors with different attribute counts are not equal", |
| 669 |
testFn: func(t *testing.T) bool { |
| 670 |
t.Helper() |
| 671 |
|
| 672 |
buf := &bytes.Buffer{} |
| 673 |
cfg := newTestConfig(buf) |
| 674 |
c1 := color.NewWithConfig(cfg, color.FgRed) |
| 675 |
c2 := color.NewWithConfig(cfg, color.FgRed, color.Bold) |
| 676 |
|
| 677 |
return !c1.Equals(c2) |
| 678 |
}, |
| 679 |
}, |
| 680 |
{ |
| 681 |
name: "empty colors are equal", |
| 682 |
testFn: func(t *testing.T) bool { |
| 683 |
t.Helper() |
| 684 |
|
| 685 |
buf := &bytes.Buffer{} |
| 686 |
cfg := newTestConfig(buf) |
| 687 |
c1 := color.NewWithConfig(cfg) |
| 688 |
c2 := color.NewWithConfig(cfg) |
| 689 |
|
| 690 |
return c1.Equals(c2) |
| 691 |
}, |
| 692 |
}, |
| 693 |
{ |
| 694 |
name: "nil colors are equal", |
| 695 |
testFn: func(t *testing.T) bool { |
| 696 |
t.Helper() |
| 697 |
|
| 698 |
var ( |
| 699 |
c1 *color.Color |
| 700 |
c2 *color.Color |
| 701 |
) |
| 702 |
|
| 703 |
return c1.Equals(c2) |
| 704 |
}, |
| 705 |
}, |
| 706 |
{ |
| 707 |
name: "nil and non-nil are not equal", |
| 708 |
testFn: func(t *testing.T) bool { |
| 709 |
t.Helper() |
| 710 |
|
| 711 |
buf := &bytes.Buffer{} |
| 712 |
cfg := newTestConfig(buf) |
| 713 |
c1 := color.NewWithConfig(cfg, color.FgRed) |
| 714 |
|
| 715 |
var c2 *color.Color |
| 716 |
|
| 717 |
return !c1.Equals(c2) |
| 718 |
}, |
| 719 |
}, |
| 720 |
{ |
| 721 |
name: "RGB colors are equal if same values", |
| 722 |
testFn: func(t *testing.T) bool { |
| 723 |
t.Helper() |
| 724 |
|
| 725 |
c1 := color.RGB(255, 128, 64) |
| 726 |
c2 := color.RGB(255, 128, 64) |
| 727 |
|
| 728 |
return c1.Equals(c2) |
| 729 |
}, |
| 730 |
}, |
| 731 |
{ |
| 732 |
name: "RGB colors are not equal if different values", |
| 733 |
testFn: func(t *testing.T) bool { |
| 734 |
t.Helper() |
| 735 |
|
| 736 |
c1 := color.RGB(255, 128, 64) |
| 737 |
c2 := color.RGB(255, 128, 65) |
| 738 |
|
| 739 |
return !c1.Equals(c2) |
| 740 |
}, |
| 741 |
}, |
| 742 |
} |
| 743 |
|
| 744 |
for _, tt := range tests { |
| 745 |
t.Run(tt.name, func(t *testing.T) { |
| 746 |
t.Parallel() |
| 747 |
result := tt.testFn(t) |
| 748 |
assert.True(t, result) |
| 749 |
}) |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
// TestIntegration_FuncVariantsIntegration tests function variants integration. |
| 754 |
func TestIntegration_FuncVariantsIntegration(t *testing.T) { |
| 755 |
t.Parallel() |
| 756 |
|
| 757 |
tests := []struct { |
| 758 |
name string |
| 759 |
testFn func(t *testing.T) |
| 760 |
}{ |
| 761 |
{ |
| 762 |
name: "SprintFunc returns function that produces colored string", |
| 763 |
testFn: func(t *testing.T) { |
| 764 |
t.Helper() |
| 765 |
|
| 766 |
buf := &bytes.Buffer{} |
| 767 |
cfg := newTestConfig(buf) |
| 768 |
fn := color.NewWithConfig(cfg, color.FgRed).SprintFunc() |
| 769 |
result := fn("test") |
| 770 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 771 |
assert.Contains(t, result, "test", "should contain text") |
| 772 |
}, |
| 773 |
}, |
| 774 |
{ |
| 775 |
name: "SprintfFunc returns function that formats and colors", |
| 776 |
testFn: func(t *testing.T) { |
| 777 |
t.Helper() |
| 778 |
|
| 779 |
buf := &bytes.Buffer{} |
| 780 |
cfg := newTestConfig(buf) |
| 781 |
fn := color.NewWithConfig(cfg, color.FgRed).SprintfFunc() |
| 782 |
result := fn("value: %d", 42) |
| 783 |
assert.Contains(t, result, "value: 42", "should contain formatted text") |
| 784 |
}, |
| 785 |
}, |
| 786 |
{ |
| 787 |
name: "SprintlnFunc returns function that adds newline and colors", |
| 788 |
testFn: func(t *testing.T) { |
| 789 |
t.Helper() |
| 790 |
|
| 791 |
buf := &bytes.Buffer{} |
| 792 |
cfg := newTestConfig(buf) |
| 793 |
fn := color.NewWithConfig(cfg, color.FgBlue).SprintlnFunc() |
| 794 |
result := fn("test") |
| 795 |
// Contains "test" and "\n", escape codes are between them |
| 796 |
assert.Contains(t, result, "test", "should contain text") |
| 797 |
assert.Contains(t, result, "\n", "should contain newline") |
| 798 |
}, |
| 799 |
}, |
| 800 |
{ |
| 801 |
name: "PrintFunc returns function that prints to Output", |
| 802 |
testFn: func(t *testing.T) { |
| 803 |
t.Helper() |
| 804 |
|
| 805 |
buf := &bytes.Buffer{} |
| 806 |
cfg := newTestConfig(buf) |
| 807 |
fn := color.NewWithConfig(cfg, color.FgRed).PrintFunc() |
| 808 |
fn("test") |
| 809 |
|
| 810 |
assert.Contains(t, buf.String(), "\x1b[", "should contain ANSI escape code") |
| 811 |
}, |
| 812 |
}, |
| 813 |
{ |
| 814 |
name: "PrintfFunc returns function that formats and prints", |
| 815 |
testFn: func(t *testing.T) { |
| 816 |
t.Helper() |
| 817 |
|
| 818 |
buf := &bytes.Buffer{} |
| 819 |
cfg := newTestConfig(buf) |
| 820 |
fn := color.NewWithConfig(cfg, color.FgRed).PrintfFunc() |
| 821 |
fn("value: %d", 42) |
| 822 |
|
| 823 |
assert.Contains(t, buf.String(), "value: 42", "should contain formatted text") |
| 824 |
}, |
| 825 |
}, |
| 826 |
{ |
| 827 |
name: "PrintlnFunc returns function that prints with newline", |
| 828 |
testFn: func(t *testing.T) { |
| 829 |
t.Helper() |
| 830 |
|
| 831 |
buf := &bytes.Buffer{} |
| 832 |
cfg := newTestConfig(buf) |
| 833 |
fn := color.NewWithConfig(cfg, color.FgMagenta).PrintlnFunc() |
| 834 |
fn("test") |
| 835 |
|
| 836 |
result := buf.String() |
| 837 |
// Contains "test" and "\n", escape codes are between them |
| 838 |
assert.Contains(t, result, "test", "should contain text") |
| 839 |
assert.Contains(t, result, "\n", "should contain newline") |
| 840 |
}, |
| 841 |
}, |
| 842 |
{ |
| 843 |
name: "FprintFunc returns function that writes to custom writer", |
| 844 |
testFn: func(t *testing.T) { |
| 845 |
t.Helper() |
| 846 |
|
| 847 |
buf := &bytes.Buffer{} |
| 848 |
cfg := newTestConfig(buf) |
| 849 |
fn := color.NewWithConfig(cfg, color.FgRed).FprintFunc() |
| 850 |
fn(buf, "test") |
| 851 |
|
| 852 |
assert.Contains(t, buf.String(), "\x1b[", "should contain ANSI escape code") |
| 853 |
}, |
| 854 |
}, |
| 855 |
{ |
| 856 |
name: "FprintfFunc returns function that formats and writes", |
| 857 |
testFn: func(t *testing.T) { |
| 858 |
t.Helper() |
| 859 |
|
| 860 |
buf := &bytes.Buffer{} |
| 861 |
cfg := newTestConfig(buf) |
| 862 |
fn := color.NewWithConfig(cfg, color.FgRed).FprintfFunc() |
| 863 |
fn(buf, "value: %d", 42) |
| 864 |
|
| 865 |
assert.Contains(t, buf.String(), "value: 42", "should contain formatted text") |
| 866 |
}, |
| 867 |
}, |
| 868 |
{ |
| 869 |
name: "FprintlnFunc returns function that writes with newline", |
| 870 |
testFn: func(t *testing.T) { |
| 871 |
t.Helper() |
| 872 |
|
| 873 |
buf := &bytes.Buffer{} |
| 874 |
cfg := newTestConfig(buf) |
| 875 |
fn := color.NewWithConfig(cfg, color.FgGreen).FprintlnFunc() |
| 876 |
fn(buf, "test") |
| 877 |
|
| 878 |
result := buf.String() |
| 879 |
// Contains "test" and "\n", escape codes are between them |
| 880 |
assert.Contains(t, result, "test", "should contain text") |
| 881 |
assert.Contains(t, result, "\n", "should contain newline") |
| 882 |
}, |
| 883 |
}, |
| 884 |
} |
| 885 |
|
| 886 |
for _, tt := range tests { |
| 887 |
t.Run(tt.name, func(t *testing.T) { |
| 888 |
t.Parallel() |
| 889 |
tt.testFn(t) |
| 890 |
}) |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
// TestIntegration_DisableEnableColorIntegration tests DisableColor and EnableColor methods. |
| 895 |
func TestIntegration_DisableEnableColorIntegration(t *testing.T) { |
| 896 |
t.Parallel() |
| 897 |
tests := []struct { |
| 898 |
name string |
| 899 |
testFn func(t *testing.T) string |
| 900 |
}{ |
| 901 |
{ |
| 902 |
name: "DisableColor prevents color output", |
| 903 |
testFn: func(t *testing.T) string { |
| 904 |
t.Helper() |
| 905 |
|
| 906 |
buf := &bytes.Buffer{} |
| 907 |
cfg := newTestConfig(buf) |
| 908 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 909 |
c.DisableColor() |
| 910 |
|
| 911 |
return c.Sprint("test") |
| 912 |
}, |
| 913 |
}, |
| 914 |
{ |
| 915 |
name: "EnableColor allows color output", |
| 916 |
testFn: func(t *testing.T) string { |
| 917 |
t.Helper() |
| 918 |
|
| 919 |
buf := &bytes.Buffer{} |
| 920 |
cfg := newTestConfig(buf) |
| 921 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 922 |
c.EnableColor() |
| 923 |
|
| 924 |
return c.Sprint("test") |
| 925 |
}, |
| 926 |
}, |
| 927 |
{ |
| 928 |
name: "Disable then Enable toggles color state", |
| 929 |
testFn: func(t *testing.T) string { |
| 930 |
t.Helper() |
| 931 |
|
| 932 |
buf := &bytes.Buffer{} |
| 933 |
cfg := newTestConfig(buf) |
| 934 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 935 |
c.DisableColor() |
| 936 |
_ = c.Sprint("test") // Should be plain |
| 937 |
|
| 938 |
c.EnableColor() |
| 939 |
|
| 940 |
return c.Sprint("test") |
| 941 |
}, |
| 942 |
}, |
| 943 |
{ |
| 944 |
name: "Enable then Disable toggles color state off", |
| 945 |
testFn: func(t *testing.T) string { |
| 946 |
t.Helper() |
| 947 |
|
| 948 |
buf := &bytes.Buffer{} |
| 949 |
cfg := newTestConfig(buf) |
| 950 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 951 |
c.EnableColor() |
| 952 |
_ = c.Sprint("test") // Should have color |
| 953 |
|
| 954 |
c.DisableColor() |
| 955 |
|
| 956 |
return c.Sprint("test") |
| 957 |
}, |
| 958 |
}, |
| 959 |
} |
| 960 |
|
| 961 |
for _, tt := range tests { |
| 962 |
t.Run(tt.name, func(t *testing.T) { |
| 963 |
t.Parallel() |
| 964 |
result := tt.testFn(t) |
| 965 |
// Each test case has specific expected behavior |
| 966 |
switch tt.name { |
| 967 |
case "DisableColor prevents color output": |
| 968 |
assert.Equal(t, "test", result, "should be plain text") |
| 969 |
case "EnableColor allows color output": |
| 970 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 971 |
case "Disable then Enable toggles color state": |
| 972 |
assert.Contains(t, result, "\x1b[", "should have color after enable") |
| 973 |
case "Enable then Disable toggles color state off": |
| 974 |
assert.Equal(t, "test", result, "should be plain after disable") |
| 975 |
} |
| 976 |
}) |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
// TestIntegration_AddMethodChainingIntegration tests Add method chaining. |
| 981 |
func TestIntegration_AddMethodChainingIntegration(t *testing.T) { |
| 982 |
t.Parallel() |
| 983 |
tests := []struct { |
| 984 |
name string |
| 985 |
testFn func(t *testing.T) string |
| 986 |
}{ |
| 987 |
{ |
| 988 |
name: "Add chains multiple attributes", |
| 989 |
testFn: func(t *testing.T) string { |
| 990 |
t.Helper() |
| 991 |
|
| 992 |
buf := &bytes.Buffer{} |
| 993 |
cfg := newTestConfig(buf) |
| 994 |
c := color.NewWithConfig(cfg, color.FgRed).Add(color.Bold).Add(color.Underline) |
| 995 |
|
| 996 |
return c.Sprint("test") |
| 997 |
}, |
| 998 |
}, |
| 999 |
{ |
| 1000 |
name: "Add can be called multiple times", |
| 1001 |
testFn: func(t *testing.T) string { |
| 1002 |
t.Helper() |
| 1003 |
|
| 1004 |
buf := &bytes.Buffer{} |
| 1005 |
cfg := newTestConfig(buf) |
| 1006 |
c := color.NewWithConfig(cfg) |
| 1007 |
c.Add(color.FgRed) |
| 1008 |
c.Add(color.Bold) |
| 1009 |
c.Add(color.Underline) |
| 1010 |
|
| 1011 |
return c.Sprint("test") |
| 1012 |
}, |
| 1013 |
}, |
| 1014 |
{ |
| 1015 |
name: "AddRGB chains RGB foreground", |
| 1016 |
testFn: func(t *testing.T) string { |
| 1017 |
t.Helper() |
| 1018 |
|
| 1019 |
buf := &bytes.Buffer{} |
| 1020 |
cfg := newTestConfig(buf) |
| 1021 |
c := color.NewWithConfig(cfg).AddRGB(255, 128, 64) |
| 1022 |
|
| 1023 |
return c.Sprint("test") |
| 1024 |
}, |
| 1025 |
}, |
| 1026 |
{ |
| 1027 |
name: "AddBgRGB chains RGB background", |
| 1028 |
testFn: func(t *testing.T) string { |
| 1029 |
t.Helper() |
| 1030 |
|
| 1031 |
buf := &bytes.Buffer{} |
| 1032 |
cfg := newTestConfig(buf) |
| 1033 |
c := color.NewWithConfig(cfg).AddBgRGB(128, 64, 32) |
| 1034 |
|
| 1035 |
return c.Sprint("test") |
| 1036 |
}, |
| 1037 |
}, |
| 1038 |
} |
| 1039 |
|
| 1040 |
for _, tt := range tests { |
| 1041 |
t.Run(tt.name, func(t *testing.T) { |
| 1042 |
t.Parallel() |
| 1043 |
result := tt.testFn(t) |
| 1044 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 1045 |
assert.Contains(t, result, "test", "should contain text") |
| 1046 |
}) |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
// TestIntegration_SetWriterIntegration tests SetWriter and UnsetWriter methods. |
| 1051 |
func TestIntegration_SetWriterIntegration(t *testing.T) { |
| 1052 |
t.Parallel() |
| 1053 |
|
| 1054 |
tests := []struct { |
| 1055 |
name string |
| 1056 |
testFn func(t *testing.T) |
| 1057 |
}{ |
| 1058 |
{ |
| 1059 |
name: "SetWriter and UnsetWriter work together", |
| 1060 |
testFn: func(t *testing.T) { |
| 1061 |
t.Helper() |
| 1062 |
|
| 1063 |
buf := &bytes.Buffer{} |
| 1064 |
cfg := newTestConfig(buf) |
| 1065 |
|
| 1066 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 1067 |
c.SetWriter(buf) |
| 1068 |
_, _ = c.Print("test") |
| 1069 |
|
| 1070 |
result := buf.String() |
| 1071 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 1072 |
|
| 1073 |
c.UnsetWriter(buf) |
| 1074 |
}, |
| 1075 |
}, |
| 1076 |
{ |
| 1077 |
name: "SetWriter is skipped when NoColor is set", |
| 1078 |
testFn: func(t *testing.T) { |
| 1079 |
t.Helper() |
| 1080 |
|
| 1081 |
buf := &bytes.Buffer{} |
| 1082 |
cfg := &color.Config{NoColor: true, Output: buf, Error: buf} |
| 1083 |
|
| 1084 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 1085 |
c.SetWriter(buf) |
| 1086 |
_, _ = c.Print("test") |
| 1087 |
|
| 1088 |
result := buf.String() |
| 1089 |
assert.Equal(t, "test", result, "should write plain text when NoColor is set") |
| 1090 |
}, |
| 1091 |
}, |
| 1092 |
} |
| 1093 |
|
| 1094 |
for _, tt := range tests { |
| 1095 |
t.Run(tt.name, func(t *testing.T) { |
| 1096 |
t.Parallel() |
| 1097 |
tt.testFn(t) |
| 1098 |
}) |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
// TestIntegration_SetMethodIntegration tests Set method integration. |
| 1103 |
func TestIntegration_SetMethodIntegration(t *testing.T) { |
| 1104 |
t.Parallel() |
| 1105 |
|
| 1106 |
tests := []struct { |
| 1107 |
name string |
| 1108 |
testFn func(t *testing.T) |
| 1109 |
}{ |
| 1110 |
{ |
| 1111 |
name: "Set method applies color", |
| 1112 |
testFn: func(t *testing.T) { |
| 1113 |
t.Helper() |
| 1114 |
|
| 1115 |
buf := &bytes.Buffer{} |
| 1116 |
cfg := newTestConfig(buf) |
| 1117 |
|
| 1118 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 1119 |
c.Set() |
| 1120 |
|
| 1121 |
_ = buf.String() |
| 1122 |
}, |
| 1123 |
}, |
| 1124 |
{ |
| 1125 |
name: "Set is skipped when NoColor is set", |
| 1126 |
testFn: func(t *testing.T) { |
| 1127 |
t.Helper() |
| 1128 |
|
| 1129 |
buf := &bytes.Buffer{} |
| 1130 |
cfg := &color.Config{NoColor: true, Output: buf, Error: buf} |
| 1131 |
|
| 1132 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 1133 |
c.Set() |
| 1134 |
|
| 1135 |
_ = buf.String() |
| 1136 |
}, |
| 1137 |
}, |
| 1138 |
} |
| 1139 |
|
| 1140 |
for _, tt := range tests { |
| 1141 |
t.Run(tt.name, func(t *testing.T) { |
| 1142 |
t.Parallel() |
| 1143 |
tt.testFn(t) |
| 1144 |
}) |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
// TestIntegration_HiIntensityColorsIntegration tests hi-intensity color helpers. |
| 1149 |
func TestIntegration_HiIntensityColorsIntegration(t *testing.T) { |
| 1150 |
t.Parallel() |
| 1151 |
|
| 1152 |
tests := []struct { |
| 1153 |
name string |
| 1154 |
testFn func() string |
| 1155 |
}{ |
| 1156 |
{ |
| 1157 |
name: "HiRed produces hi-intensity red", |
| 1158 |
testFn: func() string { |
| 1159 |
buf := &bytes.Buffer{} |
| 1160 |
cfg := newTestConfig(buf) |
| 1161 |
|
| 1162 |
return color.NewWithConfig(cfg, color.FgHiRed).Sprint("test") |
| 1163 |
}, |
| 1164 |
}, |
| 1165 |
{ |
| 1166 |
name: "HiGreen produces hi-intensity green", |
| 1167 |
testFn: func() string { |
| 1168 |
buf := &bytes.Buffer{} |
| 1169 |
cfg := newTestConfig(buf) |
| 1170 |
|
| 1171 |
return color.NewWithConfig(cfg, color.FgHiGreen).Sprint("test") |
| 1172 |
}, |
| 1173 |
}, |
| 1174 |
{ |
| 1175 |
name: "HiBlue produces hi-intensity blue", |
| 1176 |
testFn: func() string { |
| 1177 |
buf := &bytes.Buffer{} |
| 1178 |
cfg := newTestConfig(buf) |
| 1179 |
|
| 1180 |
return color.NewWithConfig(cfg, color.FgHiBlue).Sprint("test") |
| 1181 |
}, |
| 1182 |
}, |
| 1183 |
{ |
| 1184 |
name: "HiYellow produces hi-intensity yellow", |
| 1185 |
testFn: func() string { |
| 1186 |
buf := &bytes.Buffer{} |
| 1187 |
cfg := newTestConfig(buf) |
| 1188 |
|
| 1189 |
return color.NewWithConfig(cfg, color.FgHiYellow).Sprint("test") |
| 1190 |
}, |
| 1191 |
}, |
| 1192 |
{ |
| 1193 |
name: "HiCyan produces hi-intensity cyan", |
| 1194 |
testFn: func() string { |
| 1195 |
buf := &bytes.Buffer{} |
| 1196 |
cfg := newTestConfig(buf) |
| 1197 |
|
| 1198 |
return color.NewWithConfig(cfg, color.FgHiCyan).Sprint("test") |
| 1199 |
}, |
| 1200 |
}, |
| 1201 |
{ |
| 1202 |
name: "HiMagenta produces hi-intensity magenta", |
| 1203 |
testFn: func() string { |
| 1204 |
buf := &bytes.Buffer{} |
| 1205 |
cfg := newTestConfig(buf) |
| 1206 |
|
| 1207 |
return color.NewWithConfig(cfg, color.FgHiMagenta).Sprint("test") |
| 1208 |
}, |
| 1209 |
}, |
| 1210 |
{ |
| 1211 |
name: "HiWhite produces hi-intensity white", |
| 1212 |
testFn: func() string { |
| 1213 |
buf := &bytes.Buffer{} |
| 1214 |
cfg := newTestConfig(buf) |
| 1215 |
|
| 1216 |
return color.NewWithConfig(cfg, color.FgHiWhite).Sprint("test") |
| 1217 |
}, |
| 1218 |
}, |
| 1219 |
{ |
| 1220 |
name: "HiBlack produces hi-intensity black", |
| 1221 |
testFn: func() string { |
| 1222 |
buf := &bytes.Buffer{} |
| 1223 |
cfg := newTestConfig(buf) |
| 1224 |
|
| 1225 |
return color.NewWithConfig(cfg, color.FgHiBlack).Sprint("test") |
| 1226 |
}, |
| 1227 |
}, |
| 1228 |
} |
| 1229 |
|
| 1230 |
for _, tt := range tests { |
| 1231 |
t.Run(tt.name, func(t *testing.T) { |
| 1232 |
t.Parallel() |
| 1233 |
|
| 1234 |
result := tt.testFn() |
| 1235 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 1236 |
assert.Contains(t, result, "test", "should contain text") |
| 1237 |
}) |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
// TestIntegration_BackgroundColorsIntegration tests background color helpers. |
| 1242 |
func TestIntegration_BackgroundColorsIntegration(t *testing.T) { |
| 1243 |
t.Parallel() |
| 1244 |
|
| 1245 |
tests := []struct { |
| 1246 |
name string |
| 1247 |
testFn func() string |
| 1248 |
}{ |
| 1249 |
{ |
| 1250 |
name: "BgBlack background", |
| 1251 |
testFn: func() string { |
| 1252 |
buf := &bytes.Buffer{} |
| 1253 |
cfg := newTestConfig(buf) |
| 1254 |
|
| 1255 |
return color.NewWithConfig(cfg, color.BgBlack).Sprint("test") |
| 1256 |
}, |
| 1257 |
}, |
| 1258 |
{ |
| 1259 |
name: "BgRed background", |
| 1260 |
testFn: func() string { |
| 1261 |
buf := &bytes.Buffer{} |
| 1262 |
cfg := newTestConfig(buf) |
| 1263 |
|
| 1264 |
return color.NewWithConfig(cfg, color.BgRed).Sprint("test") |
| 1265 |
}, |
| 1266 |
}, |
| 1267 |
{ |
| 1268 |
name: "BgGreen background", |
| 1269 |
testFn: func() string { |
| 1270 |
buf := &bytes.Buffer{} |
| 1271 |
cfg := newTestConfig(buf) |
| 1272 |
|
| 1273 |
return color.NewWithConfig(cfg, color.BgGreen).Sprint("test") |
| 1274 |
}, |
| 1275 |
}, |
| 1276 |
{ |
| 1277 |
name: "BgYellow background", |
| 1278 |
testFn: func() string { |
| 1279 |
buf := &bytes.Buffer{} |
| 1280 |
cfg := newTestConfig(buf) |
| 1281 |
|
| 1282 |
return color.NewWithConfig(cfg, color.BgYellow).Sprint("test") |
| 1283 |
}, |
| 1284 |
}, |
| 1285 |
{ |
| 1286 |
name: "BgBlue background", |
| 1287 |
testFn: func() string { |
| 1288 |
buf := &bytes.Buffer{} |
| 1289 |
cfg := newTestConfig(buf) |
| 1290 |
|
| 1291 |
return color.NewWithConfig(cfg, color.BgBlue).Sprint("test") |
| 1292 |
}, |
| 1293 |
}, |
| 1294 |
{ |
| 1295 |
name: "BgMagenta background", |
| 1296 |
testFn: func() string { |
| 1297 |
buf := &bytes.Buffer{} |
| 1298 |
cfg := newTestConfig(buf) |
| 1299 |
|
| 1300 |
return color.NewWithConfig(cfg, color.BgMagenta).Sprint("test") |
| 1301 |
}, |
| 1302 |
}, |
| 1303 |
{ |
| 1304 |
name: "BgCyan background", |
| 1305 |
testFn: func() string { |
| 1306 |
buf := &bytes.Buffer{} |
| 1307 |
cfg := newTestConfig(buf) |
| 1308 |
|
| 1309 |
return color.NewWithConfig(cfg, color.BgCyan).Sprint("test") |
| 1310 |
}, |
| 1311 |
}, |
| 1312 |
{ |
| 1313 |
name: "BgWhite background", |
| 1314 |
testFn: func() string { |
| 1315 |
buf := &bytes.Buffer{} |
| 1316 |
cfg := newTestConfig(buf) |
| 1317 |
|
| 1318 |
return color.NewWithConfig(cfg, color.BgWhite).Sprint("test") |
| 1319 |
}, |
| 1320 |
}, |
| 1321 |
} |
| 1322 |
|
| 1323 |
for _, tt := range tests { |
| 1324 |
t.Run(tt.name, func(t *testing.T) { |
| 1325 |
t.Parallel() |
| 1326 |
|
| 1327 |
result := tt.testFn() |
| 1328 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 1329 |
assert.Contains(t, result, "test", "should contain text") |
| 1330 |
}) |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
// TestIntegration_StyleAttributesIntegration tests style attribute helpers. |
| 1335 |
func TestIntegration_StyleAttributesIntegration(t *testing.T) { |
| 1336 |
t.Parallel() |
| 1337 |
|
| 1338 |
tests := []struct { |
| 1339 |
name string |
| 1340 |
testFn func() string |
| 1341 |
}{ |
| 1342 |
{ |
| 1343 |
name: "Bold style", |
| 1344 |
testFn: func() string { |
| 1345 |
buf := &bytes.Buffer{} |
| 1346 |
cfg := newTestConfig(buf) |
| 1347 |
|
| 1348 |
return color.NewWithConfig(cfg, color.Bold).Sprint("test") |
| 1349 |
}, |
| 1350 |
}, |
| 1351 |
{ |
| 1352 |
name: "Faint style", |
| 1353 |
testFn: func() string { |
| 1354 |
buf := &bytes.Buffer{} |
| 1355 |
cfg := newTestConfig(buf) |
| 1356 |
|
| 1357 |
return color.NewWithConfig(cfg, color.Faint).Sprint("test") |
| 1358 |
}, |
| 1359 |
}, |
| 1360 |
{ |
| 1361 |
name: "Italic style", |
| 1362 |
testFn: func() string { |
| 1363 |
buf := &bytes.Buffer{} |
| 1364 |
cfg := newTestConfig(buf) |
| 1365 |
|
| 1366 |
return color.NewWithConfig(cfg, color.Italic).Sprint("test") |
| 1367 |
}, |
| 1368 |
}, |
| 1369 |
{ |
| 1370 |
name: "Underline style", |
| 1371 |
testFn: func() string { |
| 1372 |
buf := &bytes.Buffer{} |
| 1373 |
cfg := newTestConfig(buf) |
| 1374 |
|
| 1375 |
return color.NewWithConfig(cfg, color.Underline).Sprint("test") |
| 1376 |
}, |
| 1377 |
}, |
| 1378 |
{ |
| 1379 |
name: "BlinkSlow style", |
| 1380 |
testFn: func() string { |
| 1381 |
buf := &bytes.Buffer{} |
| 1382 |
cfg := newTestConfig(buf) |
| 1383 |
|
| 1384 |
return color.NewWithConfig(cfg, color.BlinkSlow).Sprint("test") |
| 1385 |
}, |
| 1386 |
}, |
| 1387 |
{ |
| 1388 |
name: "BlinkRapid style", |
| 1389 |
testFn: func() string { |
| 1390 |
buf := &bytes.Buffer{} |
| 1391 |
cfg := newTestConfig(buf) |
| 1392 |
|
| 1393 |
return color.NewWithConfig(cfg, color.BlinkRapid).Sprint("test") |
| 1394 |
}, |
| 1395 |
}, |
| 1396 |
{ |
| 1397 |
name: "ReverseVideo style", |
| 1398 |
testFn: func() string { |
| 1399 |
buf := &bytes.Buffer{} |
| 1400 |
cfg := newTestConfig(buf) |
| 1401 |
|
| 1402 |
return color.NewWithConfig(cfg, color.ReverseVideo).Sprint("test") |
| 1403 |
}, |
| 1404 |
}, |
| 1405 |
{ |
| 1406 |
name: "Concealed style", |
| 1407 |
testFn: func() string { |
| 1408 |
buf := &bytes.Buffer{} |
| 1409 |
cfg := newTestConfig(buf) |
| 1410 |
|
| 1411 |
return color.NewWithConfig(cfg, color.Concealed).Sprint("test") |
| 1412 |
}, |
| 1413 |
}, |
| 1414 |
{ |
| 1415 |
name: "CrossedOut style", |
| 1416 |
testFn: func() string { |
| 1417 |
buf := &bytes.Buffer{} |
| 1418 |
cfg := newTestConfig(buf) |
| 1419 |
|
| 1420 |
return color.NewWithConfig(cfg, color.CrossedOut).Sprint("test") |
| 1421 |
}, |
| 1422 |
}, |
| 1423 |
} |
| 1424 |
|
| 1425 |
for _, tt := range tests { |
| 1426 |
t.Run(tt.name, func(t *testing.T) { |
| 1427 |
t.Parallel() |
| 1428 |
|
| 1429 |
result := tt.testFn() |
| 1430 |
assert.Contains(t, result, "\x1b[", "should contain ANSI escape code") |
| 1431 |
assert.Contains(t, result, "test", "should contain text") |
| 1432 |
}) |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
// TestIntegration_ComplexScenariosIntegration tests complex real-world scenarios. |
| 1437 |
func TestIntegration_ComplexScenariosIntegration(t *testing.T) { |
| 1438 |
t.Parallel() |
| 1439 |
tests := []struct { |
| 1440 |
name string |
| 1441 |
testFn func(t *testing.T) |
| 1442 |
}{ |
| 1443 |
{ |
| 1444 |
name: "colored log message simulation", |
| 1445 |
testFn: func(t *testing.T) { |
| 1446 |
t.Helper() |
| 1447 |
|
| 1448 |
buf := &bytes.Buffer{} |
| 1449 |
cfg := newTestConfig(buf) |
| 1450 |
timestamp := color.NewWithConfig(cfg, color.FgWhite).Sprint("2024-01-15 10:30:00") |
| 1451 |
level := color.NewWithConfig(cfg, color.FgRed).Sprint("[ERROR]") |
| 1452 |
message := color.NewWithConfig(cfg, color.FgWhite).Sprint("Something went wrong") |
| 1453 |
|
| 1454 |
logLine := timestamp + " " + level + " " + message |
| 1455 |
|
| 1456 |
assert.Contains(t, logLine, "2024-01-15 10:30:00") |
| 1457 |
assert.Contains(t, logLine, "[ERROR]") |
| 1458 |
assert.Contains(t, logLine, "Something went wrong") |
| 1459 |
}, |
| 1460 |
}, |
| 1461 |
{ |
| 1462 |
name: "table output simulation", |
| 1463 |
testFn: func(t *testing.T) { |
| 1464 |
t.Helper() |
| 1465 |
|
| 1466 |
buf := &bytes.Buffer{} |
| 1467 |
cfg := newTestConfig(buf) |
| 1468 |
headerName := color.NewWithConfig(cfg, color.Bold).Sprint("Name") |
| 1469 |
headerAge := color.NewWithConfig(cfg, color.Bold).Sprint("Age") |
| 1470 |
row1Name := color.NewWithConfig(cfg, color.FgCyan).Sprint("Alice") |
| 1471 |
row1Age := color.NewWithConfig(cfg, color.FgGreen).Sprint("30") |
| 1472 |
|
| 1473 |
assert.NotEmpty(t, headerName) |
| 1474 |
assert.NotEmpty(t, headerAge) |
| 1475 |
assert.NotEmpty(t, row1Name) |
| 1476 |
assert.NotEmpty(t, row1Age) |
| 1477 |
}, |
| 1478 |
}, |
| 1479 |
{ |
| 1480 |
name: "mixed colored and plain text", |
| 1481 |
testFn: func(t *testing.T) { |
| 1482 |
t.Helper() |
| 1483 |
|
| 1484 |
buf := &bytes.Buffer{} |
| 1485 |
cfg := newTestConfig(buf) |
| 1486 |
prefix := "Status: " |
| 1487 |
status := color.NewWithConfig(cfg, color.FgGreen).Sprint("OK") |
| 1488 |
suffix := " - Server running" |
| 1489 |
|
| 1490 |
result := prefix + status + suffix |
| 1491 |
|
| 1492 |
assert.Contains(t, result, "Status: ") |
| 1493 |
assert.Contains(t, result, "OK") |
| 1494 |
assert.Contains(t, result, " - Server running") |
| 1495 |
}, |
| 1496 |
}, |
| 1497 |
{ |
| 1498 |
name: "colored string builder", |
| 1499 |
testFn: func(t *testing.T) { |
| 1500 |
t.Helper() |
| 1501 |
|
| 1502 |
buf := &bytes.Buffer{} |
| 1503 |
cfg := newTestConfig(buf) |
| 1504 |
|
| 1505 |
var builder bytes.Buffer |
| 1506 |
builder.WriteString(color.NewWithConfig(cfg, color.FgRed).Sprint("Error: ")) |
| 1507 |
builder.WriteString(color.NewWithConfig(cfg, color.FgWhite).Sprint("File not found")) |
| 1508 |
builder.WriteString(color.NewWithConfig(cfg, color.FgRed).Sprint("\n")) |
| 1509 |
builder.WriteString(color.NewWithConfig(cfg, color.FgYellow).Sprint("Please check the path")) |
| 1510 |
|
| 1511 |
result := builder.String() |
| 1512 |
|
| 1513 |
assert.Contains(t, result, "Error: ") |
| 1514 |
assert.Contains(t, result, "File not found") |
| 1515 |
assert.Contains(t, result, "Please check the path") |
| 1516 |
}, |
| 1517 |
}, |
| 1518 |
} |
| 1519 |
|
| 1520 |
for _, tt := range tests { |
| 1521 |
t.Run(tt.name, func(t *testing.T) { |
| 1522 |
t.Parallel() |
| 1523 |
tt.testFn(t) |
| 1524 |
}) |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
// TestIntegration_ConcurrentAccessIntegration tests concurrent access to color instances. |
| 1529 |
func TestIntegration_ConcurrentAccessIntegration(t *testing.T) { |
| 1530 |
t.Parallel() |
| 1531 |
|
| 1532 |
var wg sync.WaitGroup |
| 1533 |
|
| 1534 |
// Run concurrent accesses to different color instances |
| 1535 |
for range 100 { |
| 1536 |
wg.Go(func() { |
| 1537 |
// Each goroutine creates its own config and color instances |
| 1538 |
buf := &bytes.Buffer{} |
| 1539 |
cfg := &color.Config{ |
| 1540 |
NoColor: false, |
| 1541 |
Output: buf, |
| 1542 |
Error: buf, |
| 1543 |
} |
| 1544 |
|
| 1545 |
// Various color operations |
| 1546 |
_ = color.NewWithConfig(cfg, color.FgRed).Sprint("test") |
| 1547 |
_ = color.NewWithConfig(cfg, color.FgGreen).Sprint("test") |
| 1548 |
_ = color.NewWithConfig(cfg, color.FgBlue).Sprint("test") |
| 1549 |
_ = color.NewWithConfig(cfg, color.FgYellow).Sprint("test") |
| 1550 |
}) |
| 1551 |
} |
| 1552 |
|
| 1553 |
wg.Wait() |
| 1554 |
// If we get here without race conditions, test passes |
| 1555 |
} |
| 1556 |
|
| 1557 |
// TestIntegration_EnvNO_COLORIntegration tests NO_COLOR environment variable handling. |
| 1558 |
func TestIntegration_EnvNO_COLORIntegration(t *testing.T) { |
| 1559 |
t.Parallel() |
| 1560 |
// Save and restore environment |
| 1561 |
originalEnv := os.Getenv("NO_COLOR") |
| 1562 |
|
| 1563 |
t.Cleanup(func() { |
| 1564 |
if originalEnv == "" { |
| 1565 |
os.Unsetenv("NO_COLOR") |
| 1566 |
} else { |
| 1567 |
os.Setenv("NO_COLOR", originalEnv) |
| 1568 |
} |
| 1569 |
}) |
| 1570 |
|
| 1571 |
tests := []struct { |
| 1572 |
name string |
| 1573 |
envVal string |
| 1574 |
testFn func(t *testing.T) bool |
| 1575 |
}{ |
| 1576 |
{ |
| 1577 |
name: "NO_COLOR set disables colors via explicit config", |
| 1578 |
envVal: "1", |
| 1579 |
testFn: func(t *testing.T) bool { |
| 1580 |
t.Helper() |
| 1581 |
os.Setenv("NO_COLOR", "1") |
| 1582 |
// Create a new config which should detect NO_COLOR |
| 1583 |
cfg := &color.Config{NoColor: true, Output: &bytes.Buffer{}, Error: &bytes.Buffer{}} |
| 1584 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 1585 |
result := c.Sprint("test") |
| 1586 |
|
| 1587 |
return result == "test" |
| 1588 |
}, |
| 1589 |
}, |
| 1590 |
{ |
| 1591 |
name: "NO_COLOR unset allows colors with explicit config", |
| 1592 |
envVal: "", |
| 1593 |
testFn: func(t *testing.T) bool { |
| 1594 |
t.Helper() |
| 1595 |
os.Unsetenv("NO_COLOR") |
| 1596 |
|
| 1597 |
cfg := &color.Config{NoColor: false, Output: &bytes.Buffer{}, Error: &bytes.Buffer{}} |
| 1598 |
c := color.NewWithConfig(cfg, color.FgRed) |
| 1599 |
result := c.Sprint("test") |
| 1600 |
|
| 1601 |
return len(result) > 4 // Contains escape codes + text |
| 1602 |
}, |
| 1603 |
}, |
| 1604 |
} |
| 1605 |
|
| 1606 |
for _, tt := range tests { |
| 1607 |
t.Run(tt.name, func(t *testing.T) { |
| 1608 |
t.Parallel() |
| 1609 |
result := tt.testFn(t) |
| 1610 |
assert.True(t, result) |
| 1611 |
}) |
| 1612 |
} |
| 1613 |
} |