From 71028fa45e7b65de62402d6a049afa16baca39e8 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Wed, 25 Nov 2020 11:13:20 +0100 Subject: Add a few more tests --- lulua/test_text.py | 23 ++++++++++++++++++++--- lulua/test_util.py | 18 +++++++++++++++++- 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 (""" -

Hello & World!

+

Hello & Wörld! ♣ ∖

""")) 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: """ -- cgit v1.2.3