github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/ui/style_regions_test.go (about) 1 package ui_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/markusbkk/elvish/pkg/diag" 8 . "github.com/markusbkk/elvish/pkg/ui" 9 ) 10 11 var styleRegionsTests = []struct { 12 Name string 13 String string 14 Regions []StylingRegion 15 WantText Text 16 }{ 17 { 18 Name: "empty string and regions", 19 String: "", Regions: nil, WantText: nil, 20 }, 21 { 22 Name: "a single region", 23 String: "foobar", 24 Regions: []StylingRegion{ 25 {r(1, 3), FgRed, 0}, 26 }, 27 WantText: Concat(T("f"), T("oo", FgRed), T("bar")), 28 }, 29 30 { 31 Name: "multiple continuous regions", 32 String: "foobar", 33 Regions: []StylingRegion{ 34 {r(1, 3), FgRed, 0}, 35 {r(3, 4), FgGreen, 0}, 36 }, 37 WantText: Concat(T("f"), T("oo", FgRed), T("b", FgGreen), T("ar")), 38 }, 39 40 { 41 Name: "multiple discontinuous regions in wrong order", 42 String: "foobar", 43 Regions: []StylingRegion{ 44 {r(4, 5), FgGreen, 0}, 45 {r(1, 3), FgRed, 0}, 46 }, 47 WantText: Concat(T("f"), T("oo", FgRed), T("b"), T("a", FgGreen), T("r")), 48 }, 49 { 50 Name: "regions with the same starting position but differeng priorities", 51 String: "foobar", 52 Regions: []StylingRegion{ 53 {r(1, 3), FgRed, 0}, 54 {r(1, 2), FgGreen, 1}, 55 }, 56 WantText: Concat(T("f"), T("o", FgGreen), T("obar")), 57 }, 58 { 59 Name: "overlapping regions with different starting positions", 60 String: "foobar", 61 Regions: []StylingRegion{ 62 {r(1, 3), FgRed, 0}, 63 {r(2, 4), FgGreen, 0}, 64 }, 65 WantText: Concat(T("f"), T("oo", FgRed), T("bar")), 66 }, 67 } 68 69 func r(a, b int) diag.Ranging { return diag.Ranging{From: a, To: b} } 70 71 func TestStyleRegions(t *testing.T) { 72 for _, test := range styleRegionsTests { 73 text := StyleRegions(test.String, test.Regions) 74 if !reflect.DeepEqual(text, test.WantText) { 75 t.Errorf("got %v, want %v", text, test.WantText) 76 } 77 } 78 }