def test_truediv(): test_frac = soln.Fraction(1, 3) test2_frac = soln.Fraction(2, 3) test3_frac = soln.Fraction(0, 3) assert test_frac.__truediv__(test2_frac) == soln.Fraction(1, 2) with pytest.raises(ZeroDivisionError) as excinfo: test_frac.__truediv__(test3_frac)
def test_fraction_init(set_up_fractions): frac_1_3, frac_1_2, frac_n2_3 = set_up_fractions assert frac_1_3.numer == 1 assert frac_1_2.denom == 2 assert frac_n2_3.numer == -2 frac = soln.Fraction(30, 42) # 30/42 reduces to 5/7. assert frac.numer == 5 assert frac.denom == 7
def test_fraction_init(set_up_fractions): frac_1_1, frac_1_3, frac_1_2, frac_n2_3 = set_up_fractions assert frac_1_3.numer == 1 assert frac_1_2.denom == 2 assert frac_n2_3.numer == -2 frac = soln.Fraction(30, 42) assert frac.numer == 5 assert frac.denom == 7 with pytest.raises(ZeroDivisionError) as excinfo: soln.Fraction(4, 0) assert excinfo.value.args[0] == "denominator cannot be zero" with pytest.raises(TypeError) as excinfo: soln.Fraction(2.1, 2) assert excinfo.value.args[ 0] == "numerator and denominator must be integers"
def test_fraction_eq(set_up_fractions): frac_1_3, frac_1_2, frac_n2_3 = set_up_fractions assert frac_1_2 == soln.Fraction(1, 2) assert frac_1_3 == soln.Fraction(2, 6) assert frac_n2_3 == soln.Fraction(8, -12)
def set_up_fractions(): frac_1_3 = soln.Fraction(1, 3) frac_1_2 = soln.Fraction(1, 2) frac_n2_3 = soln.Fraction(-2, 3) return frac_1_3, frac_1_2, frac_n2_3
def test_mul(): test_frac = soln.Fraction(2, 3) test2_frac = soln.Fraction(1, 3) assert test_frac.__mul__(test2_frac) == soln.Fraction(2, 9)
def test_sub(): test_frac = soln.Fraction(2, 3) test2_frac = soln.Fraction(1, 3) assert test_frac.__sub__(test2_frac) == soln.Fraction(1, 3)
def test_add(): frac_1_3, frac_1_2 = soln.Fraction(1, 3), soln.Fraction(1, 2) assert soln.Fraction.__add__(frac_1_3, frac_1_2) == soln.Fraction(5, 6)
def test_eq(): test_frac = soln.Fraction(1, 3) test2_frac = soln.Fraction(1, 2) assert test_frac.__eq__(test_frac) == True assert test_frac.__eq__(5) == False
def test_str(): test_frac = soln.Fraction(5, 1) assert test_frac.__str__() == str(test_frac.numer), 'denom = 1 fails'
def test_fraction_true_div(): with pytest.raises(ZeroDivisionError) as excinfo: soln.Fraction(1, 1) / soln.Fraction(0, 1) assert excinfo.value.args[0] == "cannot divide by zero" assert soln.Fraction(1, 1) / soln.Fraction(1, 2) == 2
def test_fraction__mul(set_up_fractions): frac_1_1, frac_1_3, frac_1_2, frac_n2_3 = set_up_fractions assert frac_1_1 * frac_1_3 == frac_1_3 assert frac_1_2 * frac_n2_3 == soln.Fraction(-1, 3)
def test_fraction__sub(set_up_fractions): frac_1_1, frac_1_3, frac_1_2, frac_n2_3 = set_up_fractions assert frac_1_1 - frac_1_2 == frac_1_2 assert frac_1_1 - frac_n2_3 == soln.Fraction(5, 3)
def test_fraction__add(set_up_fractions): frac_1_1, frac_1_3, frac_1_2, frac_n2_3 = set_up_fractions assert soln.Fraction(1, 2) + soln.Fraction(1, 2) == 1 assert frac_1_3 + frac_n2_3 == soln.Fraction(-1, 3)