| 145 | |
| 146 | class LegacyTestMeta(type): |
| 147 | def __new__(cls, name, bases, dct): |
| 148 | |
| 149 | def generate_test(infile, outfile, normalize, kwargs): |
| 150 | def test(self): |
| 151 | with open(infile, encoding="utf-8") as f: |
| 152 | input = f.read() |
| 153 | with open(outfile, encoding="utf-8") as f: |
| 154 | # Normalize line endings |
| 155 | # (on Windows, git may have altered line endings). |
| 156 | expected = f.read().replace("\r\n", "\n") |
| 157 | output = markdown(input, **kwargs) |
| 158 | if tidylib and normalize: |
| 159 | try: |
| 160 | expected = _normalize_whitespace(expected) |
| 161 | output = _normalize_whitespace(output) |
| 162 | except OSError: |
| 163 | self.skipTest("Tidylib's c library not available.") |
| 164 | elif normalize: |
| 165 | self.skipTest('Tidylib not available.') |
| 166 | self.assertMultiLineEqual(output, expected) |
| 167 | return test |
| 168 | |
| 169 | location = dct.get('location', '') |
| 170 | exclude = dct.get('exclude', []) |
| 171 | normalize = dct.get('normalize', False) |
| 172 | input_ext = dct.get('input_ext', '.txt') |
| 173 | output_ext = dct.get('output_ext', '.html') |
| 174 | kwargs = dct.get('default_kwargs', Kwargs()) |
| 175 | |
| 176 | if os.path.isdir(location): |
| 177 | for file in os.listdir(location): |
| 178 | infile = os.path.join(location, file) |
| 179 | if os.path.isfile(infile): |
| 180 | tname, ext = os.path.splitext(file) |
| 181 | if ext == input_ext: |
| 182 | outfile = os.path.join(location, tname + output_ext) |
| 183 | tname = tname.replace(' ', '_').replace('-', '_') |
| 184 | kws = kwargs.copy() |
| 185 | if tname in dct: |
| 186 | kws.update(dct[tname]) |
| 187 | test_name = 'test_%s' % tname |
| 188 | if tname not in exclude: |
| 189 | dct[test_name] = generate_test(infile, outfile, normalize, kws) |
| 190 | else: |
| 191 | dct[test_name] = unittest.skip('Excluded')(lambda: None) |
| 192 | |
| 193 | return type.__new__(cls, name, bases, dct) |
| 194 | |
| 195 | |
| 196 | class LegacyTestCase(unittest.TestCase, metaclass=LegacyTestMeta): |