src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/ui/text_segment_test.go (about) 1 package ui 2 3 import ( 4 "testing" 5 6 "src.elv.sh/pkg/eval/vals" 7 ) 8 9 func TestTextSegmentAsElvishValue(t *testing.T) { 10 vals.TestValue(t, &Segment{Style{}, "foo"}). 11 Kind("ui:text-segment"). 12 Repr("foo"). 13 AllKeys("text", "fg-color", "bg-color", 14 "bold", "dim", "italic", "underlined", "blink", "inverse"). 15 Index("text", "foo"). 16 Index("fg-color", "default"). 17 Index("bg-color", "default"). 18 Index("bold", false). 19 Index("dim", false). 20 Index("italic", false). 21 Index("underlined", false). 22 Index("blink", false). 23 Index("inverse", false) 24 25 vals.TestValue(t, &Segment{Style{Fg: Red, Bg: Blue}, "foo"}). 26 Repr("(styled-segment foo &fg-color=red &bg-color=blue)"). 27 Index("fg-color", "red"). 28 Index("bg-color", "blue") 29 } 30 31 var textSegmentVTStringTests = []struct { 32 name string 33 seg *Segment 34 wantVTString string 35 }{ 36 { 37 name: "seg with no style", 38 seg: &Segment{Text: "foo"}, 39 wantVTString: "\033[mfoo", 40 }, 41 { 42 name: "seg with style", 43 seg: &Segment{Style: Style{Bold: true}, Text: "foo"}, 44 wantVTString: "\033[;1mfoo\033[m", 45 }, 46 } 47 48 func TestTextSegmentVTString(t *testing.T) { 49 for _, tc := range textSegmentVTStringTests { 50 t.Run(tc.name, func(t *testing.T) { 51 if got := tc.seg.VTString(); got != tc.wantVTString { 52 t.Errorf("VTString of %#v is %q, want %q", tc.seg, got, tc.wantVTString) 53 } 54 if got := tc.seg.String(); got != tc.wantVTString { 55 t.Errorf("String of %#v is %q, want %q", tc.seg, got, tc.wantVTString) 56 } 57 }) 58 } 59 }