src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/ui/styling_test.go (about) 1 package ui 2 3 import ( 4 "reflect" 5 "testing" 6 7 "src.elv.sh/pkg/tt" 8 ) 9 10 func TestStyleText(t *testing.T) { 11 tt.Test(t, StyleText, 12 // Foreground color 13 Args(T("foo"), FgRed). 14 Rets(Text{&Segment{Style{Fg: Red}, "foo"}}), 15 // Override existing foreground 16 Args(Text{&Segment{Style{Fg: Green}, "foo"}}, FgRed). 17 Rets(Text{&Segment{Style{Fg: Red}, "foo"}}), 18 // Multiple segments 19 Args(Text{ 20 &Segment{Style{}, "foo"}, 21 &Segment{Style{Fg: Green}, "bar"}}, FgRed). 22 Rets(Text{ 23 &Segment{Style{Fg: Red}, "foo"}, 24 &Segment{Style{Fg: Red}, "bar"}, 25 }), 26 // Background color 27 Args(T("foo"), BgRed). 28 Rets(Text{&Segment{Style{Bg: Red}, "foo"}}), 29 // Bold, false -> true 30 Args(T("foo"), Bold). 31 Rets(Text{&Segment{Style{Bold: true}, "foo"}}), 32 // Bold, true -> true 33 Args(Text{&Segment{Style{Bold: true}, "foo"}}, Bold). 34 Rets(Text{&Segment{Style{Bold: true}, "foo"}}), 35 // No Bold, true -> false 36 Args(Text{&Segment{Style{Bold: true}, "foo"}}, NoBold). 37 Rets(Text{&Segment{Style{}, "foo"}}), 38 // No Bold, false -> false 39 Args(T("foo"), NoBold).Rets(T("foo")), 40 // Toggle Bold, true -> false 41 Args(Text{&Segment{Style{Bold: true}, "foo"}}, ToggleBold). 42 Rets(Text{&Segment{Style{}, "foo"}}), 43 // Toggle Bold, false -> true 44 Args(T("foo"), ToggleBold). 45 Rets(Text{&Segment{Style{Bold: true}, "foo"}}), 46 // For the remaining bool transformers, we only check one case; the rest 47 // should be similar to "bold". 48 // Dim. 49 Args(T("foo"), Dim). 50 Rets(Text{&Segment{Style{Dim: true}, "foo"}}), 51 // Italic. 52 Args(T("foo"), Italic). 53 Rets(Text{&Segment{Style{Italic: true}, "foo"}}), 54 // Underlined. 55 Args(T("foo"), Underlined). 56 Rets(Text{&Segment{Style{Underlined: true}, "foo"}}), 57 // Blink. 58 Args(T("foo"), Blink). 59 Rets(Text{&Segment{Style{Blink: true}, "foo"}}), 60 // Inverse. 61 Args(T("foo"), Inverse). 62 Rets(Text{&Segment{Style{Inverse: true}, "foo"}}), 63 // TODO: Test nil styling. 64 ) 65 } 66 67 var parseStylingTests = []struct { 68 s string 69 wantStyling Styling 70 }{ 71 {"default", FgDefault}, 72 {"red", FgRed}, 73 {"fg-default", FgDefault}, 74 {"fg-red", FgRed}, 75 76 {"bg-default", BgDefault}, 77 {"bg-red", BgRed}, 78 79 {"bold", Bold}, 80 {"no-bold", NoBold}, 81 {"toggle-bold", ToggleBold}, 82 83 {"red bold", Stylings(FgRed, Bold)}, 84 } 85 86 func TestParseStyling(t *testing.T) { 87 for _, test := range parseStylingTests { 88 styling := ParseStyling(test.s) 89 if !reflect.DeepEqual(styling, test.wantStyling) { 90 t.Errorf("ParseStyling(%q) -> %v, want %v", 91 test.s, styling, test.wantStyling) 92 } 93 } 94 }