Move all tests to test/ improve Checker tests

This commit is contained in:
Sam Vervaeck 2023-06-06 17:28:56 +02:00
parent 0326bbe7f9
commit abae39c791
Signed by: samvv
SSH key fingerprint: SHA256:dIg0ywU1OP+ZYifrYxy8c5esO72cIKB+4/9wkZj1VaY
8 changed files with 77 additions and 45 deletions

View file

@ -63,8 +63,8 @@ if (BOLT_ENABLE_TESTS)
add_subdirectory(deps/googletest EXCLUDE_FROM_ALL)
add_executable(
alltests
src/TestText.cc
src/TestChecker.cc
test/TestText.cc
test/TestChecker.cc
)
target_link_libraries(
alltests

View file

@ -54,6 +54,10 @@ namespace bolt {
Diagnostics.clear();
}
std::size_t countDiagnostics() const noexcept {
return Diagnostics.size();
}
~DiagnosticStore();
};

View file

@ -99,6 +99,14 @@ namespace bolt {
inline UnificationErrorDiagnostic(Type* OrigLeft, Type* OrigRight, TypePath LeftPath, TypePath RightPath, Node* Source):
Diagnostic(DiagnosticKind::UnificationError), OrigLeft(OrigLeft), OrigRight(OrigRight), LeftPath(LeftPath), RightPath(RightPath), Source(Source) {}
inline Type* getLeft() const {
return OrigLeft->resolve(LeftPath);
}
inline Type* getRight() const {
return OrigRight->resolve(RightPath);
}
inline Node* getNode() const override {
return Source;
}

View file

@ -288,9 +288,9 @@ namespace bolt {
}
DiagnosticStore::~DiagnosticStore() {
for (auto D: Diagnostics) {
delete D;
}
// for (auto D: Diagnostics) {
// delete D;
// }
}
ConsoleDiagnostics::ConsoleDiagnostics(std::ostream& Out):

View file

@ -1,33 +0,0 @@
#include "gtest/gtest.h"
#include "bolt/CST.hpp"
#include "bolt/Diagnostics.hpp"
#include "bolt/Scanner.hpp"
#include "bolt/Parser.hpp"
#include "bolt/Checker.hpp"
using namespace bolt;
auto checkExpression(std::string Input) {
ConsoleDiagnostics DS;
TextFile T { "#<anonymous>", Input };
VectorStream<std::string, Char> Chars { Input, EOF };
Scanner S(T, Chars);
Punctuator PT(S);
Parser P(T, PT);
LanguageConfig Config;
auto SF = P.parseSourceFile();
Checker C(Config, DS);
C.check(SF);
return std::make_tuple(
static_cast<ExpressionStatement*>(SF->Elements[0])->Expression,
C
);
}
TEST(CheckerTest, InfersIntFromIntegerLiteral) {
auto [Expression, Checker] = checkExpression("1");
ASSERT_EQ(Checker.getType(Expression), Checker.getIntType());
}

View file

@ -103,7 +103,7 @@ int main(int Argc, const char* Argv[]) {
std::sort(DS.Diagnostics.begin(), DS.Diagnostics.end(), lessThan);
for (auto D: DS.Diagnostics) {
DE.printDiagnostic(*D);
DE.addDiagnostic(D);
}
if (DE.hasError()) {

53
test/TestChecker.cc Normal file
View file

@ -0,0 +1,53 @@
#include "gtest/gtest.h"
#include "bolt/CST.hpp"
#include "bolt/Diagnostics.hpp"
#include "bolt/DiagnosticEngine.hpp"
#include "bolt/Scanner.hpp"
#include "bolt/Parser.hpp"
#include "bolt/Checker.hpp"
using namespace bolt;
auto checkSourceFile(std::string Input) {
DiagnosticStore DS;
TextFile T { "#<anonymous>", Input };
VectorStream<std::string, Char> Chars { Input, EOF };
Scanner S(T, Chars);
Punctuator PT(S);
Parser P(T, PT, DS);
LanguageConfig Config;
auto SF = P.parseSourceFile();
SF->setParents();
Checker C(Config, DS);
C.check(SF);
return std::make_tuple(SF, C, DS);
}
auto checkExpression(std::string Input) {
auto [SF, C, DS] = checkSourceFile(Input);
return std::make_tuple(
static_cast<ExpressionStatement*>(SF->Elements[0])->Expression,
C,
DS
);
}
TEST(CheckerTest, InfersIntFromIntegerLiteral) {
auto [Expr, Checker, DS] = checkExpression("1");
ASSERT_EQ(DS.countDiagnostics(), 0);
ASSERT_EQ(Checker.getType(Expr), Checker.getIntType());
}
TEST(CheckerTest, TestIllegalTypingVariable) {
auto [SF, C, DS] = checkSourceFile("let a: Int = \"foo\"");
ASSERT_EQ(DS.countDiagnostics(), 1);
auto D1 = DS.Diagnostics[0];
ASSERT_EQ(D1->getKind(), DiagnosticKind::UnificationError);
auto Diag = static_cast<UnificationErrorDiagnostic*>(D1);
// TODO these types have to be sorted first
ASSERT_EQ(Diag->getLeft(), C.getIntType());
ASSERT_EQ(Diag->getRight(), C.getStringType());
}

View file

@ -1,7 +1,7 @@
#include "gtest/gtest.h"
#include "bolt/Text.hpp"
#include "bolt/CST.hpp"
using namespace bolt;
@ -23,11 +23,11 @@ TEST(TextFileTest, ReportsCorrectLine) {
TEST(TextFileTest, ReportsCorrectStartOffset) {
TextFile T1 { "foo.txt", "bar\nbaz\nbax\n" };
ASSERT_EQ(T1.getStartOffset(1), 0);
ASSERT_EQ(T1.getStartOffset(2), 4);
ASSERT_EQ(T1.getStartOffset(3), 8);
ASSERT_EQ(T1.getStartOffset(4), 12);
ASSERT_EQ(T1.getStartOffset(5), 12);
ASSERT_EQ(T1.getStartOffsetOfLine(1), 0);
ASSERT_EQ(T1.getStartOffsetOfLine(2), 4);
ASSERT_EQ(T1.getStartOffsetOfLine(3), 8);
ASSERT_EQ(T1.getStartOffsetOfLine(4), 12);
ASSERT_EQ(T1.getStartOffsetOfLine(5), 12);
}
TEST(TextFileTest, ReportsCorrectColumn) {