diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2020-11-25 11:13:20 +0100 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2020-11-25 11:13:20 +0100 |
commit | 71028fa45e7b65de62402d6a049afa16baca39e8 (patch) | |
tree | ccd3445e5e87349e8d2b1b316b0ac256fb2e8c97 | |
parent | 286bd8b6f17beb77829c7fabcb8e7b1c48b734e3 (diff) | |
download | lulua-71028fa45e7b65de62402d6a049afa16baca39e8.tar.gz lulua-71028fa45e7b65de62402d6a049afa16baca39e8.tar.bz2 lulua-71028fa45e7b65de62402d6a049afa16baca39e8.zip |
Add a few more tests
-rw-r--r-- | lulua/test_text.py | 23 | ||||
-rw-r--r-- | lulua/test_util.py | 18 | ||||
-rw-r--r-- | lulua/util.py | 5 |
3 files changed, 41 insertions, 5 deletions
diff --git a/lulua/test_text.py b/lulua/test_text.py index 75673cb..b20e7f3 100644 --- a/lulua/test_text.py +++ b/lulua/test_text.py @@ -22,7 +22,7 @@ import brotli from io import BytesIO, StringIO import html5lib -from .text import charMap, mapChars, BrotliFile, HTMLSerializer +from .text import charMap, mapChars, BrotliFile, HTMLSerializer, apply, iterchar def test_map_chars_mapped (): """ Make sure all chars in the map are mapped correctly """ @@ -61,10 +61,27 @@ def test_brotlifile (): def test_htmlserialized (): document = html5lib.parse (StringIO ("""<html><body> -<p>Hello & World!</p> +<p>Hello & <!-- comment -->Wörld! ♣ ∖</p> </body></html>""")) walker = html5lib.getTreeWalker("etree") stream = walker (document) s = HTMLSerializer() - assert ''.join (s.serialize(stream)) == ' Hello & World!\n\n ' + assert ''.join (s.serialize(stream)) == ' Hello & Wörld! ♣ \u2216\n\n ' + +def test_apply (): + def f1 (x): + yield x*3 + + def f2 (x): + yield x*5 + yield x*7 + + funcs = [f1, f2] + data = [1, 2] + assert list (apply (funcs, data)) == [1*3*5, 1*3*7, 2*3*5, 2*3*7] + +def test_iterchar (): + s = 'Hello World!'*100 + with StringIO (s) as fd: + assert ''.join (iterchar (fd)) == s diff --git a/lulua/test_util.py b/lulua/test_util.py index 1c321d9..5e15963 100644 --- a/lulua/test_util.py +++ b/lulua/test_util.py @@ -20,7 +20,7 @@ import pytest -from .util import displayText +from .util import displayText, limit, first @pytest.mark.parametrize("s,expected", [ ('foobar', False), @@ -33,3 +33,19 @@ from .util import displayText def test_displayTextCombining (s, expected): assert displayText (s).startswith ('\u25cc') == expected +@pytest.mark.parametrize("l,n,expected", [ + ([], 1, []), + (range (3), 0, []), + (range (3), 3, list (range (3))), + (range (1), 100, list (range (1))), + (range (10000), 3, list (range (3))), + ]) +def test_limit (l, n, expected): + assert list (limit (l, n)) == expected + +def test_first (): + assert first ([1, 2, 3]) == 1 + assert first (range (5)) == 0 + with pytest.raises (StopIteration): + first ([]) + diff --git a/lulua/util.py b/lulua/util.py index c5634c2..5d7ea1b 100644 --- a/lulua/util.py +++ b/lulua/util.py @@ -30,7 +30,10 @@ def limit (l, n): """ Limit the number of items drawn from iterable l to n. """ it = iter (l) for i in range (n): - yield next (it) + try: + yield next (it) + except StopIteration: + break class YamlLoader: """ |