Commit eb0b50937037a4911a1b3ce6dbd92cd0623619a7
1 parent
f938a946
tests: create unittests for oleobj file name creation
Showing
1 changed file
with
59 additions
and
0 deletions
tests/oleobj/test_basic.py
| @@ -159,6 +159,65 @@ class TestOleObj(unittest.TestCase): | @@ -159,6 +159,65 @@ class TestOleObj(unittest.TestCase): | ||
| 159 | only_run_every=4) | 159 | only_run_every=4) |
| 160 | 160 | ||
| 161 | 161 | ||
| 162 | +class TestSaneFilenameCreation(unittest.TestCase): | ||
| 163 | + """ Test sanitization / creation of sane filenames """ | ||
| 164 | + def test_with_empty_inputs(self): | ||
| 165 | + """Test empty inputs lead to several non-empty distinct outputs""" | ||
| 166 | + iter = oleobj.get_sane_embedded_filenames('', '', '', 10, 47) | ||
| 167 | + output = set() | ||
| 168 | + for attempt in range(10): | ||
| 169 | + output.add(next(iter)) | ||
| 170 | + self.assertEqual(len(output), 10) # check all 10 are different | ||
| 171 | + for fname in output: | ||
| 172 | + self.assertNotEqual(fname, '') # all are non-empty | ||
| 173 | + | ||
| 174 | + def test_that_first_has_priority(self): | ||
| 175 | + iter = oleobj.get_sane_embedded_filenames('fname.sfx', 'do_not.use', | ||
| 176 | + 'do_not.use', 10, 47) | ||
| 177 | + self.assertEqual(next(iter), 'fname.sfx') | ||
| 178 | + [next(iter) for _ in range(10)] # check this does not crash | ||
| 179 | + | ||
| 180 | + def test_that_suffixed_have_priority(self): | ||
| 181 | + iter = oleobj.get_sane_embedded_filenames('no_suffix', 'also_not', | ||
| 182 | + 'fname.sfx', 10, 47) | ||
| 183 | + self.assertEqual(next(iter), 'fname.sfx') | ||
| 184 | + self.assertEqual(next(iter), 'no_suffix') | ||
| 185 | + self.assertEqual(next(iter), 'also_not') | ||
| 186 | + [next(iter) for _ in range(10)] # check this does not crash | ||
| 187 | + | ||
| 188 | + def test_with_hardly_any_length(self): | ||
| 189 | + iter = oleobj.get_sane_embedded_filenames('fname.suffx', 'fname.sufx', | ||
| 190 | + 'fname.sfx', 4, 47) | ||
| 191 | + self.assertEqual(next(iter), '.sfx') | ||
| 192 | + [next(iter) for _ in range(10)] # check this does not crash | ||
| 193 | + | ||
| 194 | + def test_with_mean_unicode(self): | ||
| 195 | + uni_name1 = u'\xfcnic\xf6de-\xdftring' | ||
| 196 | + uni_name2 = u'keyboard:\u2328, Braille:\u2800, Phone:\u260e' | ||
| 197 | + iter = oleobj.get_sane_embedded_filenames(uni_name1, uni_name2, | ||
| 198 | + 'regular_txt', 30, 47) | ||
| 199 | + self.assertEqual(next(iter), '_nic_de-_tring') | ||
| 200 | + self.assertEqual(next(iter), 'keyboard___ Braille___ Phone__') | ||
| 201 | + self.assertEqual(next(iter), 'regular_txt') | ||
| 202 | + [next(iter) for _ in range(10)] # check this does not crash | ||
| 203 | + | ||
| 204 | + def test_last_resort(self): | ||
| 205 | + iter = oleobj.get_sane_embedded_filenames('', '', '', 10, 47) | ||
| 206 | + all_options = list(iter) | ||
| 207 | + self.assertEqual(len(all_options), oleobj.MAX_FILENAME_ATTEMPTS+1) | ||
| 208 | + self.assertIn('47', all_options[-1]) | ||
| 209 | + | ||
| 210 | + def test_realworld_lnk_example(self): | ||
| 211 | + fname = ' ' | ||
| 212 | + src_path = 'E:\\tmp\\doc_package\\doc\\6.lnk' | ||
| 213 | + tmp_path = 'C:\\Users\\1\\AppData\\Local\\Temp\\6.lnk' | ||
| 214 | + iter = oleobj.get_sane_embedded_filenames(fname, src_path, tmp_path, | ||
| 215 | + 30, 47) | ||
| 216 | + self.assertEqual(next(iter), '6.lnk') | ||
| 217 | + self.assertEqual(next(iter), '6.lnk') | ||
| 218 | + [next(iter) for _ in range(10)] # check this does not crash | ||
| 219 | + | ||
| 220 | + | ||
| 162 | # just in case somebody calls this file as a script | 221 | # just in case somebody calls this file as a script |
| 163 | if __name__ == '__main__': | 222 | if __name__ == '__main__': |
| 164 | unittest.main() | 223 | unittest.main() |