Many tests done, still more to come.
[szyfrow.git] / tests / test_utilities.py
1 import pytest
2 import string
3
4 from szyfrow.support.utilities import *
5
6 def test_pos():
7 for n, l in enumerate(string.ascii_lowercase):
8 assert n == pos(l)
9 for n, l in enumerate(string.ascii_uppercase):
10 assert n == pos(l)
11 with pytest.raises(ValueError):
12 pos('9')
13
14 def test_unpos():
15 for n, l in enumerate(string.ascii_lowercase):
16 assert l == unpos(n)
17 assert l == unpos(n + 26)
18 for n, l in enumerate(string.ascii_uppercase):
19 assert l.lower() == unpos(n)
20 with pytest.raises(ValueError):
21 pos('9')
22
23 def test_pad():
24 assert pad(10, 3, '*') == '**'
25 assert pad(10, 5, '*') == ''
26 assert pad(10, 3, lambda: '!') == '!!'
27
28 def test_every_nth():
29 assert every_nth(string.ascii_lowercase, 5) == ['afkpuz', 'bglqv', 'chmrw', 'dinsx', 'ejoty']
30 assert every_nth(string.ascii_lowercase, 1) == ['abcdefghijklmnopqrstuvwxyz']
31 assert every_nth(string.ascii_lowercase, 26) == ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
32 assert every_nth(string.ascii_lowercase, 5, fillvalue='!') == ['afkpuz', 'bglqv!', 'chmrw!', 'dinsx!', 'ejoty!']
33
34 def test_combine_every_nth():
35 for i in range(1, 27):
36 assert combine_every_nth(every_nth(string.ascii_lowercase, 5)) == string.ascii_lowercase
37
38
39 def test_chunks_on_text():
40 assert chunks('abcdefghi', 3) == ['abc', 'def', 'ghi']
41 assert chunks('abcdefghi', 4) == ['abcd', 'efgh', 'i']
42 assert chunks('abcdefghi', 4, fillvalue='!') == ['abcd', 'efgh', 'i!!!']
43
44 def test_chunks_on_nontext():
45 ns = [1,2,3,4,5,6,7,8,9]
46 assert chunks(ns, 3) == [[1,2,3],[4,5,6],[7,8,9]]
47
48 def test_transpose():
49 assert transpose(['a', 'b', 'c', 'd'], (0,1,2,3)) == ['a', 'b', 'c', 'd']
50 assert transpose(['a', 'b', 'c', 'd'], (3,1,2,0)) == ['d', 'b', 'c', 'a']
51 assert transpose([10,11,12,13,14,15], (3,2,4,1,5,0)) == [13, 12, 14, 11, 15, 10]
52
53 def test_untranspose():
54 assert untranspose(['a', 'b', 'c', 'd'], [0,1,2,3]) == ['a', 'b', 'c', 'd']
55 assert untranspose(['d', 'b', 'c', 'a'], [3,1,2,0]) == ['a', 'b', 'c', 'd']
56 assert untranspose([13, 12, 14, 11, 15, 10], [3,2,4,1,5,0]) == [10, 11, 12, 13, 14, 15]
57
58 def test_letters():
59 assert letters('The Quick') == 'TheQuick'
60 assert letters('The Quick BROWN fox jumped! over... the (9lazy) DOG') == 'TheQuickBROWNfoxjumpedoverthelazyDOG'
61
62 def test_unaccent():
63 assert unaccent('hello') == 'hello'
64 assert unaccent('HELLO') == 'HELLO'
65 assert unaccent('héllo') == 'hello'
66 assert unaccent('héllö') == 'hello'
67 assert unaccent('HÉLLÖ') == 'HELLO'
68
69 def test_sanitise():
70 assert sanitise('The Quick') == 'thequick'
71 assert sanitise('The Quick BROWN fox jumped! over... the (9lazy) DOG') == 'thequickbrownfoxjumpedoverthelazydog'
72 assert sanitise('HÉLLÖ') == 'hello'
73
74