1
2
3
4 from translate.misc import quote
5
15
17 """tests the extract function"""
18 assert quote.extract("the <quoted> part", "<", ">", "\\", 0) == ("<quoted>", False)
19 assert quote.extract("the 'quoted' part", "'", "'", "\\", 0) == ("'quoted'", False)
20 assert quote.extract("the 'isn\\'t escaping fun' part", "'", "'", "\\", 0) == ("'isn\\'t escaping fun'", False)
21 assert quote.extract("the 'isn\\'t something ", "'", "'", "\\", 0) == ("'isn\\'t something ", True)
22 assert quote.extract("<quoted>\\", "<", ">", "\\", 0) == ("<quoted>", False)
23 assert quote.extract("<quoted><again>", "<", ">", "\\", 0) == ("<quoted><again>", False)
24 assert quote.extract("<quoted>\\\\<again>", "<", ">", "\\", 0) == ("<quoted><again>", False)
25 assert quote.extract("<quoted\\>", "<", ">", "\\", 0) == ("<quoted\\>", True)
26 assert quote.extract(' -->\n<!ENTITY blah "Some">', "<!--", "-->", None, 1) == (" -->", False)
27 assert quote.extract('">\n', '"', '"', None, True) == ('"', False)
28
30 """tests the extractwithoutquotes function"""
31 assert quote.extractwithoutquotes("the <quoted> part", "<", ">", "\\", 0) == ("quoted", False)
32 assert quote.extractwithoutquotes("the 'quoted' part", "'", "'", "\\", 0) == ("quoted", False)
33 assert quote.extractwithoutquotes("the 'isn\\'t escaping fun' part", "'", "'", "\\", 0) == ("isn\\'t escaping fun", False)
34 assert quote.extractwithoutquotes("the 'isn\\'t something ", "'", "'", "\\", 0) == ("isn\\'t something ", True)
35 assert quote.extractwithoutquotes("<quoted>\\", "<", ">", "\\", 0) == ("quoted", False)
36 assert quote.extractwithoutquotes("<quoted>\\\\<again>", "<", ">", "\\", 0) == ("quotedagain", False)
37 assert quote.extractwithoutquotes("<quoted><again\\\\", "<", ">", "\\", 0, True) == ("quotedagain\\\\", True)
38
39 assert quote.extractwithoutquotes("the 'isn\\'t escaping fun' part", "'", "'", "\\", 0, False) == ("isn't escaping fun", False)
40 assert quote.extractwithoutquotes("the 'isn\\'t something ", "'", "'", "\\", 0, False) == ("isn't something ", True)
41 assert quote.extractwithoutquotes("<quoted\\", "<", ">", "\\", 0, False) == ("quoted", True)
42 assert quote.extractwithoutquotes("<quoted><again\\\\", "<", ">", "\\", 0, False) == ("quotedagain\\", True)
43
44 assert quote.extractwithoutquotes("<quoted\\>", "<", ">", "\\", 0, False) == ("quoted>", True)
45
47 if escape == "\\n" or escape == "\\t":
48 return escape
49 return escape[-1]
50
52 """tests the extractwithoutquotes function with a function for includeescapes as a parameter"""
53 assert quote.extractwithoutquotes("<test \\r \\n \\t \\\\>", "<", ">", "\\", 0, isnewlineortabescape) == ("test r \\n \\t \\", False)
54
56
58 """test that we do \uNNNN escapes for certain control characters instead of converting to UTF-8 characters"""
59 prefix, suffix = "bling", "blang"
60 for control in (u"\u0005", u"\u0006", u"\u0007", u"\u0011"):
61 string = prefix + control + suffix
62 assert quote.escapecontrols(string) == string
63
65 """test that we can wrap strings in double quotes"""
66 string = 'A string'
67 assert quote.quotestr(string) == '"A string"'
68 list = ['One', 'Two']
69 assert quote.quotestr(list) == '"One"\n"Two"'
70
72 """test that we can encode and decode HTML entities"""
73 raw_encoded = [(u"€", "€"), (u"©", "©"), (u'"', """)]
74 for raw, encoded in raw_encoded:
75 assert quote.htmlentityencode(raw) == encoded
76 assert quote.htmlentitydecode(encoded) == raw
77