OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 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 '''Unit tests for grit.gather.policy_json''' |
| 7 |
| 8 import os |
| 9 import re |
| 10 import sys |
| 11 if __name__ == '__main__': |
| 12 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../..')) |
| 13 |
| 14 import unittest |
| 15 |
| 16 from grit.gather import muppet_strings |
| 17 from grit.gather import policy_json |
| 18 |
| 19 class PolicyJsonUnittest(unittest.TestCase): |
| 20 |
| 21 def GetExpectedOutput(self, original): |
| 22 expected = eval(original) |
| 23 for key, message in expected['messages'].iteritems(): |
| 24 del message['desc'] |
| 25 return expected |
| 26 |
| 27 def testEmpty(self): |
| 28 original = "{'policy_definitions': [], 'messages': {}}" |
| 29 gatherer = policy_json.PolicyJson(original) |
| 30 gatherer.Parse() |
| 31 self.failUnless(len(gatherer.GetCliques()) == 0) |
| 32 self.failUnless(eval(original) == eval(gatherer.Translate('en'))) |
| 33 |
| 34 def testGeneralPolicy(self): |
| 35 original = ( |
| 36 "{" |
| 37 " 'policy_definitions': [" |
| 38 " {" |
| 39 " 'name': 'HomepageLocation'," |
| 40 " 'type': 'string'," |
| 41 " 'supported_on': ['chrome.*:8-']," |
| 42 " 'features': {'dynamic_refresh': 1}," |
| 43 " 'example_value': 'http://chromium.org'," |
| 44 " 'caption': 'nothing special 1'," |
| 45 " 'desc': 'nothing special 2'," |
| 46 " 'label': 'nothing special 3'," |
| 47 " }," |
| 48 " ]," |
| 49 " 'messages': {" |
| 50 " 'msg_identifier': {" |
| 51 " 'text': 'nothing special 3'," |
| 52 " 'desc': 'nothing special descr 3'," |
| 53 " }" |
| 54 " }" |
| 55 "}") |
| 56 gatherer = policy_json.PolicyJson(original) |
| 57 gatherer.Parse() |
| 58 self.failUnless(len(gatherer.GetCliques()) == 4) |
| 59 expected = self.GetExpectedOutput(original) |
| 60 self.failUnless(expected == eval(gatherer.Translate('en'))) |
| 61 |
| 62 def testEnum(self): |
| 63 original = ( |
| 64 "{" |
| 65 " 'policy_definitions': [" |
| 66 " {" |
| 67 " 'name': 'Policy1'," |
| 68 " 'items': [" |
| 69 " {" |
| 70 " 'name': 'Item1'," |
| 71 " 'caption': 'nothing special'," |
| 72 " }" |
| 73 " ]" |
| 74 " }," |
| 75 " ]," |
| 76 " 'messages': {}" |
| 77 "}") |
| 78 gatherer = policy_json.PolicyJson(original) |
| 79 gatherer.Parse() |
| 80 self.failUnless(len(gatherer.GetCliques()) == 1) |
| 81 expected = self.GetExpectedOutput(original) |
| 82 self.failUnless(expected == eval(gatherer.Translate('en'))) |
| 83 |
| 84 def testSubPolicy(self): |
| 85 original = ( |
| 86 "{" |
| 87 " 'policy_definitions': [" |
| 88 " {" |
| 89 " 'policies': [" |
| 90 " {" |
| 91 " 'name': 'Policy1'," |
| 92 " 'caption': 'nothing special'," |
| 93 " }" |
| 94 " ]" |
| 95 " }," |
| 96 " ]," |
| 97 " 'messages': {}" |
| 98 "}") |
| 99 gatherer = policy_json.PolicyJson(original) |
| 100 gatherer.Parse() |
| 101 self.failUnless(len(gatherer.GetCliques()) == 1) |
| 102 expected = self.GetExpectedOutput(original) |
| 103 self.failUnless(expected == eval(gatherer.Translate('en'))) |
| 104 |
| 105 def testEscapingAndLineBreaks(self): |
| 106 original = """{ |
| 107 'policy_definitions': [], |
| 108 'messages': { |
| 109 'msg1': { |
| 110 # The following line will contain two backslash characters when it |
| 111 # ends up in eval(). |
| 112 'text': '''backslashes, Sir? \\\\''', |
| 113 'desc': '', |
| 114 }, |
| 115 'msg2': { |
| 116 'text': '''quotes, Madam? "''', |
| 117 'desc': '', |
| 118 }, |
| 119 'msg3': { |
| 120 # The following line will contain two backslash characters when it |
| 121 # ends up in eval(). |
| 122 'text': 'backslashes, Sir? \\\\', |
| 123 'desc': '', |
| 124 }, |
| 125 'msg4': { |
| 126 'text': "quotes, Madam? '", |
| 127 'desc': '', |
| 128 }, |
| 129 'msg5': { |
| 130 'text': '''what happens |
| 131 with a newline?''', |
| 132 'desc': '' |
| 133 }, |
| 134 'msg6': { |
| 135 # The following line will contain a backslash+n when it ends up in |
| 136 # eval(). |
| 137 'text': 'what happens\\nwith a newline? (Episode 1)', |
| 138 'desc': '' |
| 139 } |
| 140 } |
| 141 }""" |
| 142 gatherer = policy_json.PolicyJson(original) |
| 143 gatherer.Parse() |
| 144 self.failUnless(len(gatherer.GetCliques()) == 6) |
| 145 expected = self.GetExpectedOutput(original) |
| 146 self.failUnless(expected == eval(gatherer.Translate('en'))) |
| 147 |
| 148 def testPlaceholders(self): |
| 149 original = """{ |
| 150 'policy_definitions': [ |
| 151 { |
| 152 'name': 'Policy1', |
| 153 'caption': '''Please install |
| 154 <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph>.''', |
| 155 }, |
| 156 ], |
| 157 'messages': {} |
| 158 }""" |
| 159 gatherer = policy_json.PolicyJson(original) |
| 160 gatherer.Parse() |
| 161 self.failUnless(len(gatherer.GetCliques()) == 1) |
| 162 expected = eval(re.sub('<ph.*ph>', '$1', original)) |
| 163 self.failUnless(expected == eval(gatherer.Translate('en'))) |
| 164 self.failUnless(gatherer.GetCliques()[0].translateable) |
| 165 msg = gatherer.GetCliques()[0].GetMessage() |
| 166 self.failUnless(len(msg.GetPlaceholders()) == 1) |
| 167 ph = msg.GetPlaceholders()[0] |
| 168 self.failUnless(ph.GetOriginal() == '$1') |
| 169 self.failUnless(ph.GetPresentation() == 'PRODUCT_NAME') |
| 170 self.failUnless(ph.GetExample() == 'Google Chrome') |
| 171 |
| 172 def testGetDescription(self): |
| 173 gatherer = policy_json.PolicyJson({}) |
| 174 self.assertEquals( |
| 175 gatherer._GetDescription({'name': 'Policy1'}, 'policy', None, 'desc'), |
| 176 'Description of the policy named Policy1') |
| 177 self.assertEquals( |
| 178 gatherer._GetDescription({'name': 'Plcy2'}, 'policy', None, 'caption'), |
| 179 'Caption of the policy named Plcy2') |
| 180 self.assertEquals( |
| 181 gatherer._GetDescription({'name': 'Plcy3'}, 'policy', None, 'label'), |
| 182 'Label of the policy named Plcy3') |
| 183 self.assertEquals( |
| 184 gatherer._GetDescription({'name': 'Item'}, 'enum_item', |
| 185 {'name': 'Policy'}, 'caption'), |
| 186 'Caption of the option named Item in policy Policy') |
| 187 |
| 188 |
| 189 if __name__ == '__main__': |
| 190 unittest.main() |
OLD | NEW |