Skip to content

Commit

Permalink
feat(calculator and error): add class calculator and error
Browse files Browse the repository at this point in the history
- new unit test
  • Loading branch information
TheRealPad committed Feb 28, 2024
1 parent 0cf5c65 commit 2b4af07
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.vscode/
.idea/
build/
cmake-build-debug/

calculator
unit_test
17 changes: 14 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ SET(TEST_NAME "unit_test")
set(CMAKE_CXX_STANDARD 17)

set(SRC
main.cpp
src/Calculator/Calculator.cpp
src/Error/Error.cpp
)

add_executable(${NAME} ${SRC})
set(TEST_SRC
tests/test.cpp
tests/Error.cpp
)

add_executable(${NAME} main.cpp ${SRC})

set_target_properties(${NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})

include_directories(${NAME} PUBLIC src/Calculator)
include_directories(${NAME} PUBLIC src/Error)

add_compile_options(-fPIC)

include(FetchContent)
Expand All @@ -29,9 +38,11 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(googletest)

add_executable(${TEST_NAME} tests/test.cpp)
add_executable(${TEST_NAME} ${SRC} ${TEST_SRC})
target_link_libraries(${TEST_NAME} PUBLIC gtest_main)

include(CTest)
include(GoogleTest)
gtest_discover_tests(${TEST_NAME})

set_target_properties(${TEST_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
7 changes: 4 additions & 3 deletions main.cpp
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;
}
26 changes: 26 additions & 0 deletions src/Error/Error.cpp
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
36 changes: 36 additions & 0 deletions src/Error/Error.h
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
38 changes: 38 additions & 0 deletions tests/Error.cpp
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);
}

0 comments on commit 2b4af07

Please sign in to comment.