| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 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 | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Unittest for chrome_messages_json.py. | |
| 7 """ | |
| 8 | |
| 9 import os | |
| 10 import sys | |
| 11 if __name__ == '__main__': | |
| 12 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) | |
| 13 | |
| 14 import unittest | |
| 15 import StringIO | |
| 16 | |
| 17 from grit import grd_reader | |
| 18 from grit import util | |
| 19 from grit.tool import build | |
| 20 | |
| 21 class ChromeMessagesJsonFormatUnittest(unittest.TestCase): | |
| 22 | |
| 23 def testMessages(self): | |
| 24 root = util.ParseGrdForUnittest(u""" | |
| 25 <messages> | |
| 26 <message name="IDS_SIMPLE_MESSAGE"> | |
| 27 Simple message. | |
| 28 </message> | |
| 29 <message name="IDS_QUOTES"> | |
| 30 element\u2019s \u201c<ph name="NAME">%s<ex>name</ex></ph>\u201d at
tribute | |
| 31 </message> | |
| 32 <message name="IDS_PLACEHOLDERS"> | |
| 33 <ph name="ERROR_COUNT">%1$d<ex>1</ex></ph> error, <ph name="WARNIN
G_COUNT">%2$d<ex>1</ex></ph> warning | |
| 34 </message> | |
| 35 <message name="IDS_PLACEHOLDERS_SUBSTITUTED_BY_GETMESSAGE"> | |
| 36 <ph name="BEGIN">$1<ex>a</ex></ph>test<ph name="END">$2<ex>b</ex><
/ph> | |
| 37 </message> | |
| 38 <message name="IDS_STARTS_WITH_SPACE"> | |
| 39 ''' (<ph name="COUNT">%d<ex>2</ex></ph>) | |
| 40 </message> | |
| 41 <message name="IDS_ENDS_WITH_SPACE"> | |
| 42 (<ph name="COUNT">%d<ex>2</ex></ph>) ''' | |
| 43 </message> | |
| 44 <message name="IDS_SPACE_AT_BOTH_ENDS"> | |
| 45 ''' (<ph name="COUNT">%d<ex>2</ex></ph>) ''' | |
| 46 </message> | |
| 47 <message name="IDS_DOUBLE_QUOTES"> | |
| 48 A "double quoted" message. | |
| 49 </message> | |
| 50 <message name="IDS_BACKSLASH"> | |
| 51 \\ | |
| 52 </message> | |
| 53 </messages> | |
| 54 """) | |
| 55 | |
| 56 buf = StringIO.StringIO() | |
| 57 build.RcBuilder.ProcessNode(root, DummyOutput('chrome_messages_json', 'en'),
buf) | |
| 58 output = buf.getvalue() | |
| 59 test = u""" | |
| 60 { | |
| 61 "SIMPLE_MESSAGE": { | |
| 62 "message": "Simple message." | |
| 63 }, | |
| 64 "QUOTES": { | |
| 65 "message": "element\\u2019s \\u201c%s\\u201d attribute" | |
| 66 }, | |
| 67 "PLACEHOLDERS": { | |
| 68 "message": "%1$d error, %2$d warning" | |
| 69 }, | |
| 70 "PLACEHOLDERS_SUBSTITUTED_BY_GETMESSAGE": { | |
| 71 "message": "$1$test$2$", | |
| 72 "placeholders": { | |
| 73 "1": { | |
| 74 "content": "$1" | |
| 75 }, | |
| 76 "2": { | |
| 77 "content": "$2" | |
| 78 } | |
| 79 } | |
| 80 }, | |
| 81 "STARTS_WITH_SPACE": { | |
| 82 "message": " (%d)" | |
| 83 }, | |
| 84 "ENDS_WITH_SPACE": { | |
| 85 "message": "(%d) " | |
| 86 }, | |
| 87 "SPACE_AT_BOTH_ENDS": { | |
| 88 "message": " (%d) " | |
| 89 }, | |
| 90 "DOUBLE_QUOTES": { | |
| 91 "message": "A \\"double quoted\\" message." | |
| 92 }, | |
| 93 "BACKSLASH": { | |
| 94 "message": "\\\\" | |
| 95 } | |
| 96 } | |
| 97 """ | |
| 98 self.assertEqual(test.strip(), output.strip()) | |
| 99 | |
| 100 def testTranslations(self): | |
| 101 root = util.ParseGrdForUnittest(""" | |
| 102 <messages> | |
| 103 <message name="ID_HELLO">Hello!</message> | |
| 104 <message name="ID_HELLO_USER">Hello <ph name="USERNAME">%s<ex> | |
| 105 Joi</ex></ph></message> | |
| 106 </messages> | |
| 107 """) | |
| 108 | |
| 109 buf = StringIO.StringIO() | |
| 110 build.RcBuilder.ProcessNode(root, DummyOutput('chrome_messages_json', 'fr'),
buf) | |
| 111 output = buf.getvalue() | |
| 112 test = u""" | |
| 113 { | |
| 114 "ID_HELLO": { | |
| 115 "message": "H\\u00e9P\\u00e9ll\\u00f4P\\u00f4!" | |
| 116 }, | |
| 117 "ID_HELLO_USER": { | |
| 118 "message": "H\\u00e9P\\u00e9ll\\u00f4P\\u00f4 %s" | |
| 119 } | |
| 120 } | |
| 121 """ | |
| 122 self.assertEqual(test.strip(), output.strip()) | |
| 123 | |
| 124 | |
| 125 class DummyOutput(object): | |
| 126 | |
| 127 def __init__(self, type, language): | |
| 128 self.type = type | |
| 129 self.language = language | |
| 130 | |
| 131 def GetType(self): | |
| 132 return self.type | |
| 133 | |
| 134 def GetLanguage(self): | |
| 135 return self.language | |
| 136 | |
| 137 def GetOutputFilename(self): | |
| 138 return 'hello.gif' | |
| 139 | |
| 140 | |
| 141 if __name__ == '__main__': | |
| 142 unittest.main() | |
| OLD | NEW |