Skip to content

Commit

Permalink
Finished example parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
loumadev committed Nov 14, 2023
1 parent d11c595 commit 1b55518
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/compiler/parser/Parser.test.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <stdio.h>

#include "unit.h"
#include "parser_assertions.h"

#include "compiler/lexer/Lexer.h"
#include "compiler/parser/Parser.h"
#include "compiler/parser/ASTNodes.h"


DESCRIBE(variable_declaration, "Variable declaration parsing") {
Expand All @@ -19,5 +22,33 @@ DESCRIBE(variable_declaration, "Variable declaration parsing") {
result = Parser_parse(&parser);

EXPECT_TRUE(result.success);
EXPECT_STATEMENT(result.node, NODE_VARIABLE_DECLARATION);

VariableDeclarationASTNode *declaration = (VariableDeclarationASTNode*)statement;
EXPECT_TRUE(declaration->isConstant);
EXPECT_NOT_NULL(declaration->declaratorList);

VariableDeclarationListASTNode *list = declaration->declaratorList;
EXPECT_NOT_NULL(list->declarators);

Array *arr = list->declarators;
EXPECT_EQUAL_INT(arr->size, 1);

VariableDeclaratorASTNode *declarator = Array_get(arr, 0);
EXPECT_NOT_NULL(declarator);

PatternASTNode *pattern = declarator->pattern;
EXPECT_NOT_NULL(pattern);
EXPECT_NULL(pattern->type);

IdentifierASTNode *id = pattern->id;
EXPECT_NOT_NULL(id);
EXPECT_TRUE(String_equals(id->name, "a"));

LiteralExpressionASTNode *initializer = (LiteralExpressionASTNode*)declarator->initializer;
EXPECT_NOT_NULL(initializer);
EXPECT_TRUE(initializer->_type == NODE_LITERAL_EXPRESSION);
EXPECT_TRUE(initializer->type == LITERAL_INTEGER);
EXPECT_EQUAL_INT(initializer->value.integer, 7);
} TEST_END();
}

0 comments on commit 1b55518

Please sign in to comment.