Skip to content

Commit

Permalink
Python custom tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtoal committed Aug 9, 2024
1 parent a02a20c commit bf36081
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ lua test.lua
### Python

```
python3 -m unittest exercises_test.py
python3 exercises_test.py
```

### JavaScript
Expand Down
2 changes: 1 addition & 1 deletion python/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def change(amount: int) -> Tuple[int, int, int, int]:
change_counts.append(coins)
return tuple(change_counts)

# Write your first_then_lowercase function here
# Write your first then lower case function here

# Write your powers generator here

Expand Down
164 changes: 78 additions & 86 deletions python/exercises_test.py
Original file line number Diff line number Diff line change
@@ -1,102 +1,94 @@
import unittest
from exercises import (
# first_then_lowercase,
# first_then_lower_case,
# say,
# powers_generator,
# misc_file_stats,
# Quaternion,
change)


class TestExercises(unittest.TestCase):
counts = {True: 0, False: 0}
def suite(title):
print(f"\nTesting {title}", end="")
def check(condition):
counts[condition] += 1
print(" ." if condition else ". F", end="")
def check_error(error_type, text, fun, *args):
try:
fun(*args)
check(False)
except Exception as e:
check(isinstance(e, error_type))
if text is not None:
check(text in str(e))

def test_change(self):
self.assertEqual(change(0), (0, 0, 0, 0))
self.assertEqual(change(99), (3, 2, 0, 4))
self.assertEqual(change(8), (0, 0, 1, 3))
self.assertEqual(change(42), (1, 1, 1, 2))
self.assertEqual(change(250), (10, 0, 0, 0))
self.assertEqual(change(144), (5, 1, 1, 4))
self.assertEqual(change(97), (3, 2, 0, 2))
self.assertEqual(change(100000000037), (4000000001, 1, 0, 2))
self.assertEqual(change(10000000000005), (400000000000, 0, 1, 0))
with self.assertRaisesRegex(TypeError, "Amount must be an integer"):
change(988.25)
with self.assertRaisesRegex(ValueError, "Amount cannot be negative"):
change(-50)
suite("change")
check_error(TypeError, "Amount must be an integer", change, 3.5)
check_error(ValueError, "Amount cannot be negative", change, -50)
check(change(0) == (0, 0, 0, 0))
check(change(99) == (3, 2, 0, 4))
check(change(8) == (0, 0, 1, 3))
check(change(42) == (1, 1, 1, 2))
check(change(250) == (10, 0, 0, 0))
check(change(144) == (5, 1, 1, 4))
check(change(97) == (3, 2, 0, 2))
check(change(100000000037) == (4000000001, 1, 0, 2))
check(change(10000000000005) == (400000000000, 0, 1, 0))

# Uncomment the following tests as you complete the exercises
# and remove this comment that tells you to uncomment!
# Uncomment the following tests as you complete the exercises
# and remove this comment that tells you to uncomment!

# def test_first_then_lowercase(self):
# def nonempty(s): return s != ""
# def length_greater_than_3(s): return len(s) > 3
# self.assertEqual(
# first_then_lowercase([], nonempty),
# None)
# self.assertEqual(
# first_then_lowercase(["", "A", "B"], nonempty),
# "a")
# self.assertEqual(
# first_then_lowercase(["", "A", "ABC"], length_greater_than_3),
# None)
# self.assertEqual(
# first_then_lowercase(["ABC", "ABCD", "ABCDE"], length_greater_than_3),
# "abcd")
# suite("first_then_lowercase")
# def nonempty(s): return s != ""
# def length_greater_than_3(s): return len(s) > 3
# check(first_then_lower_case([], nonempty) is None)
# check(first_then_lower_case(["", "A", "B"], nonempty) == "a")
# check(first_then_lower_case(["", "A", "ABC"], length_greater_than_3) is None)
# check(first_then_lower_case(["ABC", "ABCD", "ABCDE"], length_greater_than_3) == "abcd")

# def test_say(self):
# self.assertEqual(say(), "")
# self.assertEqual(say("hi")(), "hi")
# self.assertEqual(say("hi")("there")(), "hi there")
# self.assertEqual(
# say("hello")("my")("name")("is")("Colette")(),
# "hello my name is Colette"
# )
# self.assertEqual(say("h i")(), "h i")
# self.assertEqual(say("hi ")(" there")(), "hi there")
# self.assertEqual(say("")("")("dog")("")("go")(), " dog go")
# self.assertEqual(say("πŸ˜„πŸ€—")("πŸ’€πŸ‘ŠπŸΎ")(), "πŸ˜„πŸ€— πŸ’€πŸ‘ŠπŸΎ")
# suite("say")
# check(say() == "")
# check(say("hi")() == "hi")
# check(say("hi")("there")() == "hi there")
# check(say("hello")("my")("name")("is")("Colette")() == "hello my name is Colette")
# check(say("h i")() == "h i")
# check(say("hi ")(" there")() == "hi there")
# check(say("")("")("dog")("")("go")() == " dog go")
# check(say("πŸ˜„πŸ€—")("πŸ’€πŸ‘ŠπŸΎ")() == "πŸ˜„πŸ€— πŸ’€πŸ‘ŠπŸΎ")

