| 43 | | class textileFailure(Exception): |
| 44 | | """ |
| 45 | | Special error class: Try to display markup errors in a better way. |
| 46 | | """ |
| 47 | | def _format_output(self, txt): |
| 48 | | txt = txt.split("\\n") |
| 49 | | if VERBOSE == 1: |
| 50 | | txt = "".join(['%s\\n\n' % i for i in txt]) |
| 51 | | elif VERBOSE == 2: |
| 52 | | txt = "".join(['%s\n' % i for i in txt]) |
| 53 | | return txt |
| 54 | | |
| 55 | | def _diff(self, block1, block2): |
| 56 | | d = difflib.Differ() |
| 57 | | |
| 58 | | block1 = block1.replace("\\n", "\\n\n").split("\n") |
| 59 | | block2 = block2.replace("\\n", "\\n\n").split("\n") |
| 60 | | |
| 61 | | diff = d.compare(block1, block2) |
| 62 | | |
| 63 | | result = ["%2s %s\n" % (line, i) for line, i in enumerate(diff)] |
| 64 | | return "".join(result) |
| 65 | | |
| 66 | | def __str__(self): |
| 67 | | try: |
| 68 | | msg = self.args[0] |
| 69 | | |
| 70 | | # strip ' out |
| 71 | | if msg.startswith("u'"): |
| 72 | | msg = msg[2:-1] |
| 73 | | else: |
| 74 | | msg = msg[1:-1] |
| 75 | | |
| 76 | | try: |
| 77 | | block1, block2 = msg.split("' != '") |
| 78 | | except ValueError: |
| 79 | | msg = self._format_output(msg) |
| 80 | | return ( |
| 81 | | "Format error in output\n" |
| 82 | | "Info:\n%s" |
| 83 | | ) % msg |
| 84 | | |
| 85 | | #~ block1 = block1.rstrip("\\n") |
| 86 | | #~ block2 = block2.rstrip("\\n") |
| 87 | | diff = self._diff(block1, block2) |
| 88 | | |
| 89 | | block1 = self._format_output(block1) |
| 90 | | block2 = self._format_output(block2) |
| 91 | | |
| 92 | | return ( |
| 93 | | "\n\n---[Output:]---\n%s\n" |
| 94 | | "---[not equal to:]---\n%s" |
| 95 | | "\n---[diff:]---\n%s" |
| 96 | | ) % (block1, block2, diff) |
| 97 | | except: |
| 98 | | etype, value, tb = sys.exc_info() |
| 99 | | msg = traceback.format_exc(tb) |
| 100 | | return msg |
| 101 | | |
| 102 | | |
| 103 | | #_____________________________________________________________________________ |
| 104 | | |
| 105 | | |
| 106 | | |
| 107 | | class tinyTextileTest(unittest.TestCase): |
| 108 | | |
| 109 | | # Use the own error class from above |
| 110 | | failureException = textileFailure |
| | 45 | class tinyTextileTest(unittest_addons.MarkupTest): |
| 120 | | |
| 121 | | #_________________________________________________________________________ |
| 122 | | |
| 123 | | def _prepare_text(self, txt): |
| 124 | | """ |
| 125 | | prepare the multiline, indentation text. |
| 126 | | """ |
| 127 | | txt = txt.splitlines() |
| 128 | | assert txt[0]=="", "First must be empty!" |
| 129 | | txt = txt[1:] # Skip the first line |
| 130 | | |
| 131 | | # get the indentation level from the first line |
| 132 | | count = False |
| 133 | | for count, char in enumerate(txt[0]): |
| 134 | | if char!=" ": |
| 135 | | break |
| 136 | | |
| 137 | | assert count != False, "second line is empty!" |
| 138 | | |
| 139 | | # remove indentation from all lines |
| 140 | | txt = [i[count:] for i in txt] |
| 141 | | |
| 142 | | #~ txt = re.sub("\n {2,}", "\n", txt) |
| 143 | | txt = "\n".join(txt) |
| 144 | | |
| 145 | | # strip *one* newline at the begining... |
| 146 | | if txt.startswith("\n"): txt = txt[1:] |
| 147 | | # and strip *one* newline at the end of the text |
| 148 | | if txt.endswith("\n"): txt = txt[:-1] |
| 149 | | #~ print repr(txt) |
| 150 | | #~ print "-"*79 |
| 151 | | return txt |
| 200 | | def testSelf(self): |
| 201 | | out1 = self._prepare_text(""" |
| 202 | | one line |
| 203 | | line two""") |
| 204 | | self.assertEqual(out1, "one line\nline two") |
| 205 | | |
| 206 | | out2 = self._prepare_text(""" |
| 207 | | one line |
| 208 | | line two |
| 209 | | """) |
| 210 | | self.assertEqual(out2, "one line\nline two") |
| 211 | | |
| 212 | | out3 = self._prepare_text(""" |
| 213 | | one line |
| 214 | | |
| 215 | | line two |
| 216 | | """) |
| 217 | | self.assertEqual(out3, "one line\n\nline two") |
| 218 | | |
| 219 | | out4 = self._prepare_text(""" |
| 220 | | one line |
| 221 | | line two |
| 222 | | |
| 223 | | """) |
| 224 | | self.assertEqual(out4, "one line\n line two\n") |
| 225 | | |
| 226 | | out5 = self._prepare_text(""" |
| 227 | | one line |
| 228 | | line two |
| 229 | | dritte Zeile |
| 230 | | """) |
| 231 | | self.assertEqual(out5, "one line\n line two\ndritte Zeile") |
| 232 | | |
| 233 | | #_________________________________________________________________________ |
| 234 | | |