written structure and tests for lexer, missing

implementation
This commit is contained in:
Karma Riuk
2025-06-29 10:56:51 +02:00
parent ccfc3ed0f7
commit 1c928616a4
4 changed files with 62 additions and 0 deletions

0
src/lexer/lexer.cpp Normal file
View File

7
src/lexer/lexer.hpp Normal file
View File

@ -0,0 +1,7 @@
#include "token/token.hpp"
namespace lexer {
struct lexer {
token::token next_token();
};
} // namespace lexer

35
test/lexer.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "lexer/lexer.hpp"
#include "token/type.hpp"
#include <doctest.h>
#include <string>
TEST_CASE("next token") {
struct test {
token::type expectedType;
std::string expectedLiteral;
};
std::string input = "=+(){},;";
lexer::lexer l{};
test tests[] = {
{token::type::ASSIGN, "="},
{token::type::PLUS, "+"},
{token::type::LPAREN, "("},
{token::type::RPAREN, ")"},
{token::type::LBRACE, "{"},
{token::type::RBRACE, "}"},
{token::type::COMMA, ","},
{token::type::SEMICOLON, ";"},
{token::type::EOF_, ""},
};
for (const auto& t : tests) {
token::token tok = l.next_token();
CHECK(tok.type == t.expectedType);
CHECK(tok.literal == t.expectedLiteral);
}
};

20
test/test.cpp Normal file
View File

@ -0,0 +1,20 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest.h>
int factorial(int number) {
return number <= 1 ? number : factorial(number - 1) * number;
}
TEST_CASE("fact") {
CHECK(factorial(1) == 1);
CHECK(factorial(2) == 2);
CHECK(factorial(3) == 6);
CHECK(factorial(10) == 3628800);
}
TEST_CASE("fact2") {
CHECK(factorial(1) == 1);
CHECK(factorial(2) == 2);
CHECK(factorial(3) == 6);
CHECK(factorial(10) == 3628800);
}