-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(calculator and error): add class calculator and error
- new unit test
- Loading branch information
1 parent
0cf5c65
commit 2b4af07
Showing
6 changed files
with
122 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.vscode/ | ||
.idea/ | ||
build/ | ||
cmake-build-debug/ | ||
|
||
calculator | ||
unit_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
#include <iostream> | ||
#include "Calculator.h" | ||
|
||
int main(int ac, char **ag) | ||
{ | ||
std::cout << "Hello World!" << std::endl; | ||
return 0; | ||
Calculator::Calculator calculator; | ||
|
||
return calculator.run() ? 0 : 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Created by Pierre-Alexandre Delgado on 28/02/2024. | ||
// | ||
|
||
#include "Error.h" | ||
|
||
namespace ErrorCalculator { | ||
|
||
Error::Error(std::string const &msg) { | ||
this->_errorMsg = msg; | ||
this->_error = ErrorType::CUSTOM; | ||
} | ||
|
||
Error::Error(ErrorType const &type) { | ||
this->_error = type; | ||
} | ||
|
||
const char *Error::what() const noexcept { | ||
if (this->_error == ErrorType::CUSTOM) | ||
return this->_errorMsg.c_str(); | ||
if (this->_error == ErrorType::DIVISION_BY_0) | ||
return "Division by 0"; | ||
return "Unknow error"; | ||
} | ||
|
||
} // ErrorCalculator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Created by Pierre-Alexandre Delgado on 28/02/2024. | ||
// | ||
|
||
#ifndef ERROR_H | ||
#define ERROR_H | ||
|
||
#include <exception> | ||
#include <string> | ||
|
||
namespace ErrorCalculator { | ||
|
||
class Error : public std::exception { | ||
|
||
public: | ||
|
||
enum ErrorType { | ||
CUSTOM, | ||
DIVISION_BY_0 | ||
}; | ||
Error() = default; | ||
~Error() override = default; | ||
explicit Error(std::string const &msg); | ||
explicit Error(ErrorType const &type); | ||
|
||
const char *what() const noexcept override; | ||
|
||
private: | ||
ErrorType _error; | ||
std::string _errorMsg; | ||
|
||
}; | ||
|
||
} // ErrorCalculator | ||
|
||
#endif //ERROR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <gtest/gtest.h> | ||
#include "Error.h" | ||
|
||
TEST(Error, ThrowDivisionError) | ||
{ | ||
bool result = false; | ||
|
||
try { | ||
throw ErrorCalculator::Error(ErrorCalculator::Error::DIVISION_BY_0); | ||
} catch (ErrorCalculator::Error &e) { | ||
result = true; | ||
EXPECT_EQ(e.what(), "Division by 0"); | ||
} | ||
EXPECT_EQ(result, true); | ||
} | ||
|
||
TEST(Error, DontThrowDivisionError) | ||
{ | ||
bool result = false; | ||
|
||
try { | ||
} catch (ErrorCalculator::Error &e) { | ||
result = true; | ||
} | ||
EXPECT_EQ(result, false); | ||
} | ||
|
||
TEST(Error, ThrowCustomError) | ||
{ | ||
bool result = false; | ||
|
||
try { | ||
throw ErrorCalculator::Error("Hello there"); | ||
} catch (ErrorCalculator::Error &e) { | ||
result = true; | ||
} | ||
EXPECT_EQ(result, true); | ||
} |