#include #include "Calculator.hpp" #include "Operand.hpp" constexpr auto addiction = [](auto lhs, auto rhs) { return lhs + rhs; }; constexpr auto subtraction = [](auto lhs, auto rhs) { return lhs - rhs; }; TEST_CASE("Calculator - can add operations", "[calculator-challenge]") { Calculator calculator; calculator.addOperation("a", IntegerOperand{5}, IntegerOperand{10}, addiction); CHECK(calculator.computeLastValue() == 15.0); calculator.addOperation("b", IntegerOperand{1}, IntegerOperand{5}, addiction); CHECK(calculator.computeLastValue() == 6.0); calculator.addOperation("c", IntegerOperand{1}, IntegerOperand{5}, subtraction); CHECK(calculator.computeLastValue() == -4.0); } TEST_CASE("Calculator - can undo operations", "[calculator-challenge]") { Calculator calculator; calculator.addOperation("a", IntegerOperand{5}, IntegerOperand{10}, addiction); CHECK(calculator.computeLastValue() == 15.0); calculator.addOperation("b", IntegerOperand{1}, IntegerOperand{5}, subtraction); CHECK(calculator.computeLastValue() == -4.0); SECTION("one by one") { calculator.undoOperation(1); CHECK(calculator.computeLastValue() == 15.0); calculator.undoOperation(1); CHECK(calculator.computeLastValue() == 0.0); } SECTION("all") { calculator.undoOperation(2); CHECK(calculator.computeLastValue() == 0.0); } SECTION("more than was added") { calculator.undoOperation(50); CHECK(calculator.computeLastValue() == 0.0); } } TEST_CASE("Calculator - can add operations with variables", "[calculator-challenge]") { Calculator calculator; calculator.addOperation("a", IntegerOperand{1}, IntegerOperand{2}, addiction); CHECK(calculator.computeLastValue() == 3); calculator.addOperation("b", VariableOperand{"a"}, IntegerOperand{10}, addiction); CHECK(calculator.computeLastValue() == 13); calculator.addOperation("b", VariableOperand{"a"}, IntegerOperand{2}, addiction); CHECK(calculator.computeLastValue() == 5); } TEST_CASE("Calculator - if a variable is missing the last available is returned", "[calculator-challenge]") { Calculator calculator; calculator.addOperation("a", VariableOperand{"c"}, IntegerOperand{10}, addiction); CHECK(calculator.computeLastValue() == 0.0); calculator.addOperation("a", IntegerOperand{1}, IntegerOperand{2}, addiction); CHECK(calculator.computeLastValue() == 3); calculator.addOperation("a", VariableOperand{"c"}, IntegerOperand{10}, addiction); CHECK(calculator.computeLastValue() == 3.0); }