Skip to content

Commit

Permalink
Added tests for combining Lexer_nextToken and Lexer_peekToken
Browse files Browse the repository at this point in the history
  • Loading branch information
loumadev committed Oct 12, 2023
1 parent 80708b4 commit b2a655e
Showing 1 changed file with 190 additions and 0 deletions.
190 changes: 190 additions & 0 deletions test/compiler/lexer/Lexer.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,196 @@ DESCRIBE(peekToken, "Peeking tokens (peekToken)") {

LexerResult result;

TEST_BEGIN("Combination of nextToken and peekToken") {
Lexer_setSource(&lexer, "1 2 3 4 5 6 7 8");

//

result = Lexer_nextToken(&lexer);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 1);

// 1
// ^

result = Lexer_peekToken(&lexer, 0);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 1);

// 1
// ^

result = Lexer_peekToken(&lexer, 1);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 2);

// 1 2
// ^

result = Lexer_nextToken(&lexer);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 2);

// 1 2
// ^

result = Lexer_peekToken(&lexer, -1);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 1);

// 1 2
// ^

result = Lexer_peekToken(&lexer, 0);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 2);

// 1 2
// ^

result = Lexer_peekToken(&lexer, 1);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 3);

// 1 2 3
// ^

result = Lexer_nextToken(&lexer);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 3);

// 1 2 3
// ^

result = Lexer_peekToken(&lexer, 0);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 3);

// 1 2 3
// ^

result = Lexer_nextToken(&lexer);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 4);

// 1 2 3 4
// ^

result = Lexer_peekToken(&lexer, -1);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 3);

// 1 2 3 4
// ^

result = Lexer_peekToken(&lexer, 0);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 4);

// 1 2 3 4

result = Lexer_nextToken(&lexer);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 5);

// 1 2 3 4 5
// ^

result = Lexer_nextToken(&lexer);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 6);

// 1 2 3 4 5 6
// ^

result = Lexer_peekToken(&lexer, -1);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 5);

// 1 2 3 4 5 6
// ^

result = Lexer_peekToken(&lexer, 0);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 6);

// 1 2 3 4 5 6
// ^

result = Lexer_peekToken(&lexer, 2);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 8);

// 1 2 3 4 5 6 7 8
// ^

result = Lexer_nextToken(&lexer);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 7);

// 1 2 3 4 5 6 7 8
// ^

result = Lexer_nextToken(&lexer);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 8);

// 1 2 3 4 5 6 7 8
// ^

result = Lexer_peekToken(&lexer, -1);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_LITERAL);
EXPECT_TRUE(result.token->kind == TOKEN_INTEGER);
EXPECT_EQUAL_INT(result.token->value.integer, 7);

// 1 2 3 4 5 6 7 8
// ^

result = Lexer_nextToken(&lexer);
EXPECT_TRUE(result.success);
EXPECT_TRUE(result.token->type == TOKEN_EOF);

} TEST_END();

TEST_BEGIN("Tokenization of the complex source") {
Lexer_setSource(&lexer, "var myVar = myFunc(/* some params */);");

Expand Down

0 comments on commit b2a655e

Please sign in to comment.