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