From 9e663050016f711544596df6c5429b320f1bdc83 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 18 Aug 2022 01:25:02 -0400 Subject: [PATCH] Created test to check lexer errors --- lexer/lexer_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++ tests/lexer/error.arf | 2 ++ 2 files changed, 69 insertions(+) create mode 100644 tests/lexer/error.arf diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index 27216d0..d67e691 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -3,8 +3,10 @@ package lexer import "testing" import "git.tebibyte.media/sashakoshka/arf/file" import "git.tebibyte.media/sashakoshka/arf/types" +import "git.tebibyte.media/sashakoshka/arf/infoerr" func checkTokenSlice (filePath string, correct []Token, test *testing.T) { + test.Log("checking lexer results for", filePath) file, err := file.Open(filePath) if err != nil { test.Log(err) @@ -46,6 +48,62 @@ func checkTokenSlice (filePath string, correct []Token, test *testing.T) { 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) { checkTokenSlice("../tests/lexer/all.arf", []Token { Token { kind: TokenKindSeparator }, @@ -161,3 +219,12 @@ func TestTokenizeIndent (test *testing.T) { Token { kind: TokenKindNewline }, }, test) } + +func TestTokenizeErr (test *testing.T) { + compareErr ( + "../tests/lexer/error.arf", + infoerr.ErrorKindError, + "unexpected symbol character ;", + 1, 0, 1, + test) +} diff --git a/tests/lexer/error.arf b/tests/lexer/error.arf new file mode 100644 index 0000000..9957cde --- /dev/null +++ b/tests/lexer/error.arf @@ -0,0 +1,2 @@ +:arf +;