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.
6 '''Unit tests for grit.format.resource_map'''
10 if __name__ == '__main__':
11 sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
16 from grit import grd_reader
18 from grit.format import resource_map
21 class FormatResourceMapUnittest(unittest.TestCase):
22 def testFormatResourceMap(self):
23 grd = grd_reader.Parse(StringIO.StringIO(
24 '''<?xml version="1.0" encoding="UTF-8"?>
25 <grit latest_public_release="2" source_lang_id="en" current_release="3"
28 <output type="rc_header" filename="the_rc_header.h" />
29 <output type="resource_map_header"
30 filename="the_resource_map_header.h" />
33 <structures first_id="300">
34 <structure type="menu" name="IDC_KLONKMENU"
35 file="grit\\testdata\\klonk.rc" encoding="utf-16" />
37 <includes first_id="10000">
38 <include type="foo" file="abc" name="IDS_FIRSTPRESENT" />
40 <include type="foo" file="def" name="IDS_MISSING" />
42 <if expr="lang != 'es'">
43 <include type="foo" file="ghi" name="IDS_LANGUAGESPECIFIC" />
45 <if expr="lang == 'es'">
46 <include type="foo" file="jkl" name="IDS_LANGUAGESPECIFIC" />
48 <include type="foo" file="mno" name="IDS_THIRDPRESENT" />
51 </grit>'''), util.PathFromRoot('.'))
52 grd.SetOutputLanguage('en')
54 output = util.StripBlankLinesAndComments(''.join(
55 resource_map.GetFormatter('resource_map_header')(grd, 'en', '.')))
58 #ifndef GRIT_RESOURCE_MAP_STRUCT_
59 #define GRIT_RESOURCE_MAP_STRUCT_
60 struct GritResourceMap {
61 const char* const name;
64 #endif // GRIT_RESOURCE_MAP_STRUCT_
65 extern const GritResourceMap kTheRcHeader[];
66 extern const size_t kTheRcHeaderSize;''', output)
67 output = util.StripBlankLinesAndComments(''.join(
68 resource_map.GetFormatter('resource_map_source')(grd, 'en', '.')))
70 #include "the_resource_map_header.h"
71 #include "base/basictypes.h"
72 #include "the_rc_header.h"
73 const GritResourceMap kTheRcHeader[] = {
74 {"IDC_KLONKMENU", IDC_KLONKMENU},
75 {"IDS_FIRSTPRESENT", IDS_FIRSTPRESENT},
76 {"IDS_MISSING", IDS_MISSING},
77 {"IDS_LANGUAGESPECIFIC", IDS_LANGUAGESPECIFIC},
78 {"IDS_THIRDPRESENT", IDS_THIRDPRESENT},
80 const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output)
81 output = util.StripBlankLinesAndComments(''.join(
82 resource_map.GetFormatter('resource_file_map_source')(grd, 'en', '.')))
84 #include "the_resource_map_header.h"
85 #include "base/basictypes.h"
86 #include "the_rc_header.h"
87 const GritResourceMap kTheRcHeader[] = {
88 {"grit/testdata/klonk.rc", IDC_KLONKMENU},
89 {"abc", IDS_FIRSTPRESENT},
91 {"ghi", IDS_LANGUAGESPECIFIC},
92 {"jkl", IDS_LANGUAGESPECIFIC},
93 {"mno", IDS_THIRDPRESENT},
95 const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output)
97 def testFormatStringResourceMap(self):
98 grd = grd_reader.Parse(StringIO.StringIO(
99 '''<?xml version="1.0" encoding="UTF-8"?>
100 <grit latest_public_release="2" source_lang_id="en" current_release="3"
103 <output type="rc_header" filename="the_rc_header.h" />
104 <output type="resource_map_header" filename="the_rc_map_header.h" />
105 <output type="resource_map_source" filename="the_rc_map_source.cc" />
107 <release seq="1" allow_pseudo="false">
108 <messages fallback_to_english="true">
109 <message name="IDS_PRODUCT_NAME" desc="The application name">
113 <message name="IDS_DEFAULT_TAB_TITLE_TITLE_CASE"
114 desc="In Title Case: The default title in a tab.">
119 <message name="IDS_DEFAULT_TAB_TITLE"
120 desc="The default title in a tab.">
126 </grit>'''), util.PathFromRoot('.'))
127 grd.SetOutputLanguage('en')
129 output = util.StripBlankLinesAndComments(''.join(
130 resource_map.GetFormatter('resource_map_header')(grd, 'en', '.')))
131 self.assertEqual('''\
133 #ifndef GRIT_RESOURCE_MAP_STRUCT_
134 #define GRIT_RESOURCE_MAP_STRUCT_
135 struct GritResourceMap {
136 const char* const name;
139 #endif // GRIT_RESOURCE_MAP_STRUCT_
140 extern const GritResourceMap kTheRcHeader[];
141 extern const size_t kTheRcHeaderSize;''', output)
142 output = util.StripBlankLinesAndComments(''.join(
143 resource_map.GetFormatter('resource_map_source')(grd, 'en', '.')))
144 self.assertEqual('''\
145 #include "the_rc_map_header.h"
146 #include "base/basictypes.h"
147 #include "the_rc_header.h"
148 const GritResourceMap kTheRcHeader[] = {
149 {"IDS_PRODUCT_NAME", IDS_PRODUCT_NAME},
150 {"IDS_DEFAULT_TAB_TITLE_TITLE_CASE", IDS_DEFAULT_TAB_TITLE_TITLE_CASE},
152 const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output)
155 if __name__ == '__main__':