1import unittest 2from fontTools.misc.filenames import ( 3 userNameToFileName, handleClash1, handleClash2) 4 5 6class UserNameToFilenameTest(unittest.TestCase): 7 8 def test_names(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(u"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( 26 userNameToFileName("Aacute_V.swash"), 27 "A_acute_V_.swash") 28 self.assertEqual(userNameToFileName(".notdef"), "_notdef") 29 self.assertEqual(userNameToFileName("con"), "_con") 30 self.assertEqual(userNameToFileName("CON"), "C_O_N_") 31 self.assertEqual(userNameToFileName("con.alt"), "_con.alt") 32 self.assertEqual(userNameToFileName("alt.con"), "alt._con") 33 34 def test_prefix_suffix(self): 35 prefix = "TEST_PREFIX" 36 suffix = "TEST_SUFFIX" 37 name = "NAME" 38 name_file = "N_A_M_E_" 39 self.assertEqual( 40 userNameToFileName(name, prefix=prefix, suffix=suffix), 41 prefix + name_file + suffix) 42 43 def test_collide(self): 44 prefix = "TEST_PREFIX" 45 suffix = "TEST_SUFFIX" 46 name = "NAME" 47 name_file = "N_A_M_E_" 48 collision_avoidance1 = "000000000000001" 49 collision_avoidance2 = "000000000000002" 50 exist = set() 51 generated = userNameToFileName( 52 name, exist, prefix=prefix, suffix=suffix) 53 exist.add(generated.lower()) 54 self.assertEqual(generated, prefix + name_file + suffix) 55 generated = userNameToFileName( 56 name, exist, prefix=prefix, suffix=suffix) 57 exist.add(generated.lower()) 58 self.assertEqual( 59 generated, 60 prefix + name_file + collision_avoidance1 + suffix) 61 generated = userNameToFileName( 62 name, exist, prefix=prefix, suffix=suffix) 63 self.assertEqual( 64 generated, 65 prefix + name_file + collision_avoidance2+ suffix) 66 67 def test_ValueError(self): 68 with self.assertRaises(ValueError): 69 userNameToFileName(b"a") 70 with self.assertRaises(ValueError): 71 userNameToFileName({"a"}) 72 with self.assertRaises(ValueError): 73 userNameToFileName(("a",)) 74 with self.assertRaises(ValueError): 75 userNameToFileName(["a"]) 76 with self.assertRaises(ValueError): 77 userNameToFileName(["a"]) 78 with self.assertRaises(ValueError): 79 userNameToFileName(b"\xd8\x00") 80 81 def test_handleClash1(self): 82 prefix = ("0" * 5) + "." 83 suffix = "." + ("0" * 10) 84 existing = ["a" * 5] 85 86 e = list(existing) 87 self.assertEqual( 88 handleClash1(userName="A" * 5, existing=e, prefix=prefix, 89 suffix=suffix), 90 '00000.AAAAA000000000000001.0000000000' 91 ) 92 93 e = list(existing) 94 e.append(prefix + "aaaaa" + "1".zfill(15) + suffix) 95 self.assertEqual( 96 handleClash1(userName="A" * 5, existing=e, prefix=prefix, 97 suffix=suffix), 98 '00000.AAAAA000000000000002.0000000000' 99 ) 100 101 e = list(existing) 102 e.append(prefix + "AAAAA" + "2".zfill(15) + suffix) 103 self.assertEqual( 104 handleClash1(userName="A" * 5, existing=e, prefix=prefix, 105 suffix=suffix), 106 '00000.AAAAA000000000000001.0000000000' 107 ) 108 109 def test_handleClash2(self): 110 prefix = ("0" * 5) + "." 111 suffix = "." + ("0" * 10) 112 existing = [prefix + str(i) + suffix for i in range(100)] 113 114 e = list(existing) 115 self.assertEqual( 116 handleClash2(existing=e, prefix=prefix, suffix=suffix), 117 '00000.100.0000000000' 118 ) 119 120 e = list(existing) 121 e.remove(prefix + "1" + suffix) 122 self.assertEqual( 123 handleClash2(existing=e, prefix=prefix, suffix=suffix), 124 '00000.1.0000000000' 125 ) 126 127 e = list(existing) 128 e.remove(prefix + "2" + suffix) 129 self.assertEqual( 130 handleClash2(existing=e, prefix=prefix, suffix=suffix), 131 '00000.2.0000000000' 132 ) 133 134if __name__ == "__main__": 135 import sys 136 sys.exit(unittest.main()) 137