Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: grit/format/chrome_messages_json_unittest.py

Issue 11152002: Add support for messages.json format used to localize Chrome extensions. (Closed) Base URL: http://git.chromium.org/external/grit-i18n.git@master
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « grit/format/chrome_messages_json.py ('k') | grit/format/js_map_format.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Unittest for js_map_format.py. 6 """Unittest for chrome_messages_json.py.
7 """ 7 """
8 8
9 import os 9 import os
10 import sys 10 import sys
11 if __name__ == '__main__': 11 if __name__ == '__main__':
12 sys.path[0] = os.path.abspath(os.path.join(sys.path[0], '../..')) 12 sys.path[0] = os.path.abspath(os.path.join(sys.path[0], '../..'))
13 13
14 import unittest 14 import unittest
15 import StringIO 15 import StringIO
16 16
17 from grit import grd_reader 17 from grit import grd_reader
18 from grit import util 18 from grit import util
19 from grit.tool import build 19 from grit.tool import build
20 20
21 21 class ChromeMessagesJsonFormatUnittest(unittest.TestCase):
22 class JsMapFormatUnittest(unittest.TestCase):
23 22
24 def testMessages(self): 23 def testMessages(self):
25 grd_text = u""" 24 grd_text = u"""
26 <messages> 25 <messages>
27 <message name="IDS_SIMPLE_MESSAGE"> 26 <message name="IDS_SIMPLE_MESSAGE">
28 Simple message. 27 Simple message.
29 </message> 28 </message>
30 <message name="IDS_QUOTES"> 29 <message name="IDS_QUOTES">
31 element\u2019s \u201c<ph name="NAME">%s<ex>name</ex></ph>\u201d at tribute 30 element\u2019s \u201c<ph name="NAME">%s<ex>name</ex></ph>\u201d at tribute
32 </message> 31 </message>
33 <message name="IDS_PLACEHOLDERS"> 32 <message name="IDS_PLACEHOLDERS">
34 <ph name="ERROR_COUNT">%1$d<ex>1</ex></ph> error, <ph name="WARNIN G_COUNT">%2$d<ex>1</ex></ph> warning 33 <ph name="ERROR_COUNT">%1$d<ex>1</ex></ph> error, <ph name="WARNIN G_COUNT">%2$d<ex>1</ex></ph> warning
35 </message> 34 </message>
36 <message name="IDS_STARTS_WITH_SPACE"> 35 <message name="IDS_STARTS_WITH_SPACE">
37 ''' (<ph name="COUNT">%d<ex>2</ex></ph>) 36 ''' (<ph name="COUNT">%d<ex>2</ex></ph>)
38 </message> 37 </message>
39 <message name="IDS_DOUBLE_QUOTES"> 38 <message name="IDS_DOUBLE_QUOTES">
40 A "double quoted" message. 39 A "double quoted" message.
41 </message> 40 </message>
41 <message name="IDS_BACKSLASH">
42 \\
43 </message>
42 </messages> 44 </messages>
43 """ 45 """
44 root = grd_reader.Parse(StringIO.StringIO(grd_text.encode('utf-8')), 46 root = grd_reader.Parse(StringIO.StringIO(grd_text.encode('utf-8')),
45 flexible_root=True) 47 flexible_root=True)
46 util.FixRootForUnittest(root) 48 util.FixRootForUnittest(root)
47 49
48 buf = StringIO.StringIO() 50 buf = StringIO.StringIO()
49 build.RcBuilder.ProcessNode(root, DummyOutput('js_map_format', 'en'), buf) 51 build.RcBuilder.ProcessNode(root, DummyOutput('chrome_messages_json', 'en'), buf)
50 output = buf.getvalue() 52 output = buf.getvalue()
51 test = u""" 53 test = u"""
52 localizedStrings["Simple message."] = "Simple message."; 54 {
53 localizedStrings["element\u2019s \u201c%s\u201d attribute"] = "element\u2019s \u 201c%s\u201d attribute"; 55 "IDS_SIMPLE_MESSAGE": {
54 localizedStrings["%d error, %d warning"] = "%1$d error, %2$d warning"; 56 "message": "Simple message."
55 localizedStrings[" (%d)"] = " (%d)"; 57 },
56 localizedStrings["A \\\"double quoted\\\" message."] = "A \\\"double quoted\\\" message."; 58 "IDS_QUOTES": {
59 "message": "element\u2019s \u201c%s\u201d attribute"
60 },
61 "IDS_PLACEHOLDERS": {
62 "message": "%1$d error, %2$d warning"
63 },
64 "IDS_STARTS_WITH_SPACE": {
65 "message": "(%d)"
66 },
67 "IDS_DOUBLE_QUOTES": {
68 "message": "A \\"double quoted\\" message."
69 },
70 "IDS_BACKSLASH": {
71 "message": "\\\\"
72 }
73 }
57 """ 74 """
58 self.failUnless(output.strip() == test.strip()) 75 self.failUnless(output.strip() == test.strip())
59 76
60 def testTranslations(self): 77 def testTranslations(self):
61 root = grd_reader.Parse(StringIO.StringIO(""" 78 root = grd_reader.Parse(StringIO.StringIO("""
62 <messages> 79 <messages>
63 <message name="ID_HELLO">Hello!</message> 80 <message name="ID_HELLO">Hello!</message>
64 <message name="ID_HELLO_USER">Hello <ph name="USERNAME">%s<ex> 81 <message name="ID_HELLO_USER">Hello <ph name="USERNAME">%s<ex>
65 Joi</ex></ph></message> 82 Joi</ex></ph></message>
66 </messages> 83 </messages>
67 """), flexible_root=True) 84 """), flexible_root=True)
68 util.FixRootForUnittest(root) 85 util.FixRootForUnittest(root)
69 86
70 buf = StringIO.StringIO() 87 buf = StringIO.StringIO()
71 build.RcBuilder.ProcessNode(root, DummyOutput('js_map_format', 'fr'), buf) 88 build.RcBuilder.ProcessNode(root, DummyOutput('chrome_messages_json', 'fr'), buf)
72 output = buf.getvalue() 89 output = buf.getvalue()
73 test = u""" 90 test = u"""
74 localizedStrings["Hello!"] = "H\xe9P\xe9ll\xf4P\xf4!"; 91 {
75 localizedStrings["Hello %s"] = "H\xe9P\xe9ll\xf4P\xf4 %s"; 92 "ID_HELLO": {
93 "message": "H\xe9P\xe9ll\xf4P\xf4!"
94 },
95 "ID_HELLO_USER": {
96 "message": "H\xe9P\xe9ll\xf4P\xf4 %s"
97 }
98 }
76 """ 99 """
77 self.failUnless(output.strip() == test.strip()) 100 self.failUnless(output.strip() == test.strip())
78 101
79 102
80 class DummyOutput(object): 103 class DummyOutput(object):
81 104
82 def __init__(self, type, language): 105 def __init__(self, type, language):
83 self.type = type 106 self.type = type
84 self.language = language 107 self.language = language
85 108
86 def GetType(self): 109 def GetType(self):
87 return self.type 110 return self.type
88 111
89 def GetLanguage(self): 112 def GetLanguage(self):
90 return self.language 113 return self.language
91 114
92 def GetOutputFilename(self): 115 def GetOutputFilename(self):
93 return 'hello.gif' 116 return 'hello.gif'
94 117
118
95 if __name__ == '__main__': 119 if __name__ == '__main__':
96 unittest.main() 120 unittest.main()
OLDNEW
« no previous file with comments | « grit/format/chrome_messages_json.py ('k') | grit/format/js_map_format.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698