| 1 |
//===- llvm/unittest/Support/MustacheTest.cpp ----------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// Test conforming to Mustache 1.4.2 spec found here: |
| 10 |
// https://github.com/mustache/spec |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "llvm/Support/Mustache.h" |
| 15 |
#include "llvm/Support/raw_ostream.h" |
| 16 |
#include "gtest/gtest.h" |
| 17 |
|
| 18 |
using namespace llvm; |
| 19 |
using namespace llvm::mustache; |
| 20 |
using namespace llvm::json; |
| 21 |
|
| 22 |
TEST(MustacheInterpolation, NoInterpolation) { |
| 23 |
// Mustache-free templates should render as-is. |
| 24 |
Value D = {}; |
| 25 |
auto T = Template("Hello from {Mustache}!\n"); |
| 26 |
std::string Out; |
| 27 |
raw_string_ostream OS(Out); |
| 28 |
T.render(D, OS); |
| 29 |
EXPECT_EQ("Hello from {Mustache}!\n", Out); |
| 30 |
} |
| 31 |
|
| 32 |
TEST(MustacheInterpolation, BasicInterpolation) { |
| 33 |
// Unadorned tags should interpolate content into the template. |
| 34 |
Value D = Object{{"subject", "World"}}; |
| 35 |
auto T = Template("Hello, {{subject}}!"); |
| 36 |
std::string Out; |
| 37 |
raw_string_ostream OS(Out); |
| 38 |
T.render(D, OS); |
| 39 |
EXPECT_EQ("Hello, World!", Out); |
| 40 |
} |
| 41 |
|
| 42 |
TEST(MustacheInterpolation, NoReinterpolation) { |
| 43 |
// Interpolated tag output should not be re-interpolated. |
| 44 |
Value D = Object{{"template", "{{planet}}"}, {"planet", "Earth"}}; |
| 45 |
auto T = Template("{{template}}: {{planet}}"); |
| 46 |
std::string Out; |
| 47 |
raw_string_ostream OS(Out); |
| 48 |
T.render(D, OS); |
| 49 |
EXPECT_EQ("{{planet}}: Earth", Out); |
| 50 |
} |
| 51 |
|
| 52 |
TEST(MustacheInterpolation, HTMLEscaping) { |
| 53 |
// Interpolated tag output should not be re-interpolated. |
| 54 |
Value D = Object{ |
| 55 |
{"forbidden", "& \" < >"}, |
| 56 |
}; |
| 57 |
auto T = Template("These characters should be HTML escaped: {{forbidden}}\n"); |
| 58 |
std::string Out; |
| 59 |
raw_string_ostream OS(Out); |
| 60 |
T.render(D, OS); |
| 61 |
EXPECT_EQ("These characters should be HTML escaped: & " < >\n", |
| 62 |
Out); |
| 63 |
} |
| 64 |
|
| 65 |
TEST(MustacheInterpolation, Ampersand) { |
| 66 |
// Interpolated tag output should not be re-interpolated. |
| 67 |
Value D = Object{ |
| 68 |
{"forbidden", "& \" < >"}, |
| 69 |
}; |
| 70 |
auto T = |
| 71 |
Template("These characters should not be HTML escaped: {{&forbidden}}\n"); |
| 72 |
std::string Out; |
| 73 |
raw_string_ostream OS(Out); |
| 74 |
T.render(D, OS); |
| 75 |
EXPECT_EQ("These characters should not be HTML escaped: & \" < >\n", Out); |
| 76 |
} |
| 77 |
|
| 78 |
TEST(MustacheInterpolation, BasicIntegerInterpolation) { |
| 79 |
// Integers should interpolate seamlessly. |
| 80 |
Value D = Object{{"mph", 85}}; |
| 81 |
auto T = Template("{{mph}} miles an hour!"); |
| 82 |
std::string Out; |
| 83 |
raw_string_ostream OS(Out); |
| 84 |
T.render(D, OS); |
| 85 |
EXPECT_EQ("85 miles an hour!", Out); |
| 86 |
} |
| 87 |
|
| 88 |
TEST(MustacheInterpolation, AmpersandIntegerInterpolation) { |
| 89 |
// Integers should interpolate seamlessly. |
| 90 |
Value D = Object{{"mph", 85}}; |
| 91 |
auto T = Template("{{&mph}} miles an hour!"); |
| 92 |
std::string Out; |
| 93 |
raw_string_ostream OS(Out); |
| 94 |
T.render(D, OS); |
| 95 |
EXPECT_EQ("85 miles an hour!", Out); |
| 96 |
} |
| 97 |
|
| 98 |
TEST(MustacheInterpolation, BasicDecimalInterpolation) { |
| 99 |
// Decimals should interpolate seamlessly with proper significance. |
| 100 |
Value D = Object{{"power", 1.21}}; |
| 101 |
auto T = Template("{{power}} jiggawatts!"); |
| 102 |
std::string Out; |
| 103 |
raw_string_ostream OS(Out); |
| 104 |
T.render(D, OS); |
| 105 |
EXPECT_EQ("1.21 jiggawatts!", Out); |
| 106 |
} |
| 107 |
|
| 108 |
TEST(MustacheInterpolation, BasicNullInterpolation) { |
| 109 |
// Nulls should interpolate as the empty string. |
| 110 |
Value D = Object{{"cannot", nullptr}}; |
| 111 |
auto T = Template("I ({{cannot}}) be seen!"); |
| 112 |
std::string Out; |
| 113 |
raw_string_ostream OS(Out); |
| 114 |
T.render(D, OS); |
| 115 |
EXPECT_EQ("I () be seen!", Out); |
| 116 |
} |
| 117 |
|
| 118 |
TEST(MustacheInterpolation, AmpersandNullInterpolation) { |
| 119 |
// Nulls should interpolate as the empty string. |
| 120 |
Value D = Object{{"cannot", nullptr}}; |
| 121 |
auto T = Template("I ({{&cannot}}) be seen!"); |
| 122 |
std::string Out; |
| 123 |
raw_string_ostream OS(Out); |
| 124 |
T.render(D, OS); |
| 125 |
EXPECT_EQ("I () be seen!", Out); |
| 126 |
} |
| 127 |
|
| 128 |
TEST(MustacheInterpolation, BasicContextMissInterpolation) { |
| 129 |
// Failed context lookups should default to empty strings. |
| 130 |
Value D = Object{}; |
| 131 |
auto T = Template("I ({{cannot}}) be seen!"); |
| 132 |
std::string Out; |
| 133 |
raw_string_ostream OS(Out); |
| 134 |
T.render(D, OS); |
| 135 |
EXPECT_EQ("I () be seen!", Out); |
| 136 |
} |
| 137 |
|
| 138 |
TEST(MustacheInterpolation, DottedNamesBasicInterpolation) { |
| 139 |
// Dotted names should be considered a form of shorthand for sections. |
| 140 |
Value D = Object{{"person", Object{{"name", "Joe"}}}}; |
| 141 |
auto T = Template("{{person.name}} == {{#person}}{{name}}{{/person}}"); |
| 142 |
std::string Out; |
| 143 |
raw_string_ostream OS(Out); |
| 144 |
T.render(D, OS); |
| 145 |
EXPECT_EQ("Joe == Joe", Out); |
| 146 |
} |
| 147 |
|
| 148 |
TEST(MustacheInterpolation, DottedNamesAmpersandInterpolation) { |
| 149 |
// Dotted names should be considered a form of shorthand for sections. |
| 150 |
Value D = Object{{"person", Object{{"name", "Joe"}}}}; |
| 151 |
auto T = Template("{{&person.name}} == {{#person}}{{&name}}{{/person}}"); |
| 152 |
std::string Out; |
| 153 |
raw_string_ostream OS(Out); |
| 154 |
T.render(D, OS); |
| 155 |
EXPECT_EQ("Joe == Joe", Out); |
| 156 |
} |
| 157 |
|
| 158 |
TEST(MustacheInterpolation, DottedNamesArbitraryDepth) { |
| 159 |
// Dotted names should be functional to any level of nesting. |
| 160 |
Value D = Object{ |
| 161 |
{"a", |
| 162 |
Object{{"b", |
| 163 |
Object{{"c", |
| 164 |
Object{{"d", |
| 165 |
Object{{"e", Object{{"name", "Phil"}}}}}}}}}}}}; |
| 166 |
auto T = Template("{{a.b.c.d.e.name}}"); |
| 167 |
std::string Out; |
| 168 |
raw_string_ostream OS(Out); |
| 169 |
T.render(D, OS); |
| 170 |
EXPECT_EQ("Phil", Out); |
| 171 |
} |
| 172 |
|
| 173 |
TEST(MustacheInterpolation, DottedNamesBrokenChains) { |
| 174 |
// Any falsey value prior to the last part of the name should yield ''. |
| 175 |
Value D = Object{{"a", Object{}}}; |
| 176 |
auto T = Template("{{a.b.c}} == "); |
| 177 |
std::string Out; |
| 178 |
raw_string_ostream OS(Out); |
| 179 |
T.render(D, OS); |
| 180 |
EXPECT_EQ(" == ", Out); |
| 181 |
} |
| 182 |
|
| 183 |
TEST(MustacheInterpolation, DottedNamesBrokenChainResolution) { |
| 184 |
// Each part of a dotted name should resolve only against its parent. |
| 185 |
Value D = |
| 186 |
Object{{"a", Object{{"b", Object{}}}}, {"c", Object{{"name", "Jim"}}}}; |
| 187 |
auto T = Template("{{a.b.c.name}} == "); |
| 188 |
std::string Out; |
| 189 |
raw_string_ostream OS(Out); |
| 190 |
T.render(D, OS); |
| 191 |
EXPECT_EQ(" == ", Out); |
| 192 |
} |
| 193 |
|
| 194 |
TEST(MustacheInterpolation, DottedNamesInitialResolution) { |
| 195 |
// The first part of a dotted name should resolve as any other name. |
| 196 |
Value D = Object{ |
| 197 |
{"a", |
| 198 |
Object{ |
| 199 |
{"b", |
| 200 |
Object{{"c", |
| 201 |
Object{{"d", Object{{"e", Object{{"name", "Phil"}}}}}}}}}}}, |
| 202 |
{"b", |
| 203 |
Object{{"c", Object{{"d", Object{{"e", Object{{"name", "Wrong"}}}}}}}}}}; |
| 204 |
auto T = Template("{{#a}}{{b.c.d.e.name}}{{/a}}"); |
| 205 |
std::string Out; |
| 206 |
raw_string_ostream OS(Out); |
| 207 |
T.render(D, OS); |
| 208 |
EXPECT_EQ("Phil", Out); |
| 209 |
} |
| 210 |
|
| 211 |
TEST(MustacheInterpolation, DottedNamesContextPrecedence) { |
| 212 |
// Dotted names should be resolved against former resolutions. |
| 213 |
Value D = |
| 214 |
Object{{"a", Object{{"b", Object{}}}}, {"b", Object{{"c", "ERROR"}}}}; |
| 215 |
auto T = Template("{{#a}}{{b.c}}{{/a}}"); |
| 216 |
std::string Out; |
| 217 |
raw_string_ostream OS(Out); |
| 218 |
T.render(D, OS); |
| 219 |
EXPECT_EQ("", Out); |
| 220 |
} |
| 221 |
|
| 222 |
TEST(MustacheInterpolation, DottedNamesAreNotSingleKeys) { |
| 223 |
// Dotted names shall not be parsed as single, atomic keys |
| 224 |
Value D = Object{{"a.b", "c"}}; |
| 225 |
auto T = Template("{{a.b}}"); |
| 226 |
std::string Out; |
| 227 |
raw_string_ostream OS(Out); |
| 228 |
T.render(D, OS); |
| 229 |
EXPECT_EQ("", Out); |
| 230 |
} |
| 231 |
|
| 232 |
TEST(MustacheInterpolation, DottedNamesNoMasking) { |
| 233 |
// Dotted Names in a given context are unavailable due to dot splitting |
| 234 |
Value D = Object{{"a.b", "c"}, {"a", Object{{"b", "d"}}}}; |
| 235 |
auto T = Template("{{a.b}}"); |
| 236 |
std::string Out; |
| 237 |
raw_string_ostream OS(Out); |
| 238 |
T.render(D, OS); |
| 239 |
EXPECT_EQ("d", Out); |
| 240 |
} |
| 241 |
|
| 242 |
TEST(MustacheInterpolation, ImplicitIteratorsBasicInterpolation) { |
| 243 |
// Unadorned tags should interpolate content into the template. |
| 244 |
Value D = "world"; |
| 245 |
auto T = Template("Hello, {{.}}!\n"); |
| 246 |
std::string Out; |
| 247 |
raw_string_ostream OS(Out); |
| 248 |
T.render(D, OS); |
| 249 |
EXPECT_EQ("Hello, world!\n", Out); |
| 250 |
} |
| 251 |
|
| 252 |
TEST(MustacheInterpolation, ImplicitIteratorsAmersand) { |
| 253 |
// Basic interpolation should be HTML escaped. |
| 254 |
Value D = "& \" < >"; |
| 255 |
auto T = Template("These characters should not be HTML escaped: {{&.}}\n"); |
| 256 |
std::string Out; |
| 257 |
raw_string_ostream OS(Out); |
| 258 |
T.render(D, OS); |
| 259 |
EXPECT_EQ("These characters should not be HTML escaped: & \" < >\n", Out); |
| 260 |
} |
| 261 |
|
| 262 |
TEST(MustacheInterpolation, ImplicitIteratorsInteger) { |
| 263 |
// Integers should interpolate seamlessly. |
| 264 |
Value D = 85; |
| 265 |
auto T = Template("{{.}} miles an hour!\n"); |
| 266 |
std::string Out; |
| 267 |
raw_string_ostream OS(Out); |
| 268 |
T.render(D, OS); |
| 269 |
EXPECT_EQ("85 miles an hour!\n", Out); |
| 270 |
} |
| 271 |
|
| 272 |
TEST(MustacheInterpolation, InterpolationSurroundingWhitespace) { |
| 273 |
// Interpolation should not alter surrounding whitespace. |
| 274 |
Value D = Object{{"string", "---"}}; |
| 275 |
auto T = Template("| {{string}} |"); |
| 276 |
std::string Out; |
| 277 |
raw_string_ostream OS(Out); |
| 278 |
T.render(D, OS); |
| 279 |
EXPECT_EQ("| --- |", Out); |
| 280 |
} |
| 281 |
|
| 282 |
TEST(MustacheInterpolation, AmersandSurroundingWhitespace) { |
| 283 |
// Interpolation should not alter surrounding whitespace. |
| 284 |
Value D = Object{{"string", "---"}}; |
| 285 |
auto T = Template("| {{&string}} |"); |
| 286 |
std::string Out; |
| 287 |
raw_string_ostream OS(Out); |
| 288 |
T.render(D, OS); |
| 289 |
EXPECT_EQ("| --- |", Out); |
| 290 |
} |
| 291 |
|
| 292 |
TEST(MustacheInterpolation, StandaloneInterpolationWithWhitespace) { |
| 293 |
// Standalone interpolation should not alter surrounding whitespace. |
| 294 |
Value D = Object{{"string", "---"}}; |
| 295 |
auto T = Template(" {{string}}\n"); |
| 296 |
std::string Out; |
| 297 |
raw_string_ostream OS(Out); |
| 298 |
T.render(D, OS); |
| 299 |
EXPECT_EQ(" ---\n", Out); |
| 300 |
} |
| 301 |
|
| 302 |
TEST(MustacheInterpolation, StandaloneAmpersandWithWhitespace) { |
| 303 |
// Standalone interpolation should not alter surrounding whitespace. |
| 304 |
Value D = Object{{"string", "---"}}; |
| 305 |
auto T = Template(" {{&string}}\n"); |
| 306 |
std::string Out; |
| 307 |
raw_string_ostream OS(Out); |
| 308 |
T.render(D, OS); |
| 309 |
EXPECT_EQ(" ---\n", Out); |
| 310 |
} |
| 311 |
|
| 312 |
TEST(MustacheInterpolation, InterpolationWithPadding) { |
| 313 |
// Superfluous in-tag whitespace should be ignored. |
| 314 |
Value D = Object{{"string", "---"}}; |
| 315 |
auto T = Template("|{{ string }}|"); |
| 316 |
std::string Out; |
| 317 |
raw_string_ostream OS(Out); |
| 318 |
T.render(D, OS); |
| 319 |
EXPECT_EQ("|---|", Out); |
| 320 |
} |
| 321 |
|
| 322 |
TEST(MustacheInterpolation, AmpersandWithPadding) { |
| 323 |
// Superfluous in-tag whitespace should be ignored. |
| 324 |
Value D = Object{{"string", "---"}}; |
| 325 |
auto T = Template("|{{& string }}|"); |
| 326 |
std::string Out; |
| 327 |
raw_string_ostream OS(Out); |
| 328 |
T.render(D, OS); |
| 329 |
EXPECT_EQ("|---|", Out); |
| 330 |
} |
| 331 |
|
| 332 |
TEST(MustacheInterpolation, InterpolationWithPaddingAndNewlines) { |
| 333 |
// Superfluous in-tag whitespace should be ignored. |
| 334 |
Value D = Object{{"string", "---"}}; |
| 335 |
auto T = Template("|{{ string \n\n\n }}|"); |
| 336 |
std::string Out; |
| 337 |
raw_string_ostream OS(Out); |
| 338 |
T.render(D, OS); |
| 339 |
EXPECT_EQ("|---|", Out); |
| 340 |
} |
| 341 |
|
| 342 |
TEST(MustacheSections, Truthy) { |
| 343 |
Value D = Object{{"boolean", true}}; |
| 344 |
auto T = Template("{{#boolean}}This should be rendered.{{/boolean}}"); |
| 345 |
std::string Out; |
| 346 |
raw_string_ostream OS(Out); |
| 347 |
T.render(D, OS); |
| 348 |
EXPECT_EQ("This should be rendered.", Out); |
| 349 |
} |
| 350 |
|
| 351 |
TEST(MustacheSections, Falsey) { |
| 352 |
Value D = Object{{"boolean", false}}; |
| 353 |
auto T = Template("{{#boolean}}This should not be rendered.{{/boolean}}"); |
| 354 |
std::string Out; |
| 355 |
raw_string_ostream OS(Out); |
| 356 |
T.render(D, OS); |
| 357 |
EXPECT_EQ("", Out); |
| 358 |
} |
| 359 |
|
| 360 |
TEST(MustacheInterpolation, IsFalseyNull) { |
| 361 |
// Mustache-free templates should render as-is. |
| 362 |
Value D = Object{{"boolean", nullptr}}; |
| 363 |
auto T = Template("Hello, {{#boolean}}World{{/boolean}}"); |
| 364 |
std::string Out; |
| 365 |
raw_string_ostream OS(Out); |
| 366 |
T.render(D, OS); |
| 367 |
EXPECT_EQ("Hello, ", Out); |
| 368 |
} |
| 369 |
|
| 370 |
TEST(MustacheInterpolation, IsFalseyArray) { |
| 371 |
// Mustache-free templates should render as-is. |
| 372 |
Value D = Object{{"boolean", Array()}}; |
| 373 |
auto T = Template("Hello, {{#boolean}}World{{/boolean}}"); |
| 374 |
std::string Out; |
| 375 |
raw_string_ostream OS(Out); |
| 376 |
T.render(D, OS); |
| 377 |
EXPECT_EQ("Hello, ", Out); |
| 378 |
} |
| 379 |
|
| 380 |
TEST(MustacheInterpolation, IsFalseyObject) { |
| 381 |
// Mustache-free templates should render as-is. |
| 382 |
Value D = Object{{"boolean", Object{}}}; |
| 383 |
auto T = Template("Hello, {{#boolean}}World{{/boolean}}"); |
| 384 |
std::string Out; |
| 385 |
raw_string_ostream OS(Out); |
| 386 |
T.render(D, OS); |
| 387 |
EXPECT_EQ("Hello, World", Out); |
| 388 |
} |
| 389 |
|
| 390 |
TEST(MustacheInterpolation, DoubleRendering) { |
| 391 |
// Mustache-free templates should render as-is. |
| 392 |
Value D1 = Object{{"subject", "World"}}; |
| 393 |
auto T = Template("Hello, {{subject}}!"); |
| 394 |
std::string Out1; |
| 395 |
raw_string_ostream OS1(Out1); |
| 396 |
T.render(D1, OS1); |
| 397 |
EXPECT_EQ("Hello, World!", Out1); |
| 398 |
std::string Out2; |
| 399 |
raw_string_ostream OS2(Out2); |
| 400 |
Value D2 = Object{{"subject", "New World"}}; |
| 401 |
T.render(D2, OS2); |
| 402 |
EXPECT_EQ("Hello, New World!", Out2); |
| 403 |
} |
| 404 |
|
| 405 |
TEST(MustacheSections, NullIsFalsey) { |
| 406 |
Value D = Object{{"null", nullptr}}; |
| 407 |
auto T = Template("{{#null}}This should not be rendered.{{/null}}"); |
| 408 |
std::string Out; |
| 409 |
raw_string_ostream OS(Out); |
| 410 |
T.render(D, OS); |
| 411 |
EXPECT_EQ("", Out); |
| 412 |
} |
| 413 |
|
| 414 |
TEST(MustacheSections, Context) { |
| 415 |
Value D = Object{{"context", Object{{"name", "Joe"}}}}; |
| 416 |
auto T = Template("{{#context}}Hi {{name}}.{{/context}}"); |
| 417 |
std::string Out; |
| 418 |
raw_string_ostream OS(Out); |
| 419 |
T.render(D, OS); |
| 420 |
EXPECT_EQ("Hi Joe.", Out); |
| 421 |
} |
| 422 |
|
| 423 |
TEST(MustacheSections, ParentContexts) { |
| 424 |
Value D = Object{{"a", "foo"}, |
| 425 |
{"b", "wrong"}, |
| 426 |
{"sec", Object{{"b", "bar"}}}, |
| 427 |
{"c", Object{{"d", "baz"}}}}; |
| 428 |
auto T = Template("{{#sec}}{{a}}, {{b}}, {{c.d}}{{/sec}}"); |
| 429 |
std::string Out; |
| 430 |
raw_string_ostream OS(Out); |
| 431 |
T.render(D, OS); |
| 432 |
EXPECT_EQ("foo, bar, baz", Out); |
| 433 |
} |
| 434 |
|
| 435 |
TEST(MustacheSections, VariableTest) { |
| 436 |
Value D = Object{{"foo", "bar"}}; |
| 437 |
auto T = Template("{{#foo}}{{.}} is {{foo}}{{/foo}}"); |
| 438 |
std::string Out; |
| 439 |
raw_string_ostream OS(Out); |
| 440 |
T.render(D, OS); |
| 441 |
EXPECT_EQ("bar is bar", Out); |
| 442 |
} |
| 443 |
|
| 444 |
TEST(MustacheSections, ListContexts) { |
| 445 |
Value D = Object{ |
| 446 |
{"tops", |
| 447 |
Array{Object{ |
| 448 |
{"tname", Object{{"upper", "A"}, {"lower", "a"}}}, |
| 449 |
{"middles", |
| 450 |
Array{Object{{"mname", "1"}, |
| 451 |
{"bottoms", Array{Object{{"bname", "x"}}, |
| 452 |
Object{{"bname", "y"}}}}}}}}}}}; |
| 453 |
auto T = Template("{{#tops}}" |
| 454 |
"{{#middles}}" |
| 455 |
"{{tname.lower}}{{mname}}." |
| 456 |
"{{#bottoms}}" |
| 457 |
"{{tname.upper}}{{mname}}{{bname}}." |
| 458 |
"{{/bottoms}}" |
| 459 |
"{{/middles}}" |
| 460 |
"{{/tops}}"); |
| 461 |
std::string Out; |
| 462 |
raw_string_ostream OS(Out); |
| 463 |
T.render(D, OS); |
| 464 |
EXPECT_EQ("a1.A1x.A1y.", Out); |
| 465 |
} |
| 466 |
|
| 467 |
TEST(MustacheSections, DeeplyNestedContexts) { |
| 468 |
Value D = Object{ |
| 469 |
{"a", Object{{"one", 1}}}, |
| 470 |
{"b", Object{{"two", 2}}}, |
| 471 |
{"c", Object{{"three", 3}, {"d", Object{{"four", 4}, {"five", 5}}}}}}; |
| 472 |
auto T = Template( |
| 473 |
"{{#a}}\n{{one}}\n{{#b}}\n{{one}}{{two}}{{one}}\n{{#c}}\n{{one}}{{two}}{{" |
| 474 |
"three}}{{two}}{{one}}\n{{#d}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{" |
| 475 |
"{two}}{{one}}\n{{#five}}\n{{one}}{{two}}{{three}}{{four}}{{five}}{{four}" |
| 476 |
"}{{three}}{{two}}{{one}}\n{{one}}{{two}}{{three}}{{four}}{{.}}6{{.}}{{" |
| 477 |
"four}}{{three}}{{two}}{{one}}\n{{one}}{{two}}{{three}}{{four}}{{five}}{{" |
| 478 |
"four}}{{three}}{{two}}{{one}}\n{{/" |
| 479 |
"five}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}\n{{/" |
| 480 |
"d}}\n{{one}}{{two}}{{three}}{{two}}{{one}}\n{{/" |
| 481 |
"c}}\n{{one}}{{two}}{{one}}\n{{/b}}\n{{one}}\n{{/a}}\n"); |
| 482 |
std::string Out; |
| 483 |
raw_string_ostream OS(Out); |
| 484 |
T.render(D, OS); |
| 485 |
EXPECT_EQ("1\n121\n12321\n1234321\n123454321\n12345654321\n123454321\n1234321" |
| 486 |
"\n12321\n121\n1\n", |
| 487 |
Out); |
| 488 |
} |
| 489 |
|
| 490 |
TEST(MustacheSections, List) { |
| 491 |
Value D = Object{{"list", Array{Object{{"item", 1}}, Object{{"item", 2}}, |
| 492 |
Object{{"item", 3}}}}}; |
| 493 |
auto T = Template("{{#list}}{{item}}{{/list}}"); |
| 494 |
std::string Out; |
| 495 |
raw_string_ostream OS(Out); |
| 496 |
T.render(D, OS); |
| 497 |
EXPECT_EQ("123", Out); |
| 498 |
} |
| 499 |
|
| 500 |
TEST(MustacheSections, EmptyList) { |
| 501 |
Value D = Object{{"list", Array{}}}; |
| 502 |
auto T = Template("{{#list}}Yay lists!{{/list}}"); |
| 503 |
std::string Out; |
| 504 |
raw_string_ostream OS(Out); |
| 505 |
T.render(D, OS); |
| 506 |
EXPECT_EQ("", Out); |
| 507 |
} |
| 508 |
|
| 509 |
TEST(MustacheSections, Doubled) { |
| 510 |
Value D = Object{{"bool", true}, {"two", "second"}}; |
| 511 |
auto T = Template("{{#bool}}\n* first\n{{/bool}}\n* " |
| 512 |
"{{two}}\n{{#bool}}\n* third\n{{/bool}}\n"); |
| 513 |
std::string Out; |
| 514 |
raw_string_ostream OS(Out); |
| 515 |
T.render(D, OS); |
| 516 |
EXPECT_EQ("* first\n* second\n* third\n", Out); |
| 517 |
} |
| 518 |
|
| 519 |
TEST(MustacheSections, NestedTruthy) { |
| 520 |
Value D = Object{{"bool", true}}; |
| 521 |
auto T = Template("| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |"); |
| 522 |
std::string Out; |
| 523 |
raw_string_ostream OS(Out); |
| 524 |
T.render(D, OS); |
| 525 |
EXPECT_EQ("| A B C D E |", Out); |
| 526 |
} |
| 527 |
|
| 528 |
TEST(MustacheSections, NestedFalsey) { |
| 529 |
Value D = Object{{"bool", false}}; |
| 530 |
auto T = Template("| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |"); |
| 531 |
std::string Out; |
| 532 |
raw_string_ostream OS(Out); |
| 533 |
T.render(D, OS); |
| 534 |
EXPECT_EQ("| A E |", Out); |
| 535 |
} |
| 536 |
|
| 537 |
TEST(MustacheSections, ContextMisses) { |
| 538 |
Value D = Object{}; |
| 539 |
auto T = Template("[{{#missing}}Found key 'missing'!{{/missing}}]"); |
| 540 |
std::string Out; |
| 541 |
raw_string_ostream OS(Out); |
| 542 |
T.render(D, OS); |
| 543 |
EXPECT_EQ("[]", Out); |
| 544 |
} |
| 545 |
|
| 546 |
TEST(MustacheSections, ImplicitIteratorString) { |
| 547 |
Value D = Object{{"list", Array{"a", "b", "c", "d", "e"}}}; |
| 548 |
auto T = Template("{{#list}}({{.}}){{/list}}"); |
| 549 |
std::string Out; |
| 550 |
raw_string_ostream OS(Out); |
| 551 |
T.render(D, OS); |
| 552 |
EXPECT_EQ("(a)(b)(c)(d)(e)", Out); |
| 553 |
} |
| 554 |
|
| 555 |
TEST(MustacheSections, ImplicitIteratorInteger) { |
| 556 |
Value D = Object{{"list", Array{1, 2, 3, 4, 5}}}; |
| 557 |
auto T = Template("{{#list}}({{.}}){{/list}}"); |
| 558 |
std::string Out; |
| 559 |
raw_string_ostream OS(Out); |
| 560 |
T.render(D, OS); |
| 561 |
EXPECT_EQ("(1)(2)(3)(4)(5)", Out); |
| 562 |
} |
| 563 |
|
| 564 |
TEST(MustacheSections, ImplicitIteratorArray) { |
| 565 |
Value D = Object{{"list", Array{Array{1, 2, 3}, Array{"a", "b", "c"}}}}; |
| 566 |
auto T = Template("{{#list}}({{#.}}{{.}}{{/.}}){{/list}}"); |
| 567 |
std::string Out; |
| 568 |
raw_string_ostream OS(Out); |
| 569 |
T.render(D, OS); |
| 570 |
EXPECT_EQ("(123)(abc)", Out); |
| 571 |
} |
| 572 |
|
| 573 |
TEST(MustacheSections, ImplicitIteratorHTMLEscaping) { |
| 574 |
Value D = Object{{"list", Array{"&", "\"", "<", ">"}}}; |
| 575 |
auto T = Template("{{#list}}({{.}}){{/list}}"); |
| 576 |
std::string Out; |
| 577 |
raw_string_ostream OS(Out); |
| 578 |
T.render(D, OS); |
| 579 |
EXPECT_EQ("(&)(")(<)(>)", Out); |
| 580 |
} |
| 581 |
|
| 582 |
TEST(MustacheSections, ImplicitIteratorAmpersand) { |
| 583 |
Value D = Object{{"list", Array{"&", "\"", "<", ">"}}}; |
| 584 |
auto T = Template("{{#list}}({{&.}}){{/list}}"); |
| 585 |
std::string Out; |
| 586 |
raw_string_ostream OS(Out); |
| 587 |
T.render(D, OS); |
| 588 |
EXPECT_EQ("(&)(\")(<)(>)", Out); |
| 589 |
} |
| 590 |
|
| 591 |
TEST(MustacheSections, ImplicitIteratorRootLevel) { |
| 592 |
Value D = Array{Object{{"value", "a"}}, Object{{"value", "b"}}}; |
| 593 |
auto T = Template("{{#.}}({{value}}){{/.}}"); |
| 594 |
std::string Out; |
| 595 |
raw_string_ostream OS(Out); |
| 596 |
T.render(D, OS); |
| 597 |
EXPECT_EQ("(a)(b)", Out); |
| 598 |
} |
| 599 |
|
| 600 |
TEST(MustacheSections, DottedNamesTruthy) { |
| 601 |
Value D = Object{{"a", Object{{"b", Object{{"c", true}}}}}}; |
| 602 |
auto T = Template("{{#a.b.c}}Here{{/a.b.c}} == Here"); |
| 603 |
std::string Out; |
| 604 |
raw_string_ostream OS(Out); |
| 605 |
T.render(D, OS); |
| 606 |
EXPECT_EQ("Here == Here", Out); |
| 607 |
} |
| 608 |
|
| 609 |
TEST(MustacheSections, DottedNamesFalsey) { |
| 610 |
Value D = Object{{"a", Object{{"b", Object{{"c", false}}}}}}; |
| 611 |
auto T = Template("{{#a.b.c}}Here{{/a.b.c}} == "); |
| 612 |
std::string Out; |
| 613 |
raw_string_ostream OS(Out); |
| 614 |
T.render(D, OS); |
| 615 |
EXPECT_EQ(" == ", Out); |
| 616 |
} |
| 617 |
|
| 618 |
TEST(MustacheSections, DottedNamesBrokenChains) { |
| 619 |
Value D = Object{{"a", Object{}}}; |
| 620 |
auto T = Template("{{#a.b.c}}Here{{/a.b.c}} == "); |
| 621 |
std::string Out; |
| 622 |
raw_string_ostream OS(Out); |
| 623 |
T.render(D, OS); |
| 624 |
EXPECT_EQ(" == ", Out); |
| 625 |
} |
| 626 |
|
| 627 |
TEST(MustacheSections, SurroundingWhitespace) { |
| 628 |
Value D = Object{{"boolean", true}}; |
| 629 |
auto T = Template(" | {{#boolean}}\t|\t{{/boolean}} | \n"); |
| 630 |
std::string Out; |
| 631 |
raw_string_ostream OS(Out); |
| 632 |
T.render(D, OS); |
| 633 |
EXPECT_EQ(" | \t|\t | \n", Out); |
| 634 |
} |
| 635 |
|
| 636 |
TEST(MustacheSections, InternalWhitespace) { |
| 637 |
Value D = Object{{"boolean", true}}; |
| 638 |
auto T = Template( |
| 639 |
" | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n"); |
| 640 |
std::string Out; |
| 641 |
raw_string_ostream OS(Out); |
| 642 |
T.render(D, OS); |
| 643 |
EXPECT_EQ(" | \n | \n", Out); |
| 644 |
} |
| 645 |
|
| 646 |
TEST(MustacheSections, IndentedInlineSections) { |
| 647 |
Value D = Object{{"boolean", true}}; |
| 648 |
auto T = |
| 649 |
Template(" {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n"); |
| 650 |
std::string Out; |
| 651 |
raw_string_ostream OS(Out); |
| 652 |
T.render(D, OS); |
| 653 |
EXPECT_EQ(" YES\n GOOD\n", Out); |
| 654 |
} |
| 655 |
|
| 656 |
TEST(MustacheSections, StandaloneLines) { |
| 657 |
Value D = Object{{"boolean", true}}; |
| 658 |
auto T = Template("| This Is\n{{#boolean}}\n|\n{{/boolean}}\n| A Line\n"); |
| 659 |
std::string Out; |
| 660 |
raw_string_ostream OS(Out); |
| 661 |
T.render(D, OS); |
| 662 |
EXPECT_EQ("| This Is\n|\n| A Line\n", Out); |
| 663 |
} |
| 664 |
|
| 665 |
TEST(MustacheSections, IndentedStandaloneLines) { |
| 666 |
Value D = Object{{"boolean", true}}; |
| 667 |
auto T = Template("| This Is\n {{#boolean}}\n|\n {{/boolean}}\n| A Line\n"); |
| 668 |
std::string Out; |
| 669 |
raw_string_ostream OS(Out); |
| 670 |
T.render(D, OS); |
| 671 |
EXPECT_EQ("| This Is\n|\n| A Line\n", Out); |
| 672 |
} |
| 673 |
|
| 674 |
TEST(MustacheSections, StandaloneLineEndings) { |
| 675 |
Value D = Object{{"boolean", true}}; |
| 676 |
auto T = Template("|\r\n{{#boolean}}\r\n{{/boolean}}\r\n|"); |
| 677 |
std::string Out; |
| 678 |
raw_string_ostream OS(Out); |
| 679 |
T.render(D, OS); |
| 680 |
EXPECT_EQ("|\r\n|", Out); |
| 681 |
} |
| 682 |
|
| 683 |
TEST(MustacheSections, StandaloneWithoutPreviousLine) { |
| 684 |
Value D = Object{{"boolean", true}}; |
| 685 |
auto T = Template(" {{#boolean}}\n#{{/boolean}}\n/"); |
| 686 |
std::string Out; |
| 687 |
raw_string_ostream OS(Out); |
| 688 |
T.render(D, OS); |
| 689 |
EXPECT_EQ("#\n/", Out); |
| 690 |
} |
| 691 |
|
| 692 |
TEST(MustacheSections, StandaloneWithoutNewline) { |
| 693 |
Value D = Object{{"boolean", true}}; |
| 694 |
auto T = Template("#{{#boolean}}\n/\n {{/boolean}}"); |
| 695 |
std::string Out; |
| 696 |
raw_string_ostream OS(Out); |
| 697 |
T.render(D, OS); |
| 698 |
EXPECT_EQ("#\n/\n", Out); |
| 699 |
} |
| 700 |
|
| 701 |
TEST(MustacheSections, Padding) { |
| 702 |
Value D = Object{{"boolean", true}}; |
| 703 |
auto T = Template("|{{# boolean }}={{/ boolean }}|"); |
| 704 |
std::string Out; |
| 705 |
raw_string_ostream OS(Out); |
| 706 |
T.render(D, OS); |
| 707 |
EXPECT_EQ("|=|", Out); |
| 708 |
} |
| 709 |
|
| 710 |
TEST(MustacheInvertedSections, Falsey) { |
| 711 |
Value D = Object{{"boolean", false}}; |
| 712 |
auto T = Template("{{^boolean}}This should be rendered.{{/boolean}}"); |
| 713 |
std::string Out; |
| 714 |
raw_string_ostream OS(Out); |
| 715 |
T.render(D, OS); |
| 716 |
EXPECT_EQ("This should be rendered.", Out); |
| 717 |
} |
| 718 |
|
| 719 |
TEST(MustacheInvertedSections, Truthy) { |
| 720 |
Value D = Object{{"boolean", true}}; |
| 721 |
auto T = Template("{{^boolean}}This should not be rendered.{{/boolean}}"); |
| 722 |
std::string Out; |
| 723 |
raw_string_ostream OS(Out); |
| 724 |
T.render(D, OS); |
| 725 |
EXPECT_EQ("", Out); |
| 726 |
} |
| 727 |
|
| 728 |
TEST(MustacheInvertedSections, NullIsFalsey) { |
| 729 |
Value D = Object{{"null", nullptr}}; |
| 730 |
auto T = Template("{{^null}}This should be rendered.{{/null}}"); |
| 731 |
std::string Out; |
| 732 |
raw_string_ostream OS(Out); |
| 733 |
T.render(D, OS); |
| 734 |
EXPECT_EQ("This should be rendered.", Out); |
| 735 |
} |
| 736 |
|
| 737 |
TEST(MustacheInvertedSections, Context) { |
| 738 |
Value D = Object{{"context", Object{{"name", "Joe"}}}}; |
| 739 |
auto T = Template("{{^context}}Hi {{name}}.{{/context}}"); |
| 740 |
std::string Out; |
| 741 |
raw_string_ostream OS(Out); |
| 742 |
T.render(D, OS); |
| 743 |
EXPECT_EQ("", Out); |
| 744 |
} |
| 745 |
|
| 746 |
TEST(MustacheInvertedSections, List) { |
| 747 |
Value D = Object{ |
| 748 |
{"list", Array{Object{{"n", 1}}, Object{{"n", 2}}, Object{{"n", 3}}}}}; |
| 749 |
auto T = Template("{{^list}}{{n}}{{/list}}"); |
| 750 |
std::string Out; |
| 751 |
raw_string_ostream OS(Out); |
| 752 |
T.render(D, OS); |
| 753 |
EXPECT_EQ("", Out); |
| 754 |
} |
| 755 |
|
| 756 |
TEST(MustacheInvertedSections, EmptyList) { |
| 757 |
Value D = Object{{"list", Array{}}}; |
| 758 |
auto T = Template("{{^list}}Yay lists!{{/list}}"); |
| 759 |
std::string Out; |
| 760 |
raw_string_ostream OS(Out); |
| 761 |
T.render(D, OS); |
| 762 |
EXPECT_EQ("Yay lists!", Out); |
| 763 |
} |
| 764 |
|
| 765 |
TEST(MustacheInvertedSections, Doubled) { |
| 766 |
Value D = Object{{"bool", false}, {"two", "second"}}; |
| 767 |
auto T = Template("{{^bool}}\n* first\n{{/bool}}\n* " |
| 768 |
"{{two}}\n{{^bool}}\n* third\n{{/bool}}\n"); |
| 769 |
std::string Out; |
| 770 |
raw_string_ostream OS(Out); |
| 771 |
T.render(D, OS); |
| 772 |
EXPECT_EQ("* first\n* second\n* third\n", Out); |
| 773 |
} |
| 774 |
|
| 775 |
TEST(MustacheInvertedSections, NestedFalsey) { |
| 776 |
Value D = Object{{"bool", false}}; |
| 777 |
auto T = Template("| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |"); |
| 778 |
std::string Out; |
| 779 |
raw_string_ostream OS(Out); |
| 780 |
T.render(D, OS); |
| 781 |
EXPECT_EQ("| A B C D E |", Out); |
| 782 |
} |
| 783 |
|
| 784 |
TEST(MustacheInvertedSections, NestedTruthy) { |
| 785 |
Value D = Object{{"bool", true}}; |
| 786 |
auto T = Template("| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |"); |
| 787 |
std::string Out; |
| 788 |
raw_string_ostream OS(Out); |
| 789 |
T.render(D, OS); |
| 790 |
EXPECT_EQ("| A E |", Out); |
| 791 |
} |
| 792 |
|
| 793 |
TEST(MustacheInvertedSections, ContextMisses) { |
| 794 |
Value D = Object{}; |
| 795 |
auto T = Template("[{{^missing}}Cannot find key 'missing'!{{/missing}}]"); |
| 796 |
std::string Out; |
| 797 |
raw_string_ostream OS(Out); |
| 798 |
T.render(D, OS); |
| 799 |
EXPECT_EQ("[Cannot find key 'missing'!]", Out); |
| 800 |
} |
| 801 |
|
| 802 |
TEST(MustacheInvertedSections, DottedNamesTruthy) { |
| 803 |
Value D = Object{{"a", Object{{"b", Object{{"c", true}}}}}}; |
| 804 |
auto T = Template("{{^a.b.c}}Not Here{{/a.b.c}} == "); |
| 805 |
std::string Out; |
| 806 |
raw_string_ostream OS(Out); |
| 807 |
T.render(D, OS); |
| 808 |
EXPECT_EQ(" == ", Out); |
| 809 |
} |
| 810 |
|
| 811 |
TEST(MustacheInvertedSections, DottedNamesFalsey) { |
| 812 |
Value D = Object{{"a", Object{{"b", Object{{"c", false}}}}}}; |
| 813 |
auto T = Template("{{^a.b.c}}Not Here{{/a.b.c}} == Not Here"); |
| 814 |
std::string Out; |
| 815 |
raw_string_ostream OS(Out); |
| 816 |
T.render(D, OS); |
| 817 |
EXPECT_EQ("Not Here == Not Here", Out); |
| 818 |
} |
| 819 |
|
| 820 |
TEST(MustacheInvertedSections, DottedNamesBrokenChains) { |
| 821 |
Value D = Object{{"a", Object{}}}; |
| 822 |
auto T = Template("{{^a.b.c}}Not Here{{/a.b.c}} == Not Here"); |
| 823 |
std::string Out; |
| 824 |
raw_string_ostream OS(Out); |
| 825 |
T.render(D, OS); |
| 826 |
EXPECT_EQ("Not Here == Not Here", Out); |
| 827 |
} |
| 828 |
|
| 829 |
TEST(MustacheInvertedSections, SurroundingWhitespace) { |
| 830 |
Value D = Object{{"boolean", false}}; |
| 831 |
auto T = Template(" | {{^boolean}}\t|\t{{/boolean}} | \n"); |
| 832 |
std::string Out; |
| 833 |
raw_string_ostream OS(Out); |
| 834 |
T.render(D, OS); |
| 835 |
EXPECT_EQ(" | \t|\t | \n", Out); |
| 836 |
} |
| 837 |
|
| 838 |
TEST(MustacheInvertedSections, InternalWhitespace) { |
| 839 |
Value D = Object{{"boolean", false}}; |
| 840 |
auto T = Template( |
| 841 |
" | {{^boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n"); |
| 842 |
std::string Out; |
| 843 |
raw_string_ostream OS(Out); |
| 844 |
T.render(D, OS); |
| 845 |
EXPECT_EQ(" | \n | \n", Out); |
| 846 |
} |
| 847 |
|
| 848 |
TEST(MustacheInvertedSections, IndentedInlineSections) { |
| 849 |
Value D = Object{{"boolean", false}}; |
| 850 |
auto T = |
| 851 |
Template(" {{^boolean}}NO{{/boolean}}\n {{^boolean}}WAY{{/boolean}}\n"); |
| 852 |
std::string Out; |
| 853 |
raw_string_ostream OS(Out); |
| 854 |
T.render(D, OS); |
| 855 |
EXPECT_EQ(" NO\n WAY\n", Out); |
| 856 |
} |
| 857 |
|
| 858 |
TEST(MustacheInvertedSections, StandaloneLines) { |
| 859 |
Value D = Object{{"boolean", false}}; |
| 860 |
auto T = Template("| This Is\n{{^boolean}}\n|\n{{/boolean}}\n| A Line\n"); |
| 861 |
std::string Out; |
| 862 |
raw_string_ostream OS(Out); |
| 863 |
T.render(D, OS); |
| 864 |
EXPECT_EQ("| This Is\n|\n| A Line\n", Out); |
| 865 |
} |
| 866 |
|
| 867 |
TEST(MustacheInvertedSections, StandaloneIndentedLines) { |
| 868 |
Value D = Object{{"boolean", false}}; |
| 869 |
auto T = Template("| This Is\n {{^boolean}}\n|\n {{/boolean}}\n| A Line\n"); |
| 870 |
std::string Out; |
| 871 |
raw_string_ostream OS(Out); |
| 872 |
T.render(D, OS); |
| 873 |
EXPECT_EQ("| This Is\n|\n| A Line\n", Out); |
| 874 |
} |
| 875 |
|
| 876 |
TEST(MustacheInvertedSections, StandaloneLineEndings) { |
| 877 |
Value D = Object{{"boolean", false}}; |
| 878 |
auto T = Template("|\r\n{{^boolean}}\r\n{{/boolean}}\r\n|"); |
| 879 |
std::string Out; |
| 880 |
raw_string_ostream OS(Out); |
| 881 |
T.render(D, OS); |
| 882 |
EXPECT_EQ("|\r\n|", Out); |
| 883 |
} |
| 884 |
|
| 885 |
TEST(MustacheInvertedSections, StandaloneWithoutPreviousLine) { |
| 886 |
Value D = Object{{"boolean", false}}; |
| 887 |
auto T = Template(" {{^boolean}}\n^{{/boolean}}\n/"); |
| 888 |
std::string Out; |
| 889 |
raw_string_ostream OS(Out); |
| 890 |
T.render(D, OS); |
| 891 |
EXPECT_EQ("^\n/", Out); |
| 892 |
} |
| 893 |
|
| 894 |
TEST(MustacheInvertedSections, StandaloneWithoutNewline) { |
| 895 |
Value D = Object{{"boolean", false}}; |
| 896 |
auto T = Template("^{{^boolean}}\n/\n {{/boolean}}"); |
| 897 |
std::string Out; |
| 898 |
raw_string_ostream OS(Out); |
| 899 |
T.render(D, OS); |
| 900 |
EXPECT_EQ("^\n/\n", Out); |
| 901 |
} |
| 902 |
|
| 903 |
TEST(MustacheInvertedSections, Padding) { |
| 904 |
Value D = Object{{"boolean", false}}; |
| 905 |
auto T = Template("|{{^ boolean }}={{/ boolean }}|"); |
| 906 |
std::string Out; |
| 907 |
raw_string_ostream OS(Out); |
| 908 |
T.render(D, OS); |
| 909 |
EXPECT_EQ("|=|", Out); |
| 910 |
} |
| 911 |
|
| 912 |
TEST(MustachePartials, BasicBehavior) { |
| 913 |
Value D = Object{}; |
| 914 |
auto T = Template("{{>text}}"); |
| 915 |
T.registerPartial("text", "from partial"); |
| 916 |
std::string Out; |
| 917 |
raw_string_ostream OS(Out); |
| 918 |
T.render(D, OS); |
| 919 |
EXPECT_EQ("from partial", Out); |
| 920 |
} |
| 921 |
|
| 922 |
TEST(MustachePartials, FailedLookup) { |
| 923 |
Value D = Object{}; |
| 924 |
auto T = Template("{{>text}}"); |
| 925 |
std::string Out; |
| 926 |
raw_string_ostream OS(Out); |
| 927 |
T.render(D, OS); |
| 928 |
EXPECT_EQ("", Out); |
| 929 |
} |
| 930 |
|
| 931 |
TEST(MustachePartials, Context) { |
| 932 |
Value D = Object{{"text", "content"}}; |
| 933 |
auto T = Template("{{>partial}}"); |
| 934 |
T.registerPartial("partial", "*{{text}}*"); |
| 935 |
std::string Out; |
| 936 |
raw_string_ostream OS(Out); |
| 937 |
T.render(D, OS); |
| 938 |
EXPECT_EQ("*content*", Out); |
| 939 |
} |
| 940 |
|
| 941 |
TEST(MustachePartials, Recursion) { |
| 942 |
Value D = |
| 943 |
Object{{"content", "X"}, |
| 944 |
{"nodes", Array{Object{{"content", "Y"}, {"nodes", Array{}}}}}}; |
| 945 |
auto T = Template("{{>node}}"); |
| 946 |
T.registerPartial("node", "{{content}}({{#nodes}}{{>node}}{{/nodes}})"); |
| 947 |
std::string Out; |
| 948 |
raw_string_ostream OS(Out); |
| 949 |
T.render(D, OS); |
| 950 |
EXPECT_EQ("X(Y())", Out); |
| 951 |
} |
| 952 |
|
| 953 |
TEST(MustachePartials, Nested) { |
| 954 |
Value D = Object{{"a", "hello"}, {"b", "world"}}; |
| 955 |
auto T = Template("{{>outer}}"); |
| 956 |
T.registerPartial("outer", "*{{a}} {{>inner}}*"); |
| 957 |
T.registerPartial("inner", "{{b}}!"); |
| 958 |
std::string Out; |
| 959 |
raw_string_ostream OS(Out); |
| 960 |
T.render(D, OS); |
| 961 |
EXPECT_EQ("*hello world!*", Out); |
| 962 |
} |
| 963 |
|
| 964 |
TEST(MustachePartials, SurroundingWhitespace) { |
| 965 |
Value D = Object{}; |
| 966 |
auto T = Template("| {{>partial}} |"); |
| 967 |
T.registerPartial("partial", "\t|\t"); |
| 968 |
std::string Out; |
| 969 |
raw_string_ostream OS(Out); |
| 970 |
T.render(D, OS); |
| 971 |
EXPECT_EQ("| \t|\t |", Out); |
| 972 |
} |
| 973 |
|
| 974 |
TEST(MustachePartials, InlineIndentation) { |
| 975 |
Value D = Object{{"data", "|"}}; |
| 976 |
auto T = Template(" {{data}} {{> partial}}\n"); |
| 977 |
T.registerPartial("partial", "<\n<"); |
| 978 |
std::string Out; |
| 979 |
raw_string_ostream OS(Out); |
| 980 |
T.render(D, OS); |
| 981 |
EXPECT_EQ(" | <\n<\n", Out); |
| 982 |
} |
| 983 |
|
| 984 |
TEST(MustachePartials, PaddingWhitespace) { |
| 985 |
Value D = Object{{"boolean", true}}; |
| 986 |
auto T = Template("|{{> partial }}|"); |
| 987 |
T.registerPartial("partial", "[]"); |
| 988 |
std::string Out; |
| 989 |
raw_string_ostream OS(Out); |
| 990 |
T.render(D, OS); |
| 991 |
EXPECT_EQ("|[]|", Out); |
| 992 |
} |
| 993 |
|
| 994 |
TEST(MustacheLambdas, BasicInterpolation) { |
| 995 |
Value D = Object{}; |
| 996 |
auto T = Template("Hello, {{lambda}}!"); |
| 997 |
Lambda L = []() -> llvm::json::Value { return "World"; }; |
| 998 |
T.registerLambda("lambda", L); |
| 999 |
std::string Out; |
| 1000 |
raw_string_ostream OS(Out); |
| 1001 |
T.render(D, OS); |
| 1002 |
EXPECT_EQ("Hello, World!", Out); |
| 1003 |
} |
| 1004 |
|
| 1005 |
TEST(MustacheLambdas, InterpolationExpansion) { |
| 1006 |
Value D = Object{{"planet", "World"}}; |
| 1007 |
auto T = Template("Hello, {{lambda}}!"); |
| 1008 |
Lambda L = []() -> llvm::json::Value { return "{{planet}}"; }; |
| 1009 |
T.registerLambda("lambda", L); |
| 1010 |
std::string Out; |
| 1011 |
raw_string_ostream OS(Out); |
| 1012 |
T.render(D, OS); |
| 1013 |
EXPECT_EQ("Hello, World!", Out); |
| 1014 |
} |
| 1015 |
|
| 1016 |
TEST(MustacheLambdas, BasicMultipleCalls) { |
| 1017 |
Value D = Object{}; |
| 1018 |
auto T = Template("{{lambda}} == {{lambda}} == {{lambda}}"); |
| 1019 |
int I = 0; |
| 1020 |
Lambda L = [&I]() -> llvm::json::Value { |
| 1021 |
I += 1; |
| 1022 |
return I; |
| 1023 |
}; |
| 1024 |
T.registerLambda("lambda", L); |
| 1025 |
std::string Out; |
| 1026 |
raw_string_ostream OS(Out); |
| 1027 |
T.render(D, OS); |
| 1028 |
EXPECT_EQ("1 == 2 == 3", Out); |
| 1029 |
} |
| 1030 |
|
| 1031 |
TEST(MustacheLambdas, Escaping) { |
| 1032 |
Value D = Object{}; |
| 1033 |
auto T = Template("<{{lambda}}{{&lambda}}"); |
| 1034 |
Lambda L = []() -> llvm::json::Value { return ">"; }; |
| 1035 |
T.registerLambda("lambda", L); |
| 1036 |
std::string Out; |
| 1037 |
raw_string_ostream OS(Out); |
| 1038 |
T.render(D, OS); |
| 1039 |
EXPECT_EQ("<>>", Out); |
| 1040 |
} |
| 1041 |
|
| 1042 |
TEST(MustacheLambdas, Sections) { |
| 1043 |
Value D = Object{}; |
| 1044 |
auto T = Template("<{{#lambda}}{{x}}{{/lambda}}>"); |
| 1045 |
SectionLambda L = [](StringRef Text) -> llvm::json::Value { |
| 1046 |
if (Text == "{{x}}") { |
| 1047 |
return "yes"; |
| 1048 |
} |
| 1049 |
return "no"; |
| 1050 |
}; |
| 1051 |
T.registerLambda("lambda", L); |
| 1052 |
std::string Out; |
| 1053 |
raw_string_ostream OS(Out); |
| 1054 |
T.render(D, OS); |
| 1055 |
EXPECT_EQ("<yes>", Out); |
| 1056 |
} |
| 1057 |
|
| 1058 |
TEST(MustacheLambdas, SectionExpansion) { |
| 1059 |
Value D = Object{ |
| 1060 |
{"planet", "Earth"}, |
| 1061 |
}; |
| 1062 |
auto T = Template("<{{#lambda}}-{{/lambda}}>"); |
| 1063 |
SectionLambda L = [](StringRef Text) -> llvm::json::Value { |
| 1064 |
SmallString<128> Result; |
| 1065 |
Result += Text; |
| 1066 |
Result += "{{planet}}"; |
| 1067 |
Result += Text; |
| 1068 |
return Result; |
| 1069 |
}; |
| 1070 |
T.registerLambda("lambda", L); |
| 1071 |
std::string Out; |
| 1072 |
raw_string_ostream OS(Out); |
| 1073 |
T.render(D, OS); |
| 1074 |
EXPECT_EQ("<-Earth->", Out); |
| 1075 |
} |
| 1076 |
|
| 1077 |
TEST(MustacheLambdas, SectionsMultipleCalls) { |
| 1078 |
Value D = Object{}; |
| 1079 |
auto T = Template("{{#lambda}}FILE{{/lambda}} != {{#lambda}}LINE{{/lambda}}"); |
| 1080 |
SectionLambda L = [](StringRef Text) -> llvm::json::Value { |
| 1081 |
SmallString<128> Result; |
| 1082 |
Result += "__"; |
| 1083 |
Result += Text; |
| 1084 |
Result += "__"; |
| 1085 |
return Result; |
| 1086 |
}; |
| 1087 |
T.registerLambda("lambda", L); |
| 1088 |
std::string Out; |
| 1089 |
raw_string_ostream OS(Out); |
| 1090 |
T.render(D, OS); |
| 1091 |
EXPECT_EQ("__FILE__ != __LINE__", Out); |
| 1092 |
} |
| 1093 |
|
| 1094 |
TEST(MustacheLambdas, InvertedSections) { |
| 1095 |
Value D = Object{{"static", "static"}}; |
| 1096 |
auto T = Template("<{{^lambda}}{{static}}{{/lambda}}>"); |
| 1097 |
SectionLambda L = [](StringRef Text) -> llvm::json::Value { return false; }; |
| 1098 |
T.registerLambda("lambda", L); |
| 1099 |
std::string Out; |
| 1100 |
raw_string_ostream OS(Out); |
| 1101 |
T.render(D, OS); |
| 1102 |
EXPECT_EQ("<>", Out); |
| 1103 |
} |
| 1104 |
|
| 1105 |
TEST(MustacheComments, Inline) { |
| 1106 |
// Comment blocks should be removed from the template. |
| 1107 |
Value D = {}; |
| 1108 |
auto T = Template("12345{{! Comment Block! }}67890"); |
| 1109 |
std::string Out; |
| 1110 |
raw_string_ostream OS(Out); |
| 1111 |
T.render(D, OS); |
| 1112 |
EXPECT_EQ("1234567890", Out); |
| 1113 |
} |
| 1114 |
|
| 1115 |
TEST(MustacheComments, Multiline) { |
| 1116 |
// Multiline comments should be permitted. |
| 1117 |
Value D = {}; |
| 1118 |
auto T = |
| 1119 |
Template("12345{{!\n This is a\n multi-line comment...\n}}67890\n"); |
| 1120 |
std::string Out; |
| 1121 |
raw_string_ostream OS(Out); |
| 1122 |
T.render(D, OS); |
| 1123 |
EXPECT_EQ("1234567890\n", Out); |
| 1124 |
} |
| 1125 |
|
| 1126 |
TEST(MustacheComments, Standalone) { |
| 1127 |
// All standalone comment lines should be removed. |
| 1128 |
Value D = {}; |
| 1129 |
auto T = Template("Begin.\n{{! Comment Block! }}\nEnd.\n"); |
| 1130 |
std::string Out; |
| 1131 |
raw_string_ostream OS(Out); |
| 1132 |
T.render(D, OS); |
| 1133 |
EXPECT_EQ("Begin.\nEnd.\n", Out); |
| 1134 |
} |
| 1135 |
|
| 1136 |
TEST(MustacheComments, IndentedStandalone) { |
| 1137 |
// All standalone comment lines should be removed. |
| 1138 |
Value D = {}; |
| 1139 |
auto T = Template("Begin.\n {{! Indented Comment Block! }}\nEnd.\n"); |
| 1140 |
std::string Out; |
| 1141 |
raw_string_ostream OS(Out); |
| 1142 |
T.render(D, OS); |
| 1143 |
EXPECT_EQ("Begin.\nEnd.\n", Out); |
| 1144 |
} |
| 1145 |
|
| 1146 |
TEST(MustacheComments, StandaloneLineEndings) { |
| 1147 |
// "\r\n" should be considered a newline for standalone tags. |
| 1148 |
Value D = {}; |
| 1149 |
auto T = Template("|\r\n{{! Standalone Comment }}\r\n|"); |
| 1150 |
std::string Out; |
| 1151 |
raw_string_ostream OS(Out); |
| 1152 |
T.render(D, OS); |
| 1153 |
EXPECT_EQ("|\r\n|", Out); |
| 1154 |
} |
| 1155 |
|
| 1156 |
TEST(MustacheComments, StandaloneWithoutPreviousLine) { |
| 1157 |
// Standalone tags should not require a newline to precede them. |
| 1158 |
Value D = {}; |
| 1159 |
auto T = Template(" {{! I'm Still Standalone }}\n!"); |
| 1160 |
std::string Out; |
| 1161 |
raw_string_ostream OS(Out); |
| 1162 |
T.render(D, OS); |
| 1163 |
EXPECT_EQ("!", Out); |
| 1164 |
} |
| 1165 |
|
| 1166 |
TEST(MustacheComments, StandaloneWithoutNewline) { |
| 1167 |
// Standalone tags should not require a newline to follow them. |
| 1168 |
Value D = {}; |
| 1169 |
auto T = Template("!\n {{! I'm Still Standalone }}"); |
| 1170 |
std::string Out; |
| 1171 |
raw_string_ostream OS(Out); |
| 1172 |
T.render(D, OS); |
| 1173 |
EXPECT_EQ("!\n", Out); |
| 1174 |
} |
| 1175 |
|
| 1176 |
TEST(MustacheComments, MultilineStandalone) { |
| 1177 |
// All standalone comment lines should be removed. |
| 1178 |
Value D = {}; |
| 1179 |
auto T = Template("Begin.\n{{!\nSomething's going on here...\n}}\nEnd.\n"); |
| 1180 |
std::string Out; |
| 1181 |
raw_string_ostream OS(Out); |
| 1182 |
T.render(D, OS); |
| 1183 |
EXPECT_EQ("Begin.\nEnd.\n", Out); |
| 1184 |
} |
| 1185 |
|
| 1186 |
TEST(MustacheComments, IndentedMultilineStandalone) { |
| 1187 |
// All standalone comment lines should be removed. |
| 1188 |
Value D = {}; |
| 1189 |
auto T = |
| 1190 |
Template("Begin.\n {{!\n Something's going on here...\n }}\nEnd.\n"); |
| 1191 |
std::string Out; |
| 1192 |
raw_string_ostream OS(Out); |
| 1193 |
T.render(D, OS); |
| 1194 |
EXPECT_EQ("Begin.\nEnd.\n", Out); |
| 1195 |
} |
| 1196 |
|
| 1197 |
TEST(MustacheComments, IndentedInline) { |
| 1198 |
// Inline comments should not strip whitespace. |
| 1199 |
Value D = {}; |
| 1200 |
auto T = Template(" 12 {{! 34 }}\n"); |
| 1201 |
std::string Out; |
| 1202 |
raw_string_ostream OS(Out); |
| 1203 |
T.render(D, OS); |
| 1204 |
EXPECT_EQ(" 12 \n", Out); |
| 1205 |
} |
| 1206 |
|
| 1207 |
TEST(MustacheComments, SurroundingWhitespace) { |
| 1208 |
// Comment removal should preserve surrounding whitespace. |
| 1209 |
Value D = {}; |
| 1210 |
auto T = Template("12345 {{! Comment Block! }} 67890"); |
| 1211 |
std::string Out; |
| 1212 |
raw_string_ostream OS(Out); |
| 1213 |
T.render(D, OS); |
| 1214 |
EXPECT_EQ("12345 67890", Out); |
| 1215 |
} |
| 1216 |
|
| 1217 |
TEST(MustacheComments, VariableNameCollision) { |
| 1218 |
// Comments must never render, even if a variable with the same name exists. |
| 1219 |
Value D = Object{ |
| 1220 |
{"! comment", 1}, {"! comment ", 2}, {"!comment", 3}, {"comment", 4}}; |
| 1221 |
auto T = Template("comments never show: >{{! comment }}<"); |
| 1222 |
std::string Out; |
| 1223 |
raw_string_ostream OS(Out); |
| 1224 |
T.render(D, OS); |
| 1225 |
EXPECT_EQ("comments never show: ><", Out); |
| 1226 |
} |