File: style_regions_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (78 lines) | stat: -rw-r--r-- 1,861 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ui_test

import (
	"reflect"
	"testing"

	"src.elv.sh/pkg/diag"
	"src.elv.sh/pkg/ui"
)

var styleRegionsTests = []struct {
	Name     string
	String   string
	Regions  []ui.StylingRegion
	WantText ui.Text
}{
	{
		Name:   "empty string and regions",
		String: "", Regions: nil, WantText: nil,
	},
	{
		Name:   "a single region",
		String: "foobar",
		Regions: []ui.StylingRegion{
			{r(1, 3), ui.FgRed, 0},
		},
		WantText: ui.Concat(ui.T("f"), ui.T("oo", ui.FgRed), ui.T("bar")),
	},

	{
		Name:   "multiple continuous regions",
		String: "foobar",
		Regions: []ui.StylingRegion{
			{r(1, 3), ui.FgRed, 0},
			{r(3, 4), ui.FgGreen, 0},
		},
		WantText: ui.Concat(ui.T("f"), ui.T("oo", ui.FgRed), ui.T("b", ui.FgGreen), ui.T("ar")),
	},

	{
		Name:   "multiple discontinuous regions in wrong order",
		String: "foobar",
		Regions: []ui.StylingRegion{
			{r(4, 5), ui.FgGreen, 0},
			{r(1, 3), ui.FgRed, 0},
		},
		WantText: ui.Concat(ui.T("f"), ui.T("oo", ui.FgRed), ui.T("b"), ui.T("a", ui.FgGreen), ui.T("r")),
	},
	{
		Name:   "regions with the same starting position but differeng priorities",
		String: "foobar",
		Regions: []ui.StylingRegion{
			{r(1, 3), ui.FgRed, 0},
			{r(1, 2), ui.FgGreen, 1},
		},
		WantText: ui.Concat(ui.T("f"), ui.T("o", ui.FgGreen), ui.T("obar")),
	},
	{
		Name:   "overlapping regions with different starting positions",
		String: "foobar",
		Regions: []ui.StylingRegion{
			{r(1, 3), ui.FgRed, 0},
			{r(2, 4), ui.FgGreen, 0},
		},
		WantText: ui.Concat(ui.T("f"), ui.T("oo", ui.FgRed), ui.T("bar")),
	},
}

func r(a, b int) diag.Ranging { return diag.Ranging{From: a, To: b} }

func TestStyleRegions(t *testing.T) {
	for _, test := range styleRegionsTests {
		text := ui.StyleRegions(test.String, test.Regions)
		if !reflect.DeepEqual(text, test.WantText) {
			t.Errorf("got %v, want %v", text, test.WantText)
		}
	}
}