# def test_powers_generator(self):
# g1 = powers_generator(base=2, limit=1)
# self.assertEqual(next(g1), 1)
# with self.assertRaises(StopIteration):
# next(g1)
# g2 = powers_generator(base=3, limit=100)
# self.assertEqual(next(g2), 1)
# self.assertEqual(next(g2), 3)
# self.assertEqual(next(g2), 9)
# self.assertEqual(next(g2), 27)
# self.assertEqual(next(g2), 81)
# with self.assertRaises(StopIteration):
# next(g2)
# self.assertEqual(list(powers_generator(base=3, limit=27)), [1, 3, 9, 27])
# suite("powers_generator")
# g1 = powers_generator(base=2, limit=1)
# check(next(g1) == 1)
# check_error(StopIteration, None, next, g1)
# g2 = powers_generator(base=3, limit=100)
# check(next(g2) == 1)
# check(next(g2) == 3)
# check(next(g2) == 9)
# check(next(g2) == 27)
# check(next(g2) == 81)
# check_error(StopIteration, None, next, g2)
# check(list(powers_generator(base=3, limit=27)) == [1, 3, 9, 27])

# def test_misc_file_stats(self):
# self.assertEqual(
# misc_file_stats("NoSuchFile.txt"),
# {"error": "No such file"})
# self.assertEqual(
# misc_file_stats("../test-for-line-count.txt"),
# {"lineCount": 13, "blankLineCount": 7, "hashedLineCount": 2})
# suite("misc_file_stats")
# check(misc_file_stats("NoSuchFile.txt") == {"error": "No such file"})
# check(misc_file_stats("../test-for-line-count.txt") == {
# "lineCount": 13, "blankLineCount": 7, "hashedLineCount": 2})

# def test_Quaternion(self):
# q1 = Quaternion(8, 5, -3, 1)
# self.assertEqual(q1.coefficients, (8, 5, -3, 1))
# self.assertEqual(q1, Quaternion(8, 5, -3, 1))
# self.assertEqual(q1.conjugate, Quaternion(8, -5, 3, -1))
# q2 = Quaternion(0, 0, 0, 0)
# self.assertEqual(q2.coefficients, (0, 0, 0, 0))
# self.assertEqual(q2.conjugate, Quaternion(0, 0, 0, 0))
# q3 = Quaternion(13, 21, -5, -21)
# q4 = Quaternion(2, -1, -55, 2.5)
# self.assertEqual((q3 + q4).coefficients, (15, 20, -60, -18.5))
# self.assertEqual((q3 + q4).conjugate, Quaternion(15, -20, 60, 18.5))
# q5 = Quaternion(3, -5, 1, -8)
# q6 = Quaternion(2, -13, -2, 3)
# self.assertEqual((q5 * q6).coefficients, (-33, -62, 115, 16))
# self.assertEqual((q5 * q6).conjugate, Quaternion(-33, 62, -115, -16))
# suite("Quaternion")
# q1 = Quaternion(8, 5, -3, 1)
# check(q1.coefficients == (8, 5, -3, 1))
# check(q1 == Quaternion(8, 5, -3, 1))
# check(q1.conjugate == Quaternion(8, -5, 3, -1))
# q2 = Quaternion(0, 0, 0, 0)
# check(q2.coefficients == (0, 0, 0, 0))
# check(q2.conjugate == Quaternion(0, 0, 0, 0))
# q3 = Quaternion(13, 21, -5, -21)
# q4 = Quaternion(2, -1, -55, 2.5)
# check((q3 + q4).coefficients == (15, 20, -60, -18.5))
# check((q3 + q4).conjugate == Quaternion(15, -20, 60, 18.5))
# q5 = Quaternion(3, -5, 1, -8)
# q6 = Quaternion(2, -13, -2, 3)
# check((q5 * q6).coefficients == (-33, -62, 115, 16))
# check((q5 * q6).conjugate == Quaternion(-33, 62, -115, -16))


if __name__ == '__main__':
unittest.main()
print(f"\n{counts[True]} passed, {counts[False]} failed")

0 comments on commit bf36081

Please sign in to comment.