Don't get testy with me

This commit is contained in:
2022-08-29 12:23:48 +10:00
parent 1937d727bb
commit 61efb96fe2
4 changed files with 184 additions and 0 deletions

View File

@@ -23,3 +23,4 @@ bool setScopeVariable(Scope* scope, Literal key, Literal value, bool constCheck)
bool getScopeVariable(Scope* scope, Literal key, Literal* value); bool getScopeVariable(Scope* scope, Literal key, Literal* value);
Literal getScopeType(Scope* scope, Literal key); Literal getScopeType(Scope* scope, Literal key);

View File

@@ -0,0 +1,38 @@
#include "literal_dictionary.h"
#include "memory.h"
#include "console_colors.h"
#include <stdio.h>
int main() {
{
//test init & cleanup
LiteralDictionary dictionary;
initLiteralDictionary(&dictionary);
freeLiteralDictionary(&dictionary);
}
{
//test insertion and deletion
char* idn_raw = "foobar";
char* str_raw = "hello world";
Literal identifier = TO_IDENTIFIER_LITERAL(copyString(idn_raw, strlen(idn_raw)), strlen(idn_raw));
Literal string = TO_STRING_LITERAL(copyString(str_raw, strlen(str_raw)), strlen(str_raw));
LiteralDictionary dictionary;
initLiteralDictionary(&dictionary);
setLiteralDictionary(&dictionary, identifier, string);
freeLiteral(identifier);
freeLiteral(string);
freeLiteralDictionary(&dictionary);
}
printf(NOTICE "All good\n" RESET);
return 0;
}

21
test/test_node.c Normal file
View File

@@ -0,0 +1,21 @@
#include "node.h"
#include "memory.h"
#include "console_colors.h"
#include <stdio.h>
int main() {
{
//test literals
char* str = "foobar";
Node* node;
emitNodeLiteral(&node, TO_STRING_LITERAL(copyString(str, strlen(str)), strlen(str)) );
freeNode(node);
}
printf(NOTICE "All good\n" RESET);
return 0;
}

124
test/test_scope.c Normal file
View File

@@ -0,0 +1,124 @@
#include "scope.h"
#include "memory.h"
#include "console_colors.h"
#include <stdio.h>
int main() {
{
//test init & quit
Scope* scope = pushScope(NULL);
scope = popScope(scope);
}
{
//prerequisites
char* idn_raw = "foobar";
Literal identifier = TO_IDENTIFIER_LITERAL(copyString(idn_raw, strlen(idn_raw)), strlen(idn_raw));
Literal value = TO_INTEGER_LITERAL(42);
Literal type = TO_TYPE_LITERAL(value.type, false);
//test declarations & assignments
Scope* scope = pushScope(NULL);
//declare & assign
if (!declareScopeVariable(scope, identifier, type)) {
printf(ERROR "Failed to declare scope variable" RESET);
return -1;
}
if (!setScopeVariable(scope, identifier, value, true)) {
printf(ERROR "Failed to sete scope variable" RESET);
return -1;
}
//cleanup
scope = popScope(scope);
freeLiteral(identifier);
freeLiteral(value);
freeLiteral(type);
}
{
//prerequisites
char* idn_raw = "foobar";
Literal identifier = TO_IDENTIFIER_LITERAL(copyString(idn_raw, strlen(idn_raw)), strlen(idn_raw));
Literal type = TO_TYPE_LITERAL(LITERAL_INTEGER, false);
//test declarations & assignments
Scope* scope = pushScope(NULL);
//declare & assign
if (!declareScopeVariable(scope, identifier, type)) {
printf(ERROR "Failed to declare the scope variable" RESET);
return -1;
}
if (!setScopeVariable(scope, identifier, TO_INTEGER_LITERAL(42), true)) {
printf(ERROR "Failed to set the scope variable" RESET);
return -1;
}
//deeper scope
scope = pushScope(scope);
//test shadowing
Literal ref;
if (!getScopeVariable(scope, identifier, &ref)) {
printf(ERROR "Failed to get the scope variable" RESET);
return -1;
}
if (AS_INTEGER(ref) != 42) {
printf(ERROR "Failed to retreive the correct variable value" RESET);
return -1;
}
if (!declareScopeVariable(scope, identifier, type)) {
printf(ERROR "Failed to declare the scope variable (shadowing)" RESET);
return -1;
}
if (!setScopeVariable(scope, identifier, TO_INTEGER_LITERAL(69), true)) {
printf(ERROR "Failed to set the scope variable (shadowing)" RESET);
return -1;
}
if (!getScopeVariable(scope, identifier, &ref)) {
printf(ERROR "Failed to get the scope variable (shadowing)" RESET);
return -1;
}
if (AS_INTEGER(ref) != 69) {
printf(ERROR "Failed to retreive the correct variable value (shadowing)" RESET);
return -1;
}
//unwind
scope = popScope(scope);
if (!getScopeVariable(scope, identifier, &ref)) {
printf(ERROR "Failed to get the scope variable" RESET);
return -1;
}
if (AS_INTEGER(ref) != 42) {
printf(ERROR "Failed to retreive the correct variable value" RESET);
return -1;
}
//cleanup
scope = popScope(scope);
freeLiteral(identifier);
freeLiteral(type);
}
printf(NOTICE "All good\n" RESET);
return 0;
}