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 the rc_header formatter''' 7 8# GRD samples exceed the 80 character limit. 9# pylint: disable-msg=C6310 10 11import os 12import sys 13if __name__ == '__main__': 14 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) 15 16import StringIO 17import unittest 18 19from grit import exception 20from grit import grd_reader 21from grit import util 22from grit.format import rc_header 23 24 25class RcHeaderFormatterUnittest(unittest.TestCase): 26 def FormatAll(self, grd): 27 output = rc_header.FormatDefines(grd, grd.ShouldOutputAllResourceDefines()) 28 return ''.join(output).replace(' ', '') 29 30 def testFormatter(self): 31 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> 32 <grit latest_public_release="2" source_lang_id="en" current_release="3" base_dir="."> 33 <release seq="3"> 34 <includes first_id="300" comment="bingo"> 35 <include type="gif" name="ID_LOGO" file="images/logo.gif" /> 36 </includes> 37 <messages first_id="10000"> 38 <message name="IDS_GREETING" desc="Printed to greet the currently logged in user"> 39 Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing today? 40 </message> 41 <message name="IDS_BONGO"> 42 Bongo! 43 </message> 44 </messages> 45 <structures> 46 <structure type="dialog" name="IDD_NARROW_DIALOG" file="rc_files/dialogs.rc" /> 47 <structure type="version" name="VS_VERSION_INFO" file="rc_files/version.rc" /> 48 </structures> 49 </release> 50 </grit>'''), '.') 51 output = self.FormatAll(grd) 52 self.failUnless(output.count('IDS_GREETING10000')) 53 self.failUnless(output.count('ID_LOGO300')) 54 55 def testOnlyDefineResourcesThatSatisfyOutputCondition(self): 56 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> 57 <grit latest_public_release="2" source_lang_id="en" current_release="3" 58 base_dir="." output_all_resource_defines="false"> 59 <release seq="3"> 60 <includes first_id="300" comment="bingo"> 61 <include type="gif" name="ID_LOGO" file="images/logo.gif" /> 62 </includes> 63 <messages first_id="10000"> 64 <message name="IDS_FIRSTPRESENTSTRING" desc="Present in .rc file."> 65 I will appear in the .rc file. 66 </message> 67 <if expr="False"> <!--Do not include in the .rc files until used.--> 68 <message name="IDS_MISSINGSTRING" desc="Not present in .rc file."> 69 I will not appear in the .rc file. 70 </message> 71 </if> 72 <if expr="lang != 'es'"> 73 <message name="IDS_LANGUAGESPECIFICSTRING" desc="Present in .rc file."> 74 Hello. 75 </message> 76 </if> 77 <if expr="lang == 'es'"> 78 <message name="IDS_LANGUAGESPECIFICSTRING" desc="Present in .rc file."> 79 Hola. 80 </message> 81 </if> 82 <message name="IDS_THIRDPRESENTSTRING" desc="Present in .rc file."> 83 I will also appear in the .rc file. 84 </message> 85 </messages> 86 </release> 87 </grit>'''), '.') 88 output = self.FormatAll(grd) 89 self.failUnless(output.count('IDS_FIRSTPRESENTSTRING10000')) 90 self.failIf(output.count('IDS_MISSINGSTRING')) 91 self.failIf(output.count('10001')) # IDS_MISSINGSTRING should get this ID 92 self.failUnless(output.count('IDS_LANGUAGESPECIFICSTRING10002')) 93 self.failUnless(output.count('IDS_THIRDPRESENTSTRING10003')) 94 95 def testExplicitFirstIdOverlaps(self): 96 # second first_id will overlap preexisting range 97 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> 98 <grit latest_public_release="2" source_lang_id="en" current_release="3" base_dir="."> 99 <release seq="3"> 100 <includes first_id="300" comment="bingo"> 101 <include type="gif" name="ID_LOGO" file="images/logo.gif" /> 102 <include type="gif" name="ID_LOGO2" file="images/logo2.gif" /> 103 </includes> 104 <messages first_id="301"> 105 <message name="IDS_GREETING" desc="Printed to greet the currently logged in user"> 106 Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing today? 107 </message> 108 <message name="IDS_SMURFGEBURF">Frubegfrums</message> 109 </messages> 110 </release> 111 </grit>'''), '.') 112 self.assertRaises(exception.IdRangeOverlap, self.FormatAll, grd) 113 114 def testImplicitOverlapsPreexisting(self): 115 # second message in <messages> will overlap preexisting range 116 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> 117 <grit latest_public_release="2" source_lang_id="en" current_release="3" base_dir="."> 118 <release seq="3"> 119 <includes first_id="301" comment="bingo"> 120 <include type="gif" name="ID_LOGO" file="images/logo.gif" /> 121 <include type="gif" name="ID_LOGO2" file="images/logo2.gif" /> 122 </includes> 123 <messages first_id="300"> 124 <message name="IDS_GREETING" desc="Printed to greet the currently logged in user"> 125 Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing today? 126 </message> 127 <message name="IDS_SMURFGEBURF">Frubegfrums</message> 128 </messages> 129 </release> 130 </grit>'''), '.') 131 self.assertRaises(exception.IdRangeOverlap, self.FormatAll, grd) 132 133 def testEmit(self): 134 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> 135 <grit latest_public_release="2" source_lang_id="en" current_release="3" base_dir="."> 136 <outputs> 137 <output type="rc_all" filename="dummy"> 138 <emit emit_type="prepend">Wrong</emit> 139 </output> 140 <if expr="False"> 141 <output type="rc_header" filename="dummy"> 142 <emit emit_type="prepend">No</emit> 143 </output> 144 </if> 145 <output type="rc_header" filename="dummy"> 146 <emit emit_type="append">Error</emit> 147 </output> 148 <output type="rc_header" filename="dummy"> 149 <emit emit_type="prepend">Bingo</emit> 150 </output> 151 </outputs> 152 </grit>'''), '.') 153 output = ''.join(rc_header.Format(grd, 'en', '.')) 154 output = util.StripBlankLinesAndComments(output) 155 self.assertEqual('#pragma once\nBingo', output) 156 157 158if __name__ == '__main__': 159 unittest.main() 160