github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/diag/context_test.go (about) 1 package diag 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 var sourceRangeTests = []struct { 9 Name string 10 Context *Context 11 Indent string 12 13 WantShow string 14 WantShowCompact string 15 }{ 16 { 17 Name: "single-line culprit", 18 Context: parseContext("echo (bad)", "(", ")", true), 19 Indent: "_", 20 21 WantShow: lines( 22 "[test], line 1:", 23 "_echo <(bad)>", 24 ), 25 WantShowCompact: "[test], line 1: echo <(bad)>", 26 }, 27 { 28 Name: "multi-line culprit", 29 Context: parseContext("echo (bad\nbad)", "(", ")", true), 30 Indent: "_", 31 32 WantShow: lines( 33 "[test], line 1-2:", 34 "_echo <(bad>", 35 "_<bad)>", 36 ), 37 WantShowCompact: lines( 38 "[test], line 1-2: echo <(bad>", 39 "_ <bad)>", 40 ), 41 }, 42 { 43 Name: "empty culprit", 44 Context: parseContext("echo x", "x", "x", false), 45 Indent: "", 46 47 WantShow: lines( 48 "[test], line 1:", 49 "echo <^>x", 50 ), 51 WantShowCompact: "[test], line 1: echo <^>x", 52 }, 53 } 54 55 func TestContext(t *testing.T) { 56 culpritLineBegin = "<" 57 culpritLineEnd = ">" 58 for _, test := range sourceRangeTests { 59 t.Run(test.Name, func(t *testing.T) { 60 gotShow := test.Context.Show(test.Indent) 61 if gotShow != test.WantShow { 62 t.Errorf("Show() -> %q, want %q", gotShow, test.WantShow) 63 } 64 gotShowCompact := test.Context.ShowCompact(test.Indent) 65 if gotShowCompact != test.WantShowCompact { 66 t.Errorf("ShowCompact() -> %q, want %q", 67 gotShowCompact, test.WantShowCompact) 68 } 69 }) 70 } 71 } 72 73 // Parse a string into a source range, using the first appearance of certain 74 // texts as start and end positions. 75 func parseContext(s, starter, ender string, endAfter bool) *Context { 76 end := strings.Index(s, ender) 77 if endAfter { 78 end += len(ender) 79 } 80 return NewContext("[test]", s, Ranging{From: strings.Index(s, starter), To: end}) 81 }