|
| 1 | +package stern |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/fatih/color" |
| 7 | +) |
| 8 | + |
| 9 | +func TestParseColors(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + desc string |
| 12 | + podColors []string |
| 13 | + containerColors []string |
| 14 | + want [][2]*color.Color |
| 15 | + wantError bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + desc: "both pod and container colors are specified", |
| 19 | + podColors: []string{"91", "92", "93"}, |
| 20 | + containerColors: []string{"31", "32", "33"}, |
| 21 | + want: [][2]*color.Color{ |
| 22 | + {color.New(color.FgHiRed), color.New(color.FgRed)}, |
| 23 | + {color.New(color.FgHiGreen), color.New(color.FgGreen)}, |
| 24 | + {color.New(color.FgHiYellow), color.New(color.FgYellow)}, |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + desc: "only pod colors are specified", |
| 29 | + podColors: []string{"91", "92", "93"}, |
| 30 | + containerColors: []string{}, |
| 31 | + want: [][2]*color.Color{ |
| 32 | + {color.New(color.FgHiRed), color.New(color.FgHiRed)}, |
| 33 | + {color.New(color.FgHiGreen), color.New(color.FgHiGreen)}, |
| 34 | + {color.New(color.FgHiYellow), color.New(color.FgHiYellow)}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + desc: "multiple attributes", |
| 39 | + podColors: []string{"4;91"}, |
| 40 | + containerColors: []string{"38;2;255;97;136"}, |
| 41 | + want: [][2]*color.Color{ |
| 42 | + { |
| 43 | + color.New(color.Underline, color.FgHiRed), |
| 44 | + color.New(38, 2, 255, 97, 136), // 24-bit color |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + desc: "spaces are ignored", |
| 50 | + podColors: []string{" 91 ", "\t92\t"}, |
| 51 | + containerColors: []string{}, |
| 52 | + want: [][2]*color.Color{ |
| 53 | + {color.New(color.FgHiRed), color.New(color.FgHiRed)}, |
| 54 | + {color.New(color.FgHiGreen), color.New(color.FgHiGreen)}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + // error patterns |
| 58 | + { |
| 59 | + desc: "only container colors are specified", |
| 60 | + podColors: []string{}, |
| 61 | + containerColors: []string{"31", "32", "33"}, |
| 62 | + wantError: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + desc: "both pod and container colors are empty", |
| 66 | + podColors: []string{}, |
| 67 | + containerColors: []string{}, |
| 68 | + wantError: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + desc: "invalid color", |
| 72 | + podColors: []string{"a"}, |
| 73 | + containerColors: []string{""}, |
| 74 | + wantError: true, |
| 75 | + }, |
| 76 | + } |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.desc, func(t *testing.T) { |
| 79 | + colorList, err := parseColors(tt.podColors, tt.containerColors) |
| 80 | + |
| 81 | + if tt.wantError { |
| 82 | + if err == nil { |
| 83 | + t.Error("expected err, but got nil") |
| 84 | + } |
| 85 | + return |
| 86 | + } |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("unexpected err: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + if len(tt.want) != len(colorList) { |
| 92 | + t.Fatalf("expected colorList of size %d, but got %d", len(tt.want), len(colorList)) |
| 93 | + } |
| 94 | + |
| 95 | + for i, wantPair := range tt.want { |
| 96 | + gotPair := colorList[i] |
| 97 | + if !wantPair[0].Equals(gotPair[0]) { |
| 98 | + t.Errorf("colorList[%d][0]: expected %v, but got %v", i, wantPair[0], gotPair[0]) |
| 99 | + } |
| 100 | + if !wantPair[1].Equals(gotPair[1]) { |
| 101 | + t.Errorf("colorList[%d][1]: expected %v, but got %v", i, wantPair[1], gotPair[1]) |
| 102 | + } |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
0 commit comments