Coverage for cogapp/test_whiteutils.py: 26.47%

68 statements  

« prev     ^ index     » next       coverage.py v7.5.0a1.dev1, created at 2024-04-15 15:50 -0400

1""" Test the cogapp.whiteutils module. 

2""" 

3 

4from unittest import TestCase 

5 

6from .whiteutils import commonPrefix, reindentBlock, whitePrefix 

7 

8 

9class WhitePrefixTests(TestCase): 

10 """ Test cases for cogapp.whiteutils. 

11 """ 

12 def testSingleLine(self): 

13 self.assertEqual(whitePrefix(['']), '') 

14 self.assertEqual(whitePrefix([' ']), '') 

15 self.assertEqual(whitePrefix(['x']), '') 

16 self.assertEqual(whitePrefix([' x']), ' ') 

17 self.assertEqual(whitePrefix(['\tx']), '\t') 

18 self.assertEqual(whitePrefix([' x']), ' ') 

19 self.assertEqual(whitePrefix([' \t \tx ']), ' \t \t') 

20 

21 def testMultiLine(self): 

22 self.assertEqual(whitePrefix([' x',' x',' x']), ' ') 

23 self.assertEqual(whitePrefix([' y',' y',' y']), ' ') 

24 self.assertEqual(whitePrefix([' y',' y',' y']), ' ') 

25 

26 def testBlankLinesAreIgnored(self): 

27 self.assertEqual(whitePrefix([' x',' x','',' x']), ' ') 

28 self.assertEqual(whitePrefix(['',' x',' x',' x']), ' ') 

29 self.assertEqual(whitePrefix([' x',' x',' x','']), ' ') 

30 self.assertEqual(whitePrefix([' x',' x',' ',' x']), ' ') 

31 

32 def testTabCharacters(self): 

33 self.assertEqual(whitePrefix(['\timport sys', '', '\tprint sys.argv']), '\t') 

34 

35 def testDecreasingLengths(self): 

36 self.assertEqual(whitePrefix([' x',' x',' x']), ' ') 

37 self.assertEqual(whitePrefix([' x',' x',' x']), ' ') 

38 

39 

40class ReindentBlockTests(TestCase): 

41 """ Test cases for cogapp.reindentBlock. 

42 """ 

43 def testNonTermLine(self): 

44 self.assertEqual(reindentBlock(''), '') 

45 self.assertEqual(reindentBlock('x'), 'x') 

46 self.assertEqual(reindentBlock(' x'), 'x') 

47 self.assertEqual(reindentBlock(' x'), 'x') 

48 self.assertEqual(reindentBlock('\tx'), 'x') 

49 self.assertEqual(reindentBlock('x', ' '), ' x') 

50 self.assertEqual(reindentBlock('x', '\t'), '\tx') 

51 self.assertEqual(reindentBlock(' x', ' '), ' x') 

52 self.assertEqual(reindentBlock(' x', '\t'), '\tx') 

53 self.assertEqual(reindentBlock(' x', ' '), ' x') 

54 

55 def testSingleLine(self): 

56 self.assertEqual(reindentBlock('\n'), '\n') 

57 self.assertEqual(reindentBlock('x\n'), 'x\n') 

58 self.assertEqual(reindentBlock(' x\n'), 'x\n') 

59 self.assertEqual(reindentBlock(' x\n'), 'x\n') 

60 self.assertEqual(reindentBlock('\tx\n'), 'x\n') 

61 self.assertEqual(reindentBlock('x\n', ' '), ' x\n') 

62 self.assertEqual(reindentBlock('x\n', '\t'), '\tx\n') 

63 self.assertEqual(reindentBlock(' x\n', ' '), ' x\n') 

64 self.assertEqual(reindentBlock(' x\n', '\t'), '\tx\n') 

65 self.assertEqual(reindentBlock(' x\n', ' '), ' x\n') 

66 

67 def testRealBlock(self): 

68 self.assertEqual( 

69 reindentBlock('\timport sys\n\n\tprint sys.argv\n'), 

70 'import sys\n\nprint sys.argv\n' 

71 ) 

72 

73 

74class CommonPrefixTests(TestCase): 

75 """ Test cases for cogapp.commonPrefix. 

76 """ 

77 def testDegenerateCases(self): 

78 self.assertEqual(commonPrefix([]), '') 

79 self.assertEqual(commonPrefix(['']), '') 

80 self.assertEqual(commonPrefix(['','','','','']), '') 

81 self.assertEqual(commonPrefix(['cat in the hat']), 'cat in the hat') 

82 

83 def testNoCommonPrefix(self): 

84 self.assertEqual(commonPrefix(['a','b']), '') 

85 self.assertEqual(commonPrefix(['a','b','c','d','e','f']), '') 

86 self.assertEqual(commonPrefix(['a','a','a','a','a','x']), '') 

87 

88 def testUsualCases(self): 

89 self.assertEqual(commonPrefix(['ab', 'ac']), 'a') 

90 self.assertEqual(commonPrefix(['aab', 'aac']), 'aa') 

91 self.assertEqual(commonPrefix(['aab', 'aab', 'aab', 'aac']), 'aa') 

92 

93 def testBlankLine(self): 

94 self.assertEqual(commonPrefix(['abc', 'abx', '', 'aby']), '') 

95 

96 def testDecreasingLengths(self): 

97 self.assertEqual(commonPrefix(['abcd', 'abc', 'ab']), 'ab')