Skip to content

Commit

Permalink
Rework all exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Feb 18, 2024
1 parent 6bbeadc commit ad8bad3
Show file tree
Hide file tree
Showing 28 changed files with 2,388 additions and 960 deletions.
9 changes: 9 additions & 0 deletions include/fintamath/exceptions/Exception.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
#pragma once

#include <exception>
#include <string>
#include <utility>

namespace fintamath {

class Exception : public std::exception {
public:
explicit Exception(std::string inMessage)
: message(std::move(inMessage)) {
}

const char *what() const noexcept override {
return "Something went wrong...";
}

private:
std::string message;
};

}
48 changes: 9 additions & 39 deletions include/fintamath/exceptions/InvalidInputException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,30 @@

#include <cstdint>
#include <string>
#include <vector>
#include <utility>

#include "fintamath/exceptions/Exception.hpp"

namespace fintamath {

class InvalidInputException : public Exception {
public:
InvalidInputException() = default;

explicit InvalidInputException(const std::string &input) {
content += ": " + input;
}

const char *what() const noexcept override {
return content.c_str();
explicit InvalidInputException(std::string inMessage)
: Exception(std::move(inMessage)) {
}

protected:
std::string content = "Invalid input";
};

class InvalidInputFunctionException final : public InvalidInputException {
public:
explicit InvalidInputFunctionException(const std::string &func, const std::vector<std::string> &argVect) {
content += ": " + func + "(";

if (!argVect.empty()) {
for (const auto &arg : argVect) {
content += arg + ',';
}

content.pop_back();
}

content += ")";
}

const char *what() const noexcept override {
return content.c_str();
explicit InvalidInputFunctionException(const std::string &func, const std::vector<std::string> &)
: InvalidInputException(func) {
}
};

class InvalidInputBinaryOperatorException final : public InvalidInputException {
public:
explicit InvalidInputBinaryOperatorException(const std::string &oper, const std::string &lhs, const std::string &rhs) {
content += ": (" + lhs + ")" + oper + "(" + rhs + ")";
explicit InvalidInputBinaryOperatorException(const std::string &oper, const std::string &, const std::string &)
: InvalidInputException(oper) {
}
};

Expand All @@ -60,15 +37,8 @@ class InvalidInputUnaryOperatorException final : public InvalidInputException {
};

public:
explicit InvalidInputUnaryOperatorException(const std::string &oper, const std::string &rhs, const Type type) {
switch (type) {
case Type::Prefix:
content += ": " + oper + "(" + rhs + ")";
break;
case Type::Postfix:
content += ": (" + rhs + ")" + oper;
break;
}
explicit InvalidInputUnaryOperatorException(const std::string &oper, const std::string &, const Type)
: InvalidInputException(oper) {
}
};

Expand Down
47 changes: 9 additions & 38 deletions include/fintamath/exceptions/UndefinedException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>
#include <string>
#include <utility>
#include <vector>

#include "fintamath/exceptions/Exception.hpp"
Expand All @@ -10,45 +11,22 @@ namespace fintamath {

class UndefinedException : public Exception {
public:
UndefinedException() = default;

explicit UndefinedException(const std::string &input) {
content += ": " + input;
}

const char *what() const noexcept override {
return content.c_str();
explicit UndefinedException(std::string inMessage)
: Exception(std::move(inMessage)) {
}

protected:
std::string content = "Undefined";
};

class UndefinedFunctionException final : public UndefinedException {
public:
explicit UndefinedFunctionException(const std::string &func, const std::vector<std::string> &argVect) {
content += ": " + func + "(";

if (!argVect.empty()) {
for (const auto &arg : argVect) {
content += arg + ',';
}

content.pop_back();
}

content += ")";
}

const char *what() const noexcept override {
return content.c_str();
explicit UndefinedFunctionException(const std::string &func, const std::vector<std::string> &)
: UndefinedException(func) {
}
};

class UndefinedBinaryOperatorException final : public UndefinedException {
public:
explicit UndefinedBinaryOperatorException(const std::string &oper, const std::string &lhs, const std::string &rhs) {
content += ": (" + lhs + ")" + oper + "(" + rhs + ")";
explicit UndefinedBinaryOperatorException(const std::string &oper, const std::string &, const std::string &)
: UndefinedException(oper) {
}
};

Expand All @@ -60,15 +38,8 @@ class UndefinedUnaryOperatorException final : public UndefinedException {
};

public:
explicit UndefinedUnaryOperatorException(const std::string &oper, const std::string &rhs, const Type type) {
switch (type) {
case Type::Prefix:
content += ": " + oper + "(" + rhs + ")";
break;
case Type::Postfix:
content += ": (" + rhs + ")" + oper;
break;
}
explicit UndefinedUnaryOperatorException(const std::string &oper, const std::string &, const Type)
: UndefinedException(oper) {
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ std::unique_ptr<IMathObject> parseFintamath(const std::string &str) {
try {
auto tokens = Tokenizer::tokenize(str);
auto terms = Expression::tokensToTerms(tokens);
auto stack = Expression::termsToOperands(terms);
auto obj = Expression::operandsToObject(stack);
return obj;
auto operands = Expression::termsToOperands(terms);
auto object = Expression::operandsToObject(operands);
return object;
}
catch (const InvalidInputException &) {
throw InvalidInputException(str);
Expand Down
41 changes: 33 additions & 8 deletions tests/src/core/IArithmeticTests.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "fintamath/core/IArithmetic.hpp"
Expand Down Expand Up @@ -67,8 +68,14 @@ TEST(IArithmeticTests, addTest) {
EXPECT_TRUE(is<Rational>(*m3 + *m2));
EXPECT_TRUE(is<Integer>(*m3 + *m3));

EXPECT_THROW(*m1 + TestArithmetic(), InvalidInputBinaryOperatorException);
EXPECT_THROW(TestArithmetic() + *m1, InvalidInputBinaryOperatorException);
EXPECT_THAT(
[&] { *m1 + TestArithmetic(); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[&] { TestArithmetic() + *m1; },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));

Integer a;
EXPECT_EQ((a += 3).toString(), "3");
Expand Down Expand Up @@ -103,8 +110,14 @@ TEST(IArithmeticTests, subTest) {
EXPECT_TRUE(is<Rational>(*m3 - *m2));
EXPECT_TRUE(is<Integer>(*m3 - *m3));

EXPECT_THROW(*m1 - TestArithmetic(), InvalidInputBinaryOperatorException);
EXPECT_THROW(TestArithmetic() - *m1, InvalidInputBinaryOperatorException);
EXPECT_THAT(
[&] { *m1 - TestArithmetic(); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[&] { TestArithmetic() - *m1; },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));

Integer a;
EXPECT_EQ((a -= 3).toString(), "-3");
Expand Down Expand Up @@ -139,8 +152,14 @@ TEST(IArithmeticTests, mulTest) {
EXPECT_TRUE(is<Integer>(*m3 * *m2));
EXPECT_TRUE(is<Rational>(*m3 * *m3));

EXPECT_THROW(*m1 * TestArithmetic(), InvalidInputBinaryOperatorException);
EXPECT_THROW(TestArithmetic() * *m1, InvalidInputBinaryOperatorException);
EXPECT_THAT(
[&] { *m1 * TestArithmetic(); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[&] { TestArithmetic() * *m1; },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));

Integer a = 2;
EXPECT_EQ((a *= 3).toString(), "6");
Expand Down Expand Up @@ -175,8 +194,14 @@ TEST(IArithmeticTests, divTest) {
EXPECT_TRUE(is<Rational>(*m3 / *m2));
EXPECT_TRUE(is<Integer>(*m3 / *m3));

EXPECT_THROW(*m1 / TestArithmetic(), InvalidInputBinaryOperatorException);
EXPECT_THROW(TestArithmetic() / *m1, InvalidInputBinaryOperatorException);
EXPECT_THAT(
[&] { *m1 / TestArithmetic(); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[&] { TestArithmetic() / *m1; },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));

Integer a = 4;
EXPECT_EQ((a /= 2).toString(), "2");
Expand Down
41 changes: 33 additions & 8 deletions tests/src/core/IComparableTests.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "fintamath/core/IComparable.hpp"
Expand Down Expand Up @@ -59,8 +60,14 @@ TEST(IComparableTests, lessTest) {
EXPECT_TRUE(*m3 < *m1);
EXPECT_TRUE(*m3 < *m2);

EXPECT_THROW((void)(*m1 < TestComparable()), InvalidInputBinaryOperatorException);
EXPECT_THROW(void(TestComparable() < *m1), InvalidInputBinaryOperatorException);
EXPECT_THAT(
[&] { *m1 < TestComparable(); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[&] { TestComparable() < *m1; },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));

EXPECT_TRUE(Integer() < 1);
EXPECT_TRUE(-1 < Integer());
Expand All @@ -84,8 +91,14 @@ TEST(IComparableTests, moreTest) {
EXPECT_FALSE(*m3 > *m1);
EXPECT_FALSE(*m3 > *m2);

EXPECT_THROW(void(*m1 > TestComparable()), InvalidInputBinaryOperatorException);
EXPECT_THROW(void(TestComparable() > *m1), InvalidInputBinaryOperatorException);
EXPECT_THAT(
[&] { *m1 > TestComparable(); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[&] { TestComparable() > *m1; },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));

EXPECT_FALSE(Integer() > 1);
EXPECT_FALSE(-1 > Integer());
Expand All @@ -109,8 +122,14 @@ TEST(IComparableTests, lessEqualsTest) {
EXPECT_TRUE(*m3 <= *m1);
EXPECT_TRUE(*m3 <= *m2);

EXPECT_THROW(void(*m1 <= TestComparable()), InvalidInputBinaryOperatorException);
EXPECT_THROW(void(TestComparable() <= *m1), InvalidInputBinaryOperatorException);
EXPECT_THAT(
[&] { *m1 <= TestComparable(); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[&] { TestComparable() <= *m1; },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));

EXPECT_TRUE(Integer() <= 1);
EXPECT_TRUE(-1 <= Integer());
Expand All @@ -134,8 +153,14 @@ TEST(IComparableTests, moreEqualsTest) {
EXPECT_FALSE(*m3 >= *m1);
EXPECT_FALSE(*m3 >= *m2);

EXPECT_THROW(void(*m1 >= TestComparable()), InvalidInputBinaryOperatorException);
EXPECT_THROW(void(TestComparable() >= *m1), InvalidInputBinaryOperatorException);
EXPECT_THAT(
[&] { *m1 >= TestComparable(); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[&] { TestComparable() >= *m1; },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));

EXPECT_FALSE(Integer() >= 1);
EXPECT_FALSE(-1 >= Integer());
Expand Down
4 changes: 2 additions & 2 deletions tests/src/exceptions/ExceptionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace fintamath;

TEST(ExceptionTests, whatTest) {
EXPECT_THAT(
[] { throw Exception(); },
[] { throw Exception("Unknown exception"); },
testing::ThrowsMessage<Exception>(
testing::StrEq("Something went wrong...")));
testing::StrEq("")));
}
8 changes: 2 additions & 6 deletions tests/src/exceptions/InvalidInputExceptionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ using namespace fintamath;

TEST(InvalidInputExceptionTests, whatTest) {
EXPECT_THAT(
[] { throw InvalidInputException(); },
[] { throw InvalidInputException(R"(Integer("abc"))"); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("Invalid input")));
EXPECT_THAT(
[] { throw InvalidInputException("123"); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("Invalid input: 123")));
testing::StrEq(R"(Integer("abc")")));
}
8 changes: 2 additions & 6 deletions tests/src/exceptions/UndefinedExceptionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ using namespace fintamath;

TEST(UndefinedExceptionTests, whatTest) {
EXPECT_THAT(
[] { throw UndefinedException(); },
[] { throw UndefinedException("Undefined 0/0"); },
testing::ThrowsMessage<UndefinedException>(
testing::StrEq("Undefined")));
EXPECT_THAT(
[] { throw UndefinedException("123"); },
testing::ThrowsMessage<UndefinedException>(
testing::StrEq("Undefined: 123")));
testing::StrEq("Undefined 0/0")));
}
Loading

0 comments on commit ad8bad3

Please sign in to comment.