class TestCalculate(unittest.TestCase): def setUp(self): self.calc = Calculator() def test_add_method_returns_correct_result(self): self.assertEqual(4, self.calc.add(2, 2)) self.assertEqual(0, self.calc.add(2, -2)) self.assertEqual(0, self.calc.add(-2, 2)) self.assertEqual(1, self.calc.add(1, 0)) def test_divide_method_returns_correct_result(self): self.assertEqual(1, self.calc.divide(2, 2)) self.assertEqual(1.5, self.calc.divide(3, 2)) def test_add_method_fails_with_nan_parameter(self): self.assertRaises(TypeError, self.calc.add, "2", 2) self.assertRaises(TypeError, self.calc.add, 2, "2") self.assertRaises(TypeError, self.calc.add, "2", "2") self.assertRaises(TypeError, self.calc.add, None, 2) self.assertRaises(TypeError, self.calc.add, 2, None) self.assertRaises(TypeError, self.calc.add, object(), 2) self.assertRaises(TypeError, self.calc.add, 2, object()) def test_divide_method_fails_with_nan_parameter(self): self.assertRaises(TypeError, self.calc.divide, "2", 2) self.assertRaises(TypeError, self.calc.divide, 2, "2") self.assertRaises(TypeError, self.calc.divide, "2", "2") def test_divide_method_fails_with_division_by_zero(self): self.assertRaises(TypeError, self.calc.divide, 2, 0) self.assertRaises(TypeError, self.calc.divide, 2, -0) self.assertRaises(TypeError, self.calc.divide, 0, 0) self.assertRaises(TypeError, self.calc.divide, "0", 0)
def test_modulus(): calc = Calculator() a = calc.do("7%2") assert type(a) == float assert a == 1.0 b = calc.do("40%5") assert type(b) == float assert b == 0.0 c = calc.do("13%11") assert type(c) == float assert c == 2.0
def test_division(): calc = Calculator() a = calc.do("40/5") assert type(a) == float assert a == 8.0 b = calc.do("24/3/2") assert type(b) == float assert b == 4.0 c = calc.do("3/3") assert type(c) == float assert c == 1.0
def test_multiplication(): calc = Calculator() a = calc.do("8*5") assert type(a) == float assert a == 40.0 b = calc.do("3*2*4") assert type(b) == float assert b == 24.0 c = calc.do("3*3*3*3*3*3") assert type(c) == float assert c == 729.0
def test_addition(): calc = Calculator() a = calc.do("3+4") assert type(a) == float assert a == 7.0 b = calc.do("1+2+3") assert type(b) == float assert b == 6.0 c = calc.do("1+2+3+4+5+6+7+8+9") assert type(c) == float assert c == 45.0
def test_subtraction(): calc = Calculator() a = calc.do("8-5") assert type(a) == float assert a == 3.0 b = calc.do("3-2-1") assert type(b) == float assert b == 0.0 c = calc.do("1000-1-1-1-1-1") assert type(c) == float assert c == 995.0
def test_power(): calc = Calculator() a = calc.do("2^5") assert type(a) == float assert a == 32.0 # Intended to mean (2^3)^5, not 2^(3^5). b = calc.do("2^3^5") assert type(b) == float assert b == 32768.0 c = calc.do("0^0") assert type(c) == float assert c == 1.0
class TestCalculate(unittest.TestCase): def setUp(self): self.calc = Calculator() def test_add_method_returns_correct_result(self): self.assertEqual(4, self.calc.add(2, 2)) self.assertEqual(0, self.calc.add(2, -2)) self.assertEqual(0, self.calc.add(-2, 2)) self.assertEqual(1, self.calc.add(1, 0)) def test_divide_method_returns_correct_result(self): self.assertEqual(1, self.calc.divide(2, 2)) self.assertEqual(1.5, self.calc.divide(3, 2)) def test_add_method_fails_with_nan_parameter(self): self.assertRaises(TypeError, self.calc.add, "2", 2) self.assertRaises(TypeError, self.calc.add, 2, "2") self.assertRaises(TypeError, self.calc.add, "2", "2") self.assertRaises(TypeError, self.calc.add, None, 2) self.assertRaises(TypeError, self.calc.add, 2, None) self.assertRaises(TypeError, self.calc.add, object(), 2) self.assertRaises(TypeError, self.calc.add, 2, object()) def test_divide_method_fails_with_nan_parameter(self): self.assertRaises(TypeError, self.calc.divide, "2", 2) self.assertRaises(TypeError, self.calc.divide, 2, "2") self.assertRaises(TypeError, self.calc.divide, "2", "2") def test_divide_method_fails_with_division_by_zero(self): self.assertRaises(TypeError, self.calc.divide, 2, 0) self.assertRaises(TypeError, self.calc.divide, 2, -0) self.assertRaises(TypeError, self.calc.divide, 0, 0) self.assertRaises(TypeError, self.calc.divide, "0", 0) @patch('app.util.validate_permissions', side_effect=mocked_validation, create=True) def test_multiply_method_returns_correct_result(self, _validate_permissions): self.assertEqual(4, self.calc.multiply(2, 2)) self.assertEqual(0, self.calc.multiply(1, 0)) self.assertEqual(0, self.calc.multiply(-1, 0)) self.assertEqual(-2, self.calc.multiply(-1, 2))
from app.calc import Calculator #a = input("Input maths here\n>") b = "41*5" mathser = Calculator() print(mathser.do(b))
def up_down(): c = Calculator() print('start calculating') yield c print('\ncalculating finished')
def step_impl(context): context.calc = Calculator()
import http.client from flask import Flask from app import util from app.calc import Calculator CALCULATOR = Calculator() api_application = Flask(__name__) HEADERS = {"Content-Type": "text/plain", "Access-Control-Allow-Origin": "*"} @api_application.route("/") def hello(): return "Hello from The Calculator!\n" @api_application.route("/calc/add/<op_1>/<op_2>", methods=["GET"]) def add(op_1, op_2): try: num_1, num_2 = util.convert_to_number(op_1), util.convert_to_number( op_2) return ("{}".format(CALCULATOR.add(num_1, num_2)), http.client.OK, HEADERS) except TypeError as e: return (str(e), http.client.BAD_REQUEST, HEADERS) @api_application.route("/calc/substract/<op_1>/<op_2>", methods=["GET"]) def substract(op_1, op_2): try:
def setUp(self): self.calc = Calculator()
import pytest from app.calc import Calculator, adding_static, ComplicatedCalculator calc = Calculator() class TestCalc: def setup(self): self.calc = Calculator def test_adding(self): assert self.calc.adding(self.calc, 2, 2) == 4 def test_multiplying(self): assert self.calc.multiply(self.calc, -2, 2) == -4 def test_dividing(self): assert self.calc.division(self.calc, -2, -2) == 1 def test_substricting(self): assert self.calc.subtraction(self.calc, -2, 0) == -2 def test_adding5(self): assert self.calc.adding(self.calc, 1.5, 1.5) == 3