blob: 190ac9591a6173b30c8618ed84f1e7e952cba851 [file] [log] [blame]
Brian Silverman70325d62015-09-20 17:00:43 -04001// Copyright (c) 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include "config_for_unittests.h"
31#include "tests/template_test_util.h"
32
33#include <stdio.h>
34#include <string>
35#include <vector>
36
37#include "base/arena.h"
38#include <ctemplate/template_dictionary.h>
39#include <ctemplate/template_string.h>
40#include "base/util.h"
41TEST_INIT // defines RUN_ALL_TESTS()
42
43#define ASSERT_EQ(a, b) EXPECT_EQ(a, b)
44
45using std::vector;
46using std::string;
47using GOOGLE_NAMESPACE::UnsafeArena;
48
49using GOOGLE_NAMESPACE::TemplateDictionary;
50using GOOGLE_NAMESPACE::TemplateDictionaryPeer;
51using GOOGLE_NAMESPACE::TemplateString;
52using GOOGLE_NAMESPACE::StaticTemplateString;
53
54namespace {
55
56TEST(TemplateTestUtilTest, GetSectionValue) {
57 TemplateDictionary dict("test_GetSectionValue");
58 dict.SetValue("VALUE", "value");
59
60 TemplateDictionaryPeer peer(&dict);
61 EXPECT_STREQ("value", peer.GetSectionValue("VALUE"));
62}
63
64TEST(TemplateTestUtilTest, IsHiddenSection) {
65 TemplateDictionary dict("test_IsHiddenSection");
66
67 {
68 TemplateDictionaryPeer peer(&dict);
69 EXPECT_TRUE(peer.IsHiddenSection("SECTION"));
70 }
71
72 dict.AddSectionDictionary("SECTION");
73
74 {
75 TemplateDictionaryPeer peer(&dict);
76 EXPECT_FALSE(peer.IsHiddenSection("SECTION"));
77 }
78}
79
80TEST(TemplateTestUtilTest, GetSectionDictionaries) {
81 TemplateDictionary dict("test_GetSectionDictionaries");
82
83 {
84 TemplateDictionaryPeer peer(&dict);
85 vector<const TemplateDictionary*> dicts;
86 // Add some dummy value into the vector to confirm that the call to
87 // GetSectionDictionaries will correctly clear the vector.
88 dicts.push_back(NULL);
89 EXPECT_EQ(0, peer.GetSectionDictionaries("SECTION", &dicts));
90 EXPECT_TRUE(dicts.empty());
91 }
92
93 dict.AddSectionDictionary("SECTION")->SetValue("SECTION_VALUE", "0");
94
95 {
96 TemplateDictionaryPeer peer(&dict);
97 vector<const TemplateDictionary*> dicts;
98 ASSERT_EQ(1, peer.GetSectionDictionaries("SECTION", &dicts));
99
100 TemplateDictionaryPeer peer_section(dicts[0]);
101 EXPECT_STREQ("0", peer_section.GetSectionValue("SECTION_VALUE"));
102 }
103
104 dict.AddSectionDictionary("SECTION")->SetValue("SECTION_VALUE", "1");
105 dict.AddSectionDictionary("ANOTHER_SECTION")->SetValue("ANOTHER_VALUE", "2");
106
107 {
108 TemplateDictionaryPeer peer(&dict);
109 vector<const TemplateDictionary*> dicts;
110 ASSERT_EQ(2, peer.GetSectionDictionaries("SECTION", &dicts));
111
112 TemplateDictionaryPeer peer_section0(dicts[0]);
113 EXPECT_STREQ("0", peer_section0.GetSectionValue("SECTION_VALUE"));
114
115 TemplateDictionaryPeer peer_section1(dicts[1]);
116 EXPECT_STREQ("1", peer_section1.GetSectionValue("SECTION_VALUE"));
117 }
118}
119
120TEST(TemplateTestUtilTest, GetIncludeDictionaries) {
121 TemplateDictionary dict("test_GetIncludeDictionaries");
122
123 {
124 TemplateDictionaryPeer peer(&dict);
125 vector<const TemplateDictionary*> dicts;
126 // Add some dummy value into the vector to confirm that the call to
127 // GetSectionDictionaries will correctly clear the vector.
128 dicts.push_back(NULL);
129 EXPECT_EQ(0, peer.GetIncludeDictionaries("SECTION", &dicts));
130 EXPECT_TRUE(dicts.empty());
131 }
132
133 dict.AddIncludeDictionary("SECTION")->SetValue("SECTION_VALUE", "0");
134
135 {
136 TemplateDictionaryPeer peer(&dict);
137 vector<const TemplateDictionary*> dicts;
138 ASSERT_EQ(1, peer.GetIncludeDictionaries("SECTION", &dicts));
139
140 TemplateDictionaryPeer peer_section(dicts[0]);
141 EXPECT_STREQ("0", peer_section.GetSectionValue("SECTION_VALUE"));
142 }
143
144 dict.AddIncludeDictionary("SECTION")->SetValue("SECTION_VALUE", "1");
145 dict.AddIncludeDictionary("ANOTHER_SECTION")->SetValue("ANOTHER_VALUE", "2");
146
147 {
148 TemplateDictionaryPeer peer(&dict);
149 vector<const TemplateDictionary*> dicts;
150 ASSERT_EQ(2, peer.GetIncludeDictionaries("SECTION", &dicts));
151
152 TemplateDictionaryPeer peer_section0(dicts[0]);
153 EXPECT_STREQ("0", peer_section0.GetSectionValue("SECTION_VALUE"));
154
155 TemplateDictionaryPeer peer_section1(dicts[1]);
156 EXPECT_STREQ("1", peer_section1.GetSectionValue("SECTION_VALUE"));
157 }
158}
159
160TEST(TemplateTestUtilTest, GetIncludeAndSectionDictionaries) {
161 TemplateDictionary dict("test_GetIncludeAndSectionDictionaries");
162
163 {
164 TemplateDictionaryPeer peer(&dict);
165 vector<const TemplateDictionary*> dicts;
166 EXPECT_EQ(0, peer.GetIncludeDictionaries("SECTION", &dicts));
167 EXPECT_EQ(0, peer.GetSectionDictionaries("SECTION", &dicts));
168 }
169
170 dict.AddIncludeDictionary("SECTION")->SetValue("SECTION_VALUE", "0");
171 dict.AddSectionDictionary("SECTION")->SetValue("SECTION_VALUE", "1");
172
173 {
174 TemplateDictionaryPeer peer(&dict);
175 vector<const TemplateDictionary*> include_dicts;
176 ASSERT_EQ(1, peer.GetIncludeDictionaries("SECTION", &include_dicts));
177
178 TemplateDictionaryPeer include_peer(include_dicts[0]);
179 EXPECT_STREQ("0", include_peer.GetSectionValue("SECTION_VALUE"));
180
181 vector<const TemplateDictionary*> section_dicts;
182 ASSERT_EQ(1, peer.GetSectionDictionaries("SECTION", &section_dicts));
183
184 TemplateDictionaryPeer section_peer(section_dicts[0]);
185 EXPECT_STREQ("1", section_peer.GetSectionValue("SECTION_VALUE"));
186 }
187
188 dict.AddIncludeDictionary("SECTION")->SetValue("SECTION_VALUE", "2");
189 dict.AddIncludeDictionary("ANOTHER_SECTION")->SetValue("ANOTHER_VALUE", "3");
190
191 dict.AddSectionDictionary("SECTION")->SetValue("SECTION_VALUE", "4");
192 dict.AddSectionDictionary("ONE_MORE_SECTION")->SetValue("ANOTHER_VALUE", "5");
193
194 {
195 TemplateDictionaryPeer peer(&dict);
196 vector<const TemplateDictionary*> dicts;
197 ASSERT_EQ(2, peer.GetIncludeDictionaries("SECTION", &dicts));
198
199 TemplateDictionaryPeer include_peer0(dicts[0]);
200 EXPECT_STREQ("0", include_peer0.GetSectionValue("SECTION_VALUE"));
201
202 TemplateDictionaryPeer include_peer1(dicts[1]);
203 EXPECT_STREQ("2", include_peer1.GetSectionValue("SECTION_VALUE"));
204
205 EXPECT_EQ(1, peer.GetIncludeDictionaries("ANOTHER_SECTION", &dicts));
206 EXPECT_EQ(0, peer.GetIncludeDictionaries("ONE_MORE_SECTION", &dicts));
207
208 vector<const TemplateDictionary*> section_dicts;
209 ASSERT_EQ(2, peer.GetSectionDictionaries("SECTION", &section_dicts));
210
211 TemplateDictionaryPeer section_peer0(section_dicts[0]);
212 EXPECT_STREQ("1", section_peer0.GetSectionValue("SECTION_VALUE"));
213
214 TemplateDictionaryPeer section_peer1(section_dicts[1]);
215 EXPECT_STREQ("4", section_peer1.GetSectionValue("SECTION_VALUE"));
216
217 EXPECT_EQ(0, peer.GetSectionDictionaries("ANOTHER_SECTION", &dicts));
218 EXPECT_EQ(1, peer.GetSectionDictionaries("ONE_MORE_SECTION", &dicts));
219 }
220}
221
222TEST(TemplateTestUtilTest, GetFilename) {
223 TemplateDictionary parent("test_GetFilename");
224 TemplateDictionary* child = parent.AddIncludeDictionary("INCLUDE_marker");
225 child->SetFilename("included_filename");
226
227 TemplateDictionaryPeer parent_peer(&parent);
228 EXPECT_EQ(NULL, parent_peer.GetFilename());
229
230 TemplateDictionaryPeer child_peer(child);
231 EXPECT_STREQ("included_filename", child_peer.GetFilename());
232}
233
234StaticTemplateString GetTestTemplateString(UnsafeArena* arena) {
235 string will_go_out_of_scope("VALUE");
236 // We want to ensure that the STS_INIT_FOR_TEST macro:
237 // - Can produce a StaticTemplateString (guard again its format changing).
238 // - Produces a StaticTemplateString that is still valid after the string
239 // used to initialize it goes out-of-scope.
240 StaticTemplateString sts = STS_INIT_FOR_TEST(will_go_out_of_scope.c_str(),
241 will_go_out_of_scope.length(),
242 arena);
243 return sts;
244}
245
246TEST(TemplateUtilTest, InitStaticTemplateStringForTest) {
247 UnsafeArena arena(1024);
248 StaticTemplateString kValue = GetTestTemplateString(&arena);
249
250 TemplateDictionary dict("test_GetSectionValue");
251 dict.SetValue(kValue, "value");
252
253 TemplateDictionaryPeer peer(&dict);
254 EXPECT_STREQ("value", peer.GetSectionValue(kValue));
255}
256
257} // namespace anonymous
258
259int main(int argc, char **argv) {
260
261 return RUN_ALL_TESTS();
262}