Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Pull Request #56

Open
wants to merge 9 commits into
base: 55-None
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

def add(x, y):
return x + y
class Calculator:
def add(self, x, y):
return x + y

def subtract(x, y):
return x - y
def subtract(self, x, y):
return x - y

def multiply(x, y):
return x * y
def multiply(self, x, y):
return x * y

def divide(x, y):
if y == 0:
raise ValueError('Cannot divide by zero')
return x / y
def divide(self, x, y):
if y == 0:
raise ValueError('Cannot divide by zero!')
return x / y
63 changes: 48 additions & 15 deletions test_calculator.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@

import unittest
from calculator import add, subtract, multiply, divide

from calculator import Calculator


class TestCalculator(unittest.TestCase):
def setUp(self):
self.calc = Calculator()

def test_add(self):
self.assertEqual(add(1, 2), 3)
self.assertEqual(add(-1, 1), 0)
self.assertEqual(add(-1, -1), -2)
self.assertEqual(self.calc.add(10, 5), 15)

def test_add_negative_with_positive(self):
self.assertEqual(self.calc.add(-1, 1), 0)

def test_add_negative_with_negative(self):
self.assertEqual(self.calc.add(-1, -1), -2)

def test_add_zeros(self):
self.assertEqual(self.calc.add(0, 0), 0)

def test_subtract(self):
self.assertEqual(subtract(10, 5), 5)
self.assertEqual(subtract(-1, 1), -2)
self.assertEqual(subtract(-1, -1), 0)
self.assertEqual(self.calc.subtract(10, 5), 5)

def test_subtract_negative_with_positive(self):
self.assertEqual(self.calc.subtract(-1, 1), -2)

def test_subtract_negative_with_negative(self):
self.assertEqual(self.calc.subtract(-1, -1), 0)

def test_subtract_zeros(self):
self.assertEqual(self.calc.subtract(0, 0), 0)

def test_multiply(self):
self.assertEqual(multiply(3, 7), 21)
self.assertEqual(multiply(-1, 8), -8)
self.assertEqual(multiply(-2, -2), 4)
self.assertEqual(self.calc.multiply(3, 5), 15)

def test_multiply_by_zero(self):
self.assertEqual(self.calc.multiply(3, 0), 0)
self.assertEqual(self.calc.multiply(0, 5), 0)

def test_multiply_with_negative(self):
self.assertEqual(self.calc.multiply(-3, 5), -15)

def test_divide(self):
self.assertEqual(divide(10, 2), 5)
self.assertEqual(divide(9, 3), 3)
self.assertEqual(divide(-6, 2), -3)
self.assertEqual(self.calc.divide(10, 2), 5)

def test_divide_with_floats(self):
self.assertAlmostEqual(self.calc.divide(7, 2), 3.5)

def test_divide_by_one(self):
self.assertEqual(self.calc.divide(7, 1), 7)

def test_divide_negative(self):
self.assertEqual(self.calc.divide(-10, 2), -5)

def test_divide_by_zero(self):
with self.assertRaises(ValueError):
divide(10, 0)
self.calc.divide(10, 0)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()