Compare commits

...

2 Commits

5 changed files with 102 additions and 96 deletions

View File

@ -6,6 +6,7 @@ import "github.com/google/uuid"
import "git.tebibyte.media/fspl/fspl/lexer"
import "git.tebibyte.media/fspl/fspl/errors"
import "git.tebibyte.media/fspl/fspl/entity"
import "git.tebibyte.media/fspl/fspl/testcommon"
import "git.tebibyte.media/fspl/fspl/parser/fspl"
func testStringErr (
@ -25,7 +26,7 @@ func testStringErr (
test.Error("analyzer did not return error")
return
}
compareErr(test, address, string(address), errMessage, errRow, errStart, err)
testcommon.CompareErr(test, string(address), errMessage, errRow, errStart, err)
}
func testString (test *testing.T, input string) {
@ -110,7 +111,7 @@ func testUnitsErr (
err := tree.Analyze(address.UUID(), nicknames, ast)
if err != nil {
compareErr(test, address, errFile, errMessage, errRow, errStart, err)
testcommon.CompareErr(test, errFile, errMessage, errRow, errStart, err)
return
}
@ -129,7 +130,7 @@ func testUnitsErr (
if !ok { return }
err := tree.Analyze(address.UUID(), nicknames, ast)
if err != nil {
compareErr(test, address, errFile, errMessage, errRow, errStart, err)
testcommon.CompareErr(test, errFile, errMessage, errRow, errStart, err)
return
}
@ -157,43 +158,3 @@ func treeOf (test *testing.T, address entity.Address, input string, skim bool) (
return ast, true
}
func compareErr (
test *testing.T,
address entity.Address,
errFile string,
errMessage string,
errRow int,
errStart int,
err error,
) bool {
got := err.(errors.Error)
gotMessage := got.Error()
gotFile := got.Position().File
gotRow := got.Position().Row + 1
gotStart := got.Position().Start + 1
correct :=
gotFile == errFile &&
gotMessage == errMessage &&
gotRow == errRow &&
gotStart == errStart
if correct { return true }
test.Log("errors do not match:")
if gotMessage != errMessage {
test.Log("- messages do not match:")
test.Logf(" [%s]", gotMessage)
test.Logf(" [%s]", errMessage)
}
if gotRow != errRow {
test.Logf("- rows do not match: (%d, %d)", gotRow, errRow)
}
if gotStart != errStart {
test.Logf("- columns do not match: (%d, %d)", gotStart, errStart)
}
test.Log("got:\n" + errors.Format(got))
test.Logf("correct:\n%v:%v: %v", errRow, errStart, errMessage)
test.Fail()
return false
}

View File

@ -1,55 +1,6 @@
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) {
@ -69,7 +20,6 @@ func TestGetXInTabbedString (test *testing.T) {
getXCase("x\tyyyy\tzy", 7, 16)
}
func TestFormatTabs (test *testing.T) {
fmtCase := func (line string, correct string) {
got := formatTabs(line)

View File

@ -1,6 +1,7 @@
package errors
package errorstest
import "testing"
import "git.tebibyte.media/fspl/fspl/errors"
import "git.tebibyte.media/fspl/fspl/testcommon"
func testString (test *testing.T, correct string, got string) {
@ -11,6 +12,6 @@ func testString (test *testing.T, correct string, got string) {
testcommon.Compare(test, correct, got)
}
func testError (test *testing.T, correct string, err Error) {
testString(test, correct, Format(err))
func testError (test *testing.T, correct string, err errors.Error) {
testString(test, correct, errors.Format(err))
}

View File

@ -0,0 +1,52 @@
package errorstest
import "testing"
import "git.tebibyte.media/fspl/fspl/errors"
func TestError (test *testing.T) {
testError (test,
`example.fspl:11:7: some error
11 | lorem ipsum dolor
^^^^^`,
errors.Errorf (
errors.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
^^^^^`,
errors.Errorf (
errors.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
^^^^^^^^^`,
errors.Errorf (
errors.Position {
File: "example.fspl",
Line: "\tlorem\tipsum\tdolor",
Row: 10,
Start: 7,
End: 14,
},
"some error"))
}

View File

@ -5,6 +5,7 @@ import "testing"
import "strings"
import "strconv"
import "encoding/hex"
import "git.tebibyte.media/fspl/fspl/errors"
// LogColumns logs columns of text side by side, each column 80 characters wide.
func LogColumns (test *testing.T, width int, columns ...any) {
@ -62,3 +63,44 @@ func CompareHex (test *testing.T, correct, got string) {
got = hex.Dump([]byte(got))
Compare(test, correct, got)
}
// CompareErr compares a correct error with a result error, and causes the test
// to fail and returns false if they differ. Otherwise, it returns true.
func CompareErr (
test *testing.T,
errFile string,
errMessage string,
errRow int,
errStart int,
err error,
) bool {
got := err.(errors.Error)
gotMessage := got.Error()
gotFile := got.Position().File
gotRow := got.Position().Row + 1
gotStart := got.Position().Start + 1
correct :=
gotFile == errFile &&
gotMessage == errMessage &&
gotRow == errRow &&
gotStart == errStart
if correct { return true }
test.Log("errors do not match:")
if gotMessage != errMessage {
test.Log("- messages do not match:")
test.Logf(" [%s]", gotMessage)
test.Logf(" [%s]", errMessage)
}
if gotRow != errRow {
test.Logf("- rows do not match: (%d, %d)", gotRow, errRow)
}
if gotStart != errStart {
test.Logf("- columns do not match: (%d, %d)", gotStart, errStart)
}
test.Log("got:\n" + errors.Format(got))
test.Logf("correct:\n%v:%v: %v", errRow, errStart, errMessage)
test.Fail()
return false
}