Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_text.py 6.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.python.text}.
  5. """
  6. from io import StringIO
  7. from twisted.python import text
  8. from twisted.trial import unittest
  9. sampleText = """Every attempt to employ mathematical methods in the study of chemical
  10. questions must be considered profoundly irrational and contrary to the
  11. spirit of chemistry ... If mathematical analysis should ever hold a
  12. prominent place in chemistry - an aberration which is happily almost
  13. impossible - it would occasion a rapid and widespread degeneration of that
  14. science.
  15. -- Auguste Comte, Philosophie Positive, Paris, 1838
  16. """
  17. class WrapTests(unittest.TestCase):
  18. """
  19. Tests for L{text.greedyWrap}.
  20. """
  21. def setUp(self):
  22. self.lineWidth = 72
  23. self.sampleSplitText = sampleText.split()
  24. self.output = text.wordWrap(sampleText, self.lineWidth)
  25. def test_wordCount(self):
  26. """
  27. Compare the number of words.
  28. """
  29. words = []
  30. for line in self.output:
  31. words.extend(line.split())
  32. wordCount = len(words)
  33. sampleTextWordCount = len(self.sampleSplitText)
  34. self.assertEqual(wordCount, sampleTextWordCount)
  35. def test_wordMatch(self):
  36. """
  37. Compare the lists of words.
  38. """
  39. words = []
  40. for line in self.output:
  41. words.extend(line.split())
  42. # Using assertEqual here prints out some
  43. # rather too long lists.
  44. self.assertTrue(self.sampleSplitText == words)
  45. def test_lineLength(self):
  46. """
  47. Check the length of the lines.
  48. """
  49. failures = []
  50. for line in self.output:
  51. if not len(line) <= self.lineWidth:
  52. failures.append(len(line))
  53. if failures:
  54. self.fail(
  55. "%d of %d lines were too long.\n"
  56. "%d < %s" % (len(failures), len(self.output), self.lineWidth, failures)
  57. )
  58. def test_doubleNewline(self):
  59. """
  60. Allow paragraphs delimited by two \ns.
  61. """
  62. sampleText = "et\n\nphone\nhome."
  63. result = text.wordWrap(sampleText, self.lineWidth)
  64. self.assertEqual(result, ["et", "", "phone home.", ""])
  65. class LineTests(unittest.TestCase):
  66. """
  67. Tests for L{isMultiline} and L{endsInNewline}.
  68. """
  69. def test_isMultiline(self):
  70. """
  71. L{text.isMultiline} returns C{True} if the string has a newline in it.
  72. """
  73. s = 'This code\n "breaks."'
  74. m = text.isMultiline(s)
  75. self.assertTrue(m)
  76. s = 'This code does not "break."'
  77. m = text.isMultiline(s)
  78. self.assertFalse(m)
  79. def test_endsInNewline(self):
  80. """
  81. L{text.endsInNewline} returns C{True} if the string ends in a newline.
  82. """
  83. s = "newline\n"
  84. m = text.endsInNewline(s)
  85. self.assertTrue(m)
  86. s = "oldline"
  87. m = text.endsInNewline(s)
  88. self.assertFalse(m)
  89. class StringyStringTests(unittest.TestCase):
  90. """
  91. Tests for L{text.stringyString}.
  92. """
  93. def test_tuple(self):
  94. """
  95. Tuple elements are displayed on separate lines.
  96. """
  97. s = ("a", "b")
  98. m = text.stringyString(s)
  99. self.assertEqual(m, "(a,\n b,)\n")
  100. def test_dict(self):
  101. """
  102. Dicts elements are displayed using C{str()}.
  103. """
  104. s = {"a": 0}
  105. m = text.stringyString(s)
  106. self.assertEqual(m, "{a: 0}")
  107. def test_list(self):
  108. """
  109. List elements are displayed on separate lines using C{str()}.
  110. """
  111. s = ["a", "b"]
  112. m = text.stringyString(s)
  113. self.assertEqual(m, "[a,\n b,]\n")
  114. class SplitTests(unittest.TestCase):
  115. """
  116. Tests for L{text.splitQuoted}.
  117. """
  118. def test_oneWord(self):
  119. """
  120. Splitting strings with one-word phrases.
  121. """
  122. s = 'This code "works."'
  123. r = text.splitQuoted(s)
  124. self.assertEqual(["This", "code", "works."], r)
  125. def test_multiWord(self):
  126. s = 'The "hairy monkey" likes pie.'
  127. r = text.splitQuoted(s)
  128. self.assertEqual(["The", "hairy monkey", "likes", "pie."], r)
  129. # Some of the many tests that would fail:
  130. # def test_preserveWhitespace(self):
  131. # phrase = '"MANY SPACES"'
  132. # s = 'With %s between.' % (phrase,)
  133. # r = text.splitQuoted(s)
  134. # self.assertEqual(['With', phrase, 'between.'], r)
  135. # def test_escapedSpace(self):
  136. # s = r"One\ Phrase"
  137. # r = text.splitQuoted(s)
  138. # self.assertEqual(["One Phrase"], r)
  139. class StrFileTests(unittest.TestCase):
  140. def setUp(self):
  141. self.io = StringIO("this is a test string")
  142. def tearDown(self):
  143. pass
  144. def test_1_f(self):
  145. self.assertFalse(text.strFile("x", self.io))
  146. def test_1_1(self):
  147. self.assertTrue(text.strFile("t", self.io))
  148. def test_1_2(self):
  149. self.assertTrue(text.strFile("h", self.io))
  150. def test_1_3(self):
  151. self.assertTrue(text.strFile("i", self.io))
  152. def test_1_4(self):
  153. self.assertTrue(text.strFile("s", self.io))
  154. def test_1_5(self):
  155. self.assertTrue(text.strFile("n", self.io))
  156. def test_1_6(self):
  157. self.assertTrue(text.strFile("g", self.io))
  158. def test_3_1(self):
  159. self.assertTrue(text.strFile("thi", self.io))
  160. def test_3_2(self):
  161. self.assertTrue(text.strFile("his", self.io))
  162. def test_3_3(self):
  163. self.assertTrue(text.strFile("is ", self.io))
  164. def test_3_4(self):
  165. self.assertTrue(text.strFile("ing", self.io))
  166. def test_3_f(self):
  167. self.assertFalse(text.strFile("bla", self.io))
  168. def test_large_1(self):
  169. self.assertTrue(text.strFile("this is a test", self.io))
  170. def test_large_2(self):
  171. self.assertTrue(text.strFile("is a test string", self.io))
  172. def test_large_f(self):
  173. self.assertFalse(text.strFile("ds jhfsa k fdas", self.io))
  174. def test_overlarge_f(self):
  175. self.assertFalse(
  176. text.strFile("djhsakj dhsa fkhsa s,mdbnfsauiw bndasdf hreew", self.io)
  177. )
  178. def test_self(self):
  179. self.assertTrue(text.strFile("this is a test string", self.io))
  180. def test_insensitive(self):
  181. self.assertTrue(text.strFile("ThIs is A test STRING", self.io, False))