Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions co_sim_io/impl/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,59 @@ the code where the CoSimIO is included

#ifndef CO_SIM_IO_ERROR
#include <iostream>
#include <string>
#include <stdexcept>
struct err { // helper struct to mimic behavior of KRATOS_ERROR
~err() noexcept(false) { throw std::runtime_error("Error: "); } // destructors are noexcept by default
#include <sstream>

namespace CoSimIO {

// Simplified version of kratos/includes/exception.h
class Exception : public std::exception
{
public:
explicit Exception(const std::string& rWhat) : std::exception(), mMessage(rWhat) { }

const char* what() const noexcept override
{
return mMessage.c_str();
}

/// string stream function
template<class StreamValueType>
Exception& operator << (StreamValueType const& rValue)
{
std::stringstream buffer;
buffer << rValue;

mMessage.append(buffer.str());

return *this;
}

Exception& operator << (std::ostream& (*pf)(std::ostream&))
{
std::stringstream buffer;
pf(buffer);

mMessage.append(buffer.str());

return *this;
}

Exception& operator << (const char* pString)
{
mMessage.append(pString);
return *this;
}

private:
std::string mMessage;

};
#define CO_SIM_IO_ERROR (err(), std::cout)

} // namespace CoSimIO

#define CO_SIM_IO_ERROR throw CoSimIO::Exception("Error: ")
#endif

#ifndef CO_SIM_IO_ERROR_IF
Expand All @@ -45,4 +93,4 @@ the code where the CoSimIO is included
#define CO_SIM_IO_INFO_IF(label, conditional) if (conditional) CO_SIM_IO_INFO(label)
#endif

#endif // CO_SIM_IO_MACROS_INCLUDED
#endif // CO_SIM_IO_MACROS_INCLUDED
4 changes: 2 additions & 2 deletions tests/co_sim_io/impl/test_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ TEST_CASE("info_non_existing_key")

CHECK_UNARY_FALSE(info.Has("identifier"));

CHECK_THROWS_WITH(info.Get<int>("identifier"), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(info.Get<int>("identifier"), "Error: Trying to get \"identifier\" which does not exist!\n");
}

TEST_CASE("info_wrong_type")
Expand All @@ -152,7 +152,7 @@ TEST_CASE("info_wrong_type")
info.Set<std::string>("identifier", "pressure");

CHECK_UNARY(info.Has("identifier"));
CHECK_THROWS_WITH(info.Get<int>("identifier"), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(info.Get<int>("identifier"), "Error: Wrong DataType! Trying to get \"identifier\" which is of type \"string\" with \"int\"!\n");
}

TEST_CASE("info_many_values")
Expand Down
20 changes: 10 additions & 10 deletions tests/co_sim_io/impl/test_model_part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ TEST_CASE("node_negative_id")

SUBCASE("from_coords")
{
CHECK_THROWS_WITH(Node(id, coords[0], coords[1], coords[2]), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(Node(id, coords[0], coords[1], coords[2]), "Error: Id must be >= 1!\n");
}

SUBCASE("from_coords_array")
{
CHECK_THROWS_WITH(Node(id, coords), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(Node(id, coords), "Error: Id must be >= 1!\n");
}
}

Expand Down Expand Up @@ -102,12 +102,12 @@ TEST_CASE("element_checks")

SUBCASE("negative_id")
{
CHECK_THROWS_WITH(Element element(id, type, {&node}), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(Element element(id, type, {&node}), "Error: Id must be >= 1!\n");
}

SUBCASE("no_nodes")
{
CHECK_THROWS_WITH(Element element(1, type, {}), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(Element element(1, type, {}), "Error: No nodes were passed!\n");
}
}

Expand Down Expand Up @@ -166,8 +166,8 @@ TEST_CASE("model_part_basics")

TEST_CASE("model_part_invalid_names")
{
CHECK_THROWS_WITH(ModelPart model_part(""), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(ModelPart model_part("my_name.ssss"), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(ModelPart model_part(""), "Error: Please don't use empty names (\"\") when creating a ModelPart\n");
CHECK_THROWS_WITH(ModelPart model_part("my_name.ssss"), "Error: Please don't use names containing (\".\") when creating a ModelPart (used in \"my_name.ssss\")\n");
}

TEST_CASE("model_part_create_new_node")
Expand Down Expand Up @@ -213,7 +213,7 @@ TEST_CASE("model_part_create_new_node_twice")
model_part.CreateNewNode(1, 0,0,0);
REQUIRE_EQ(model_part.NumberOfNodes(), 1);

CHECK_THROWS_WITH(model_part.CreateNewNode(1, 0,0,0), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(model_part.CreateNewNode(1, 0,0,0), "Error: The Node with Id 1 exists already!\n");
}

TEST_CASE("model_part_get_node")
Expand All @@ -240,7 +240,7 @@ TEST_CASE("model_part_get_node")

SUBCASE("non_existing")
{
CHECK_THROWS_WITH(model_part.GetNode(node_id+1), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(model_part.GetNode(node_id+1), "Error: Node with Id 692 does not exist!\n");
}
}

Expand Down Expand Up @@ -310,7 +310,7 @@ TEST_CASE("model_part_create_new_element_twice")
model_part.CreateNewElement(1, 5, {1});
REQUIRE_EQ(model_part.NumberOfElements(), 1);

CHECK_THROWS_WITH(model_part.CreateNewElement(1, 5, {1}), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(model_part.CreateNewElement(1, 5, {1}), "Error: The Element with Id 1 exists already!\n");
}

TEST_CASE("model_part_get_element")
Expand All @@ -334,7 +334,7 @@ TEST_CASE("model_part_get_element")

SUBCASE("non_existing")
{
CHECK_THROWS_WITH(model_part.GetElement(elem_id+1), "Error: "); // TODO find a better way of testing this
CHECK_THROWS_WITH(model_part.GetElement(elem_id+1), "Error: Element with Id 7 does not exist!\n");
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/co_sim_io/python/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_non_existing_key(self):

self.assertFalse(info.Has("identifier"))

with self.assertRaisesRegex(Exception, 'Error: '): # TODO improve this once errors are working better
with self.assertRaisesRegex(Exception, 'Error: Trying to get "identifier" which does not exist!'):
info.GetBool("identifier")

def test_wrong_type(self):
Expand All @@ -119,7 +119,7 @@ def test_wrong_type(self):

self.assertTrue(info.Has("identifier"))

with self.assertRaisesRegex(Exception, 'Error: '): # TODO improve this once errors are working better
with self.assertRaisesRegex(Exception, 'Error: Wrong DataType! Trying to get "identifier" which is of type "string" with "bool"!'):
info.GetBool("identifier")

def test_many_values(self):
Expand Down