OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 '''Common tools for unit-testing writers.''' |
| 6 |
| 7 |
| 8 import os |
| 9 import sys |
| 10 if __name__ == '__main__': |
| 11 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../..')) |
| 12 |
| 13 import tempfile |
| 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 |
| 22 class DummyOutput(object): |
| 23 def __init__(self, type, language, file = 'hello.gif'): |
| 24 self.type = type |
| 25 self.language = language |
| 26 self.file = file |
| 27 def GetType(self): |
| 28 return self.type |
| 29 def GetLanguage(self): |
| 30 return self.language |
| 31 def GetOutputFilename(self): |
| 32 return self.file |
| 33 |
| 34 |
| 35 class WriterUnittestCommon(unittest.TestCase): |
| 36 '''Common class for unittesting writers.''' |
| 37 |
| 38 def PrepareTest(self, policy_json): |
| 39 '''Prepares and parses a grit tree along with a data structure of policies. |
| 40 |
| 41 Args: |
| 42 policy_json: The policy data structure in JSON format. |
| 43 ''' |
| 44 # First create a temporary file that contains the JSON policy list. |
| 45 tmp_file_name = 'test.json' |
| 46 tmp_dir_name = tempfile.gettempdir() |
| 47 json_file_path = tmp_dir_name + '/' + tmp_file_name |
| 48 f = open(json_file_path, 'w') |
| 49 f.write(policy_json.strip()) |
| 50 f.close() |
| 51 # Then assemble the grit tree. |
| 52 grd_text = ''' |
| 53 <grit base_dir="." latest_public_release="0" current_release="1" source_lang
_id="en"> |
| 54 <release seq="1"> |
| 55 <structures> |
| 56 <structure name="IDD_POLICY_SOURCE_FILE" file="%s" type="policy_templa
te_metafile" /> |
| 57 </structures> |
| 58 </release> |
| 59 </grit>''' % json_file_path |
| 60 grd_string_io = StringIO.StringIO(grd_text) |
| 61 # Parse the grit tree and load the policies' JSON with a gatherer. |
| 62 grd = grd_reader.Parse(grd_string_io, dir=tmp_dir_name) |
| 63 grd.RunGatherers(recursive=True) |
| 64 # Remove the policies' JSON. |
| 65 os.unlink(json_file_path) |
| 66 return grd |
| 67 |
| 68 def GetOutput(self, grd, env_lang, env_defs, out_type, out_lang): |
| 69 '''Generates an output of a writer. |
| 70 |
| 71 Args: |
| 72 grd: The root of the grit tree. |
| 73 env_lang: The environment language. |
| 74 env_defs: Environment definitions. |
| 75 out_type: Type of the output node for which output will be generated. |
| 76 This selects the writer. |
| 77 out_lang: Language of the output node for which output will be generated. |
| 78 |
| 79 Returns: |
| 80 The string of the tamplete created by the writer. |
| 81 ''' |
| 82 grd.SetOutputContext(env_lang, env_defs) |
| 83 buf = StringIO.StringIO() |
| 84 build.RcBuilder.ProcessNode(grd, DummyOutput(out_type, out_lang), buf) |
| 85 return buf.getvalue() |
OLD | NEW |