package errors import "testing" import "strings" func TestError (test *testing.T) { testError (test, `example.fspl:11:7: some error 11 | lorem ipsum dolor ^^^^^`, Errorf ( Position { File: "example.fspl", Line: "lorem ipsum dolor", Row: 10, Start: 6, End: 11, }, "some error")) } func TestErrorTab (test *testing.T) { testError (test, `example.fspl:11:8: some error 11 | lorem ipsum dolor ^^^^^`, Errorf ( Position { File: "example.fspl", Line: "\tlorem\tipsum\tdolor", Row: 10, Start: 7, End: 12, }, "some error")) } func TestErrorTabInBetween (test *testing.T) { testError (test, `example.fspl:11:8: some error 11 | lorem ipsum dolor ^^^^^^^^^`, Errorf ( Position { File: "example.fspl", Line: "\tlorem\tipsum\tdolor", Row: 10, Start: 7, End: 14, }, "some error")) } func TestGetXInTabbedString (test *testing.T) { getXCase := func (line string, column, correct int) { x := getXInTabbedString(line, column) if x != correct { test.Logf("[%s]: %d", formatTabs(line), column) test.Log("expecting", correct, "got", x) test.Fail() } } getXCase("\t", 1, 8) getXCase("x\ty", 1, 1) getXCase("x\ty", 2, 8) getXCase("x\tyyy", 3, 9) getXCase("x\ty", 3, 9) getXCase("x\tyyyy\tzy", 7, 16) } func TestFormatTabs (test *testing.T) { fmtCase := func (line string, correct string) { got := formatTabs(line) if got != correct { test.Logf("[%s]", strings.ReplaceAll(line, "\t", "\\t")) test.Logf("expecting [%s] got [%s]", correct, got) test.Fail() } } fmtCase("\t", " ") fmtCase("\ty", " y") fmtCase("x\ty", "x y") fmtCase("x\tyyyy\tz", "x yyyy z") fmtCase("x\tyyyyyyy\tz", "x yyyyyyy z") fmtCase("\tjustice!\tz", " justice! z") }