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 tests for <structure> nodes. 7''' 8 9import os 10import os.path 11import sys 12if __name__ == '__main__': 13 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) 14 15import platform 16import tempfile 17import unittest 18import StringIO 19 20from grit import util 21from grit.node import structure 22from grit.format import rc 23 24 25class StructureUnittest(unittest.TestCase): 26 def testSkeleton(self): 27 grd = util.ParseGrdForUnittest(''' 28 <structures> 29 <structure type="dialog" name="IDD_ABOUTBOX" file="klonk.rc" encoding="utf-16-le"> 30 <skeleton expr="lang == 'fr'" variant_of_revision="1" file="klonk-alternate-skeleton.rc" /> 31 </structure> 32 </structures>''', base_dir=util.PathFromRoot('grit/testdata')) 33 grd.SetOutputLanguage('fr') 34 grd.RunGatherers() 35 transl = ''.join(rc.Format(grd, 'fr', '.')) 36 self.failUnless(transl.count('040704') and transl.count('110978')) 37 self.failUnless(transl.count('2005",IDC_STATIC')) 38 39 def testRunCommandOnCurrentPlatform(self): 40 node = structure.StructureNode() 41 node.attrs = node.DefaultAttributes() 42 self.failUnless(node.RunCommandOnCurrentPlatform()) 43 node.attrs['run_command_on_platforms'] = 'Nosuch' 44 self.failIf(node.RunCommandOnCurrentPlatform()) 45 node.attrs['run_command_on_platforms'] = ( 46 'Nosuch,%s,Othernot' % platform.system()) 47 self.failUnless(node.RunCommandOnCurrentPlatform()) 48 49 def testVariables(self): 50 grd = util.ParseGrdForUnittest(''' 51 <structures> 52 <structure type="chrome_html" name="hello_tmpl" file="structure_variables.html" expand_variables="true" variables="GREETING=Hello,THINGS=foo,, bar,, baz,EQUATION=2+2==4,filename=simple" flattenhtml="true"></structure> 53 </structures>''', base_dir=util.PathFromRoot('grit/testdata')) 54 grd.SetOutputLanguage('en') 55 grd.RunGatherers() 56 node, = grd.GetChildrenOfType(structure.StructureNode) 57 filename = node.Process(tempfile.gettempdir()) 58 with open(os.path.join(tempfile.gettempdir(), filename)) as f: 59 result = f.read() 60 self.failUnlessEqual(('<h1>Hello!</h1>\n' 61 'Some cool things are foo, bar, baz.\n' 62 'Did you know that 2+2==4?\n' 63 '<p>\n' 64 ' Hello!\n' 65 '</p>\n'), result) 66 67 68if __name__ == '__main__': 69 unittest.main() 70