Skip to content

Commit

Permalink
test exception
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Nov 6, 2024
1 parent 225cd63 commit 8405111
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/solver/modelConverter/convertorVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ std::any ConvertorVisitor::visit(antlr4::tree::ParseTree* tree)
class NoParameterOrVariableWithThisName: public std::exception
{
public:
NoParameterOrVariableWithThisName(std::string id):
id_(id)
NoParameterOrVariableWithThisName(const std::string& id)
{
msg_ = "No parameter or variable found for this identifier: " + id;
}

const char* what() const noexcept override
{
return ("No parameter or variable found for this identifier: " + id_).c_str();
return msg_.c_str();
}

private:
std::string id_ = "";
std::string msg_;
};

std::any ConvertorVisitor::visitIdentifier(ExprParser::IdentifierContext* context)
Expand Down
31 changes: 31 additions & 0 deletions src/tests/src/solver/modelParser/testConvertorVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ BOOST_FIXTURE_TEST_CASE(identifier, Fixture)
std::string expression = "param1";
auto* n = ModelConverter::convertExpressionToNode(expression, registry, model0);
BOOST_CHECK_EQUAL(n->name(), "ParameterNode");

expression = "varP";
n = ModelConverter::convertExpressionToNode(expression, registry, model0);
BOOST_CHECK_EQUAL(n->name(), "VariableNode");
}

bool expectedMessage(const std::exception& ex)
{
BOOST_CHECK_EQUAL(ex.what(),
std::string("No parameter or variable found for this identifier: abc"));
return true;
}

BOOST_FIXTURE_TEST_CASE(identifierNotFound, Fixture)
{
ModelParser::Library library;
ModelParser::Model model0{
.id = "model0",
.description = "description",
.parameters = {{"param1", true, false}, {"param2", false, false}},
.variables = {{"varP", "7", "pmin", ModelParser::ValueType::CONTINUOUS},
{"var2", "0", "param2", ModelParser::ValueType::CONTINUOUS}},
.ports = {},
.port_field_definitions = {},
.constraints = {},
.objective = "objectives"};

std::string expression = "abc"; // not a param or var
BOOST_CHECK_EXCEPTION(ModelConverter::convertExpressionToNode(expression, registry, model0),
std::exception,
expectedMessage);
}

BOOST_FIXTURE_TEST_CASE(AddSub, Fixture)
Expand Down

0 comments on commit 8405111

Please sign in to comment.