Coverage for cogapp/test_whiteutils.py: 26.47%
68 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-06 06:21 -0500
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-06 06:21 -0500
1"""Test the cogapp.whiteutils module."""
3from unittest import TestCase
5from .whiteutils import common_prefix, reindent_block, white_prefix
8class WhitePrefixTests(TestCase):
9 """Test cases for cogapp.whiteutils."""
11 def test_single_line(self):
12 self.assertEqual(white_prefix([""]), "")
13 self.assertEqual(white_prefix([" "]), "")
14 self.assertEqual(white_prefix(["x"]), "")
15 self.assertEqual(white_prefix([" x"]), " ")
16 self.assertEqual(white_prefix(["\tx"]), "\t")
17 self.assertEqual(white_prefix([" x"]), " ")
18 self.assertEqual(white_prefix([" \t \tx "]), " \t \t")
20 def test_multi_line(self):
21 self.assertEqual(white_prefix([" x", " x", " x"]), " ")
22 self.assertEqual(white_prefix([" y", " y", " y"]), " ")
23 self.assertEqual(white_prefix([" y", " y", " y"]), " ")
25 def test_blank_lines_are_ignored(self):
26 self.assertEqual(white_prefix([" x", " x", "", " x"]), " ")
27 self.assertEqual(white_prefix(["", " x", " x", " x"]), " ")
28 self.assertEqual(white_prefix([" x", " x", " x", ""]), " ")
29 self.assertEqual(white_prefix([" x", " x", " ", " x"]), " ")
31 def test_tab_characters(self):
32 self.assertEqual(white_prefix(["\timport sys", "", "\tprint sys.argv"]), "\t")
34 def test_decreasing_lengths(self):
35 self.assertEqual(white_prefix([" x", " x", " x"]), " ")
36 self.assertEqual(white_prefix([" x", " x", " x"]), " ")
39class ReindentBlockTests(TestCase):
40 """Test cases for cogapp.reindentBlock."""
42 def test_non_term_line(self):
43 self.assertEqual(reindent_block(""), "")
44 self.assertEqual(reindent_block("x"), "x")
45 self.assertEqual(reindent_block(" x"), "x")
46 self.assertEqual(reindent_block(" x"), "x")
47 self.assertEqual(reindent_block("\tx"), "x")
48 self.assertEqual(reindent_block("x", " "), " x")
49 self.assertEqual(reindent_block("x", "\t"), "\tx")
50 self.assertEqual(reindent_block(" x", " "), " x")
51 self.assertEqual(reindent_block(" x", "\t"), "\tx")
52 self.assertEqual(reindent_block(" x", " "), " x")
54 def test_single_line(self):
55 self.assertEqual(reindent_block("\n"), "\n")
56 self.assertEqual(reindent_block("x\n"), "x\n")
57 self.assertEqual(reindent_block(" x\n"), "x\n")
58 self.assertEqual(reindent_block(" x\n"), "x\n")
59 self.assertEqual(reindent_block("\tx\n"), "x\n")
60 self.assertEqual(reindent_block("x\n", " "), " x\n")
61 self.assertEqual(reindent_block("x\n", "\t"), "\tx\n")
62 self.assertEqual(reindent_block(" x\n", " "), " x\n")
63 self.assertEqual(reindent_block(" x\n", "\t"), "\tx\n")
64 self.assertEqual(reindent_block(" x\n", " "), " x\n")
66 def test_real_block(self):
67 self.assertEqual(
68 reindent_block("\timport sys\n\n\tprint sys.argv\n"),
69 "import sys\n\nprint sys.argv\n",
70 )
73class CommonPrefixTests(TestCase):
74 """Test cases for cogapp.commonPrefix."""
76 def test_degenerate_cases(self):
77 self.assertEqual(common_prefix([]), "")
78 self.assertEqual(common_prefix([""]), "")
79 self.assertEqual(common_prefix(["", "", "", "", ""]), "")
80 self.assertEqual(common_prefix(["cat in the hat"]), "cat in the hat")
82 def test_no_common_prefix(self):
83 self.assertEqual(common_prefix(["a", "b"]), "")
84 self.assertEqual(common_prefix(["a", "b", "c", "d", "e", "f"]), "")
85 self.assertEqual(common_prefix(["a", "a", "a", "a", "a", "x"]), "")
87 def test_usual_cases(self):
88 self.assertEqual(common_prefix(["ab", "ac"]), "a")
89 self.assertEqual(common_prefix(["aab", "aac"]), "aa")
90 self.assertEqual(common_prefix(["aab", "aab", "aab", "aac"]), "aa")
92 def test_blank_line(self):
93 self.assertEqual(common_prefix(["abc", "abx", "", "aby"]), "")
95 def test_decreasing_lengths(self):
96 self.assertEqual(common_prefix(["abcd", "abc", "ab"]), "ab")