예제 #1
0
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"))
예제 #2
0
 def test_more_args_than_arity_zero(self):
     with self.assertRaises(ValueError):
         curry_explicit(lambda *xs: print(xs), 0)(1)
예제 #3
0
 def call_of_already_called_fun(self):
     with self.assertRaises(ValueError):
         curry_explicit(lambda *xs: print(xs), 0)()()
예제 #4
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
예제 #5
0
 def test_n(self):
     f0 = curry_explicit(lambda *xs: print(xs), 0)
     f0()  # there aren't any error
예제 #6
0
 def test_one_argument_more_args_than_arity(self):
     with self.assertRaises(TypeError):
         curry_explicit(fun_with_one_argument, 1)("1")("2")
예제 #7
0
 def test_zero_arguments_more_args_than_arity(self):
     with self.assertRaises(ValueError):
         curry_explicit(fun_with_no_argument, 0)("1")("2")
예제 #8
0
 def test_several_arguments_less_args_than_arity(self):
     self.assert_(isinstance(curry_explicit(fun_with_several_arguments, 3)("1")("2"), Callable))
예제 #9
0
 def test_one_argument_less_args_than_arity(self):
     self.assert_(isinstance(curry_explicit(fun_with_one_argument, 1), Callable))
예제 #10
0
 def test_negative_arity(self):
     with self.assertRaises(AssertionError):
         curry_explicit(fun_with_infinity_arguments, -5)("1")
예제 #11
0
 def test_infinity_arguments_less_args_than_arity(self):
     with self.assertRaises(TypeError):
         curry_explicit(fun_with_infinity_arguments, 1)("1")("2")
예제 #12
0
 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")
     )
예제 #13
0
 def test_infinity_arguments_arity1(self):
     return self.assertEqual(curry_explicit(fun_with_infinity_arguments, 1)("1"), fun_with_infinity_arguments("1"))
예제 #14
0
 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"))
예제 #15
0
 def test_one_argument(self):
     return self.assertEqual(curry_explicit(fun_with_one_argument, 1)("str"), fun_with_one_argument("str"))
예제 #16
0
 def test_zero_arguments(self):
     return self.assertEqual(curry_explicit(fun_with_no_argument, 0)(), fun_with_no_argument())