File: test_operator_jy.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (82 lines) | stat: -rw-r--r-- 2,560 bytes parent folder | download | duplicates (4)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Misc operator module tests

Made for Jython.
"""
import collections
import operator
import sys
import unittest
from test import test_support

class OperatorTestCase(unittest.TestCase):

    class NewStyle(object):
        pass
    class OldStyle:
        pass
    class HasGetitem(object):
        def __getitem__(self, name):
            return 'foo'
    class HasInt(object):
        def __int__(self):
            return 1
    class HasLong(object):
        def __long__(self):
            return 1
    class HasFloat(object):
        def __float__(self):
            return 1.0

    # obj, isNumberType, isMappingType, isSequenceType
    tests = (
        (type, False, False, False),
        (type.__dict__, False, True, False), # dictproxy
        (globals(), False, True, False), # stringmap
        ({}, False, True, False),
        ('', False, False, True),
        (u'', False, False, True),
        ([], False, False, True),
        ((), False, False, True),
        (xrange(5), False, False, True),
        (set(), False, False, False),
        (frozenset(), False, False, False),
        (1, True, False, False),
        (2L, True, False, False),
        (3.0, True, False, False),
        (4j, True, False, False),
        (None, False, False, False),
        (Ellipsis, False, False, False),
        (Exception(), False, False, True),
        (collections.deque(), False, False, True),
        (collections.defaultdict(), False, True, False),
        (collections.namedtuple('test', 't'), False, False, False),
        (NewStyle(), False, False, False),
        (OldStyle(), not sys.platform.startswith('java'), False, False),
        (HasGetitem(), False, True, True),
        (HasInt(), True, False, False),
        (HasFloat(), True, False, False),
        )
    
    def test_isNumberType(self):
        for obj, isNumberType, _, _ in self.tests:
            self.assert_istype(operator.isNumberType, obj, isNumberType)

    def test_isMappingType(self):
        for obj, _, isMappingType, _ in self.tests:
            self.assert_istype(operator.isMappingType, obj, isMappingType)

    def test_isSequenceType(self):
        for obj, _, _, isSequenceType in self.tests:
            self.assert_istype(operator.isSequenceType, obj, isSequenceType)

    def assert_istype(self, func, obj, result):
        self.assertEqual(func(obj), result, '%s %s should be: %s' %
                         (type(obj), func.__name__, result))


def test_main():
    test_support.run_unittest(OperatorTestCase)


if __name__ == "__main__":
    test_main()