github.com/elves/elvish@v0.15.0/pkg/diag/error_test.go (about) 1 package diag 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestError(t *testing.T) { 9 err := &Error{ 10 Type: "some error", 11 Message: "bad list", 12 Context: *parseContext("echo [x]", "[", "]", true), 13 } 14 15 wantErrorString := "some error: 5-8 in [test]: bad list" 16 if gotErrorString := err.Error(); gotErrorString != wantErrorString { 17 t.Errorf("Error() -> %q, want %q", gotErrorString, wantErrorString) 18 } 19 20 wantRanging := Ranging{From: 5, To: 8} 21 if gotRanging := err.Range(); gotRanging != wantRanging { 22 t.Errorf("Range() -> %v, want %v", gotRanging, wantRanging) 23 } 24 25 culpritLineBegin = "<" 26 culpritLineEnd = ">" 27 wantShow := lines( 28 "some error: \033[31;1mbad list\033[m", 29 "[test], line 1: echo <[x]>", 30 ) 31 if gotShow := err.Show(""); gotShow != wantShow { 32 t.Errorf("Show() -> %q, want %q", gotShow, wantShow) 33 } 34 } 35 36 func lines(lines ...string) string { 37 return strings.Join(lines, "\n") 38 }