Bladeren bron

Improve Boolean class

- add additional error checks to Boolean class
- extend tests for Boolean class
- improve naming in Boolean class tests
Patrick-Christopher Mattulat 3 jaren geleden
bovenliggende
commit
36dd459ac8
3 gewijzigde bestanden met toevoegingen van 127 en 54 verwijderingen
  1. 5 3
      include/ls_std/boxing/Boolean.hpp
  2. 25 5
      source/ls_std/boxing/Boolean.cpp
  3. 97 46
      test/cases/boxing/BooleanTest.cpp

+ 5 - 3
include/ls_std/boxing/Boolean.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2021-05-01
+ * Changed:         2021-05-30
  *
  * */
 
@@ -70,8 +70,8 @@ namespace ls_std
       // additional functionality
 
       bool getValue() const;
-      void setSerializable(std::shared_ptr<ls_std::ISerializable> _serializable);
-      void setStorable(std::shared_ptr<ls_std::IStorable> _storable);
+      void setSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable);
+      void setStorable(const std::shared_ptr<ls_std::IStorable>& _storable);
       static bool XOR(const ls_std::Boolean &_leftExpression, const ls_std::Boolean &_rightExpression);
       static bool XOR(const ls_std::Boolean &_leftExpression, bool _rightExpression);
       static bool XOR(bool _leftExpression, const ls_std::Boolean &_rightExpression);
@@ -86,6 +86,8 @@ namespace ls_std
       const std::string FALSE_STRING = "false";
       const std::string TRUE_STRING = "true";
 
+      void _assignSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable);
+      void _assignStorable(const std::shared_ptr<ls_std::IStorable>& _storable);
       std::string _toString() const;
   };
 }

+ 25 - 5
source/ls_std/boxing/Boolean.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2021-05-01
+ * Changed:         2021-05-30
  *
  * */
 
@@ -146,14 +146,14 @@ bool ls_std::Boolean::getValue() const
   return this->value;
 }
 
-void ls_std::Boolean::setSerializable(std::shared_ptr<ls_std::ISerializable> _serializable)
+void ls_std::Boolean::setSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable)
 {
-  this->serializable = std::move(_serializable);
+  this->_assignSerializable(_serializable);
 }
 
-void ls_std::Boolean::setStorable(std::shared_ptr<ls_std::IStorable> _storable)
+void ls_std::Boolean::setStorable(const std::shared_ptr<ls_std::IStorable>& _storable)
 {
-  this->storable = std::move(_storable);
+  this->_assignStorable(_storable);
 }
 
 bool ls_std::Boolean::XOR(const ls_std::Boolean &_leftExpression, const ls_std::Boolean &_rightExpression)
@@ -176,6 +176,26 @@ bool ls_std::Boolean::XOR(bool _leftExpression, bool _rightExpression)
   return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
 }
 
+void ls_std::Boolean::_assignSerializable(const std::shared_ptr<ls_std::ISerializable> &_serializable)
+{
+  if (_serializable == nullptr)
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->serializable = _serializable;
+}
+
+void ls_std::Boolean::_assignStorable(const std::shared_ptr<ls_std::IStorable> &_storable)
+{
+  if (_storable == nullptr)
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->storable = _storable;
+}
+
 std::string ls_std::Boolean::_toString() const
 {
   std::string booleanString{};

+ 97 - 46
test/cases/boxing/BooleanTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2021-05-02
+ * Changed:         2021-05-30
  *
  * */
 
@@ -25,24 +25,47 @@ namespace
 
       void TearDown() override
       {}
+
+      static std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Boolean>> createTestExpression()
+      {
+        std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>();
+        std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_bool.json";
+        std::shared_ptr<ls_std::File> file = std::make_shared<ls_std::File>(path);
+        file->createNewFile();
+        ls_std::FileWriter writer{*file};
+        writer.write(R"({"value":true})");
+
+        auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
+        expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+        auto storable = std::make_shared<ls_std::StorableFile>(path);
+        expression->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
+
+        return std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Boolean>>(file, expression);
+      }
   };
 
   // assignment operators
 
-  TEST_F(BooleanTest, operatorAssignment)
+  TEST_F(BooleanTest, operator_assignment_boolean)
   {
     ls_std::Boolean expression{};
-
     expression = true;
+
     ASSERT_TRUE(expression);
+  }
 
+  TEST_F(BooleanTest, operator_assignment_integer)
+  {
+    ls_std::Boolean expression{};
     expression = 1;
+
     ASSERT_TRUE(expression);
   }
 
   // stream operators
 
-  TEST_F(BooleanTest, operatorOutputStream)
+  TEST_F(BooleanTest, operator_output_stream)
   {
     ls_std::Boolean expression{true};
     std::ostringstream _stream{};
@@ -53,19 +76,19 @@ namespace
 
   // logical operators
 
-  TEST_F(BooleanTest, operatorNot)
+  TEST_F(BooleanTest, operator_negation_negative_value)
   {
     ls_std::Boolean expression{};
     ASSERT_TRUE(!expression);
   }
 
-  TEST_F(BooleanTest, operatorNotNegative)
+  TEST_F(BooleanTest, operator_negation_positive_value)
   {
     ls_std::Boolean expression{true};
     ASSERT_FALSE(!expression);
   }
 
-  TEST_F(BooleanTest, operatorAnd)
+  TEST_F(BooleanTest, operator_and)
   {
     ls_std::Boolean expressionA{true};
     ls_std::Boolean expressionB{3 == 3};
@@ -77,7 +100,7 @@ namespace
     ASSERT_TRUE(1 && expressionB);
   }
 
-  TEST_F(BooleanTest, operatorAndNegative)
+  TEST_F(BooleanTest, operator_and_with_false_result)
   {
     ls_std::Boolean expressionA{true};
     ls_std::Boolean expressionB{3 == 4};
@@ -89,7 +112,7 @@ namespace
     ASSERT_FALSE(expressionB && false);
   }
 
-  TEST_F(BooleanTest, operatorOr)
+  TEST_F(BooleanTest, operator_or)
   {
     ls_std::Boolean expressionA{false};
     ls_std::Boolean expressionB{3 == 3};
@@ -101,7 +124,7 @@ namespace
     ASSERT_TRUE(1 || expressionA);
   }
 
-  TEST_F(BooleanTest, operatorOrNegative)
+  TEST_F(BooleanTest, operator_or_with_false_result)
   {
     ls_std::Boolean expressionA{false};
     ls_std::Boolean expressionB{3 == 4};
@@ -119,41 +142,37 @@ namespace
   {
     // preparation
 
-    std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>();
-    std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_bool.json";
-    ls_std::File file{path};
-    file.createNewFile();
-    ls_std::FileWriter writer{file};
-    writer.write(R"({"value":true})");
-
-    auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(x);
-    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-
-    auto storable = std::make_shared<ls_std::StorableFile>(path);
-    x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
+    auto storableExpression = createTestExpression();
 
     // check
 
-    x->load();
-    ASSERT_TRUE(*x);
+    storableExpression.second->load();
+    ASSERT_TRUE(*storableExpression.second);
 
-    file.remove();
+    storableExpression.first->remove();
   }
 
-  TEST_F(BooleanTest, marshal)
+  TEST_F(BooleanTest, marshal_positive_value)
   {
-    std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(true);
+    std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>(true);
+
+    auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
+    expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    ASSERT_STREQ(R"({"value":true})", expression->marshal().c_str());
+  }
 
-    auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(x);
-    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+  TEST_F(BooleanTest, marshal_negative_value)
+  {
+    std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>(false);
 
-    ASSERT_STREQ(R"({"value":true})", x->marshal().c_str());
+    auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
+    expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
 
-    *x = false;
-    ASSERT_STREQ(R"({"value":false})", x->marshal().c_str());
+    ASSERT_STREQ(R"({"value":false})", expression->marshal().c_str());
   }
 
-  TEST_F(BooleanTest, parse)
+  TEST_F(BooleanTest, parse_true_value)
   {
     ls_std::Boolean expression{};
 
@@ -167,7 +186,7 @@ namespace
     ASSERT_TRUE(expression);
   }
 
-  TEST_F(BooleanTest, parseNegative)
+  TEST_F(BooleanTest, parse_false_value)
   {
     ls_std::Boolean expression{};
 
@@ -181,26 +200,26 @@ namespace
     ASSERT_FALSE(expression);
   }
 
-  TEST_F(BooleanTest, toString)
+  TEST_F(BooleanTest, toString_true)
   {
     ls_std::Boolean expression{2 < 3};
     ASSERT_STREQ("true", expression.toString().c_str());
+  }
 
-    expression = 4 < 3;
+  TEST_F(BooleanTest, toString_false)
+  {
+    ls_std::Boolean expression{4 < 3};
     ASSERT_STREQ("false", expression.toString().c_str());
   }
 
   TEST_F(BooleanTest, unmarshal)
   {
-    std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(false);
-
-    ASSERT_FALSE(*x);
+    std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>(false);
+    auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
+    expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+    expression->unmarshal(R"({"value":true})");
 
-    auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(x);
-    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-    x->unmarshal(R"({"value":true})");
-
-    ASSERT_TRUE(*x);
+    ASSERT_TRUE(*expression);
   }
 
   // additional functionality
@@ -211,7 +230,39 @@ namespace
     ASSERT_TRUE(x.getValue());
   }
 
-  TEST_F(BooleanTest, XOR)
+  TEST_F(BooleanTest, setSerialiable_no_reference)
+  {
+    ls_std::Boolean expression{};
+
+    EXPECT_THROW({
+                   try
+                   {
+                     expression.setSerializable(nullptr);
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
+  TEST_F(BooleanTest, setStorable_no_reference)
+  {
+    ls_std::Boolean expression{};
+
+    EXPECT_THROW({
+                   try
+                   {
+                     expression.setStorable(nullptr);
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
+  TEST_F(BooleanTest, XOR_with_positive_result)
   {
     ls_std::Boolean x{2 < 3};
     ls_std::Boolean y{4 < 3};
@@ -224,7 +275,7 @@ namespace
     ASSERT_TRUE(ls_std::Boolean::XOR(false, true));
   }
 
-  TEST_F(BooleanTest, XORNegative)
+  TEST_F(BooleanTest, XOR_with_negative_result)
   {
     ls_std::Boolean w{};
     ls_std::Boolean x{true};