Selaa lähdekoodia

Added Boolean boxing class

- added Boolean boxing class with basic
functionality
- added tests for Boolean boxing class
Patrick 4 vuotta sitten
vanhempi
commit
d6149ca6e6
4 muutettua tiedostoa jossa 191 lisäystä ja 2 poistoa
  1. 6 2
      CMakeLists.txt
  2. 59 0
      source/boxing/Boolean.cpp
  3. 63 0
      source/boxing/Boolean.hpp
  4. 63 0
      test/cases/boxing/BooleanTest.cpp

+ 6 - 2
CMakeLists.txt

@@ -38,10 +38,14 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Integer.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/IBoxing.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/exception/IllegalOperationException.hpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/base/Types.hpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/base/Types.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Boolean.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/boxing/Boolean.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/exception/IllegalArgumentException.hpp)
 
 set(TEST_FILES
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/BooleanTest.cpp)
 
 ##########################################################
 # Build

+ 59 - 0
source/boxing/Boolean.cpp

@@ -0,0 +1,59 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-09
+ * Changed:         2020-08-09
+ *
+ * */
+
+#include <algorithm>
+#include "Boolean.hpp"
+#include "../exception/IllegalArgumentException.hpp"
+
+ls_std::Boolean::Boolean() : Class("Boolean")
+{}
+
+ls_std::Boolean::Boolean(bool _value) : Class("Boolean"),
+value(_value)
+{}
+
+ls_std::Boolean::operator bool() const
+{
+  return this->value;
+}
+
+void ls_std::Boolean::parse(std::string parseText)
+{
+  std::transform(parseText.begin(), parseText.end(), parseText.begin(), ::tolower);
+
+  if(parseText != this->TRUE_STRING && parseText != this->FALSE_STRING) {
+    throw ls_std::IllegalArgumentException {};
+  } else {
+    if(parseText == this->TRUE_STRING) {
+      this->value = true;
+    }
+
+    if(parseText == this->FALSE_STRING) {
+      this->value = false;
+    }
+  }
+}
+
+std::string ls_std::Boolean::toString()
+{
+  return this->_toString();
+}
+
+std::string ls_std::Boolean::_toString() const
+{
+  std::string booleanString {};
+
+  if(this->value) {
+    booleanString = this->TRUE_STRING;
+  } else {
+    booleanString = this->FALSE_STRING;
+  }
+
+  return booleanString;
+}

+ 63 - 0
source/boxing/Boolean.hpp

@@ -0,0 +1,63 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-09
+ * Changed:         2020-08-09
+ *
+ * */
+
+#ifndef BOOLEAN_HPP
+#define BOOLEAN_HPP
+
+#include "../base/Class.hpp"
+#include "IBoxing.hpp"
+
+namespace ls_std {
+  class Boolean : public Class, IBoxing {
+    public:
+
+      explicit Boolean(bool _value);
+      Boolean();
+      ~Boolean() = default;
+
+      // conversion operator
+
+      operator bool() const;
+
+      // stream operators
+
+      friend std::ostream& operator<<(std::ostream& outputStream, const Boolean& _boolean) {
+        outputStream << _boolean._toString();
+        return outputStream;
+      }
+
+      // logical operators
+
+      friend bool operator!(const Boolean& _boolean) {
+        return !_boolean.value;
+      }
+
+//      bool operator&&(const Boolean& _boolean) const;
+//      bool operator&&(bool _value) const;
+//      bool operator&&(int _value) const;
+//      bool operator||(const Boolean& _boolean) const;
+//      bool operator||(bool _value) const;
+//      bool operator||(int _value) const;
+
+      // implementation
+
+      void parse(std::string parseText) override;
+      std::string toString() override;
+
+    private:
+
+      bool value {};
+      const std::string TRUE_STRING = "true";
+      const std::string FALSE_STRING = "false";
+
+      std::string _toString() const;
+  };
+}
+
+#endif

+ 63 - 0
test/cases/boxing/BooleanTest.cpp

@@ -0,0 +1,63 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-09
+ * Changed:         2020-08-09
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../source/boxing/Boolean.hpp"
+
+namespace {
+  class BooleanTest : public ::testing::Test {
+    protected:
+
+      BooleanTest() = default;
+      ~BooleanTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(BooleanTest, operatorOutputStream)
+  {
+    ls_std::Boolean expression {true};
+    std::ostringstream _stream {};
+    _stream << expression;
+
+    ASSERT_STREQ("true", _stream.str().c_str());
+  }
+
+  TEST_F(BooleanTest, parse)
+  {
+    ls_std::Boolean expression {};
+
+    expression.parse("true");
+    ASSERT_TRUE(expression);
+
+    expression.parse("tRue");
+    ASSERT_TRUE(expression);
+
+    expression.parse("TRUE");
+    ASSERT_TRUE(expression);
+  }
+
+  TEST_F(BooleanTest, parseNegative)
+  {
+    ls_std::Boolean expression {};
+
+    expression.parse("false");
+    ASSERT_FALSE(expression);
+
+    expression.parse("fAlSe");
+    ASSERT_FALSE(expression);
+
+    expression.parse("FALSE");
+    ASSERT_FALSE(expression);
+  }
+}