Completed
Push — master ( 155177...203d27 )
by Chris
01:26
created

TestCamel2Hyphen.test_valid_repeating_upper()   A

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
dl 0
loc 3
rs 10
c 1
b 1
f 0
1
"""Test jinja filters."""
2
3
import json
4
from flask_extras.filters import filters
5
6
7
class MockClass:
8
    """Empty class for testing."""
9
10
11
class TestCamel2Hyphen:
12
    """All tests for camel2hyphen function."""
13
14
    def test_empty_str(self):
15
        """Test."""
16
        assert filters.camel2hyphen('', ) == ''
17
18
    def test_valid_single_upper_first(self):
19
        """Test."""
20
        assert filters.camel2hyphen('CamelCase', ) == 'camel-case'
21
22
    def test_valid_three_hyphen(self):
23
        """Test."""
24
        assert filters.camel2hyphen('bCamelCase', ) == 'b-camel-case'
25
26
    def test_valid_repeating_upper(self):
27
        """Test."""
28
        assert filters.camel2hyphen('bCamelCASE', ) == 'b-camel-case'
29
30
    def test_valid_nubers(self):
31
        """Test."""
32
        assert filters.camel2hyphen('bCamelCASE123', ) == 'b-camel-case123'
33
34
35
class TestToJson:
36
    """All tests for css_selector function."""
37
38
    def test_empty_str(self):
39
        """Test."""
40
        assert filters.to_json('', ) == json.dumps('')
41
42
    def test_empty_str_with_indent(self):
43
        """Test."""
44
        assert filters.to_json('', indent=4) == json.dumps('', indent=4)
45
46
    def test_dict(self):
47
        """Test."""
48
        data = dict(name='bar', id=123)
49
        assert filters.to_json(data) == json.dumps(data)
50
51
    def test_dict_with_indent(self):
52
        """Test."""
53
        data = dict(name='bar', id=123)
54
        assert filters.to_json(data, indent=4) == json.dumps(data, indent=4)
55
56
57
class TestCssSelector:
58
    """All tests for css_selector function."""
59
60
    def test_title_returns_invalid(self):
61
        """Test the return value for a valid type."""
62
        assert filters.css_selector(123) == 123
63
64
    def test_title_returns_str(self):
65
        """Test the return value for a valid type."""
66
        assert isinstance(filters.css_selector('foo bar'), str)
67
68
    def test_basic(self):
69
        """Test the argument."""
70
        assert filters.css_selector('Hello World') == 'hello_world'
71
72
    def test_no_lowercase(self):
73
        """Test the argument."""
74
        expected = 'Hello_World'
75
        assert filters.css_selector('Hello World', lowercase=False) == expected
76
77
78
class TestTitle:
79
    """All tests for title function."""
80
81
    def test_title_returns_str(self):
82
        """Test the return value for a valid type."""
83
        assert isinstance(filters.title('foo bar'), str)
84
85
    def test_title_word(self):
86
        """Test the `word` argument."""
87
        assert filters.title('foo bar') == 'Foo bar'
88
89
    def test_title_capitalize(self):
90
        """Test the `capitalize` argument."""
91
        assert filters.title('foo bar', capitalize=True) == 'Foo Bar'
92
93
    def test_title_capitalize_sentence(self):
94
        """Test the `capitalize` argument."""
95
        res = filters.title('the quick brown fox... ah forget it',
96
                            capitalize=True)
97
        expected = 'The Quick Brown Fox... Ah Forget It'
98
        assert res == expected
99
100
    def test_title_none(self):
101
        """Test the function with None argument."""
102
        assert filters.questionize_label(None) == ''
103
104
105
class TestQuestionizeLabel:
106
    """All tests for questionize label function."""
107
108
    def test_questionize_label_returns_str(self):
109
        """Test the return value for a valid type."""
110
        assert isinstance(filters.questionize_label('foo bar'), str)
111
112
    def test_questionize_label_word_is(self):
113
        """Test the `word` argument."""
114
        assert filters.questionize_label('is_cool') == 'cool?'
115
116
    def test_questionize_label_word_has(self):
117
        """Test the `word` argument."""
118
        assert filters.questionize_label('has_stuff') == 'stuff?'
