Skip to content
Snippets Groups Projects
Commit 07e4640d authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add missing tests

Also improve existing error tests (check for error message instead of
just exception throw)
parent a73b442e
No related branches found
No related tags found
1 merge request!89Add missing compatibility check when affecting lists to R^d or R^dxd
...@@ -77,10 +77,12 @@ ASTSymbolTableBuilder::buildSymbolTable(ASTNode& n, std::shared_ptr<SymbolTable> ...@@ -77,10 +77,12 @@ ASTSymbolTableBuilder::buildSymbolTable(ASTNode& n, std::shared_ptr<SymbolTable>
} else if (n.is_type<language::function_definition>()) { } else if (n.is_type<language::function_definition>()) {
auto register_and_initialize_symbol = [&](const ASTNode& argument_node) { auto register_and_initialize_symbol = [&](const ASTNode& argument_node) {
if (symbol_table->getBuiltinFunctionSymbolList(argument_node.string(), argument_node.begin()).size() > 0) { if (symbol_table->getBuiltinFunctionSymbolList(argument_node.string(), argument_node.begin()).size() > 0) {
// LCOV_EXCL_START
std::ostringstream error_message; std::ostringstream error_message;
error_message << "symbol '" << rang::fg::red << argument_node.string() << rang::fg::reset error_message << "symbol '" << rang::fg::red << argument_node.string() << rang::fg::reset
<< "' already denotes a builtin function!"; << "' already denotes a builtin function!";
throw ParseError(error_message.str(), std::vector{argument_node.begin()}); throw ParseError(error_message.str(), std::vector{argument_node.begin()});
// LCOV_EXCL_STOP
} }
auto [i_symbol, success] = symbol_table->add(argument_node.string(), argument_node.begin()); auto [i_symbol, success] = symbol_table->add(argument_node.string(), argument_node.begin());
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <catch2/matchers/catch_matchers_all.hpp> #include <catch2/matchers/catch_matchers_all.hpp>
#include <language/ast/ASTBuilder.hpp> #include <language/ast/ASTBuilder.hpp>
#include <language/ast/ASTModulesImporter.hpp>
#include <language/ast/ASTSymbolTableBuilder.hpp> #include <language/ast/ASTSymbolTableBuilder.hpp>
#include <language/utils/ParseError.hpp> #include <language/utils/ParseError.hpp>
...@@ -78,7 +79,7 @@ let n:N, n = a; ...@@ -78,7 +79,7 @@ let n:N, n = a;
string_input input{data, "test.pgs"}; string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input); auto ast = ASTBuilder::build(input);
REQUIRE_THROWS_AS(ASTSymbolTableBuilder{*ast}, ParseError); REQUIRE_THROWS_WITH(ASTSymbolTableBuilder{*ast}, "undefined symbol 'a'");
} }
SECTION("Re-declared symbol") SECTION("Re-declared symbol")
...@@ -91,7 +92,7 @@ let n:N, n = 1; ...@@ -91,7 +92,7 @@ let n:N, n = 1;
string_input input{data, "test.pgs"}; string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input); auto ast = ASTBuilder::build(input);
REQUIRE_THROWS_AS(ASTSymbolTableBuilder{*ast}, ParseError); REQUIRE_THROWS_WITH(ASTSymbolTableBuilder{*ast}, "symbol 'n' was already defined!");
} }
SECTION("Re-declared symbol (function)") SECTION("Re-declared symbol (function)")
...@@ -104,7 +105,36 @@ let f : R -> R, x -> 1; ...@@ -104,7 +105,36 @@ let f : R -> R, x -> 1;
string_input input{data, "test.pgs"}; string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input); auto ast = ASTBuilder::build(input);
REQUIRE_THROWS_AS(ASTSymbolTableBuilder{*ast}, ParseError); REQUIRE_THROWS_WITH(ASTSymbolTableBuilder{*ast}, "symbol 'f' was already defined!");
}
SECTION("Re-declared symbol (builtin function)")
{
std::string_view data = R"(
import math;
let cos:N;
)";
string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input);
ASTModulesImporter{*ast};
REQUIRE_THROWS_WITH(ASTSymbolTableBuilder{*ast}, "symbol 'cos' already denotes a builtin function!");
}
SECTION("Re-declared symbol (builtin function) 2")
{
std::string_view data = R"(
import math;
let cos: R -> R, x->2*x;
)";
string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input);
ASTModulesImporter{*ast};
// REQUIRE_THROWS_AS(ASTSymbolTableBuilder{*ast}, ParseError);
REQUIRE_THROWS_WITH(ASTSymbolTableBuilder{*ast}, "symbol 'cos' already denotes a builtin function!");
} }
SECTION("Re-declared parameter (function)") SECTION("Re-declared parameter (function)")
...@@ -116,7 +146,7 @@ let f : R*R*N -> R, (x,y,x) -> 1; ...@@ -116,7 +146,7 @@ let f : R*R*N -> R, (x,y,x) -> 1;
string_input input{data, "test.pgs"}; string_input input{data, "test.pgs"};
auto ast = ASTBuilder::build(input); auto ast = ASTBuilder::build(input);
REQUIRE_THROWS_AS(ASTSymbolTableBuilder{*ast}, ParseError); REQUIRE_THROWS_WITH(ASTSymbolTableBuilder{*ast}, "symbol 'x' was already defined!");
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment