forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_expressions_unittest.cc
91 lines (79 loc) · 3.95 KB
/
template_expressions_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/template_expressions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ui {
// Tip: ui_base_unittests --gtest_filter='TemplateExpressionsTest.*' to run
// these tests.
TEST(TemplateExpressionsTest, ReplaceTemplateExpressionsPieces) {
TemplateReplacements substitutions;
substitutions["test"] = "word";
substitutions["5"] = "number";
EXPECT_EQ("", ReplaceTemplateExpressions("", substitutions));
EXPECT_EQ("word", ReplaceTemplateExpressions("$i18n{test}", substitutions));
EXPECT_EQ("number ", ReplaceTemplateExpressions("$i18n{5} ", substitutions));
EXPECT_EQ("multiple: word, number.",
ReplaceTemplateExpressions("multiple: $i18n{test}, $i18n{5}.",
substitutions));
}
TEST(TemplateExpressionsTest,
ReplaceTemplateExpressionsConsecutiveDollarSignsPieces) {
TemplateReplacements substitutions;
substitutions["a"] = "x";
EXPECT_EQ("$ $$ $$$", ReplaceTemplateExpressions("$ $$ $$$", substitutions));
EXPECT_EQ("$x", ReplaceTemplateExpressions("$$i18n{a}", substitutions));
EXPECT_EQ("$$x", ReplaceTemplateExpressions("$$$i18n{a}", substitutions));
EXPECT_EQ("$i1812", ReplaceTemplateExpressions("$i1812", substitutions));
}
TEST(TemplateExpressionsTest, ReplaceTemplateExpressionsEscaping) {
static TemplateReplacements substitutions;
substitutions["punctuationSample"] = "a\"b'c<d>e&f";
substitutions["htmlSample"] = "<div>hello</div>";
EXPECT_EQ(
"a"b'c<d>e&f",
ReplaceTemplateExpressions("$i18n{punctuationSample}", substitutions));
EXPECT_EQ("<div>hello</div>",
ReplaceTemplateExpressions("$i18n{htmlSample}", substitutions));
EXPECT_EQ(
"multiple: <div>hello</div>, a"b'c<d>e&f.",
ReplaceTemplateExpressions(
"multiple: $i18n{htmlSample}, $i18n{punctuationSample}.",
substitutions));
}
TEST(TemplateExpressionsTest, ReplaceTemplateExpressionsRaw) {
static TemplateReplacements substitutions;
substitutions["rawSample"] = "<a href=\"example.com\">hello</a>";
EXPECT_EQ("<a href=\"example.com\">hello</a>",
ReplaceTemplateExpressions("$i18nRaw{rawSample}", substitutions));
}
TEST(TemplateExpressionsTest, ReplaceTemplateExpressionsPolymerQuoting) {
static TemplateReplacements substitutions;
substitutions["singleSample"] = "don't do it";
substitutions["doubleSample"] = "\"moo\" said the cow";
// This resolves |Call('don\'t do it')| to Polymer, which is presented as
// |don't do it| to the user.
EXPECT_EQ("<div>[[Call('don\\'t do it')]]",
ReplaceTemplateExpressions(
"<div>[[Call('$i18nPolymer{singleSample}')]]", substitutions));
// This resolves |Call('\"moo\" said the cow')| to Polymer, which is
// presented as |"moo" said the cow| to the user.
EXPECT_EQ("<div>[[Call('\\\"moo\\\" said the cow')]]",
ReplaceTemplateExpressions(
"<div>[[Call('$i18nPolymer{doubleSample}')]]", substitutions));
}
TEST(TemplateExpressionsTest, ReplaceTemplateExpressionsPolymerMixed) {
static TemplateReplacements substitutions;
substitutions["punctuationSample"] = "a\"b'c<d>e&f,g";
substitutions["htmlSample"] = "<div>hello</div>";
EXPECT_EQ("a\\\"b\\'c<d>e&f\\,g",
ReplaceTemplateExpressions("$i18nPolymer{punctuationSample}",
substitutions));
EXPECT_EQ("<div>hello</div>", ReplaceTemplateExpressions(
"$i18nPolymer{htmlSample}", substitutions));
EXPECT_EQ("multiple: <div>hello</div>, a\\\"b\\'c<d>e&f\\,g.",
ReplaceTemplateExpressions("multiple: $i18nPolymer{htmlSample}, "
"$i18nPolymer{punctuationSample}.",
substitutions));
}
} // namespace ui