Skip to content

Commit

Permalink
adding LexerSuite, #410
Browse files Browse the repository at this point in the history
  • Loading branch information
almibe committed May 6, 2023
1 parent 3ddbf80 commit b4f2f60
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
3 changes: 2 additions & 1 deletion wander/shared/src/main/scala/dev/ligature/wander/Lexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum Token:
case StringLiteral(value: String)
case Name(value: String)
case OpenBrace, CloseBrace, Colon, OpenParen, CloseParen, NewLine,
Arrow, IfKeyword, ElseKeyword, EqualSign, LetKeyword, Comment
Arrow, IfKeyword, ElsifKeyword, ElseKeyword, EqualSign, LetKeyword, Comment

def tokenize(input: String): Either[ScriptError, Seq[Token]] = {
val gaze = Gaze.from(input)
Expand Down Expand Up @@ -75,6 +75,7 @@ val nameTokenNib = takeAll(
value.mkString match {
case "let" => Seq(Token.LetKeyword)
case "if" => Seq(Token.IfKeyword)
case "elsif" => Seq(Token.ElsifKeyword)
case "else" => Seq(Token.ElseKeyword)
case "true" => Seq(Token.BooleanLiteral(true))
case "false" => Seq(Token.BooleanLiteral(false))
Expand Down
45 changes: 36 additions & 9 deletions wander/shared/src/test/scala/dev/ligature/wander/LexerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,42 @@ import dev.ligature.{Identifier, StringLiteral}
import munit.FunSuite

class LexerSuite extends FunSuite {
test("") {
// val binding = Bindings()
// val binding2 = binding.bindVariable(identifier, value1).getOrElse(???)
// val res = binding.read(identifier)
// val res2 = binding2.read(identifier)
def check(script: String, tokens: Either[ScriptError, Seq[Token]]) =
assertEquals(tokenize(script), tokens)
def ident(identifier: String): Token =
Identifier.fromString(identifier) match
case Left(value) => ??? //just crash
case Right(value) => Token.Identifier(value)
val sp = Token.Spaces(" ")

// assert(res.isLeft)
// assert(binding.read(identifier2).isLeft)
// assertEquals(res2, Right(value1))
// assert(binding2.read(identifier2).isLeft)
test("tokenize booleans") {
val script = "true false"
val tokens = Right(Seq(Token.BooleanLiteral(true), sp, Token.BooleanLiteral(false)))
check(script, tokens)
}
test("tokenize Identifiers") {
val script = "<hello><123><_4><https://ligature.dev>"
val tokens = Right(Seq(ident("hello"), ident("123"), ident("_4"), ident("https://ligature.dev")))
check(script, tokens)
}
test("tokenize Integers") {
val script = "123 0 -123"
val tokens = Right(Seq(Token.IntegerLiteral(123), sp, Token.IntegerLiteral(0), sp, Token.IntegerLiteral(-123)))
check(script, tokens)
}
test("tokenize Strings") {
val script = "\"hello, world!\""
val tokens = Right(Seq(Token.StringLiteral("hello, world!")))
check(script, tokens)
}
test("tokenize Names") {
val script = "hello world _test also_a_321test123"
val tokens = Right(Seq(Token.Name("hello"), sp, Token.Name("world"), sp, Token.Name("_test"), sp, Token.Name("also_a_321test123")))
check(script, tokens)
}
test("tokenize symbols") {
val script = "{}:()\n\r\n->if elsif else = let --test"
val tokens = Right(Seq(Token.OpenBrace, Token.CloseBrace, Token.Colon, Token.OpenParen, Token.CloseParen, Token.NewLine, Token.NewLine,
Token.Arrow, Token.IfKeyword, sp, Token.ElsifKeyword, sp, Token.ElseKeyword, sp, Token.EqualSign, sp, Token.LetKeyword, sp, Token.Comment))
}
}

0 comments on commit b4f2f60

Please sign in to comment.