mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Merge pull request #217 from decentral1se/add-more-moulinette-unit-tests
Add tests for text/serialize modules
This commit is contained in:
commit
6b5d271068
4 changed files with 39 additions and 3 deletions
|
@ -12,9 +12,10 @@ class JSONExtendedEncoder(JSONEncoder):
|
|||
|
||||
"""Extended JSON encoder
|
||||
|
||||
Extend default JSON encoder to recognize more types and classes. It
|
||||
will never raise if the object can't be encoded and return its repr
|
||||
Extend default JSON encoder to recognize more types and classes. It will
|
||||
never raise an exception if the object can't be encoded and return its repr
|
||||
instead.
|
||||
|
||||
The following objects and types are supported:
|
||||
- set: converted into list
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ def search(pattern, text, count=0, flags=0):
|
|||
"""Search for pattern in a text
|
||||
|
||||
Scan through text looking for all locations where the regular
|
||||
expresion pattern matches, and return them as a list of strings.
|
||||
expression pattern matches, and return them as a list of strings.
|
||||
|
||||
The optional argument count is the maximum number of pattern
|
||||
occurences to return; count must be an integer. If omitted or zero,
|
||||
|
|
14
test/test_serialize.py
Normal file
14
test/test_serialize.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
from datetime import datetime as dt
|
||||
from moulinette.utils.serialize import JSONExtendedEncoder
|
||||
|
||||
|
||||
def test_json_extended_encoder(caplog):
|
||||
encoder = JSONExtendedEncoder()
|
||||
|
||||
assert encoder.default(set([1, 2, 3])) == [1, 2, 3]
|
||||
|
||||
assert encoder.default(dt(1917, 3, 8)) == '1917-03-08T00:00:00+00:00'
|
||||
|
||||
assert encoder.default(None) == 'None'
|
||||
for message in caplog.messages:
|
||||
assert 'cannot properly encode in JSON' in message
|
21
test/test_text.py
Normal file
21
test/test_text.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from moulinette.utils.text import search, searchf, prependlines, random_ascii
|
||||
|
||||
|
||||
def test_search():
|
||||
assert search('a', 'a a a') == ['a', 'a', 'a']
|
||||
assert search('a', 'a a a', count=2) == ['a', 'a']
|
||||
assert not search('a', 'c c d')
|
||||
|
||||
|
||||
def test_searchf(test_file):
|
||||
assert searchf('bar', str(test_file)) == ['bar']
|
||||
assert not searchf('baz', str(test_file))
|
||||
|
||||
|
||||
def test_prependlines():
|
||||
assert prependlines('abc\nedf\nghi', 'XXX') == 'XXXabc\nXXXedf\nXXXghi'
|
||||
assert prependlines('', 'XXX') == 'XXX'
|
||||
|
||||
|
||||
def test_random_ascii():
|
||||
assert isinstance(random_ascii(length=2), unicode)
|
Loading…
Reference in a new issue