Package translate :: Package misc :: Module test_quote
[hide private]
[frames] | no frames]

Source Code for Module translate.misc.test_quote

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3   
 4  from translate.misc import quote 
 5   
6 -def test_find_all():
7 """tests the find_all function""" 8 assert quote.find_all("", "a") == [] 9 assert quote.find_all("a", "b") == [] 10 assert quote.find_all("a", "a") == [0] 11 assert quote.find_all("aa", "a") == [0, 1] 12 assert quote.find_all("abba", "ba") == [2] 13 # check we skip the whole instance 14 assert quote.find_all("banana", "ana") == [1]
15
16 -def test_extract():
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
29 -def test_extractwithoutquotes():
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 # don't include escapes... 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 # escaping of quote char 44 assert quote.extractwithoutquotes("<quoted\\>", "<", ">", "\\", 0, False) == ("quoted>", True)
45
46 -def isnewlineortabescape(escape):
47 if escape == "\\n" or escape == "\\t": 48 return escape 49 return escape[-1]
50
51 -def test_extractwithoutquotes_passfunc():
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
55 -class TestQuote:
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
64 - def test_quote_wrapping(self):
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
71 - def test_htmlencoding(self):
72 """test that we can encode and decode HTML entities""" 73 raw_encoded = [(u"€", "&euro;"), (u"©", "&copy;"), (u'"', "&quot;")] 74 for raw, encoded in raw_encoded: 75 assert quote.htmlentityencode(raw) == encoded 76 assert quote.htmlentitydecode(encoded) == raw
77