File: test_extensions.py

package info (click to toggle)
python-assertpy 1.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 628 kB
  • sloc: python: 5,712; makefile: 4; sh: 3
file content (230 lines) | stat: -rw-r--r-- 7,676 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Copyright (c) 2015-2019, Activision Publishing, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import numbers
from assertpy import assert_that, add_extension, remove_extension, fail


def is_even(self):
    if isinstance(self.val, numbers.Integral) is False:
        raise TypeError('val must be an integer')
    if self.val % 2 != 0:
        self.error('Expected <%s> to be even, but was not.' % (self.val))
    return self


def is_multiple_of(self, other):
    if isinstance(self.val, numbers.Integral) is False or self.val <= 0:
        raise TypeError('val must be a positive integer')

    if isinstance(other, numbers.Integral) is False or other <= 0:
        raise TypeError('given arg must be a positive integer')

    _, rem = divmod(self.val, other)
    if rem > 0:
        self.error('Expected <%s> to be multiple of <%s>, but was not.' % (self.val, other))

    return self


def is_factor_of(self, other):
    if isinstance(self.val, numbers.Integral) is False or self.val <= 0:
        raise TypeError('val must be a positive integer')

    if isinstance(other, numbers.Integral) is False or other <= 0:
        raise TypeError('given arg must be a positive integer')

    _, rem = divmod(other, self.val)
    if rem > 0:
        self.error('Expected <%s> to be factor of <%s>, but was not.' % (self.val, other))

    return self


add_extension(is_even)
add_extension(is_multiple_of)
add_extension(is_factor_of)


def test_is_even_extension():
    assert_that(124).is_even()
    assert_that(124).is_type_of(int).is_even().is_greater_than(123).is_less_than(125).is_equal_to(124)


def test_is_even_extension_failure():
    try:
        assert_that(123).is_even()
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to('Expected <123> to be even, but was not.')


def test_is_even_extension_failure_not_callable():
    try:
        add_extension('foo')
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to('func must be callable')


def test_is_even_extension_failure_not_integer():
    try:
        assert_that(124.0).is_even()
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to('val must be an integer')


def test_is_multiple_of_extension():
    assert_that(24).is_multiple_of(1)
    assert_that(24).is_multiple_of(2)
    assert_that(24).is_multiple_of(3)
    assert_that(24).is_multiple_of(4)
    assert_that(24).is_multiple_of(6)
    assert_that(24).is_multiple_of(8)
    assert_that(24).is_multiple_of(12)
    assert_that(24).is_multiple_of(24)
    assert_that(124).is_type_of(int).is_even().is_multiple_of(31).is_equal_to(124)


def test_is_multiple_of_extension_failure():
    try:
        assert_that(24).is_multiple_of(5)
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to('Expected <24> to be multiple of <5>, but was not.')


def test_is_multiple_of_extension_failure_bad_val():
    try:
        assert_that(24.0).is_multiple_of(5)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to('val must be a positive integer')


def test_is_multiple_of_extension_failure_negative_val():
    try:
        assert_that(-24).is_multiple_of(6)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to('val must be a positive integer')


def test_is_multiple_of_extension_failure_bad_arg():
    try:
        assert_that(24).is_multiple_of('foo')
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to('given arg must be a positive integer')


def test_is_multiple_of_extension_failure_negative_arg():
    try:
        assert_that(24).is_multiple_of(-6)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to('given arg must be a positive integer')


def test_is_factor_of_extension():
    assert_that(1).is_factor_of(24)
    assert_that(2).is_factor_of(24)
    assert_that(3).is_factor_of(24)
    assert_that(4).is_factor_of(24)
    assert_that(6).is_factor_of(24)
    assert_that(8).is_factor_of(24)
    assert_that(12).is_factor_of(24)
    assert_that(24).is_factor_of(24)
    assert_that(31).is_type_of(int).is_factor_of(124).is_equal_to(31)


def test_is_factor_of_extension_failure():
    try:
        assert_that(5).is_factor_of(24)
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to('Expected <5> to be factor of <24>, but was not.')


def test_call_missing_extension():
    def is_missing(): pass
    try:
        remove_extension(is_even)
        remove_extension(is_multiple_of)
        remove_extension(is_factor_of)
        remove_extension(is_missing)
        assert_that(24).is_multiple_of(6)
        fail('should have raised error')
    except AttributeError as ex:
        assert_that(str(ex)).is_equal_to('assertpy has no assertion <is_multiple_of()>')


def test_remove_bad_extension():
    try:
        remove_extension('foo')
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to('func must be callable')


def is_foo(self):
    if self.val != 'foo':
        self.error('Expected <%s> to be foo, but was not.' % (self.val))
    return self


def dupe1():
    add_extension(is_foo)
    assert_that('foo').is_foo()
    try:
        assert_that('FOO').is_foo()
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to('Expected <FOO> to be foo, but was not.')


def dupe2():
    def is_foo(self):
        if self.val != 'FOO':
            self.error('Expected <%s> to be FOO, but was not.' % (self.val))
        return self

    add_extension(is_foo)
    assert_that('FOO').is_foo()
    try:
        assert_that('foo').is_foo()
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to('Expected <foo> to be FOO, but was not.')


def test_dupe_extensions():
    dupe1()
    dupe2()
    dupe1()