File: test_utils.py

package info (click to toggle)
python-pbcommand 0.2.17-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 556 kB
  • sloc: python: 3,451; makefile: 200
file content (53 lines) | stat: -rw-r--r-- 1,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import functools
import unittest
from pbcommand.utils import Singleton, compose


class TestSingleton(unittest.TestCase):

    def test_basic(self):
        class Lithium(object):
            __metaclass__ = Singleton

            def __init__(self):
                self.name = 'Lithium'
                self.number = 3

        a = Lithium()
        b = Lithium()
        self.assertEqual(id(a), id(b))


class TestCompose(unittest.TestCase):
    def test_simple(self):
        f = lambda x: x * 2
        g = lambda y: y + 2

        h = compose(f, g)
        value = h(7)
        self.assertEquals(value, 18)

    def test_no_args_list(self):

        def _f():
            return compose()

        self.assertRaises(ValueError, _f)

    def test_empty_list(self):
        def _f():
            return compose([])

        self.assertRaises(TypeError, _f)

    def test_partial(self):

        def add(a, b):
            return a + b

        add_five = functools.partial(add, 5)
        add_two = functools.partial(add, 2)

        f = compose(add_five, add_two)
        value = f(5)
        self.assertEquals(value, 12)