This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
arf/lexer/lexer_test.go

286 lines
8.2 KiB
Go
Raw Normal View History

2022-08-10 09:28:29 -06:00
package lexer
import "testing"
2022-08-29 23:11:10 -06:00
import "git.tebibyte.media/arf/arf/file"
import "git.tebibyte.media/arf/arf/types"
import "git.tebibyte.media/arf/arf/infoerr"
2022-08-10 09:28:29 -06:00
2022-08-18 09:14:42 -06:00
func quickToken (width int, kind TokenKind, value any) (token Token) {
token.location.SetWidth(width)
token.kind = kind
token.value = value
return
}
func checkTokenSlice (filePath string, test *testing.T, correct ...Token) {
2022-08-17 23:25:02 -06:00
test.Log("checking lexer results for", filePath)
2022-08-11 03:23:06 -06:00
file, err := file.Open(filePath)
2022-08-10 09:28:29 -06:00
if err != nil {
test.Log(err)
test.Fail()
2022-08-10 10:58:47 -06:00
return
2022-08-10 09:28:29 -06:00
}
tokens, err := Tokenize(file)
2022-08-10 13:02:08 -06:00
// print all tokens
2022-08-11 02:58:45 -06:00
for index, token := range tokens {
test.Log(index, "\tgot token:", token.Describe())
2022-08-10 13:02:08 -06:00
}
if err != nil {
test.Log("returned error:")
test.Log(err.Error())
2022-08-10 09:28:29 -06:00
test.Fail()
2022-08-10 10:58:47 -06:00
return
2022-08-10 09:28:29 -06:00
}
2022-08-11 03:23:06 -06:00
if len(tokens) != len(correct) {
test.Log("lexed", len(tokens), "tokens, want", len(correct))
test.Fail()
return
}
test.Log("token slice length match", len(tokens), "=", len(correct))
for index, token := range tokens {
2022-08-18 09:14:42 -06:00
if token.location.Width() != correct[index].location.Width() {
test.Log("token", index, "has bad width")
test.Log (
"have", token.location.Width(),
"want", correct[index].location.Width())
test.Fail()
return
}
2022-08-11 03:23:06 -06:00
if !token.Equals(correct[index]) {
test.Log("token", index, "not equal")
test.Log (
"have", token.Describe(),
"want", correct[index].Describe())
test.Fail()
return
}
}
test.Log("token slice content match")
}
2022-08-17 23:25:02 -06:00
func compareErr (
filePath string,
correctKind infoerr.ErrorKind,
correctMessage string,
correctRow int,
correctColumn int,
correctWidth int,
test *testing.T,
) {
2022-10-04 14:47:32 -06:00
test.Log("testing error in", filePath)
2022-08-17 23:25:02 -06:00
file, err := file.Open(filePath)
if err != nil {
test.Log(err)
test.Fail()
return
}
2022-10-04 14:51:53 -06:00
tokens, err := Tokenize(file)
2022-10-04 14:47:32 -06:00
check, isCorrectType := err.(infoerr.Error)
2022-10-04 14:51:53 -06:00
for index, token := range tokens {
test.Log(index, "\tgot token:", token.Describe())
}
if err == nil {
test.Log("no error was recieved, test failed.")
test.Fail()
return
}
2022-08-17 23:25:02 -06:00
2022-08-17 23:47:35 -06:00
test.Log("error that was recieved:")
test.Log(check)
2022-10-04 14:47:32 -06:00
if !isCorrectType {
test.Log("error is not infoerr.Error, something has gone wrong.")
test.Fail()
return
}
2022-08-17 23:25:02 -06:00
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()
}
}
2022-08-11 03:23:06 -06:00
func TestTokenizeAll (test *testing.T) {
checkTokenSlice("../tests/lexer/all.arf", test,
2022-08-18 09:14:42 -06:00
quickToken(3, TokenKindSeparator, nil),
quickToken(2, TokenKindPermission, types.PermissionReadWrite),
2022-08-18 09:14:42 -06:00
quickToken(2, TokenKindReturnDirection, nil),
quickToken(10, TokenKindInt, int64(-349820394)),
quickToken(9, TokenKindUInt, uint64(932748397)),
quickToken(12, TokenKindFloat, 239485.37520),
quickToken(16, TokenKindString, "hello world!\n"),
2022-10-04 14:35:00 -06:00
quickToken(3, TokenKindString, "E"),
2022-08-18 09:14:42 -06:00
quickToken(10, TokenKindName, "helloWorld"),
quickToken(1, TokenKindColon, nil),
quickToken(1, TokenKindDot, nil),
quickToken(1, TokenKindComma, nil),
quickToken(2, TokenKindElipsis, nil),
2022-09-13 15:04:43 -06:00
quickToken(1, TokenKindLParen, nil),
quickToken(1, TokenKindRParen, nil),
2022-08-18 09:14:42 -06:00
quickToken(1, TokenKindLBracket, nil),
quickToken(1, TokenKindRBracket, nil),
quickToken(1, TokenKindLBrace, nil),
quickToken(1, TokenKindRBrace, nil),
quickToken(1, TokenKindNewline, nil),
quickToken(1, TokenKindPlus, nil),
quickToken(1, TokenKindMinus, nil),
quickToken(2, TokenKindIncrement, nil),
quickToken(2, TokenKindDecrement, nil),
quickToken(1, TokenKindAsterisk, nil),
quickToken(1, TokenKindSlash, nil),
quickToken(1, TokenKindAt, nil),
quickToken(1, TokenKindExclamation, nil),
quickToken(1, TokenKindPercent, nil),
2022-08-25 21:17:42 -06:00
quickToken(2, TokenKindPercentAssignment, nil),
2022-08-18 09:14:42 -06:00
quickToken(1, TokenKindTilde, nil),
2022-08-25 21:17:42 -06:00
quickToken(2, TokenKindTildeAssignment, nil),
quickToken(1, TokenKindAssignment, nil),
quickToken(2, TokenKindEqualTo, nil),
2022-08-24 22:05:40 -06:00
quickToken(2, TokenKindNotEqualTo, nil),
2022-08-18 09:14:42 -06:00
quickToken(1, TokenKindLessThan, nil),
2022-08-24 22:05:40 -06:00
quickToken(2, TokenKindLessThanEqualTo, nil),
2022-08-18 09:14:42 -06:00
quickToken(2, TokenKindLShift, nil),
2022-08-25 21:17:42 -06:00
quickToken(3, TokenKindLShiftAssignment, nil),
2022-08-18 09:14:42 -06:00
quickToken(1, TokenKindGreaterThan, nil),
2022-08-24 22:05:40 -06:00
quickToken(2, TokenKindGreaterThanEqualTo, nil),
2022-08-18 09:14:42 -06:00
quickToken(2, TokenKindRShift, nil),
2022-08-25 21:17:42 -06:00
quickToken(3, TokenKindRShiftAssignment, nil),
2022-08-18 09:14:42 -06:00
quickToken(1, TokenKindBinaryOr, nil),
2022-08-25 21:17:42 -06:00
quickToken(2, TokenKindBinaryOrAssignment, nil),
2022-08-18 09:14:42 -06:00
quickToken(2, TokenKindLogicalOr, nil),
quickToken(1, TokenKindBinaryAnd, nil),
2022-08-25 21:17:42 -06:00
quickToken(2, TokenKindBinaryAndAssignment, nil),
2022-08-18 09:14:42 -06:00
quickToken(2, TokenKindLogicalAnd, nil),
2022-08-25 21:17:42 -06:00
quickToken(1, TokenKindBinaryXor, nil),
quickToken(2, TokenKindBinaryXorAssignment, nil),
2022-08-18 09:14:42 -06:00
quickToken(1, TokenKindNewline, nil),
)
2022-08-11 03:23:06 -06:00
}
2022-08-10 09:28:29 -06:00
2022-08-11 03:23:06 -06:00
func TestTokenizeNumbers (test *testing.T) {
checkTokenSlice("../tests/lexer/numbers.arf", test,
2022-08-18 09:14:42 -06:00
quickToken(1, TokenKindUInt, uint64(0)),
quickToken(1, TokenKindNewline, nil),
quickToken(1, TokenKindUInt, uint64(8)),
quickToken(1, TokenKindNewline, nil),
quickToken(8, TokenKindUInt, uint64(83628266)),
quickToken(1, TokenKindNewline, nil),
quickToken(29, TokenKindUInt, uint64(83628266)),
quickToken(1, TokenKindNewline, nil),
quickToken(9, TokenKindUInt, uint64(83628266)),
quickToken(1, TokenKindNewline, nil),
quickToken(10, TokenKindUInt, uint64(83628266)),
quickToken(1, TokenKindNewline, nil),
2022-08-11 03:23:06 -06:00
2022-08-18 09:14:42 -06:00
quickToken(9, TokenKindInt, int64(-83628266)),
quickToken(1, TokenKindNewline, nil),
quickToken(30, TokenKindInt, int64(-83628266)),
quickToken(1, TokenKindNewline, nil),
quickToken(10, TokenKindInt, int64(-83628266)),
quickToken(1, TokenKindNewline, nil),
quickToken(11, TokenKindInt, int64(-83628266)),
quickToken(1, TokenKindNewline, nil),
2022-08-11 03:23:06 -06:00
2022-08-18 09:14:42 -06:00
quickToken(8, TokenKindFloat, float64(0.123478)),
quickToken(1, TokenKindNewline, nil),
quickToken(8, TokenKindFloat, float64(234.3095)),
quickToken(1, TokenKindNewline, nil),
quickToken(6, TokenKindFloat, float64(-2.312)),
quickToken(1, TokenKindNewline, nil),
)
2022-08-10 09:28:29 -06:00
}
func TestTokenizeText (test *testing.T) {
checkTokenSlice("../tests/lexer/text.arf", test,
2022-10-04 14:47:32 -06:00
quickToken(32, TokenKindString, "hello world!\a\b\f\n\r\t\v'\\"),
2022-10-04 14:35:00 -06:00
quickToken(1, TokenKindNewline, nil),
quickToken(4, TokenKindString, "\a"),
quickToken(4, TokenKindString, "\b"),
quickToken(4, TokenKindString, "\f"),
quickToken(4, TokenKindString, "\n"),
quickToken(4, TokenKindString, "\r"),
quickToken(4, TokenKindString, "\t"),
quickToken(4, TokenKindString, "\v"),
quickToken(4, TokenKindString, "'"),
quickToken(4, TokenKindString, "\\"),
2022-08-18 09:14:42 -06:00
quickToken(1, TokenKindNewline, nil),
quickToken(35, TokenKindString, "hello world \x40\u0040\U00000040!"),
quickToken(1, TokenKindNewline, nil),
)
}
2022-08-11 11:47:50 -06:00
func TestTokenizeIndent (test *testing.T) {
checkTokenSlice("../tests/lexer/indent.arf", test,
2022-08-18 09:14:42 -06:00
quickToken(5, TokenKindName, "line1"),
quickToken(1, TokenKindNewline, nil),
quickToken(1, TokenKindIndent, 1),
quickToken(5, TokenKindName, "line2"),
quickToken(1, TokenKindNewline, nil),
quickToken(4, TokenKindIndent, 4),
quickToken(5, TokenKindName, "line3"),
quickToken(1, TokenKindNewline, nil),
quickToken(5, TokenKindName, "line4"),
quickToken(1, TokenKindNewline, nil),
quickToken(2, TokenKindIndent, 2),
quickToken(5, TokenKindName, "line5"),
quickToken(1, TokenKindNewline, nil),
)
2022-08-11 11:47:50 -06:00
}
2022-08-17 23:25:02 -06:00
2022-10-04 14:47:32 -06:00
func TestTokenizeErrUnexpectedSymbol (test *testing.T) {
2022-08-17 23:25:02 -06:00
compareErr (
2022-08-17 23:47:35 -06:00
"../tests/lexer/error/unexpectedSymbol.arf",
2022-08-17 23:25:02 -06:00
infoerr.ErrorKindError,
"unexpected symbol character ;",
2022-08-17 23:47:35 -06:00
1, 5, 1,
test)
2022-10-04 14:47:32 -06:00
}
func TestTokenizeErrUnknownEscape (test *testing.T) {
2022-08-17 23:47:35 -06:00
compareErr (
"../tests/lexer/error/unknownEscape.arf",
infoerr.ErrorKindError,
"unknown escape character g",
2022-08-18 00:06:00 -06:00
1, 2, 1,
2022-08-17 23:25:02 -06:00
test)
}