Coverage for cogapp/test_whiteutils.py: 27.54%

1""" Test the cogapp.whiteutils module.

2 http://nedbatchelder.com/code/cog

3

4 Copyright 2004-2019, Ned Batchelder.

5"""

6

7 from __future__ import absolute_import

8

9 from .backward import TestCase

10 from .whiteutils import *

11

12

13 class WhitePrefixTests(TestCase):

14 """ Test cases for cogapp.whiteutils.

15 """

16 def testSingleLine(self):

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

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

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

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

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

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

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

24

25 def testMultiLine(self):

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

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

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

29

30 def testBlankLinesAreIgnored(self):

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

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

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

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

35

36 def testTabCharacters(self):

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

38

39 def testDecreasingLengths(self):

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

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

42

43

44 class ReindentBlockTests(TestCase):

45 """ Test cases for cogapp.reindentBlock.

46 """

47 def testNonTermLine(self):

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

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

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

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

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

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

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

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

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

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

58

59 def testSingleLine(self):

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

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

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

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

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

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

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

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

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

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

70

71 def testRealBlock(self):

72 self.assertEqual(

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

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

75 )

76

77

78 class CommonPrefixTests(TestCase):

79 """ Test cases for cogapp.commonPrefix.

80 """

81 def testDegenerateCases(self):

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

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

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

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

86

87 def testNoCommonPrefix(self):

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

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

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

91

92 def testUsualCases(self):

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

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

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

96

97 def testBlankLine(self):

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

99

100 def testDecreasingLengths(self):

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