Debian Codegraph @

pkg/color/color_test.go

raw
1 package color
2
3 import (
4 "bytes"
5 "io"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 )
11
12 // Ensure Color implements io.Writer interface.
13 var _ io.Writer = (*bytes.Buffer)(nil)
14
15 // newTestColor creates a Color with a test configuration for thread-safe testing.
16 func newTestColor(output *bytes.Buffer, noColor bool, attrs ...Attribute) *Color {
17 cfg := &Config{
18 NoColor: noColor,
19 Output: output,
20 Error: output,
21 }
22
23 return NewWithConfig(cfg, attrs...)
24 }
25
26 // TestColor_Add tests the Add method which chains SGR parameters.
27 func TestColor_Add(t *testing.T) {
28 t.Parallel()
29
30 tests := []struct {
31 name string
32 initialAttrs []Attribute
33 addAttrs []Attribute
34 wantParamCount int
35 }{
36 {
37 name: "adds single attribute",
38 initialAttrs: []Attribute{FgRed},
39 addAttrs: []Attribute{Bold},
40 wantParamCount: 2,
41 },
42 {
43 name: "adds multiple attributes",
44 initialAttrs: []Attribute{FgGreen},
45 addAttrs: []Attribute{Bold, Underline},
46 wantParamCount: 3,
47 },
48 {
49 name: "adds to empty color",
50 initialAttrs: []Attribute{},
51 addAttrs: []Attribute{FgBlue},
52 wantParamCount: 1,
53 },
54 {
55 name: "adds background color",
56 initialAttrs: []Attribute{FgRed},
57 addAttrs: []Attribute{BgYellow},
58 wantParamCount: 2,
59 },
60 {
61 name: "returns same color instance for chaining",
62 initialAttrs: []Attribute{FgRed},
63 addAttrs: []Attribute{Bold},
64 wantParamCount: 2,
65 },
66 }
67
68 for _, tt := range tests {
69 t.Run(tt.name, func(t *testing.T) {
70 t.Parallel()
71
72 buf := &bytes.Buffer{}
73 c := newTestColor(buf, false, tt.initialAttrs...)
74 originalColor := c
75
76 result := c.Add(tt.addAttrs...)
77
78 assert.Len(t, c.params, tt.wantParamCount, "param count should match")
79 assert.Same(t, originalColor, result, "should return same color instance for chaining")
80 })
81 }
82 }
83
84 // TestColor_AddBgRGB tests the AddBgRGB method for background RGB colors.
85 func TestColor_AddBgRGB(t *testing.T) {
86 t.Parallel()
87
88 tests := []struct {
89 name string
90 r int
91 g int
92 b int
93 wantParams int
94 }{
95 {
96 name: "adds RGB background color",
97 r: 255,
98 g: 128,
99 b: 0,
100 wantParams: 5,
101 },
102 {
103 name: "adds zero RGB background",
104 r: 0,
105 g: 0,
106 b: 0,
107 wantParams: 5,
108 },
109 {
110 name: "adds max RGB background",
111 r: 255,
112 g: 255,
113 b: 255,
114 wantParams: 5,
115 },
116 }
117
118 for _, tt := range tests {
119 t.Run(tt.name, func(t *testing.T) {
120 t.Parallel()
121
122 buf := &bytes.Buffer{}
123 c := newTestColor(buf, false)
124 result := c.AddBgRGB(tt.r, tt.g, tt.b)
125
126 assert.Len(t, c.params, tt.wantParams, "should have correct param count")
127 assert.Same(t, c, result, "should return same color for chaining")
128 })
129 }
130 }
131
132 // TestColor_AddRGB tests the AddRGB method for foreground RGB colors.
133 func TestColor_AddRGB(t *testing.T) {
134 t.Parallel()
135
136 tests := []struct {
137 name string
138 r int
139 g int
140 b int
141 wantParams int
142 }{
143 {
144 name: "adds RGB foreground color",
145 r: 255,
146 g: 128,
147 b: 0,
148 wantParams: 5,
149 },
150 {
151 name: "adds zero RGB foreground",
152 r: 0,
153 g: 0,
154 b: 0,
155 wantParams: 5,
156 },
157 {
158 name: "adds max RGB foreground",
159 r: 255,
160 g: 255,
161 b: 255,
162 wantParams: 5,
163 },
164 }
165
166 for _, tt := range tests {
167 t.Run(tt.name, func(t *testing.T) {
168 t.Parallel()
169
170 buf := &bytes.Buffer{}
171 c := newTestColor(buf, false)
172 result := c.AddRGB(tt.r, tt.g, tt.b)
173
174 assert.Len(t, c.params, tt.wantParams, "should have correct param count")
175 assert.Same(t, c, result, "should return same color for chaining")
176 })
177 }
178 }
179
180 // TestColor_DisableColor tests the DisableColor method.
181 func TestColor_DisableColor(t *testing.T) {
182 t.Parallel()
183
184 tests := []struct {
185 name string
186 attrs []Attribute
187 wantNil bool
188 }{
189 {
190 name: "disables color on new color",
191 attrs: []Attribute{FgRed},
192 wantNil: false,
193 },
194 {
195 name: "disables color on empty color",
196 attrs: []Attribute{},
197 wantNil: false,
198 },
199 }
200
201 for _, tt := range tests {
202 t.Run(tt.name, func(t *testing.T) {
203 t.Parallel()
204
205 buf := &bytes.Buffer{}
206 c := newTestColor(buf, false, tt.attrs...)
207 c.DisableColor()
208
209 require.NotNil(t, c.noColor, "noColor should be set")
210 assert.True(t, *c.noColor, "noColor should be true")
211 })
212 }
213 }
214
215 // TestColor_EnableColor tests the EnableColor method.
216 func TestColor_EnableColor(t *testing.T) {
217 t.Parallel()
218
219 tests := []struct {
220 name string
221 attrs []Attribute
222 wantNil bool
223 }{
224 {
225 name: "enables color on new color",
226 attrs: []Attribute{FgRed},
227 wantNil: false,
228 },
229 {
230 name: "enables color on empty color",
231 attrs: []Attribute{},
232 wantNil: false,
233 },
234 }
235
236 for _, tt := range tests {
237 t.Run(tt.name, func(t *testing.T) {
238 t.Parallel()
239
240 buf := &bytes.Buffer{}
241 c := newTestColor(buf, true, tt.attrs...)
242 c.EnableColor()
243
244 require.NotNil(t, c.noColor, "noColor should be set")
245 assert.False(t, *c.noColor, "noColor should be false")
246 })
247 }
248 }
249
250 // TestColor_Equals tests the Equals method.
251 func TestColor_Equals(t *testing.T) {
252 t.Parallel()
253
254 tests := []struct {
255 name string
256 c *Color
257 colorToCompare *Color
258 want bool
259 }{
260 {
261 name: "equal colors",
262 c: New(FgRed, Bold),
263 colorToCompare: New(FgRed, Bold),
264 want: true,
265 },
266 {
267 name: "different colors",
268 c: New(FgRed),
269 colorToCompare: New(FgGreen),
270 want: false,
271 },
272 {
273 name: "different param counts",
274 c: New(FgRed),
275 colorToCompare: New(FgRed, Bold),
276 want: false,
277 },
278 {
279 name: "both nil",
280 c: nil,
281 colorToCompare: nil,
282 want: true,
283 },
284 {
285 name: "one nil",
286 c: New(FgRed),
287 colorToCompare: nil,
288 want: false,
289 },
290 {
291 name: "same instance",
292 c: New(FgRed),
293 colorToCompare: New(FgRed),
294 want: true,
295 },
296 {
297 name: "empty colors equal",
298 c: New(),
299 colorToCompare: New(),
300 want: true,
301 },
302 }
303
304 for _, tt := range tests {
305 t.Run(tt.name, func(t *testing.T) {
306 t.Parallel()
307
308 got := tt.c.Equals(tt.colorToCompare)
309 assert.Equal(t, tt.want, got)
310 })
311 }
312 }
313
314 // TestColor_Fprint tests the Fprint method.
315 func TestColor_Fprint(t *testing.T) {
316 t.Parallel()
317
318 tests := []struct {
319 name string
320 attrs []Attribute
321 args []any
322 noColor bool
323 check func(*testing.T, string)
324 }{
325 {
326 name: "prints with color",
327 attrs: []Attribute{FgRed},
328 args: []any{"hello"},
329 noColor: false,
330 check: func(t *testing.T, output string) {
331 t.Helper()
332 assert.Contains(t, output, "hello", "should contain the text")
333 assert.Contains(t, output, "\x1b[", "should contain ANSI escape code")
334 },
335 },
336 {
337 name: "prints without color when NoColor set",
338 attrs: []Attribute{FgRed},
339 args: []any{"hello"},
340 noColor: true,
341 check: func(t *testing.T, output string) {
342 t.Helper()
343 assert.Equal(t, "hello", output, "should be plain text")
344 },
345 },
346 {
347 name: "prints multiple args",
348 attrs: []Attribute{FgGreen},
349 args: []any{"hello", "world"},
350 noColor: false,
351 check: func(t *testing.T, output string) {
352 t.Helper()
353 assert.Contains(t, output, "hello", "should contain first arg")
354 assert.Contains(t, output, "world", "should contain second arg")
355 },
356 },
357 {
358 name: "prints empty",
359 attrs: []Attribute{FgBlue},
360 args: []any{},
361 noColor: false,
362 check: func(t *testing.T, output string) {
363 t.Helper()
364 // When colors are enabled, even empty args produce the escape codes
365 assert.NotEmpty(t, output, "should have escape codes even for empty")
366 },
367 },
368 }
369
370 for _, tt := range tests {
371 t.Run(tt.name, func(t *testing.T) {
372 t.Parallel()
373
374 buf := &bytes.Buffer{}
375 c := newTestColor(buf, tt.noColor, tt.attrs...)
376 _, err := c.Fprint(buf, tt.args...)
377
378 require.NoError(t, err)
379 tt.check(t, buf.String())
380 })
381 }
382 }
383
384 // TestColor_FprintFunc tests the FprintFunc method.
385 func TestColor_FprintFunc(t *testing.T) {
386 t.Parallel()
387
388 tests := []struct {
389 name string
390 attrs []Attribute
391 noColor bool
392 }{
393 {
394 name: "returns function with color enabled",
395 attrs: []Attribute{FgRed},
396 noColor: false,
397 },
398 {
399 name: "returns function with NoColor set",
400 attrs: []Attribute{FgGreen},
401 noColor: true,
402 },
403 }
404
405 for _, tt := range tests {
406 t.Run(tt.name, func(t *testing.T) {
407 t.Parallel()
408
409 buf := &bytes.Buffer{}
410 c := newTestColor(buf, tt.noColor, tt.attrs...)
411
412 fn := c.FprintFunc()
413 require.NotNil(t, fn)
414
415 fn(buf, "test")
416
417 if tt.noColor {
418 assert.Equal(t, "test", buf.String())
419 } else {
420 assert.NotEmpty(t, buf.String())
421 }
422 })
423 }
424 }
425
426 // TestColor_Fprintf tests the Fprintf method.
427 func TestColor_Fprintf(t *testing.T) {
428 t.Parallel()
429
430 tests := []struct {
431 name string
432 attrs []Attribute
433 format string
434 args []any
435 noColor bool
436 check func(*testing.T, string)
437 }{
438 {
439 name: "prints formatted with color",
440 attrs: []Attribute{FgRed},
441 format: "value: %d",
442 args: []any{42},
443 noColor: false,
444 check: func(t *testing.T, output string) {
445 t.Helper()
446 assert.Contains(t, output, "value: 42", "should contain formatted text")
447 assert.Contains(t, output, "\x1b[", "should contain ANSI escape code")
448 },
449 },
450 {
451 name: "prints formatted without color",
452 attrs: []Attribute{FgRed},
453 format: "value: %d",
454 args: []any{42},
455 noColor: true,
456 check: func(t *testing.T, output string) {
457 t.Helper()
458 assert.Equal(t, "value: 42", output, "should be plain text")
459 },
460 },
461 }
462
463 for _, tt := range tests {
464 t.Run(tt.name, func(t *testing.T) {
465 t.Parallel()
466
467 buf := &bytes.Buffer{}
468 c := newTestColor(buf, tt.noColor, tt.attrs...)
469 _, err := c.Fprintf(buf, tt.format, tt.args...)
470
471 require.NoError(t, err)
472 tt.check(t, buf.String())
473 })
474 }
475 }
476
477 // TestColor_FprintfFunc tests the FprintfFunc method.
478 func TestColor_FprintfFunc(t *testing.T) {
479 t.Parallel()
480
481 tests := []struct {
482 name string
483 attrs []Attribute
484 noColor bool
485 }{
486 {
487 name: "returns function with color enabled",
488 attrs: []Attribute{FgRed},
489 noColor: false,
490 },
491 {
492 name: "returns function with NoColor set",
493 attrs: []Attribute{FgGreen},
494 noColor: true,
495 },
496 }
497
498 for _, tt := range tests {
499 t.Run(tt.name, func(t *testing.T) {
500 t.Parallel()
501
502 buf := &bytes.Buffer{}
503 c := newTestColor(buf, tt.noColor, tt.attrs...)
504
505 fn := c.FprintfFunc()
506 require.NotNil(t, fn)
507
508 fn(buf, "value: %d", 42)
509
510 if tt.noColor {
511 assert.Equal(t, "value: 42", buf.String())
512 } else {
513 assert.NotEmpty(t, buf.String())
514 }
515 })
516 }
517 }
518
519 // TestColor_Fprintln tests the Fprintln method.
520 func TestColor_Fprintln(t *testing.T) {
521 t.Parallel()
522
523 tests := []struct {
524 name string
525 attrs []Attribute
526 args []any
527 noColor bool
528 check func(*testing.T, string)
529 }{
530 {
531 name: "prints with newline with color",
532 attrs: []Attribute{FgRed},
533 args: []any{"hello"},
534 noColor: false,
535 check: func(t *testing.T, output string) {
536 t.Helper()
537 assert.Contains(t, output, "hello", "should contain the text")
538 assert.Contains(t, output, "\n", "should contain newline")
539 },
540 },
541 {
542 name: "prints with newline without color",
543 attrs: []Attribute{FgRed},
544 args: []any{"hello"},
545 noColor: true,
546 check: func(t *testing.T, output string) {
547 t.Helper()
548 assert.Equal(t, "hello\n", output, "should be plain text with newline")
549 },
550 },
551 {
552 name: "prints multiple args with newline",
553 attrs: []Attribute{FgGreen},
554 args: []any{"hello", "world"},
555 noColor: true,
556 check: func(t *testing.T, output string) {
557 t.Helper()
558 assert.Equal(t, "hello world\n", output, "should be plain text with newline")
559 },
560 },
561 }
562
563 for _, tt := range tests {
564 t.Run(tt.name, func(t *testing.T) {
565 t.Parallel()
566
567 buf := &bytes.Buffer{}
568 c := newTestColor(buf, tt.noColor, tt.attrs...)
569 _, err := c.Fprintln(buf, tt.args...)
570
571 require.NoError(t, err)
572 tt.check(t, buf.String())
573 })
574 }
575 }
576
577 // TestColor_FprintlnFunc tests the FprintlnFunc method.
578 func TestColor_FprintlnFunc(t *testing.T) {
579 t.Parallel()
580
581 tests := []struct {
582 name string
583 attrs []Attribute
584 noColor bool
585 }{
586 {
587 name: "returns function with color enabled",
588 attrs: []Attribute{FgRed},
589 noColor: false,
590 },
591 {
592 name: "returns function with NoColor set",
593 attrs: []Attribute{FgGreen},
594 noColor: true,
595 },
596 }
597
598 for _, tt := range tests {
599 t.Run(tt.name, func(t *testing.T) {
600 t.Parallel()
601
602 buf := &bytes.Buffer{}
603 c := newTestColor(buf, tt.noColor, tt.attrs...)
604
605 fn := c.FprintlnFunc()
606 require.NotNil(t, fn)
607
608 fn(buf, "test")
609
610 assert.NotEmpty(t, buf.String())
611 })
612 }
613 }
614
615 // TestColor_Print tests the Print method.
616 func TestColor_Print(t *testing.T) {
617 t.Parallel()
618
619 tests := []struct {
620 name string
621 attrs []Attribute
622 args []any
623 noColor bool
624 }{
625 {
626 name: "prints with color",
627 attrs: []Attribute{FgRed},
628 args: []any{"hello"},
629 noColor: false,
630 },
631 {
632 name: "prints without color when NoColor set",
633 attrs: []Attribute{FgRed},
634 args: []any{"hello"},
635 noColor: true,
636 },
637 }
638
639 for _, tt := range tests {
640 t.Run(tt.name, func(t *testing.T) {
641 t.Parallel()
642
643 buf := &bytes.Buffer{}
644 c := newTestColor(buf, tt.noColor, tt.attrs...)
645
646 got, err := c.Print(tt.args...)
647
648 require.NoError(t, err)
649 assert.Positive(t, got)
650 })
651 }
652 }
653
654 // TestColor_PrintFunc tests the PrintFunc method.
655 func TestColor_PrintFunc(t *testing.T) {
656 t.Parallel()
657
658 tests := []struct {
659 name string
660 attrs []Attribute
661 noColor bool
662 }{
663 {
664 name: "returns function with color enabled",
665 attrs: []Attribute{FgRed},
666 noColor: false,
667 },
668 {
669 name: "returns function with NoColor set",
670 attrs: []Attribute{FgGreen},
671 noColor: true,
672 },
673 }
674
675 for _, tt := range tests {
676 t.Run(tt.name, func(t *testing.T) {
677 t.Parallel()
678
679 buf := &bytes.Buffer{}
680 c := newTestColor(buf, tt.noColor, tt.attrs...)
681
682 fn := c.PrintFunc()
683 require.NotNil(t, fn)
684
685 fn("test")
686
687 assert.NotEmpty(t, buf.String())
688 })
689 }
690 }
691
692 // TestColor_Printf tests the Printf method.
693 func TestColor_Printf(t *testing.T) {
694 t.Parallel()
695
696 tests := []struct {
697 name string
698 attrs []Attribute
699 format string
700 args []any
701 noColor bool
702 }{
703 {
704 name: "prints formatted with color",
705 attrs: []Attribute{FgRed},
706 format: "value: %d",
707 args: []any{42},
708 noColor: false,
709 },
710 {
711 name: "prints formatted without color",
712 attrs: []Attribute{FgRed},
713 format: "value: %d",
714 args: []any{42},
715 noColor: true,
716 },
717 }
718
719 for _, tt := range tests {
720 t.Run(tt.name, func(t *testing.T) {
721 t.Parallel()
722
723 buf := &bytes.Buffer{}
724 c := newTestColor(buf, tt.noColor, tt.attrs...)
725
726 got, err := c.Printf(tt.format, tt.args...)
727
728 require.NoError(t, err)
729 assert.Positive(t, got)
730 })
731 }
732 }
733
734 // TestColor_PrintfFunc tests the PrintfFunc method.
735 func TestColor_PrintfFunc(t *testing.T) {
736 t.Parallel()
737
738 tests := []struct {
739 name string
740 attrs []Attribute
741 noColor bool
742 }{
743 {
744 name: "returns function with color enabled",
745 attrs: []Attribute{FgRed},
746 noColor: false,
747 },
748 {
749 name: "returns function with NoColor set",
750 attrs: []Attribute{FgGreen},
751 noColor: true,
752 },
753 }
754
755 for _, tt := range tests {
756 t.Run(tt.name, func(t *testing.T) {
757 t.Parallel()
758
759 buf := &bytes.Buffer{}
760 c := newTestColor(buf, tt.noColor, tt.attrs...)
761
762 fn := c.PrintfFunc()
763 require.NotNil(t, fn)
764
765 fn("value: %d", 42)
766
767 assert.NotEmpty(t, buf.String())
768 })
769 }
770 }
771
772 // TestColor_Println tests the Println method.
773 func TestColor_Println(t *testing.T) {
774 t.Parallel()
775
776 tests := []struct {
777 name string
778 attrs []Attribute
779 args []any
780 noColor bool
781 check func(*testing.T, string)
782 }{
783 {
784 name: "prints with newline with color",
785 attrs: []Attribute{FgRed},
786 args: []any{"hello"},
787 noColor: false,
788 check: func(t *testing.T, output string) {
789 t.Helper()
790 assert.Contains(t, output, "hello", "should contain the text")
791 },
792 },
793 {
794 name: "prints with newline without color",
795 attrs: []Attribute{FgRed},
796 args: []any{"hello"},
797 noColor: true,
798 check: func(t *testing.T, output string) {
799 t.Helper()
800 assert.Equal(t, "hello\n", output, "should be plain text with newline")
801 },
802 },
803 }
804
805 for _, tt := range tests {
806 t.Run(tt.name, func(t *testing.T) {
807 t.Parallel()
808
809 buf := &bytes.Buffer{}
810 c := newTestColor(buf, tt.noColor, tt.attrs...)
811
812 _, err := c.Println(tt.args...)
813
814 require.NoError(t, err)
815 tt.check(t, buf.String())
816 })
817 }
818 }
819
820 // TestColor_PrintlnFunc tests the PrintlnFunc method.
821 func TestColor_PrintlnFunc(t *testing.T) {
822 t.Parallel()
823
824 tests := []struct {
825 name string
826 attrs []Attribute
827 noColor bool
828 }{
829 {
830 name: "returns function with color enabled",
831 attrs: []Attribute{FgRed},
832 noColor: false,
833 },
834 {
835 name: "returns function with NoColor set",
836 attrs: []Attribute{FgGreen},
837 noColor: true,
838 },
839 }
840
841 for _, tt := range tests {
842 t.Run(tt.name, func(t *testing.T) {
843 t.Parallel()
844
845 buf := &bytes.Buffer{}
846 c := newTestColor(buf, tt.noColor, tt.attrs...)
847
848 fn := c.PrintlnFunc()
849 require.NotNil(t, fn)
850
851 fn("test")
852
853 assert.NotEmpty(t, buf.String())
854 })
855 }
856 }
857
858 // TestColor_Set tests the Set method.
859 func TestColor_Set(t *testing.T) {
860 t.Parallel()
861
862 tests := []struct {
863 name string
864 attrs []Attribute
865 noColor bool
866 }{
867 {
868 name: "sets color",
869 attrs: []Attribute{FgRed},
870 noColor: false,
871 },
872 {
873 name: "skips when NoColor set",
874 attrs: []Attribute{FgRed},
875 noColor: true,
876 },
877 {
878 name: "sets empty color",
879 attrs: []Attribute{},
880 noColor: false,
881 },
882 }
883
884 for _, tt := range tests {
885 t.Run(tt.name, func(t *testing.T) {
886 t.Parallel()
887
888 buf := &bytes.Buffer{}
889 c := newTestColor(buf, tt.noColor, tt.attrs...)
890
891 result := c.Set()
892
893 assert.Same(t, c, result)
894 })
895 }
896 }
897
898 // TestColor_SetWriter tests the SetWriter method.
899 func TestColor_SetWriter(t *testing.T) {
900 t.Parallel()
901
902 tests := []struct {
903 name string
904 attrs []Attribute
905 noColor bool
906 }{
907 {
908 name: "sets writer with color",
909 attrs: []Attribute{FgRed},
910 noColor: false,
911 },
912 {
913 name: "skips when NoColor set",
914 attrs: []Attribute{FgRed},
915 noColor: true,
916 },
917 }
918
919 for _, tt := range tests {
920 t.Run(tt.name, func(t *testing.T) {
921 t.Parallel()
922
923 buf := &bytes.Buffer{}
924 c := newTestColor(buf, tt.noColor, tt.attrs...)
925
926 result := c.SetWriter(buf)
927
928 assert.Same(t, c, result)
929 })
930 }
931 }
932
933 // TestColor_Sprint tests the Sprint method.
934 func TestColor_Sprint(t *testing.T) {
935 t.Parallel()
936
937 tests := []struct {
938 name string
939 attrs []Attribute
940 args []any
941 noColor bool
942 check func(*testing.T, string)
943 }{
944 {
945 name: "returns colored string",
946 attrs: []Attribute{FgRed},
947 args: []any{"hello"},
948 noColor: false,
949 check: func(t *testing.T, result string) {
950 t.Helper()
951 assert.Contains(t, result, "hello", "should contain the text")
952 assert.Contains(t, result, "\x1b[", "should contain ANSI escape code")
953 },
954 },
955 {
956 name: "returns plain string when NoColor set",
957 attrs: []Attribute{FgRed},
958 args: []any{"hello"},
959 noColor: true,
960 check: func(t *testing.T, result string) {
961 t.Helper()
962 assert.Equal(t, "hello", result)
963 },
964 },
965 {
966 name: "handles multiple args",
967 attrs: []Attribute{FgGreen},
968 args: []any{"hello", "world"},
969 noColor: true,
970 check: func(t *testing.T, result string) {
971 t.Helper()
972 assert.Equal(t, "helloworld", result)
973 },
974 },
975 {
976 name: "handles empty args",
977 attrs: []Attribute{FgBlue},
978 args: []any{},
979 noColor: true,
980 check: func(t *testing.T, result string) {
981 t.Helper()
982 assert.Empty(t, result)
983 },
984 },
985 }
986
987 for _, tt := range tests {
988 t.Run(tt.name, func(t *testing.T) {
989 t.Parallel()
990
991 buf := &bytes.Buffer{}
992 c := newTestColor(buf, tt.noColor, tt.attrs...)
993
994 got := c.Sprint(tt.args...)
995 tt.check(t, got)
996 })
997 }
998 }
999
1000 // TestColor_SprintFunc tests the SprintFunc method.
1001 func TestColor_SprintFunc(t *testing.T) {
1002 t.Parallel()
1003
1004 tests := []struct {
1005 name string
1006 attrs []Attribute
1007 noColor bool
1008 }{
1009 {
1010 name: "returns function with color enabled",
1011 attrs: []Attribute{FgRed},
1012 noColor: false,
1013 },
1014 {
1015 name: "returns function with NoColor set",
1016 attrs: []Attribute{FgGreen},
1017 noColor: true,
1018 },
1019 }
1020
1021 for _, tt := range tests {
1022 t.Run(tt.name, func(t *testing.T) {
1023 t.Parallel()
1024
1025 buf := &bytes.Buffer{}
1026 c := newTestColor(buf, tt.noColor, tt.attrs...)
1027
1028 fn := c.SprintFunc()
1029 require.NotNil(t, fn)
1030
1031 result := fn("test")
1032
1033 if tt.noColor {
1034 assert.Equal(t, "test", result)
1035 } else {
1036 assert.NotEmpty(t, result)
1037 }
1038 })
1039 }
1040 }
1041
1042 // TestColor_Sprintf tests the Sprintf method.
1043 func TestColor_Sprintf(t *testing.T) {
1044 t.Parallel()
1045
1046 tests := []struct {
1047 name string
1048 attrs []Attribute
1049 format string
1050 args []any
1051 noColor bool
1052 check func(*testing.T, string)
1053 }{
1054 {
1055 name: "returns formatted colored string",
1056 attrs: []Attribute{FgRed},
1057 format: "value: %d",
1058 args: []any{42},
1059 noColor: false,
1060 check: func(t *testing.T, result string) {
1061 t.Helper()
1062 assert.Contains(t, result, "value: 42", "should contain formatted text")
1063 assert.Contains(t, result, "\x1b[", "should contain ANSI escape code")
1064 },
1065 },
1066 {
1067 name: "returns formatted plain string when NoColor set",
1068 attrs: []Attribute{FgRed},
1069 format: "value: %d",
1070 args: []any{42},
1071 noColor: true,
1072 check: func(t *testing.T, result string) {
1073 t.Helper()
1074 assert.Equal(t, "value: 42", result)
1075 },
1076 },
1077 }
1078
1079 for _, tt := range tests {
1080 t.Run(tt.name, func(t *testing.T) {
1081 t.Parallel()
1082
1083 buf := &bytes.Buffer{}
1084 c := newTestColor(buf, tt.noColor, tt.attrs...)
1085
1086 got := c.Sprintf(tt.format, tt.args...)
1087 tt.check(t, got)
1088 })
1089 }
1090 }
1091
1092 // TestColor_SprintfFunc tests the SprintfFunc method.
1093 func TestColor_SprintfFunc(t *testing.T) {
1094 t.Parallel()
1095
1096 tests := []struct {
1097 name string
1098 attrs []Attribute
1099 noColor bool
1100 }{
1101 {
1102 name: "returns function with color enabled",
1103 attrs: []Attribute{FgRed},
1104 noColor: false,
1105 },
1106 {
1107 name: "returns function with NoColor set",
1108 attrs: []Attribute{FgGreen},
1109 noColor: true,
1110 },
1111 }
1112
1113 for _, tt := range tests {
1114 t.Run(tt.name, func(t *testing.T) {
1115 t.Parallel()
1116
1117 buf := &bytes.Buffer{}
1118 c := newTestColor(buf, tt.noColor, tt.attrs...)
1119
1120 fn := c.SprintfFunc()
1121 require.NotNil(t, fn)
1122
1123 result := fn("value: %d", 42)
1124
1125 if tt.noColor {
1126 assert.Equal(t, "value: 42", result)
1127 } else {
1128 assert.NotEmpty(t, result)
1129 }
1130 })
1131 }
1132 }
1133
1134 // TestColor_Sprintln tests the Sprintln method.
1135 func TestColor_Sprintln(t *testing.T) {
1136 t.Parallel()
1137
1138 tests := []struct {
1139 name string
1140 attrs []Attribute
1141 args []any
1142 noColor bool
1143 check func(*testing.T, string)
1144 }{
1145 {
1146 name: "returns colored string with newline",
1147 attrs: []Attribute{FgRed},
1148 args: []any{"hello"},
1149 noColor: false,
1150 check: func(t *testing.T, result string) {
1151 t.Helper()
1152 assert.Contains(t, result, "hello", "should contain the text")
1153 assert.Contains(t, result, "\n", "should contain newline")
1154 },
1155 },
1156 {
1157 name: "returns plain string with newline when NoColor set",
1158 attrs: []Attribute{FgRed},
1159 args: []any{"hello"},
1160 noColor: true,
1161 check: func(t *testing.T, result string) {
1162 t.Helper()
1163 assert.Equal(t, "hello\n", result)
1164 },
1165 },
1166 }
1167
1168 for _, tt := range tests {
1169 t.Run(tt.name, func(t *testing.T) {
1170 t.Parallel()
1171
1172 buf := &bytes.Buffer{}
1173 c := newTestColor(buf, tt.noColor, tt.attrs...)
1174
1175 got := c.Sprintln(tt.args...)
1176 tt.check(t, got)
1177 })
1178 }
1179 }
1180
1181 // TestColor_SprintlnFunc tests the SprintlnFunc method.
1182 func TestColor_SprintlnFunc(t *testing.T) {
1183 t.Parallel()
1184
1185 tests := []struct {
1186 name string
1187 attrs []Attribute
1188 noColor bool
1189 }{
1190 {
1191 name: "returns function with color enabled",
1192 attrs: []Attribute{FgRed},
1193 noColor: false,
1194 },
1195 {
1196 name: "returns function with NoColor set",
1197 attrs: []Attribute{FgGreen},
1198 noColor: true,
1199 },
1200 }
1201
1202 for _, tt := range tests {
1203 t.Run(tt.name, func(t *testing.T) {
1204 t.Parallel()
1205
1206 buf := &bytes.Buffer{}
1207 c := newTestColor(buf, tt.noColor, tt.attrs...)
1208
1209 fn := c.SprintlnFunc()
1210 require.NotNil(t, fn)
1211
1212 result := fn("test")
1213
1214 if tt.noColor {
1215 assert.Equal(t, "test\n", result)
1216 } else {
1217 assert.NotEmpty(t, result)
1218 }
1219 })
1220 }
1221 }
1222
1223 // TestColor_UnsetWriter tests the UnsetWriter method.
1224 func TestColor_UnsetWriter(t *testing.T) {
1225 t.Parallel()
1226
1227 tests := []struct {
1228 name string
1229 attrs []Attribute
1230 noColor bool
1231 }{
1232 {
1233 name: "unsets writer with color",
1234 attrs: []Attribute{FgRed},
1235 noColor: false,
1236 },
1237 {
1238 name: "skips when NoColor set",
1239 attrs: []Attribute{FgRed},
1240 noColor: true,
1241 },
1242 }
1243
1244 for _, tt := range tests {
1245 t.Run(tt.name, func(t *testing.T) {
1246 t.Parallel()
1247
1248 buf := &bytes.Buffer{}
1249 c := newTestColor(buf, tt.noColor, tt.attrs...)
1250
1251 c.UnsetWriter(buf)
1252
1253 // Just verify it doesn't panic
1254 })
1255 }
1256 }
1257
1258 // TestColor_attrExists tests the attrExists method.
1259 func TestColor_attrExists(t *testing.T) {
1260 t.Parallel()
1261
1262 tests := []struct {
1263 name string
1264 c *Color
1265 attr Attribute
1266 want bool
1267 }{
1268 {
1269 name: "attribute exists",
1270 c: New(FgRed, Bold),
1271 attr: FgRed,
1272 want: true,
1273 },
1274 {
1275 name: "attribute does not exist",
1276 c: New(FgRed),
1277 attr: FgGreen,
1278 want: false,
1279 },
1280 {
1281 name: "empty color has no attributes",
1282 c: New(),
1283 attr: FgRed,
1284 want: false,
1285 },
1286 {
1287 name: "multiple attributes check one",
1288 c: New(FgRed, FgGreen, FgBlue),
1289 attr: FgGreen,
1290 want: true,
1291 },
1292 {
1293 name: "check for style attribute",
1294 c: New(FgRed, Bold, Underline),
1295 attr: Bold,
1296 want: true,
1297 },
1298 }
1299
1300 for _, tt := range tests {
1301 t.Run(tt.name, func(t *testing.T) {
1302 t.Parallel()
1303
1304 got := tt.c.attrExists(tt.attr)
1305 assert.Equal(t, tt.want, got)
1306 })
1307 }
1308 }
1309
1310 // TestColor_format tests the format method.
1311 func TestColor_format(t *testing.T) {
1312 t.Parallel()
1313
1314 tests := []struct {
1315 name string
1316 c *Color
1317 noColor bool
1318 check func(*testing.T, string)
1319 }{
1320 {
1321 name: "formats with color",
1322 c: New(FgRed),
1323 noColor: false,
1324 check: func(t *testing.T, result string) {
1325 t.Helper()
1326 assert.NotEmpty(t, result)
1327 assert.Contains(t, result, "\x1b[")
1328 },
1329 },
1330 {
1331 name: "formats empty color",
1332 c: New(),
1333 noColor: false,
1334 check: func(t *testing.T, result string) {
1335 t.Helper()
1336 // Empty color with no params returns escape + [ + m (short reset form)
1337 assert.Equal(t, "\x1b[m", result)
1338 },
1339 },
1340 {
1341 name: "formats with NoColor set on color",
1342 c: func() *Color {
1343 c := New(FgRed)
1344 c.DisableColor()
1345
1346 return c
1347 }(),
1348 noColor: false,
1349 check: func(t *testing.T, result string) {
1350 t.Helper()
1351 // format() does not check NoColor - it always generates the ANSI sequence
1352 assert.Equal(t, "\x1b[31m", result)
1353 },
1354 },
1355 }
1356
1357 for _, tt := range tests {
1358 t.Run(tt.name, func(t *testing.T) {
1359 t.Parallel()
1360
1361 got := tt.c.format()
1362 tt.check(t, got)
1363 })
1364 }
1365 }
1366
1367 // TestColor_isNoColorSet tests the isNoColorSet method.
1368 func TestColor_isNoColorSet(t *testing.T) {
1369 t.Parallel()
1370
1371 tests := []struct {
1372 name string
1373 c *Color
1374 noColor bool
1375 want bool
1376 }{
1377 {
1378 name: "returns true when noColor is explicitly set",
1379 c: func() *Color {
1380 c := New(FgRed)
1381 c.DisableColor()
1382
1383 return c
1384 }(),
1385 noColor: false,
1386 want: true,
1387 },
1388 {
1389 name: "returns false when noColor is explicitly enabled",
1390 c: func() *Color {
1391 c := New(FgRed)
1392 c.EnableColor()
1393
1394 return c
1395 }(),
1396 noColor: false,
1397 want: false,
1398 },
1399 {
1400 name: "returns config NoColor when noColor is nil (color enabled)",
1401 c: func() *Color {
1402 buf := &bytes.Buffer{}
1403 cfg := &Config{NoColor: false, Output: buf, Error: buf}
1404
1405 return NewWithConfig(cfg)
1406 }(),
1407 noColor: false,
1408 want: false,
1409 },
1410 {
1411 name: "returns config NoColor when noColor is nil (color disabled)",
1412 c: func() *Color {
1413 buf := &bytes.Buffer{}
1414 cfg := &Config{NoColor: true, Output: buf, Error: buf}
1415
1416 return NewWithConfig(cfg)
1417 }(),
1418 noColor: true,
1419 want: true,
1420 },
1421 }
1422
1423 for _, tt := range tests {
1424 t.Run(tt.name, func(t *testing.T) {
1425 t.Parallel()
1426
1427 got := tt.c.isNoColorSet()
1428 assert.Equal(t, tt.want, got)
1429 })
1430 }
1431 }
1432
1433 // TestColor_sequence tests the sequence method.
1434 func TestColor_sequence(t *testing.T) {
1435 t.Parallel()
1436
1437 tests := []struct {
1438 name string
1439 c *Color
1440 want string
1441 }{
1442 {
1443 name: "single attribute",
1444 c: New(FgRed),
1445 want: "31",
1446 },
1447 {
1448 name: "multiple attributes",
1449 c: New(FgRed, Bold),
1450 want: "31;1",
1451 },
1452 {
1453 name: "empty color",
1454 c: New(),
1455 want: "",
1456 },
1457 {
1458 name: "foreground colors",
1459 c: New(FgBlack, FgRed, FgGreen),
1460 want: "30;31;32",
1461 },
1462 {
1463 name: "background colors",
1464 c: New(BgBlack, BgRed),
1465 want: "40;41",
1466 },
1467 {
1468 name: "style attributes",
1469 c: New(Bold, Underline, Italic),
1470 want: "1;4;3",
1471 },
1472 }
1473
1474 for _, tt := range tests {
1475 t.Run(tt.name, func(t *testing.T) {
1476 t.Parallel()
1477
1478 got := tt.c.sequence()
1479 assert.Equal(t, tt.want, got)
1480 })
1481 }
1482 }
1483
1484 // TestColor_unformat tests the unformat method.
1485 func TestColor_unformat(t *testing.T) {
1486 t.Parallel()
1487
1488 tests := []struct {
1489 name string
1490 c *Color
1491 want string
1492 }{
1493 {
1494 name: "single attribute reset",
1495 c: New(FgRed),
1496 want: "\x1b[0m",
1497 },
1498 {
1499 name: "multiple attributes reset",
1500 c: New(FgRed, Bold),
1501 want: "\x1b[0;22m",
1502 },
1503 {
1504 name: "empty color",
1505 c: New(),
1506 want: "",
1507 },
1508 {
1509 name: "bold reset",
1510 c: New(Bold),
1511 want: "\x1b[22m",
1512 },
1513 {
1514 name: "underline reset",
1515 c: New(Underline),
1516 want: "\x1b[24m",
1517 },
1518 {
1519 name: "italic reset",
1520 c: New(Italic),
1521 want: "\x1b[23m",
1522 },
1523 }
1524
1525 for _, tt := range tests {
1526 t.Run(tt.name, func(t *testing.T) {
1527 t.Parallel()
1528
1529 got := tt.c.unformat()
1530 assert.Equal(t, tt.want, got)
1531 })
1532 }
1533 }
1534
1535 // TestColor_unset tests the unset method.
1536 func TestColor_unset(t *testing.T) {
1537 t.Parallel()
1538
1539 tests := []struct {
1540 name string
1541 attrs []Attribute
1542 noColor bool
1543 }{
1544 {
1545 name: "unsets with color",
1546 attrs: []Attribute{FgRed},
1547 noColor: false,
1548 },
1549 {
1550 name: "skips when NoColor set",
1551 attrs: []Attribute{FgRed},
1552 noColor: true,
1553 },
1554 }
1555
1556 for _, tt := range tests {
1557 t.Run(tt.name, func(t *testing.T) {
1558 t.Parallel()
1559
1560 buf := &bytes.Buffer{}
1561 c := newTestColor(buf, tt.noColor, tt.attrs...)
1562
1563 c.Unset()
1564
1565 // Just verify it doesn't panic
1566 })
1567 }
1568 }
1569
1570 // TestColor_wrap tests the wrap method.
1571 func TestColor_wrap(t *testing.T) {
1572 t.Parallel()
1573
1574 tests := []struct {
1575 name string
1576 attrs []Attribute
1577 s string
1578 noColor bool
1579 check func(*testing.T, string)
1580 }{
1581 {
1582 name: "wraps string with color",
1583 attrs: []Attribute{FgRed},
1584 s: "hello",
1585 noColor: false,
1586 check: func(t *testing.T, result string) {
1587 t.Helper()
1588 assert.Contains(t, result, "hello", "should contain the text")
1589 assert.Contains(t, result, "\x1b[", "should contain ANSI escape code")
1590 },
1591 },
1592 {
1593 name: "returns plain string when NoColor set",
1594 attrs: []Attribute{FgRed},
1595 s: "hello",
1596 noColor: true,
1597 check: func(t *testing.T, result string) {
1598 t.Helper()
1599 assert.Equal(t, "hello", result)
1600 },
1601 },
1602 {
1603 name: "wraps empty string",
1604 attrs: []Attribute{FgRed},
1605 s: "",
1606 noColor: false,
1607 check: func(t *testing.T, result string) {
1608 t.Helper()
1609 assert.NotEmpty(t, result, "should have escape codes even for empty string")
1610 },
1611 },
1612 }
1613
1614 for _, tt := range tests {
1615 t.Run(tt.name, func(t *testing.T) {
1616 t.Parallel()
1617
1618 buf := &bytes.Buffer{}
1619 c := newTestColor(buf, tt.noColor, tt.attrs...)
1620
1621 got := c.wrap(tt.s)
1622 tt.check(t, got)
1623 })
1624 }
1625 }
1626
1627 // TestNew tests the New function.
1628 func TestNew(t *testing.T) {
1629 t.Parallel()
1630
1631 tests := []struct {
1632 name string
1633 value []Attribute
1634 wantParams int
1635 }{
1636 {
1637 name: "creates color with single attribute",
1638 value: []Attribute{FgRed},
1639 wantParams: 1,
1640 },
1641 {
1642 name: "creates color with multiple attributes",
1643 value: []Attribute{FgRed, Bold, Underline},
1644 wantParams: 3,
1645 },
1646 {
1647 name: "creates empty color",
1648 value: []Attribute{},
1649 wantParams: 0,
1650 },
1651 {
1652 name: "creates color with no arguments",
1653 value: nil,
1654 wantParams: 0,
1655 },
1656 {
1657 name: "creates color with background",
1658 value: []Attribute{BgRed},
1659 wantParams: 1,
1660 },
1661 }
1662
1663 for _, tt := range tests {
1664 t.Run(tt.name, func(t *testing.T) {
1665 t.Parallel()
1666
1667 got := New(tt.value...)
1668
1669 require.NotNil(t, got)
1670 assert.Len(t, got.params, tt.wantParams)
1671 })
1672 }
1673 }
1674
1675 // TestNewWithConfig tests the NewWithConfig function.
1676 func TestNewWithConfig(t *testing.T) {
1677 t.Parallel()
1678
1679 tests := []struct {
1680 name string
1681 noColor bool
1682 attrs []Attribute
1683 wantNoColor bool
1684 wantNoColorNil bool
1685 }{
1686 {
1687 name: "creates color with NoColor=true",
1688 noColor: true,
1689 attrs: []Attribute{FgRed},
1690 wantNoColor: true,
1691 wantNoColorNil: false,
1692 },
1693 {
1694 name: "creates color with NoColor=false",
1695 noColor: false,
1696 attrs: []Attribute{FgRed},
1697 wantNoColor: false,
1698 wantNoColorNil: true,
1699 },
1700 {
1701 name: "creates empty color with config",
1702 noColor: false,
1703 attrs: []Attribute{},
1704 wantNoColor: false,
1705 wantNoColorNil: true,
1706 },
1707 }
1708
1709 for _, tt := range tests {
1710 t.Run(tt.name, func(t *testing.T) {
1711 t.Parallel()
1712
1713 buf := &bytes.Buffer{}
1714 cfg := &Config{
1715 NoColor: tt.noColor,
1716 Output: buf,
1717 Error: buf,
1718 }
1719
1720 got := NewWithConfig(cfg, tt.attrs...)
1721
1722 require.NotNil(t, got)
1723 assert.Equal(t, cfg, got.config)
1724
1725 if tt.wantNoColorNil {
1726 assert.Nil(t, got.noColor)
1727 } else {
1728 require.NotNil(t, got.noColor)
1729 assert.Equal(t, tt.wantNoColor, *got.noColor)
1730 }
1731 })
1732 }
1733 }
1734
1735 // TestDefaultConfig tests the DefaultConfig function.
1736 func TestDefaultConfig(t *testing.T) {
1737 t.Parallel()
1738
1739 cfg := DefaultConfig()
1740
1741 require.NotNil(t, cfg)
1742 assert.NotNil(t, cfg.Output)
1743 assert.NotNil(t, cfg.Error)
1744 // NoColor depends on environment, so we just verify it's set
1745 }
1746
1747 // TestBgRGB tests the BgRGB function.
1748 func TestBgRGB(t *testing.T) {
1749 t.Parallel()
1750
1751 tests := []struct {
1752 name string
1753 r int
1754 g int
1755 b int
1756 wantParams int
1757 }{
1758 {
1759 name: "creates background RGB color",
1760 r: 255,
1761 g: 128,
1762 b: 0,
1763 wantParams: 5,
1764 },
1765 {
1766 name: "creates black background",
1767 r: 0,
1768 g: 0,
1769 b: 0,
1770 wantParams: 5,
1771 },
1772 {
1773 name: "creates white background",
1774 r: 255,
1775 g: 255,
1776 b: 255,
1777 wantParams: 5,
1778 },
1779 }
1780
1781 for _, tt := range tests {
1782 t.Run(tt.name, func(t *testing.T) {
1783 t.Parallel()
1784
1785 got := BgRGB(tt.r, tt.g, tt.b)
1786
1787 require.NotNil(t, got)
1788 assert.Len(t, got.params, tt.wantParams)
1789 })
1790 }
1791 }
1792
1793 // TestRGB tests the RGB function.
1794 func TestRGB(t *testing.T) {
1795 t.Parallel()
1796
1797 tests := []struct {
1798 name string
1799 r int
1800 g int
1801 b int
1802 wantParams int
1803 }{
1804 {
1805 name: "creates foreground RGB color",
1806 r: 255,
1807 g: 128,
1808 b: 0,
1809 wantParams: 5,
1810 },
1811 {
1812 name: "creates black foreground",
1813 r: 0,
1814 g: 0,
1815 b: 0,
1816 wantParams: 5,
1817 },
1818 {
1819 name: "creates white foreground",
1820 r: 255,
1821 g: 255,
1822 b: 255,
1823 wantParams: 5,
1824 },
1825 }
1826
1827 for _, tt := range tests {
1828 t.Run(tt.name, func(t *testing.T) {
1829 t.Parallel()
1830
1831 got := RGB(tt.r, tt.g, tt.b)
1832
1833 require.NotNil(t, got)
1834 assert.Len(t, got.params, tt.wantParams)
1835 })
1836 }
1837 }