import unittest from typing import Callable from homeworks.homework_2.task_1.uncurry import uncurry_explicit from homeworks.homework_2.task_1.curry import curry_explicit from tests.homework_2.task_1.test_curry import ( fun_with_no_argument, fun_with_several_arguments, fun_with_one_argument, fun_with_infinity_arguments, ) fun_with_no_argument_curried = curry_explicit(fun_with_no_argument, 0) fun_with_one_argument_curried = curry_explicit(fun_with_one_argument, 1) fun_with_several_arguments_curried = curry_explicit(fun_with_several_arguments, 3) fun_with_infinity_arguments_curried = curry_explicit( fun_with_infinity_arguments, 4) class UncurryTest(unittest.TestCase): def test_zero_arguments(self): return self.assertEqual( uncurry_explicit(fun_with_no_argument_curried, 0)(), fun_with_no_argument()) def test_one_argument(self): return self.assertEqual( uncurry_explicit(fun_with_one_argument_curried, 1)("str"), fun_with_one_argument("str"))
def test_more_args_than_arity_zero(self): with self.assertRaises(ValueError): curry_explicit(lambda *xs: print(xs), 0)(1)
def call_of_already_called_fun(self): with self.assertRaises(ValueError): curry_explicit(lambda *xs: print(xs), 0)()()
def test_two_sequential_function_call(self): f1 = curry_explicit(lambda *args: " ".join(args), 4) f2 = f1("10")("20") f1("5")("15")("25")("35") # call this, if we try to call this function another time, we'll get error self.assertEqual(f2("30")("40"), "10 20 30 40") # no mistake -> we call another instance of function
def test_n(self): f0 = curry_explicit(lambda *xs: print(xs), 0) f0() # there aren't any error
def test_one_argument_more_args_than_arity(self): with self.assertRaises(TypeError): curry_explicit(fun_with_one_argument, 1)("1")("2")
def test_zero_arguments_more_args_than_arity(self): with self.assertRaises(ValueError): curry_explicit(fun_with_no_argument, 0)("1")("2")
def test_several_arguments_less_args_than_arity(self): self.assert_(isinstance(curry_explicit(fun_with_several_arguments, 3)("1")("2"), Callable))
def test_one_argument_less_args_than_arity(self): self.assert_(isinstance(curry_explicit(fun_with_one_argument, 1), Callable))
def test_negative_arity(self): with self.assertRaises(AssertionError): curry_explicit(fun_with_infinity_arguments, -5)("1")
def test_infinity_arguments_less_args_than_arity(self): with self.assertRaises(TypeError): curry_explicit(fun_with_infinity_arguments, 1)("1")("2")
def test_infinity_arguments_arity3(self): return self.assertEqual( curry_explicit(fun_with_infinity_arguments, 3)("1")("2")("3"), fun_with_infinity_arguments("1", "2", "3") )
def test_infinity_arguments_arity1(self): return self.assertEqual(curry_explicit(fun_with_infinity_arguments, 1)("1"), fun_with_infinity_arguments("1"))
def test_several_arguments(self): f2 = curry_explicit(fun_with_several_arguments, 3) return self.assertEqual(f2("123")("456")("789"), fun_with_several_arguments("123", "456", "789"))
def test_one_argument(self): return self.assertEqual(curry_explicit(fun_with_one_argument, 1)("str"), fun_with_one_argument("str"))
def test_zero_arguments(self): return self.assertEqual(curry_explicit(fun_with_no_argument, 0)(), fun_with_no_argument())