1from __future__ import absolute_import, unicode_literals 2import unittest 3from fontTools.ufoLib.filenames import userNameToFileName, handleClash1, handleClash2 4 5 6class TestFilenames(unittest.TestCase): 7 8 def test_userNameToFileName(self): 9 self.assertEqual(userNameToFileName("a"), "a") 10 self.assertEqual(userNameToFileName("A"), "A_") 11 self.assertEqual(userNameToFileName("AE"), "A_E_") 12 self.assertEqual(userNameToFileName("Ae"), "A_e") 13 self.assertEqual(userNameToFileName("ae"), "ae") 14 self.assertEqual(userNameToFileName("aE"), "aE_") 15 self.assertEqual(userNameToFileName("a.alt"), "a.alt") 16 self.assertEqual(userNameToFileName("A.alt"), "A_.alt") 17 self.assertEqual(userNameToFileName("A.Alt"), "A_.A_lt") 18 self.assertEqual(userNameToFileName("A.aLt"), "A_.aL_t") 19 self.assertEqual(userNameToFileName("A.alT"), "A_.alT_") 20 self.assertEqual(userNameToFileName("T_H"), "T__H_") 21 self.assertEqual(userNameToFileName("T_h"), "T__h") 22 self.assertEqual(userNameToFileName("t_h"), "t_h") 23 self.assertEqual(userNameToFileName("F_F_I"), "F__F__I_") 24 self.assertEqual(userNameToFileName("f_f_i"), "f_f_i") 25 self.assertEqual(userNameToFileName("Aacute_V.swash"), 26 "A_acute_V_.swash") 27 self.assertEqual(userNameToFileName(".notdef"), "_notdef") 28 self.assertEqual(userNameToFileName("con"), "_con") 29 self.assertEqual(userNameToFileName("CON"), "C_O_N_") 30 self.assertEqual(userNameToFileName("con.alt"), "_con.alt") 31 self.assertEqual(userNameToFileName("alt.con"), "alt._con") 32 33 def test_userNameToFileName_ValueError(self): 34 with self.assertRaises(ValueError): 35 userNameToFileName(b"a") 36 with self.assertRaises(ValueError): 37 userNameToFileName({"a"}) 38 with self.assertRaises(ValueError): 39 userNameToFileName(("a",)) 40 with self.assertRaises(ValueError): 41 userNameToFileName(["a"]) 42 with self.assertRaises(ValueError): 43 userNameToFileName(["a"]) 44 with self.assertRaises(ValueError): 45 userNameToFileName(b"\xd8\x00") 46 47 def test_handleClash1(self): 48 prefix = ("0" * 5) + "." 49 suffix = "." + ("0" * 10) 50 existing = ["a" * 5] 51 52 e = list(existing) 53 self.assertEqual( 54 handleClash1(userName="A" * 5, existing=e, prefix=prefix, 55 suffix=suffix), 56 '00000.AAAAA000000000000001.0000000000' 57 ) 58 59 e = list(existing) 60 e.append(prefix + "aaaaa" + "1".zfill(15) + suffix) 61 self.assertEqual( 62 handleClash1(userName="A" * 5, existing=e, prefix=prefix, 63 suffix=suffix), 64 '00000.AAAAA000000000000002.0000000000' 65 ) 66 67 e = list(existing) 68 e.append(prefix + "AAAAA" + "2".zfill(15) + suffix) 69 self.assertEqual( 70 handleClash1(userName="A" * 5, existing=e, prefix=prefix, 71 suffix=suffix), 72 '00000.AAAAA000000000000001.0000000000' 73 ) 74 75 def test_handleClash2(self): 76 prefix = ("0" * 5) + "." 77 suffix = "." + ("0" * 10) 78 existing = [prefix + str(i) + suffix for i in range(100)] 79 80 e = list(existing) 81 self.assertEqual( 82 handleClash2(existing=e, prefix=prefix, suffix=suffix), 83 '00000.100.0000000000' 84 ) 85 86 e = list(existing) 87 e.remove(prefix + "1" + suffix) 88 self.assertEqual( 89 handleClash2(existing=e, prefix=prefix, suffix=suffix), 90 '00000.1.0000000000' 91 ) 92 93 e = list(existing) 94 e.remove(prefix + "2" + suffix) 95 self.assertEqual( 96 handleClash2(existing=e, prefix=prefix, suffix=suffix), 97 '00000.2.0000000000' 98 ) 99