Added unit test

parser.sync-conflict-20210405-185821-O3W7KWN
Tristan B. V. Kildaire 2021-04-01 15:30:56 +02:00
parent a89b82c423
commit 6171d2d17e
1 changed files with 90 additions and 14 deletions

View File

@ -455,6 +455,63 @@ public final class TypeChecker
}
/* Test name colliding with container name (1/2) */
unittest
{
import std.file;
import std.stdio;
import compiler.lexer;
import compiler.parsing.core;
string sourceFile = "source/tlang/testing/collide_container.t";
File sourceFileFile;
sourceFileFile.open(sourceFile); /* TODO: Error handling with ANY file I/O */
ulong fileSize = sourceFileFile.size();
byte[] fileBytes;
fileBytes.length = fileSize;
fileBytes = sourceFileFile.rawRead(fileBytes);
sourceFileFile.close();
// gprintln("Performing tokenization on '"~sourceFile~"' ...");
/* TODO: Open source file */
string sourceCode = cast(string)fileBytes;
// string sourceCode = "hello \"world\"|| ";
//string sourceCode = "hello \"world\"||"; /* TODO: Implement this one */
// string sourceCode = "hello;";
Lexer currentLexer = new Lexer(sourceCode);
bool status = currentLexer.performLex();
Parser parser = new Parser(currentLexer.getTokens());
Module modulle = parser.parse();
gprintln("Type checking and symbol resolution...");
try
{
TypeChecker typeChecker = new TypeChecker(modulle);
assert(false);
}
catch(CollidingNameException e)
{
assert(true);
//gprintln("Stack trace:\n"~to!(string)(e.info));
}
}
/* Test name colliding with container name (2/2) */
unittest
{
}
unittest
{
/* TODO: Add some unit tests */
@ -467,6 +524,7 @@ unittest
string sourceFile = "source/tlang/testing/basic1.t";
gprintln("Reading source file '"~sourceFile~"' ...");
File sourceFileFile;
sourceFileFile.open(sourceFile); /* TODO: Error handling with ANY file I/O */
ulong fileSize = sourceFileFile.size();
@ -475,7 +533,7 @@ unittest
fileBytes = sourceFileFile.rawRead(fileBytes);
sourceFileFile.close();
gprintln("Performing tokenization on '"~sourceFile~"' ...");
/* TODO: Open source file */
string sourceCode = cast(string)fileBytes;
@ -483,27 +541,45 @@ unittest
//string sourceCode = "hello \"world\"||"; /* TODO: Implement this one */
// string sourceCode = "hello;";
Lexer currentLexer = new Lexer(sourceCode);
currentLexer.performLex();
bool status = currentLexer.performLex();
if(!status)
{
return;
}
Parser parser = new Parser(currentLexer.getTokens());
gprintln("Collected "~to!(string)(currentLexer.getTokens()));
gprintln("Parsing tokens...");
Parser parser = new Parser(currentLexer.getTokens());
Module modulle = parser.parse();
TypeChecker typeChecker = new TypeChecker(modulle);
typeChecker.check();
gprintln("Type checking and symbol resolution...");
try
{
TypeChecker typeChecker = new TypeChecker(modulle);
}
// catch(CollidingNameException e)
// {
// gprintln(e.msg, DebugType.ERROR);
// //gprintln("Stack trace:\n"~to!(string)(e.info));
// }
catch(TypeCheckerException e)
{
gprintln(e.msg, DebugType.ERROR);
}
/* Test first-level resolution */
assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz1").getName(), "clazz1")==0);
// assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz1").getName(), "clazz1")==0);
/* Test n-level resolution */
assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2").getName(), "clazz_2_2")==0);
assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2.j").getName(), "j")==0);
assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2.clazz_2_2_1").getName(), "clazz_2_2_1")==0);
assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2").getName(), "clazz_2_2")==0);
// /* Test n-level resolution */
// assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2").getName(), "clazz_2_2")==0);
// assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2.j").getName(), "j")==0);
// assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2.clazz_2_2_1").getName(), "clazz_2_2_1")==0);
// assert(cmp(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2").getName(), "clazz_2_2")==0);
/* Test invalid access to j treating it as a Container (whilst it is a Variable) */
assert(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2.j.p") is null);
// /* Test invalid access to j treating it as a Container (whilst it is a Variable) */
// assert(typeChecker.isValidEntity(modulle.getStatements(), "clazz_2_1.clazz_2_2.j.p") is null);
}