136 lines
2.5 KiB
Go
136 lines
2.5 KiB
Go
package lexer
|
|
|
|
import "testing"
|
|
|
|
func TestLexerEmpty (test *testing.T) {
|
|
testString(test, "", tok(EOF, ""))
|
|
}
|
|
|
|
func TestLexerErrUnexpectedRune (test *testing.T) {
|
|
testStringErr(test,
|
|
"unexpected rune '\"'", `ooo " hahsdklhasldk`, 2, 4,
|
|
`
|
|
valid tokens valid tokens
|
|
ooo " hahsdklhasldk
|
|
`,
|
|
tok(Ident, "valid"),
|
|
tok(Ident, "tokens"),
|
|
tok(Ident, "valid"),
|
|
tok(Ident, "tokens"),
|
|
tok(Ident, "ooo"),
|
|
tok(0, ""),
|
|
)}
|
|
|
|
func TestLexerErrUnexpectedEOF (test *testing.T) {
|
|
testStringErr(test,
|
|
"unexpected EOF", `'this is a string that does not end...`, 1, 38,
|
|
`
|
|
'this is a string that does not end...`,
|
|
tok(String, "this is a string that does not end..."),
|
|
tok(EOF, ""),
|
|
)}
|
|
|
|
func TestLexer (test *testing.T) {
|
|
testString(test,
|
|
`
|
|
normalIdent TypeIdent
|
|
[$$abcdEfGhIjKlMnOpQrStUvWxYz]{+}(-) ; this is a comment
|
|
~ ! @ # $ % ^ & *- _ = + \ | :** ::, < . .. > / ?
|
|
!! ++ -- && || 'some \'string' 'nullterm\0' '\110\117\115\105'
|
|
1234 -1234 9876540321 (754.2340)
|
|
`,
|
|
tok(Ident, "normalIdent"),
|
|
tok(TypeIdent, "TypeIdent"),
|
|
tok(LBracket, "["),
|
|
tok(Symbol, "$$"),
|
|
tok(Ident, "abcdEfGhIjKlMnOpQrStUvWxYz"),
|
|
tok(RBracket, "]"),
|
|
tok(LBrace, "{"),
|
|
tok(Symbol, "+"),
|
|
tok(RBrace, "}"),
|
|
tok(LParen, "("),
|
|
tok(Symbol, "-"),
|
|
tok(RParen, ")"),
|
|
|
|
tok(Symbol, "~"),
|
|
tok(Symbol, "!"),
|
|
tok(Symbol, "@"),
|
|
tok(Symbol, "#"),
|
|
tok(Symbol, "$"),
|
|
tok(Symbol, "%"),
|
|
tok(Symbol, "^"),
|
|
tok(Symbol, "&"),
|
|
tok(Star, "*"),
|
|
tok(Symbol, "-"),
|
|
tok(Symbol, "_"),
|
|
tok(Symbol, "="),
|
|
tok(Symbol, "+"),
|
|
tok(Symbol, "\\"),
|
|
tok(Symbol, "|"),
|
|
tok(Colon, ":"),
|
|
tok(Star, "*"),
|
|
tok(Star, "*"),
|
|
tok(DoubleColon, "::"),
|
|
tok(Symbol, ","),
|
|
tok(Symbol, "<"),
|
|
tok(Dot, "."),
|
|
tok(DoubleDot, ".."),
|
|
tok(Symbol, ">"),
|
|
tok(Symbol, "/"),
|
|
tok(Symbol, "?"),
|
|
|
|
tok(Symbol, "!!"),
|
|
tok(Symbol, "++"),
|
|
tok(Symbol, "--"),
|
|
tok(Symbol, "&&"),
|
|
tok(Symbol, "||"),
|
|
|
|
tok(String, "some 'string"),
|
|
tok(String, "nullterm\000"),
|
|
tok(String, "HOME"),
|
|
tok(Int, "1234"),
|
|
tok(Int, "-1234"),
|
|
tok(Int, "9876540321"),
|
|
|
|
tok(LParen, "("),
|
|
tok(Float, "754.2340"),
|
|
tok(RParen, ")"),
|
|
|
|
tok(EOF, ""),
|
|
)}
|
|
|
|
func TestLexerSingleParen (test *testing.T) {
|
|
testString(test,
|
|
`)`,
|
|
tok(RParen, ")"),
|
|
tok(EOF, ""),
|
|
)}
|
|
|
|
func TestLexerNoTrailingEOLInt (test *testing.T) {
|
|
testString(test,
|
|
`12345`,
|
|
tok(Int, "12345"),
|
|
tok(EOF, ""),
|
|
)}
|
|
|
|
func TestLexerNoTrailingEOLString (test *testing.T) {
|
|
testString(test,
|
|
`'12345'`,
|
|
tok(String, "12345"),
|
|
tok(EOF, ""),
|
|
)}
|
|
|
|
func TestLexerNoTrailingEOLIdent (test *testing.T) {
|
|
testString(test,
|
|
`hello`,
|
|
tok(Ident, "hello"),
|
|
tok(EOF, ""),
|
|
)}
|
|
|
|
func TestLexerNoTrailingEOLSymbol (test *testing.T) {
|
|
testString(test,
|
|
`@#$`,
|
|
tok(Symbol, "@#$"),
|
|
tok(EOF, ""),
|
|
)}
|