|
| 1 | +# https://www.codewars.com/kumite/5ab735bee7093b17b2000084?sel=5ab735bee7093b17b2000084 |
| 2 | +import codewars_test as test |
| 3 | + |
| 4 | + |
| 5 | +def f0(): |
| 6 | + pass |
| 7 | + |
| 8 | + |
| 9 | +# BaseException >> Exception |
| 10 | +def f1(): |
| 11 | + raise Exception() |
| 12 | + |
| 13 | + |
| 14 | +# BaseException >> Exception >> ArithmeticError >> ZeroDivisionError |
| 15 | +def f2(): |
| 16 | + return 1 // 0 |
| 17 | + |
| 18 | + |
| 19 | +# BaseException >> Exception >> LookupError >> KeyError |
| 20 | +def f3(): |
| 21 | + return {}[1] |
| 22 | + |
| 23 | + |
| 24 | +excn = ( |
| 25 | + "Exception", |
| 26 | + "ArithmeticError", |
| 27 | + "ZeroDivisionError", |
| 28 | + "LookupError", |
| 29 | + "KeyError", |
| 30 | + "OSError", |
| 31 | +) |
| 32 | +exc = (Exception, ArithmeticError, ZeroDivisionError, LookupError, KeyError, OSError) |
| 33 | + |
| 34 | + |
| 35 | +@test.describe("expect_error, new version") |
| 36 | +def d2(): |
| 37 | + @test.it("f0 raises nothing") |
| 38 | + def i0(): |
| 39 | + test.expect_error("f0 did not raise any exception", f0) |
| 40 | + for i in range(6): |
| 41 | + test.expect_error("f0 did not raise {}".format(excn[i]), f0, exc[i]) |
| 42 | + |
| 43 | + @test.it("f1 raises Exception") |
| 44 | + def i1(): |
| 45 | + test.expect_error("f1 did not raise Exception", f1) |
| 46 | + for i in range(6): |
| 47 | + test.expect_error("f1 did not raise {}".format(excn[i]), f1, exc[i]) |
| 48 | + |
| 49 | + @test.it("f2 raises Exception >> ArithmeticError >> ZeroDivisionError") |
| 50 | + def i2(): |
| 51 | + test.expect_error("f2 did not raise Exception", f2) |
| 52 | + for i in range(6): |
| 53 | + test.expect_error("f2 did not raise {}".format(excn[i]), f2, exc[i]) |
| 54 | + |
| 55 | + @test.it("f3 raises Exception >> LookupError >> KeyError") |
| 56 | + def i3(): |
| 57 | + test.expect_error("f3 did not raise Exception", f3) |
| 58 | + for i in range(6): |
| 59 | + test.expect_error("f3 did not raise {}".format(excn[i]), f3, exc[i]) |
| 60 | + |
0 commit comments