119
120
    def test_questionize_label_none(self):
121
        """Test the function with None argument."""
122
        assert filters.questionize_label(None) == ''
123
124
125
class TestFirstOf:
126
    """All tests for first of function."""
127
128
    def test_firstof_all_false(self):
129
        """Test what is returned when all values are falsy."""
130
        assert filters.firstof([None, False, 0]) == ''
131
132
    def test_firstof_last_true(self):
133
        """Test what is returned when last value is true."""
134
        assert filters.firstof([None, False, 0, 'yay']) == 'yay'
135
136
    def test_firstof_first_true(self):
137
        """Test what is returned when first value is true."""
138
        assert filters.firstof(['yay', False, 0, 'yay']) == 'yay'
139
140
141
class TestAdd:
142
    """All tests for add function."""
143
144
    def test_returns_updated_list(self):
145
        """Test return value."""
146
        assert filters.add([1, 2], 3) == [1, 2, 3]
147
148
149
class TestCut:
150
    """All tests for cut function."""
151
152
    def test_returns_updated_string(self):
153
        """Test return value."""
154
        assert filters.cut('Hello world', ['world']) == 'Hello '
155
156
    def test_returns_updated_multi(self):
157
        """Test return value."""
158
        assert filters.cut(
159
            'Well hello world', ['hello', 'world']) == 'Well  '
160
161
    def test_returns_updated_multispace(self):
162
        """Test return value."""
163
        assert filters.cut(
164
            'String with spaces', [' ']) == 'Stringwithspaces'
165
166
167
class TestAddSlashes:
168
    """All tests for add slashes function."""
169
170
    def test_returns_updated_basic(self):
171
        """Test return value."""
172
        res = filters.addslashes("I'm using Flask!")
173
        assert res == "I\\'m using Flask!"
174
175
    def test_returns_updated_empty(self):
176
        """Test return value."""
177
        res = filters.addslashes("Using Flask!")
178
        assert res == "Using Flask!"
179
180
    def test_returns_updated_complex(self):
181
        """Test return value."""
182
        res = filters.addslashes("I'm u's'i'n'g Flask!")
183
        assert res == "I\\'m u\\'s\\'i\\'n\\'g Flask!"
184
185
186
class TestDefaultVal:
187
    """All tests for default val function."""
188
189
    def test_returns_default(self):
190
        """Test return value."""
191
        assert filters.default(False, 'default') == 'default'
192
193
    def test_returns_original(self):
194
        """Test return value."""
195
        assert filters.default(1, 'default') == 1
196
197
198
class TestDefaultIfNoneVal:
199
    """All tests for default if none function."""
200
201
    def test_returns_default(self):
202
        """Test return value."""
203
        assert filters.default_if_none(None, 'default') == 'default'
204
205
    def test_returns_original(self):
206
        """Test return value."""
207
        assert filters.default_if_none(1, 'default') == 1
208
209
210
class TestGetDigit:
211
    """All tests for get digit function."""
212
213
    def test_returns_index_empty(self):
214
        """Test return value."""
215
        assert filters.get_digit(123456789, 0) == 123456789
216
217
    def test_returns_index_end(self):
218
        """Test return value."""
219
        assert filters.get_digit(123456789, 1) == 9
220
221
    def test_returns_index_mid(self):
222
        """Test return value."""
223
        assert filters.get_digit(123456789, 5) == 5
224
225
    def test_returns_index_beg(self):
226
        """Test return value."""
227
        assert filters.get_digit(123456789, 9) == 1
228
229
230
class TestLengthIs:
231
    """All tests for length is function."""
232
233
    def test_returns_false(self):
234
        """Test return value."""
235
        assert not filters.length_is('three', 4)
236
237
    def test_returns_true(self):
238
        """Test return value."""
239
        assert filters.length_is('one', 3)
240
241
242
class TestIsUrl:
243
    """All tests for is url function."""
244
245
    def test_returns_urls_true(self):
246
        """Test return value."""
247
        assert filters.is_url('http://foo.bar')
248
        assert filters.is_url('https://foo.bar')
249
250
    def test_returns_urls_false(self):
251
        """Test return value."""
252
        assert not filters.is_url('//foo.bar')
