Created test to check lexer errors

This commit is contained in:
Sasha Koshka 2022-08-18 01:25:02 -04:00
parent 39e4fbe844
commit 9e66305001
2 changed files with 69 additions and 0 deletions

View File

@ -3,8 +3,10 @@ package lexer
import "testing" import "testing"
import "git.tebibyte.media/sashakoshka/arf/file" import "git.tebibyte.media/sashakoshka/arf/file"
import "git.tebibyte.media/sashakoshka/arf/types" import "git.tebibyte.media/sashakoshka/arf/types"
import "git.tebibyte.media/sashakoshka/arf/infoerr"
func checkTokenSlice (filePath string, correct []Token, test *testing.T) { func checkTokenSlice (filePath string, correct []Token, test *testing.T) {
test.Log("checking lexer results for", filePath)
file, err := file.Open(filePath) file, err := file.Open(filePath)
if err != nil { if err != nil {
test.Log(err) test.Log(err)
@ -46,6 +48,62 @@ func checkTokenSlice (filePath string, correct []Token, test *testing.T) {
test.Log("token slice content match") test.Log("token slice content match")
} }
func compareErr (
filePath string,
correctKind infoerr.ErrorKind,
correctMessage string,
correctRow int,
correctColumn int,
correctWidth int,
test *testing.T,
) {
test.Log("testing errors in", filePath)
file, err := file.Open(filePath)
if err != nil {
test.Log(err)
test.Fail()
return
}
_, err = Tokenize(file)
check := err.(infoerr.Error)
if check.Kind() != correctKind {
test.Log("mismatched error kind")
test.Log("- want:", correctKind)
test.Log("- have:", check.Kind())
test.Fail()
}
if check.Message() != correctMessage {
test.Log("mismatched error message")
test.Log("- want:", correctMessage)
test.Log("- have:", check.Message())
test.Fail()
}
if check.Row() != correctRow {
test.Log("mismatched error row")
test.Log("- want:", correctRow)
test.Log("- have:", check.Row())
test.Fail()
}
if check.Column() != correctColumn {
test.Log("mismatched error column")
test.Log("- want:", correctColumn)
test.Log("- have:", check.Column())
test.Fail()
}
if check.Width() != correctWidth {
test.Log("mismatched error width")
test.Log("- want:", check.Width())
test.Log("- have:", correctWidth)
test.Fail()
}
}
func TestTokenizeAll (test *testing.T) { func TestTokenizeAll (test *testing.T) {
checkTokenSlice("../tests/lexer/all.arf", []Token { checkTokenSlice("../tests/lexer/all.arf", []Token {
Token { kind: TokenKindSeparator }, Token { kind: TokenKindSeparator },
@ -161,3 +219,12 @@ func TestTokenizeIndent (test *testing.T) {
Token { kind: TokenKindNewline }, Token { kind: TokenKindNewline },
}, test) }, test)
} }
func TestTokenizeErr (test *testing.T) {
compareErr (
"../tests/lexer/error.arf",
infoerr.ErrorKindError,
"unexpected symbol character ;",
1, 0, 1,
test)
}

2
tests/lexer/error.arf Normal file
View File

@ -0,0 +1,2 @@
:arf
;