Coverage for cogapp / test_whiteutils.py: 72.73%
11 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-17 05:48 -0400
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-17 05:48 -0400
1"""Test the cogapp.whiteutils module."""
3import pytest
5from .whiteutils import common_prefix, reindent_block, white_prefix
8@pytest.mark.parametrize(
9 "lines, expected",
10 [
11 # single line
12 ([""], ""),
13 ([" "], ""),
14 (["x"], ""),
15 ([" x"], " "),
16 (["\tx"], "\t"),
17 ([" x"], " "),
18 ([" \t \tx "], " \t \t"),
19 # multi line
20 ([" x", " x", " x"], " "),
21 ([" y", " y", " y"], " "),
22 ([" y", " y", " y"], " "),
23 # blank lines are ignored
24 ([" x", " x", "", " x"], " "),
25 (["", " x", " x", " x"], " "),
26 ([" x", " x", " x", ""], " "),
27 ([" x", " x", " ", " x"], " "),
28 # tab characters
29 (["\timport sys", "", "\tprint sys.argv"], "\t"),
30 # decreasing lengths
31 ([" x", " x", " x"], " "),
32 ([" x", " x", " x"], " "),
33 ],
34)
35def test_white_prefix(lines, expected):
36 assert white_prefix(lines) == expected
39@pytest.mark.parametrize(
40 "text, indent, expected",
41 [
42 # non-terminated line
43 ("", "", ""),
44 ("x", "", "x"),
45 (" x", "", "x"),
46 (" x", "", "x"),
47 ("\tx", "", "x"),
48 ("x", " ", " x"),
49 ("x", "\t", "\tx"),
50 (" x", " ", " x"),
51 (" x", "\t", "\tx"),
52 (" x", " ", " x"),
53 # single line
54 ("\n", "", "\n"),
55 ("x\n", "", "x\n"),
56 (" x\n", "", "x\n"),
57 (" x\n", "", "x\n"),
58 ("\tx\n", "", "x\n"),
59 ("x\n", " ", " x\n"),
60 ("x\n", "\t", "\tx\n"),
61 (" x\n", " ", " x\n"),
62 (" x\n", "\t", "\tx\n"),
63 (" x\n", " ", " x\n"),
64 # real block
65 (
66 "\timport sys\n\n\tprint sys.argv\n",
67 "",
68 "import sys\n\nprint sys.argv\n",
69 ),
70 ],
71)
72def test_reindent_block(text, indent, expected):
73 assert reindent_block(text, indent) == expected
76@pytest.mark.parametrize(
77 "strings, expected",
78 [
79 # degenerate cases
80 ([], ""),
81 ([""], ""),
82 (["", "", "", "", ""], ""),
83 (["cat in the hat"], "cat in the hat"),
84 # no common prefix
85 (["a", "b"], ""),
86 (["a", "b", "c", "d", "e", "f"], ""),
87 (["a", "a", "a", "a", "a", "x"], ""),
88 # usual cases
89 (["ab", "ac"], "a"),
90 (["aab", "aac"], "aa"),
91 (["aab", "aab", "aab", "aac"], "aa"),
92 # blank line
93 (["abc", "abx", "", "aby"], ""),
94 # decreasing lengths
95 (["abcd", "abc", "ab"], "ab"),
96 ],
97)
98def test_common_prefix(strings, expected):
99 assert common_prefix(strings) == expected