From 69217fdf90d53452cf40d49d0b9407a1b95dd69e Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sun, 29 Jun 2025 20:28:53 +0200 Subject: [PATCH] added test for full lexer (missing impl) --- test/lexer.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/test/lexer.cpp b/test/lexer.cpp index 061099e..a2f215d 100644 --- a/test/lexer.cpp +++ b/test/lexer.cpp @@ -6,7 +6,7 @@ #include #include -TEST_CASE("next token") { +TEST_CASE("Single character token") { struct test { token::type expectedType; std::string expectedLiteral; @@ -35,3 +35,70 @@ TEST_CASE("next token") { CHECK(tok.literal == t.expectedLiteral); } }; + +TEST_CASE("Full tokens") { + struct test { + token::type expectedType; + std::string expectedLiteral; + }; + + std::istringstream ss("let five = 5;\ +let ten = 10;\ +let add = fn(x, y) {\ + x + y;\ +};\ +let result = add(five, ten);\ +"); + + lexer::lexer l{ss}; + + test tests[] = { + // clang-format off + {token::type::LET, "let"}, + {token::type::IDENTIFIER, "five"}, + {token::type::ASSIGN, "="}, + {token::type::INT, "5"}, + {token::type::SEMICOLON, ";"}, + + {token::type::LET, "let"}, + {token::type::IDENTIFIER, "ten"}, + {token::type::ASSIGN, "="}, + {token::type::INT, "10"}, + {token::type::SEMICOLON, ";"}, + + {token::type::LET, "let"}, + {token::type::IDENTIFIER, "add"}, + {token::type::ASSIGN, "="}, + {token::type::FUNCTION, "fn"}, + {token::type::LPAREN, "("}, + {token::type::IDENTIFIER, "x"}, + {token::type::COMMA, ","}, + {token::type::IDENTIFIER, "y"}, + {token::type::RPAREN, ")"}, + {token::type::LBRACE, "{"}, + {token::type::IDENTIFIER, "x"}, + {token::type::PLUS, "+"}, + {token::type::IDENTIFIER, "y"}, + {token::type::SEMICOLON, ";"}, + {token::type::RBRACE, "}"}, + {token::type::SEMICOLON, ";"}, + + {token::type::LET, "let"}, + {token::type::IDENTIFIER, "result"}, + {token::type::ASSIGN, "="}, + {token::type::IDENTIFIER, "add"}, + {token::type::LPAREN, "("}, + {token::type::IDENTIFIER, "five"}, + {token::type::COMMA, ","}, + {token::type::IDENTIFIER, "ten"}, + {token::type::RPAREN, ")"}, + {token::type::SEMICOLON, ";"}, + // clang-format on + }; + + for (const auto& t : tests) { + token::token tok = l.next_token(); + CHECK(tok.type == t.expectedType); + CHECK(tok.literal == t.expectedLiteral); + } +};