Added CompareErr to testcommon

This commit is contained in:
Sasha Koshka 2024-04-20 23:07:38 -04:00
parent 293662e474
commit 33768c6d84
2 changed files with 46 additions and 43 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

@ -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
}