File: context_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 (67 lines) | stat: -rw-r--r-- 1,396 bytes parent folder | download
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
package diag

import (
	"strings"
	"testing"
)

var sourceRangeTests = []struct {
	Name    string
	Context *Context
	Indent  string

	WantShow string
}{
	{
		Name:    "single-line culprit",
		Context: contextInParen("[test]", "echo (bad)"),
		Indent:  "_",

		WantShow: dedent(`
			[test]:1:6-10: echo <(bad)>`),
	},
	{
		Name:    "multi-line culprit",
		Context: contextInParen("[test]", "echo (bad\nbad)\nmore"),
		Indent:  "_",

		WantShow: dedent(`
			[test]:1:6-2:4:
			_  echo <(bad>
			_  <bad)>`),
	},
	{
		Name:    "trailing newline in culprit is removed",
		Context: NewContext("[test]", "echo bad\n", Ranging{5, 9}),
		Indent:  "_",

		WantShow: dedent(`
			[test]:1:6-8: echo <bad>`),
	},
	{
		Name:    "empty culprit",
		Context: NewContext("[test]", "echo x", Ranging{5, 5}),

		WantShow: dedent(`
			[test]:1:6: echo <>x`),
	},
}

func TestContext(t *testing.T) {
	setContextBodyMarkers(t, "<", ">")
	for _, test := range sourceRangeTests {
		t.Run(test.Name, func(t *testing.T) {
			gotShow := test.Context.Show(test.Indent)
			if gotShow != test.WantShow {
				t.Errorf("Show() -> %q, want %q", gotShow, test.WantShow)
			}
		})
	}
}

// Returns a Context with the given name and source, and a range for the part
// between ( and ).
func contextInParen(name, src string) *Context {
	return NewContext(name, src,
		Ranging{strings.Index(src, "("), strings.Index(src, ")") + 1})
}