From 6171d2d17e1e7a6808a2b34bc355d4a38fe2f913 Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Thu, 1 Apr 2021 15:30:56 +0200 Subject: [PATCH] Added unit test --- source/tlang/compiler/typecheck/core.d | 104 +++++++++++++++++++++---- 1 file changed, 90 insertions(+), 14 deletions(-) diff --git a/source/tlang/compiler/typecheck/core.d b/source/tlang/compiler/typecheck/core.d index 7281616..06c0109 100644 --- a/source/tlang/compiler/typecheck/core.d +++ b/source/tlang/compiler/typecheck/core.d @@ -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); } \ No newline at end of file