Debian Codegraph @

tests/test_style_set_names.py

raw symbols SCIP
1 #Test getter and setter of font.style_set_names including errors
2 import fontforge
3
4 font = fontforge.font()
5 assert font.style_set_names == ()
6
7 def lang_to_lang_str(style_set_names):
8 style_set_names_with_str_lang = []
9 for name_spec in style_set_names:
10 if name_spec[0] == 0x409:
11 style_set_names_with_str_lang.append(("English (US)",) + name_spec[1:])
12 elif name_spec[0] == 0x809:
13 style_set_names_with_str_lang.append(("English (British)",) + name_spec[1:])
14 else:
15 style_set_names_with_str_lang.append(name_spec)
16 return style_set_names_with_str_lang
17
18 for style_set_names in [
19 (("English (US)", "ss01", "My English ss01"), ("English (US)", "ss02", "My English ss02")), # same language, more
20 (("English (British)", "ss01", "My English ss01"), ("French Côte d'Ivoire", "ss01", "Mon ss01 français")), # same style set, non-ascii
21 (("English (British)", "ss09", "My English ss09"),), # fewer
22 ((0x809, "ss09", "My English ss09"),), # language as int
23 (("Chinese (PRC)", "ss10", "我的中文ss10"),), # Unicode
24 (("English (British)", "ss20", "😀𝄞"),), # Unicode
25 (("English (British)", "ss20", "My English ss20 with a different name"),),
26 ((0x809, "ss20", "My English ss20 with another different name"),),
27 (("English (British)", "ss20", "My English ss20 with another Name"),),
28 (("English (US)", "cv01", ("My English cv01", "My tooltip text", "My sample text", ("My parameter text",), None)), ("English (US)", "cv02", ("My English cv02", None, None, (), None))), # same language, more
29 (("English (British)", "cv01", ("My English cv01", None, None, (), None)), ("French Côte d'Ivoire", "cv01", ("Mon cv01 français", None, None, (), None))), # same style set, non-ascii
30 (("English (British)", "cv09", ("My English cv09", None, None, (), None)),), # fewer
31 ((0x809, "cv09", ("My English cv09", None, None, (), None)),), # language as int
32 (("Chinese (PRC)", "cv10", ("我的中文cv10", None, None, (), None)),), # Unicode
33 (("English (British)", "cv99", ("😀𝄞", None, None, (), None)),), # Unicode
34 (("English (British)", "cv99", (None, "My English cv99 with a different name", None, (), None)),),
35 ((0x809, "cv99", (None, "My English cv99 with another different name", None, (), None)),),
36 (("English (British)", "cv99", (None, "My English cv99 with another Name", None, (), None)),),
37 ()
38 ]:
39 font.style_set_names = style_set_names
40 style_set_names_with_str_lang = lang_to_lang_str(style_set_names)
41 assert font.style_set_names == tuple(style_set_names_with_str_lang)
42
43 # tests for error
44
45 for style_set_names, e_type, e_str in [
46 ("test", TypeError, "Style set names must be set as a tuple."),
47 (1.23, TypeError, "Style set names must be set as a tuple."),
48 (("test",), TypeError, "Style set name specification must be a tuple."),
49 ((1.23,), TypeError, "Style set name specification must be a tuple."),
50 ((("English", "My name"),), ValueError, "Style set name specification must have 3 elements (language, style set and name)."),
51 ((("English (US)", "ss01", "My name"), ("English", "My name")), ValueError, "Style set name specification must have 3 elements (language, style set and name)."),
52 ((("English", "ss01", "My name"),), ValueError, "The first element of a style set name specification must be a valid language name if it is a string."),
53 (((0, "ss01", "My name"),), ValueError, "The first element of a style set name specification must be a valid language id if it is an integer."),
54 ((("English (US)", 1, "My name"),), TypeError, "The second element of a style set name specification must be a string."),
55 ((("English (US)", "01", "My name"),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
56 ((("English (US)", "ss00", "My name"),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
57 ((("English (US)", "ss21", "My name"),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
58 ((("English (US)", "ss30", "My name"),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
59 ((("English (US)", "ss001", "My name"),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
60 ((("English (US)", "ss0D", "My name"),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
61 ((("English (US)", "ssOI", "My name"),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
62 ((("English (US)", "ss01", 123),), TypeError, "The third element of a style set name specification must be a string."),
63 ((("English (US)", "ss01", "My name1"), ("English (US)", "ss01", "My name2")), ValueError, "The feature ss01 is named twice in language English (US):\n My name1\n My name2"),
64 ((("English (US)", "ss01", "My name1"), (0x409, "ss01", "My name2")), ValueError, "The feature ss01 is named twice in language English (US):\n My name1\n My name2"),
65 ((("English (US)", "ss01", "My name"), ("English (US)", "ss01", "My name")), ValueError, "The feature ss01 is named twice in language English (US):\n My name\n My name"),
66 ((("English (US)", "cv00", ("My name", None, None, (), None)),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
67 ((("English (US)", "cv001", ("My name", None, None, (), None)),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
68 ((("English (US)", "cv0D", ("My name", None, None, (), None)),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
69 ((("English (US)", "cvOI", ("My name", None, None, (), None)),), ValueError, "The second element of a style set name specification must be a valid style set or character variation tag."),
70 ((("English (US)", "cv01", 123),), TypeError, "The third element of a character variant name specification must be a tuple."),
71 ((("English (US)", "cv01", "My name"),), TypeError, "The third element of a character variant name specification must be a tuple."),
72 ((("English (US)", "cv01", ("My name",)),), ValueError, "The third element of a character variant name specification must have 5 elements (feature name, tooltip text, sample text, tuple of parameter labels and characters)."),
73 ((("English (US)", "cv01", (123, None, None, (), None)),), TypeError, "The first element of the third element of a character variant name specification must be a string or None."),
74 ((("English (US)", "cv01", ("My name", 123, None, (), None)),), TypeError, "The second element of the third element of a character variant name specification must be a string or None."),
75 ((("English (US)", "cv01", ("My name", None, 123, (), None)),), TypeError, "The third element of the third element of a character variant name specification must be a string or None."),
76 ((("English (US)", "cv01", ("My name", None, None, (), 123)),), TypeError, "The fifth element of the third element of a character variant name specification is reserved and must be None."),
77 ((("English (US)", "cv01", ("My name", None, None, None, None)),), TypeError, "The fourth element of the third element of a character variant name specification must be a tuple."),
78 ((("English (US)", "cv01", ("My name", None, None, (123,), None)),), TypeError, "All elements of the fourth element of the third element of a character variant name specification must be a string or None."),
79 ((("English (US)", "cv01", ("My name1", None, None, (), None)), ("English (US)", "cv01", ("My name2", None, None, (), None))), ValueError, "The feature cv01 is named twice in language English (US):\n My name1\n My name2"),
80 ((("English (US)", "cv01", ("My name1", None, None, (), None)), (0x409, "cv01", ("My name2", None, None, (), None))), ValueError, "The feature cv01 is named twice in language English (US):\n My name1\n My name2"),
81 ((("English (US)", "cv01", ("My name", None, None, (), None)), ("English (US)", "cv01", ("My name", None, None, (), None))), ValueError, "The feature cv01 is named twice in language English (US):\n My name\n My name")
82 ]:
83 try:
84 font = fontforge.font()
85 print(style_set_names)
86 font.style_set_names = style_set_names
87 style_set_names_with_str_lang = lang_to_lang_str(style_set_names)
88 raise AssertionError("no error raised, but expected")
89 except e_type as e:
90 assert str(e) == e_str, f"wrong error message for correct error type"
91 except Exception:
92 raise AssertionError("wrong error type")