From 6d4bb8b0057f55222c5a82e3c35ae7031f219ceb Mon Sep 17 00:00:00 2001 From: philbucher Date: Thu, 17 Sep 2020 10:04:15 +0200 Subject: [PATCH 1/4] updating info error checks --- tests/co_sim_io/impl/test_info.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/co_sim_io/impl/test_info.cpp b/tests/co_sim_io/impl/test_info.cpp index fbc49a37..d7556916 100644 --- a/tests/co_sim_io/impl/test_info.cpp +++ b/tests/co_sim_io/impl/test_info.cpp @@ -140,7 +140,7 @@ TEST_CASE("info_non_existing_key") CHECK_UNARY_FALSE(info.Has("identifier")); - CHECK_THROWS_WITH(info.Get("identifier"), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(info.Get("identifier"), "Error: Trying to get \"identifier\" which does not exist!"); } TEST_CASE("info_wrong_type") @@ -152,7 +152,7 @@ TEST_CASE("info_wrong_type") info.Set("identifier", "pressure"); CHECK_UNARY(info.Has("identifier")); - CHECK_THROWS_WITH(info.Get("identifier"), "Error: "); // TODO find a better way of testing this + CHECK_THROWS_WITH(info.Get("identifier"), "Error: Wrong DataType! Trying to get \"identifier\" which is of type \"string\" with \"int\"!"); } TEST_CASE("info_many_values") From 60b9bc7e3ca975f814faa2990544562cc14e10ff Mon Sep 17 00:00:00 2001 From: philbucher Date: Thu, 17 Sep 2020 10:36:13 +0200 Subject: [PATCH 2/4] First version of Exception working --- co_sim_io/impl/macros.hpp | 53 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/co_sim_io/impl/macros.hpp b/co_sim_io/impl/macros.hpp index e2ec81e2..6e49263f 100644 --- a/co_sim_io/impl/macros.hpp +++ b/co_sim_io/impl/macros.hpp @@ -21,11 +21,58 @@ the code where the CoSimIO is included #ifndef CO_SIM_IO_ERROR #include + #include #include - struct err { // helper struct to mimic behavior of KRATOS_ERROR - ~err() noexcept(false) { throw std::runtime_error("Error: "); } // destructors are noexcept by default + #include + + namespace CoSimIO { + + 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 + 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 From 7b044b08f8914b30434b7cbeeb7dffaee6523a28 Mon Sep 17 00:00:00 2001 From: philbucher Date: Thu, 17 Sep 2020 12:43:13 +0200 Subject: [PATCH 3/4] updated tests --- co_sim_io/impl/macros.hpp | 2 +- tests/co_sim_io/impl/test_info.cpp | 4 ++-- tests/co_sim_io/impl/test_model_part.cpp | 20 ++++++++++---------- tests/co_sim_io/python/test_info.py | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/co_sim_io/impl/macros.hpp b/co_sim_io/impl/macros.hpp index 6e49263f..44ebbfe8 100644 --- a/co_sim_io/impl/macros.hpp +++ b/co_sim_io/impl/macros.hpp @@ -92,4 +92,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 \ No newline at end of file +#endif // CO_SIM_IO_MACROS_INCLUDED diff --git a/tests/co_sim_io/impl/test_info.cpp b/tests/co_sim_io/impl/test_info.cpp index d7556916..781b2bb1 100644 --- a/tests/co_sim_io/impl/test_info.cpp +++ b/tests/co_sim_io/impl/test_info.cpp @@ -140,7 +140,7 @@ TEST_CASE("info_non_existing_key") CHECK_UNARY_FALSE(info.Has("identifier")); - CHECK_THROWS_WITH(info.Get("identifier"), "Error: Trying to get \"identifier\" which does not exist!"); + CHECK_THROWS_WITH(info.Get("identifier"), "Error: Trying to get \"identifier\" which does not exist!\n"); } TEST_CASE("info_wrong_type") @@ -152,7 +152,7 @@ TEST_CASE("info_wrong_type") info.Set("identifier", "pressure"); CHECK_UNARY(info.Has("identifier")); - CHECK_THROWS_WITH(info.Get("identifier"), "Error: Wrong DataType! Trying to get \"identifier\" which is of type \"string\" with \"int\"!"); + CHECK_THROWS_WITH(info.Get("identifier"), "Error: Wrong DataType! Trying to get \"identifier\" which is of type \"string\" with \"int\"!\n"); } TEST_CASE("info_many_values") diff --git a/tests/co_sim_io/impl/test_model_part.cpp b/tests/co_sim_io/impl/test_model_part.cpp index 47d42a04..c63c7cf4 100644 --- a/tests/co_sim_io/impl/test_model_part.cpp +++ b/tests/co_sim_io/impl/test_model_part.cpp @@ -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"); } } @@ -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"); } } @@ -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") @@ -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") @@ -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"); } } @@ -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") @@ -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"); } } diff --git a/tests/co_sim_io/python/test_info.py b/tests/co_sim_io/python/test_info.py index d280f8e1..33e81dce 100644 --- a/tests/co_sim_io/python/test_info.py +++ b/tests/co_sim_io/python/test_info.py @@ -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): @@ -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): From c37b453c4e3785e9669f007ee5ad4b0cb93ff986 Mon Sep 17 00:00:00 2001 From: philbucher Date: Thu, 17 Sep 2020 14:52:53 +0200 Subject: [PATCH 4/4] minor --- co_sim_io/impl/macros.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/co_sim_io/impl/macros.hpp b/co_sim_io/impl/macros.hpp index 44ebbfe8..63eaeec5 100644 --- a/co_sim_io/impl/macros.hpp +++ b/co_sim_io/impl/macros.hpp @@ -27,6 +27,7 @@ the code where the CoSimIO is included namespace CoSimIO { + // Simplified version of kratos/includes/exception.h class Exception : public std::exception { public: