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'''Unit test that checks postprocessing of files.
7   Tests postprocessing by having the postprocessor
8   modify the grd data tree, changing the message name attributes.
9'''
10
11import os
12import re
13import sys
14if __name__ == '__main__':
15  sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
16
17import unittest
18
19import grit.tool.postprocess_interface
20from grit.tool import rc2grd
21
22
23class PostProcessingUnittest(unittest.TestCase):
24
25  def testPostProcessing(self):
26    rctext = '''STRINGTABLE
27BEGIN
28  DUMMY_STRING_1         "String 1"
29  // Some random description
30  DUMMY_STRING_2        "This text was added during preprocessing"
31END
32    '''
33    tool = rc2grd.Rc2Grd()
34    class DummyOpts(object):
35      verbose = False
36      extra_verbose = False
37    tool.o = DummyOpts()
38    tool.post_process = 'grit.tool.postprocess_unittest.DummyPostProcessor'
39    result = tool.Process(rctext, '.\resource.rc')
40
41    self.failUnless(
42      result.children[2].children[2].children[0].attrs['name'] == 'SMART_STRING_1')
43    self.failUnless(
44      result.children[2].children[2].children[1].attrs['name'] == 'SMART_STRING_2')
45
46class DummyPostProcessor(grit.tool.postprocess_interface.PostProcessor):
47  '''
48  Post processing replaces all message name attributes containing "DUMMY" to
49  "SMART".
50  '''
51  def Process(self, rctext, rcpath, grdnode):
52    smarter = re.compile(r'(DUMMY)(.*)')
53    messages = grdnode.children[2].children[2]
54    for node in messages.children:
55      name_attr = node.attrs['name']
56      m = smarter.search(name_attr)
57      if m:
58         node.attrs['name'] = 'SMART' + m.group(2)
59    return grdnode
60
61if __name__ == '__main__':
62  unittest.main()
63
64