Skip to content

Commit

Permalink
Refactor Boolean::Boolean(bool)
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Mar 27, 2024
1 parent b765976 commit 5139fb5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
8 changes: 7 additions & 1 deletion include/fintamath/literals/Boolean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ class Boolean final : public ILiteralCRTP<Boolean> {

explicit Boolean(const std::string &str);

Boolean(bool val);
template <std::same_as<bool> Bool>
Boolean(const Bool val) : name(val ? trueStr : falseStr) {
}

std::string toString() const override;

operator bool() const;

private:
std::string name;

static constexpr std::string_view trueStr = "True";

static constexpr std::string_view falseStr = "False";
};

}
6 changes: 0 additions & 6 deletions src/fintamath/literals/Boolean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace fintamath {

constexpr std::string_view trueStr = "True";
constexpr std::string_view falseStr = "False";

Boolean::Boolean() : name(falseStr) {
}

Expand All @@ -18,9 +15,6 @@ Boolean::Boolean(const std::string &str) {
name = str;
}

Boolean::Boolean(const bool val) : name(val ? trueStr : falseStr) {
}

std::string Boolean::toString() const {
return name;
}
Expand Down
22 changes: 11 additions & 11 deletions tests/src/literals/BooleanTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ TEST(BooleanTests, constructorTest) {
}

TEST(BooleanTests, stringConstructorTest) {
EXPECT_EQ(Boolean(std::string("True")), true);
EXPECT_EQ(Boolean(std::string("False")), false);

EXPECT_THROW(Boolean(std::string("true")), InvalidInputException);
EXPECT_THROW(Boolean(std::string("false")), InvalidInputException);
EXPECT_THROW(Boolean(std::string("10")), InvalidInputException);
EXPECT_THROW(Boolean(std::string("i")), InvalidInputException);
EXPECT_THROW(Boolean(std::string("")), InvalidInputException);
EXPECT_THROW(Boolean(std::string("")), InvalidInputException);
EXPECT_EQ(Boolean("True"), true);
EXPECT_EQ(Boolean("False"), false);

EXPECT_THROW(Boolean("true"), InvalidInputException);
EXPECT_THROW(Boolean("false"), InvalidInputException);
EXPECT_THROW(Boolean("10"), InvalidInputException);
EXPECT_THROW(Boolean("i"), InvalidInputException);
EXPECT_THROW(Boolean(""), InvalidInputException);
EXPECT_THROW(Boolean(""), InvalidInputException);
}

TEST(BooleanTests, boolConstructorTest) {
Expand All @@ -45,6 +45,6 @@ TEST(BooleanTests, opearatorsTest) {
}

TEST(BooleanTest, getClassTest) {
EXPECT_EQ(Boolean::getClassStatic(), MathObjectClass("Boolean"));
EXPECT_EQ(Boolean::getClassStatic().getParent(), ILiteral::getClassStatic());
EXPECT_EQ(Boolean().getClass(), MathObjectClass("Boolean"));
EXPECT_EQ(Boolean().getClass().getParent(), ILiteral::getClassStatic());
}

0 comments on commit 5139fb5

Please sign in to comment.