From be9a3603d21f1fb4a351852be300f96b1079c299 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 18 Aug 2022 11:02:49 -0400 Subject: [PATCH] Made structural change to lexer test definitions --- lexer/lexer_test.go | 206 ++++++++++++++++++++++---------------------- 1 file changed, 105 insertions(+), 101 deletions(-) diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index fe700c9..0dfff07 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -5,7 +5,11 @@ 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) { +func quickToken (kind TokenKind, value any) (token Token) { + return Token { kind: kind, value: value } +} + +func checkTokenSlice (filePath string, test *testing.T, correct ...Token) { test.Log("checking lexer results for", filePath) file, err := file.Open(filePath) if err != nil { @@ -108,119 +112,119 @@ func compareErr ( } func TestTokenizeAll (test *testing.T) { - checkTokenSlice("../tests/lexer/all.arf", []Token { - Token { kind: TokenKindSeparator }, - Token { kind: TokenKindPermission, value: types.Permission { + checkTokenSlice("../tests/lexer/all.arf", test, + quickToken(TokenKindSeparator, nil), + quickToken (TokenKindPermission, types.Permission { Internal: types.ModeRead, External: types.ModeWrite, - }}, - Token { kind: TokenKindReturnDirection }, - Token { kind: TokenKindInt, value: int64(-349820394) }, - Token { kind: TokenKindUInt, value: uint64(932748397) }, - Token { kind: TokenKindFloat, value: 239485.37520 }, - Token { kind: TokenKindString, value: "hello world!\n" }, - Token { kind: TokenKindRune, value: 'E' }, - Token { kind: TokenKindName, value: "helloWorld" }, - Token { kind: TokenKindColon }, - Token { kind: TokenKindDot }, - Token { kind: TokenKindComma }, - Token { kind: TokenKindElipsis }, - Token { kind: TokenKindLBracket }, - Token { kind: TokenKindRBracket }, - Token { kind: TokenKindLBrace }, - Token { kind: TokenKindRBrace }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindPlus }, - Token { kind: TokenKindMinus }, - Token { kind: TokenKindIncrement }, - Token { kind: TokenKindDecrement }, - Token { kind: TokenKindAsterisk }, - Token { kind: TokenKindSlash }, - Token { kind: TokenKindAt }, - Token { kind: TokenKindExclamation }, - Token { kind: TokenKindPercent }, - Token { kind: TokenKindTilde }, - Token { kind: TokenKindLessThan }, - Token { kind: TokenKindLShift }, - Token { kind: TokenKindGreaterThan }, - Token { kind: TokenKindRShift }, - Token { kind: TokenKindBinaryOr }, - Token { kind: TokenKindLogicalOr }, - Token { kind: TokenKindBinaryAnd }, - Token { kind: TokenKindLogicalAnd }, - Token { kind: TokenKindNewline }, - }, test) + }), + quickToken(TokenKindReturnDirection, nil), + quickToken(TokenKindInt, int64(-349820394)), + quickToken(TokenKindUInt, uint64(932748397)), + quickToken(TokenKindFloat, 239485.37520), + quickToken(TokenKindString, "hello world!\n"), + quickToken(TokenKindRune, 'E'), + quickToken(TokenKindName, "helloWorld"), + quickToken(TokenKindColon, nil), + quickToken(TokenKindDot, nil), + quickToken(TokenKindComma, nil), + quickToken(TokenKindElipsis, nil), + quickToken(TokenKindLBracket, nil), + quickToken(TokenKindRBracket, nil), + quickToken(TokenKindLBrace, nil), + quickToken(TokenKindRBrace, nil), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindPlus, nil), + quickToken(TokenKindMinus, nil), + quickToken(TokenKindIncrement, nil), + quickToken(TokenKindDecrement, nil), + quickToken(TokenKindAsterisk, nil), + quickToken(TokenKindSlash, nil), + quickToken(TokenKindAt, nil), + quickToken(TokenKindExclamation, nil), + quickToken(TokenKindPercent, nil), + quickToken(TokenKindTilde, nil), + quickToken(TokenKindLessThan, nil), + quickToken(TokenKindLShift, nil), + quickToken(TokenKindGreaterThan, nil), + quickToken(TokenKindRShift, nil), + quickToken(TokenKindBinaryOr, nil), + quickToken(TokenKindLogicalOr, nil), + quickToken(TokenKindBinaryAnd, nil), + quickToken(TokenKindLogicalAnd, nil), + quickToken(TokenKindNewline, nil), + ) } func TestTokenizeNumbers (test *testing.T) { - checkTokenSlice("../tests/lexer/numbers.arf", []Token { - Token { kind: TokenKindUInt, value: uint64(0) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindUInt, value: uint64(8) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindUInt, value: uint64(83628266) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindUInt, value: uint64(83628266) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindUInt, value: uint64(83628266) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindUInt, value: uint64(83628266) }, - Token { kind: TokenKindNewline }, + checkTokenSlice("../tests/lexer/numbers.arf", test, + quickToken(TokenKindUInt, uint64(0)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindUInt, uint64(8)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindUInt, uint64(83628266)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindUInt, uint64(83628266)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindUInt, uint64(83628266)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindUInt, uint64(83628266)), + quickToken(TokenKindNewline, nil), - Token { kind: TokenKindInt, value: int64(-83628266) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindInt, value: int64(-83628266) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindInt, value: int64(-83628266) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindInt, value: int64(-83628266) }, - Token { kind: TokenKindNewline }, + quickToken(TokenKindInt, int64(-83628266)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindInt, int64(-83628266)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindInt, int64(-83628266)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindInt, int64(-83628266)), + quickToken(TokenKindNewline, nil), - Token { kind: TokenKindFloat, value: float64(0.123478) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindFloat, value: float64(234.3095) }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindFloat, value: float64(-2.312) }, - Token { kind: TokenKindNewline }, - }, test) + quickToken(TokenKindFloat, float64(0.123478)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindFloat, float64(234.3095)), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindFloat, float64(-2.312)), + quickToken(TokenKindNewline, nil), + ) } func TestTokenizeText (test *testing.T) { - checkTokenSlice("../tests/lexer/text.arf", []Token { - Token { kind: TokenKindString, value: "hello world!\a\b\f\n\r\t\v'\"\\" }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindRune, value: '\a' }, - Token { kind: TokenKindRune, value: '\b' }, - Token { kind: TokenKindRune, value: '\f' }, - Token { kind: TokenKindRune, value: '\n' }, - Token { kind: TokenKindRune, value: '\r' }, - Token { kind: TokenKindRune, value: '\t' }, - Token { kind: TokenKindRune, value: '\v' }, - Token { kind: TokenKindRune, value: '\'' }, - Token { kind: TokenKindRune, value: '"' }, - Token { kind: TokenKindRune, value: '\\' }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindString, value: "hello world \x40\u0040\U00000040!" }, - Token { kind: TokenKindNewline }, - }, test) + checkTokenSlice("../tests/lexer/text.arf", test, + quickToken(TokenKindString, "hello world!\a\b\f\n\r\t\v'\"\\"), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindRune, '\a'), + quickToken(TokenKindRune, '\b'), + quickToken(TokenKindRune, '\f'), + quickToken(TokenKindRune, '\n'), + quickToken(TokenKindRune, '\r'), + quickToken(TokenKindRune, '\t'), + quickToken(TokenKindRune, '\v'), + quickToken(TokenKindRune, '\''), + quickToken(TokenKindRune, '"' ), + quickToken(TokenKindRune, '\\'), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindString, "hello world \x40\u0040\U00000040!"), + quickToken(TokenKindNewline, nil), + ) } func TestTokenizeIndent (test *testing.T) { - checkTokenSlice("../tests/lexer/indent.arf", []Token { - Token { kind: TokenKindName, value: "line1" }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindIndent, value: 1 }, - Token { kind: TokenKindName, value: "line2" }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindIndent, value: 4 }, - Token { kind: TokenKindName, value: "line3" }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindName, value: "line4" }, - Token { kind: TokenKindNewline }, - Token { kind: TokenKindIndent, value: 2 }, - Token { kind: TokenKindName, value: "line5" }, - Token { kind: TokenKindNewline }, - }, test) + checkTokenSlice("../tests/lexer/indent.arf", test, + quickToken(TokenKindName, "line1"), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindIndent, 1), + quickToken(TokenKindName, "line2"), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindIndent, 4), + quickToken(TokenKindName, "line3"), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindName, "line4"), + quickToken(TokenKindNewline, nil), + quickToken(TokenKindIndent, 2), + quickToken(TokenKindName, "line5"), + quickToken(TokenKindNewline, nil), + ) } func TestTokenizeErr (test *testing.T) {