diff --git a/pkg/cmd/gist/list/list_test.go b/pkg/cmd/gist/list/list_test.go index 870f58147..4f6c8a9f7 100644 --- a/pkg/cmd/gist/list/list_test.go +++ b/pkg/cmd/gist/list/list_test.go @@ -486,7 +486,7 @@ func Test_listRun(t *testing.T) { ) }, wantOut: heredoc.Docf(` - %[1]s[0;2;4;37mID %[1]s[0m %[1]s[0;2;4;37mDESCRIPTION %[1]s[0m %[1]s[0;2;4;37mFILES %[1]s[0m %[1]s[0;2;4;37mVISIBILITY%[1]s[0m %[1]s[0;2;4;37mUPDATED %[1]s[0m + %[1]s[0;4;39mID %[1]s[0m %[1]s[0;4;39mDESCRIPTION %[1]s[0m %[1]s[0;4;39mFILES %[1]s[0m %[1]s[0;4;39mVISIBILITY%[1]s[0m %[1]s[0;4;39mUPDATED %[1]s[0m 1234 %[1]s[0;30;43mocto%[1]s[0m%[1]s[0;1;39m match in the description%[1]s[0m 1 file %[1]s[0;32mpublic %[1]s[0m %[1]s[38;5;242mabout 6 hours ago%[1]s[m 2345 %[1]s[0;1;39mmatch in the file name %[1]s[0m %[1]s[0;30;43m2 files%[1]s[0m %[1]s[0;31msecret %[1]s[0m %[1]s[38;5;242mabout 6 hours ago%[1]s[m `, "\x1b"), diff --git a/pkg/iostreams/color_test.go b/pkg/iostreams/color_test.go index 9c84f72e1..b35c2eb73 100644 --- a/pkg/iostreams/color_test.go +++ b/pkg/iostreams/color_test.go @@ -1,6 +1,7 @@ package iostreams import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -93,3 +94,84 @@ func TestHexToRGB(t *testing.T) { assert.Equal(t, tt.wants, output) } } + +func TestTableHeader(t *testing.T) { + reset := "\x1b[0m" + defaultUnderline := "\x1b[0;4;39m" + brightBlackUnderline := "\x1b[0;4;90m" + dimBlackUnderline := "\x1b[0;2;4;37m" + + tests := []struct { + name string + cs *ColorScheme + input string + expected string + }{ + { + name: "when color is disabled, text is not stylized", + cs: NewColorScheme(false, false, false, NoTheme), + input: "this should not be stylized", + expected: "this should not be stylized", + }, + { + name: "when 4-bit color is enabled but no theme, 4-bit default color and underline are used", + cs: NewColorScheme(true, false, false, NoTheme), + input: "this should have no explicit color but underlined", + expected: fmt.Sprintf("%sthis should have no explicit color but underlined%s", defaultUnderline, reset), + }, + { + name: "when 4-bit color is enabled and theme is light, 4-bit dark color and underline are used", + cs: NewColorScheme(true, false, false, LightTheme), + input: "this should have dark foreground color and underlined", + expected: fmt.Sprintf("%sthis should have dark foreground color and underlined%s", brightBlackUnderline, reset), + }, + { + name: "when 4-bit color is enabled and theme is dark, 4-bit light color and underline are used", + cs: NewColorScheme(true, false, false, DarkTheme), + input: "this should have light foreground color and underlined", + expected: fmt.Sprintf("%sthis should have light foreground color and underlined%s", dimBlackUnderline, reset), + }, + { + name: "when 8-bit color is enabled but no theme, 4-bit default color and underline are used", + cs: NewColorScheme(true, true, false, NoTheme), + input: "this should have no explicit color but underlined", + expected: fmt.Sprintf("%sthis should have no explicit color but underlined%s", defaultUnderline, reset), + }, + { + name: "when 8-bit color is enabled and theme is light, 4-bit dark color and underline are used", + cs: NewColorScheme(true, true, false, LightTheme), + input: "this should have dark foreground color and underlined", + expected: fmt.Sprintf("%sthis should have dark foreground color and underlined%s", brightBlackUnderline, reset), + }, + { + name: "when 8-bit color is true and theme is dark, 4-bit light color and underline are used", + cs: NewColorScheme(true, true, false, DarkTheme), + input: "this should have light foreground color and underlined", + expected: fmt.Sprintf("%sthis should have light foreground color and underlined%s", dimBlackUnderline, reset), + }, + { + name: "when 24-bit color is enabled but no theme, 4-bit default color and underline are used", + cs: NewColorScheme(true, true, true, NoTheme), + input: "this should have no explicit color but underlined", + expected: fmt.Sprintf("%sthis should have no explicit color but underlined%s", defaultUnderline, reset), + }, + { + name: "when 24-bit color is enabled and theme is light, 4-bit dark color and underline are used", + cs: NewColorScheme(true, true, true, LightTheme), + input: "this should have dark foreground color and underlined", + expected: fmt.Sprintf("%sthis should have dark foreground color and underlined%s", brightBlackUnderline, reset), + }, + { + name: "when 24-bit color is true and theme is dark, 4-bit light color and underline are used", + cs: NewColorScheme(true, true, true, DarkTheme), + input: "this should have light foreground color and underlined", + expected: fmt.Sprintf("%sthis should have light foreground color and underlined%s", dimBlackUnderline, reset), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.expected, tt.cs.TableHeader(tt.input)) + }) + } +}