From 0e79bc77ed288e3f5d15f4fa9a7be3808cbf475c Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Thu, 1 Apr 2021 21:08:37 +0200 Subject: [PATCH] Added member collision unit test --- source/tlang/compiler/typecheck/core.d | 44 ++++++++++++++++++++ source/tlang/compiler/typecheck/exceptions.d | 2 +- source/tlang/testing/collide_member.t | 16 ++++--- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/source/tlang/compiler/typecheck/core.d b/source/tlang/compiler/typecheck/core.d index ba31151..b828af0 100644 --- a/source/tlang/compiler/typecheck/core.d +++ b/source/tlang/compiler/typecheck/core.d @@ -568,6 +568,50 @@ unittest } } +/* Test name colliding with member */ +unittest +{ + import std.file; + import std.stdio; + import compiler.lexer; + import compiler.parsing.core; + + string sourceFile = "source/tlang/testing/collide_member.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(); + + string sourceCode = cast(string) fileBytes; + Lexer currentLexer = new Lexer(sourceCode); + currentLexer.performLex(); + + Parser parser = new Parser(currentLexer.getTokens()); + Module modulle = parser.parse(); + TypeChecker typeChecker = new TypeChecker(modulle); + + /* Setup testing variables */ + Entity memberFirst = typeChecker.getResolver().resolveBest(typeChecker.getModule, "a.b"); + + try + { + /* Perform test */ + typeChecker.beginCheck(); + + /* Shouldn't reach here, collision exception MUST occur */ + assert(false); + } + catch (CollidingNameException e) + { + /* Make sure the member a.b.c.c collided with a.b.c container */ + assert(e.attempted != memberFirst); + } +} + /* Test name colliding with container name (1/2) */ unittest diff --git a/source/tlang/compiler/typecheck/exceptions.d b/source/tlang/compiler/typecheck/exceptions.d index b691121..4f85b47 100644 --- a/source/tlang/compiler/typecheck/exceptions.d +++ b/source/tlang/compiler/typecheck/exceptions.d @@ -27,7 +27,7 @@ public final class CollidingNameException : TypeCheckerException /** * The colliding Entity */ - private Entity attempted; + public Entity attempted; /** * The Container we are in diff --git a/source/tlang/testing/collide_member.t b/source/tlang/testing/collide_member.t index fead7ce..c1de4fa 100644 --- a/source/tlang/testing/collide_member.t +++ b/source/tlang/testing/collide_member.t @@ -1,8 +1,14 @@ -module x; +module y; -int y; - -void y() +class a { - + class b + { + + } + + class b + { + + } } \ No newline at end of file