253
254
255
class TestLJust:
256
    """All tests for ljust function."""
257
258
    def test_returns_lpadding(self):
259
        """Test return value."""
260
        assert filters.ljust('Flask', 10) == 'Flask     '
261
262
263
class TestRJust:
264
    """All tests for rjust function."""
265
266
    def test_returns_rpadding(self):
267
        """Test return value."""
268
        assert filters.rjust('Flask', 10) == '     Flask'
269
270
271
class TestMakeList:
272
    """All tests for make list function."""
273
274
    def test_list2list(self):
275
        """Test return value."""
276
        assert filters.make_list([1, 2]) == [1, 2]
277
278
    def test_ints_not_coerced(self):
279
        """Test return value."""
280
        assert filters.make_list(
281
            '12', coerce_numbers=False) == ['1', '2']
282
283
    def test_ints_coerced(self):
284
        """Test return value."""
285
        assert filters.make_list('12') == [1, 2]
286
287
    def test_dict(self):
288
        """Test return value."""
289
        assert filters.make_list({'foo': 'bar'}) == [('foo', 'bar')]
290
291
    def test_list(self):
292
        """Test return value."""
293
        assert filters.make_list([1, 2]) == [1, 2]
294
295
    def test_str(self):
296
        """Test return value."""
297
        assert filters.make_list('abc') == ['a', 'b', 'c']
298
299
300
class TestPhone2Numeric:
301
    """All tests for phone2numeric function."""
302
303
    def test_basic(self):
304
        """Test return value."""
305
        assert filters.phone2numeric('1800-COLLeCT') == '1800-2655328'
306
307
    def test_1_thru_9(self):
308
        """Test return value."""
309
        assert filters.phone2numeric('1800-ADGJMPTX') == '1800-23456789'
310
311
312
class TestSlugify:
313
    """All tests for slugify function."""
314
315
    def test_slugify_plain(self):
316
        """Test return value."""
317
        assert filters.slugify('My news title!') == 'my-news-title'
318
319
    def test_slugify_complex(self):
320
        """Test return value."""
321
        res = filters.slugify('I am an OBFUsc@@Ted URL!!! Foo bar')
322
        expected = 'i-am-an-obfusc--ted-url----foo-bar'
323
        assert res == expected
324
325
326
class TestPagetitle:
327
    """All tests for pagetitle function."""
328
329
    def test_title_plain(self):
330
        """Test return value."""
331
        assert filters.pagetitle('/foo/bar/bam') == ' > foo > bar > bam'
332
333
    def test_title_removefirst(self):
334
        """Test return value."""
335
        res = filters.pagetitle('/foo/bar/bam', divider=' | ')
336
        expected = ' | foo | bar | bam'
337
        assert res == expected
338
339
    def test_title_divider(self):
340
        """Test return value."""
341
        res = filters.pagetitle('/foo/bar/bam', remove_first=True)
342
        assert res == 'foo > bar > bam'
343
344
345
class TestGreet:
346
    """All tests for greet function."""
347
348
    def test_greet(self):
349
        """Test return value."""
350
        assert filters.greet('Chris') == 'Hello, Chris!'
351
352
    def test_greet_override(self):
353
        """Test return value."""
354
        assert filters.greet(
355
            'Chris', greeting='Bonjour' == 'Bonjour, Chris!')
356
357
358
class TestIsList:
359
    """All tests for islist function."""
360
361
    def test_islist(self):
362
        """Test return value."""
363
        assert filters.islist([1, 2, 3])
364
365
    def test_notislist(self):
366
        """Test return value."""
367
        assert not filters.islist('Foo')
368
        assert not filters.islist({'foo': 'bar'})
369
        assert not filters.islist(1)
370
        assert not filters.islist(1.0)
371
372
373
class TestSql2dict:
374
    """All tests for sql2dict function."""
375
376
    def test_none(self):
377
        """Test return value."""
378
        assert filters.sql2dict(None) == []
379
380
    def test_set(self):
381
        """Test return value."""
382
        mm = MockClass()
383
        mm.__dict__ = {'foo': 'bar'}
384
        assert filters.sql2dict([mm]) == [{'foo': 'bar'}]
385