Skip to content

Commit

Permalink
feat(proxy): check error with proxy
Browse files Browse the repository at this point in the history
- regex to check user input
- unit test
  • Loading branch information
TheRealPad committed Feb 29, 2024
1 parent 6c177a8 commit b4dbdb6
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(TEST_SRC
tests/test.cpp
tests/Error.cpp
tests/Calculator.cpp
tests/OperationsPriorities.cpp
)

add_executable(${NAME} main.cpp ${SRC})
Expand Down
2 changes: 2 additions & 0 deletions src/Error/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace ErrorCalculator {
return this->_errorMsg.c_str();
if (this->_error == ErrorType::DIVISION_BY_0)
return "Division by 0";
if (this->_error == ErrorType::TYPO_USER_INPUT)
return "Typo in user input";
return "Unknow error";
}

Expand Down
3 changes: 2 additions & 1 deletion src/Error/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace ErrorCalculator {

enum ErrorType {
CUSTOM,
DIVISION_BY_0
DIVISION_BY_0,
TYPO_USER_INPUT
};
Error() = default;
~Error() override = default;
Expand Down
21 changes: 21 additions & 0 deletions src/OperationsPriorities/ProxyOperationsPriorities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,36 @@
// Created by Pierre-Alexandre Delgado on 28/02/2024.
//

#include <regex>
#include "ProxyOperationsPriorities.h"

#include <Error.h>

namespace Operations {

ProxyOperationsPriorities::ProxyOperationsPriorities() {
this->_operationsPriorities = std::make_unique<OperationsPriorities>();
}

bool ProxyOperationsPriorities::checkParenthesis(std::string const &operation) {
unsigned int nbrLeftParenthesis = 0;

for (auto const &c : operation) {
if (c == '(')
++nbrLeftParenthesis;
if (c == ')' && nbrLeftParenthesis == 0)
return false;
if (c == ')')
--nbrLeftParenthesis;
}
return nbrLeftParenthesis == 0;
}


std::string ProxyOperationsPriorities::makeOperation(std::string &operation) {
std::regex self_regex("^[0-9()+*/%\\- ]+$",std::regex_constants::ECMAScript | std::regex_constants::icase);
if (!std::regex_search(operation, self_regex) || !this->checkParenthesis(operation))
throw ErrorCalculator::Error(ErrorCalculator::Error::TYPO_USER_INPUT);
return this->_operationsPriorities->makeOperation(operation);
}

Expand Down
2 changes: 2 additions & 0 deletions src/OperationsPriorities/ProxyOperationsPriorities.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Operations {
std::string makeOperation(std::string &operation) override;

private:
bool checkParenthesis(std::string const &operation);

std::unique_ptr<OperationsPriorities> _operationsPriorities;
};

Expand Down
157 changes: 157 additions & 0 deletions tests/OperationsPriorities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include <Error.h>
#include <gtest/gtest.h>
#include "ProxyOperationsPriorities.h"

TEST(ProxyOperationsPriorities, ValidStringOne)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "()";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, true);
}

TEST(ProxyOperationsPriorities, ValidStringTwo)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "(1)";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, true);
}

TEST(ProxyOperationsPriorities, ValidStringThree)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "(1 + 1)";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, true);
}

TEST(ProxyOperationsPriorities, ValidStringFour)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "(1+1)-4 + (1 * 23456)";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, true);
}

TEST(ProxyOperationsPriorities, ValidStringFive)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "(((1 + 4) * 3) / 2) % 1";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, true);
}

TEST(ProxyOperationsPriorities, ValidStringSix)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "(((1 + 4) * 3) / 2) % (1 +3+((3% 4) + 4))";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, true);
}

TEST(ProxyOperationsPriorities, InvalidStringOne)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "invalid content";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, false);
}

TEST(ProxyOperationsPriorities, InvalidStringTwo)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "1 + a";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, false);
}

TEST(ProxyOperationsPriorities, InvalidStringThree)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "(";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, false);
}

TEST(ProxyOperationsPriorities, InvalidStringFour)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = ")";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, false);
}

TEST(ProxyOperationsPriorities, InvalidStringFive)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "(1 + 3";
bool isValid = true;

try {
operations->makeOperation(str);
} catch (ErrorCalculator::Error &e) {
isValid = false;
}
EXPECT_EQ(isValid, false);
}

0 comments on commit b4dbdb6

Please sign in to comment.