|
| 1 | +// Copyright 2026 tsuru authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package tablecli |
| 6 | + |
| 7 | +import ( |
| 8 | + "image/color" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestParseHexColor(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + input string |
| 18 | + want color.RGBA |
| 19 | + wantErr string |
| 20 | + }{ |
| 21 | + {"full hex", "#FF8800", color.RGBA{R: 0xFF, G: 0x88, B: 0x00, A: 0xFF}, ""}, |
| 22 | + {"short hex", "#F80", color.RGBA{R: 0xFF, G: 0x88, B: 0x00, A: 0xFF}, ""}, |
| 23 | + {"black", "#000000", color.RGBA{R: 0, G: 0, B: 0, A: 0xFF}, ""}, |
| 24 | + {"white", "#FFFFFF", color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}, ""}, |
| 25 | + {"short white", "#FFF", color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}, ""}, |
| 26 | + {"short black", "#000", color.RGBA{R: 0, G: 0, B: 0, A: 0xFF}, ""}, |
| 27 | + {"lowercase", "#ff8800", color.RGBA{R: 0xFF, G: 0x88, B: 0x00, A: 0xFF}, ""}, |
| 28 | + {"mixed case", "#Ff8800", color.RGBA{R: 0xFF, G: 0x88, B: 0x00, A: 0xFF}, ""}, |
| 29 | + {"invalid length", "#FF88", color.RGBA{}, "invalid length, must be 7 or 4"}, |
| 30 | + {"too long", "#FF880000", color.RGBA{}, "invalid length, must be 7 or 4"}, |
| 31 | + {"too short", "#FF", color.RGBA{}, "invalid length, must be 7 or 4"}, |
| 32 | + {"empty", "", color.RGBA{}, "invalid length"}, |
| 33 | + {"no hash", "FF8800", color.RGBA{}, "invalid length"}, |
| 34 | + {"invalid chars", "#ZZZZZZ", color.RGBA{}, "expected integer"}, |
| 35 | + } |
| 36 | + for _, tt := range tests { |
| 37 | + t.Run(tt.name, func(t *testing.T) { |
| 38 | + got, err := parseHexColor(tt.input) |
| 39 | + if tt.wantErr != "" { |
| 40 | + assert.Error(t, err) |
| 41 | + assert.Contains(t, err.Error(), tt.wantErr) |
| 42 | + } else { |
| 43 | + assert.NoError(t, err) |
| 44 | + assert.Equal(t, tt.want, got) |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestSetBorderColorByString(t *testing.T) { |
| 51 | + t.Run("named colors", func(t *testing.T) { |
| 52 | + namedColors := []string{ |
| 53 | + "black", "red", "green", "yellow", |
| 54 | + "blue", "magenta", "cyan", "white", |
| 55 | + "hi-black", "hi-red", "hi-green", "hi-yellow", |
| 56 | + "hi-blue", "hi-magenta", "hi-cyan", "hi-white", |
| 57 | + } |
| 58 | + for _, name := range namedColors { |
| 59 | + t.Run(name, func(t *testing.T) { |
| 60 | + TableConfig.BorderColorFunc = nil |
| 61 | + SetBorderColorByString(name) |
| 62 | + assert.NotNil(t, TableConfig.BorderColorFunc, "BorderColorFunc should be set for color %q", name) |
| 63 | + }) |
| 64 | + } |
| 65 | + TableConfig.BorderColorFunc = nil |
| 66 | + }) |
| 67 | + |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + input string |
| 71 | + wantNil bool |
| 72 | + }{ |
| 73 | + {"hex color", "#FF0000", false}, |
| 74 | + {"short hex", "#F00", false}, |
| 75 | + {"invalid name", "invalid-color", true}, |
| 76 | + {"empty string", "", true}, |
| 77 | + {"invalid hex", "#ZZZZZZ", true}, |
| 78 | + {"bad hex length", "#FF", true}, |
| 79 | + } |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + TableConfig.BorderColorFunc = nil |
| 83 | + SetBorderColorByString(tt.input) |
| 84 | + if tt.wantNil { |
| 85 | + assert.Nil(t, TableConfig.BorderColorFunc) |
| 86 | + return |
| 87 | + } else { |
| 88 | + assert.NotNil(t, TableConfig.BorderColorFunc) |
| 89 | + } |
| 90 | + |
| 91 | + result := TableConfig.BorderColorFunc("hello") |
| 92 | + assert.NotEmpty(t, result) |
| 93 | + assert.Contains(t, result, "hello") |
| 94 | + TableConfig.BorderColorFunc = nil |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
0 commit comments