Skip to content

Commit e8e5050

Browse files
authored
Add template keys verificaion for check() method.
1 parent 2ef24ec commit e8e5050

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,42 @@ public List<String> check(Map<K, V> value) {
137137
final String escape = TEMPLATE_SETTINGS.get("escape");
138138
String result = template;
139139
final List<String> notFound = new ArrayList<String>();
140+
final List<String> valueKeys = new ArrayList<String>();
140141
for (final Map.Entry<K, V> element : value.entrySet()) {
142+
final String key = "" + ((Map.Entry) element).getKey();
141143
java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(interpolate.replace(ALL_SYMBOLS,
142-
"\\s*\\Q" + ((Map.Entry) element).getKey() + "\\E\\s*")).matcher(result);
144+
"\\s*\\Q" + key + "\\E\\s*")).matcher(result);
143145
boolean isFound = matcher.find();
144146
result = matcher.replaceAll(String.valueOf(((Map.Entry) element).getValue()));
145147
matcher = java.util.regex.Pattern.compile(escape.replace(ALL_SYMBOLS,
146-
"\\s*\\Q" + ((Map.Entry) element).getKey() + "\\E\\s*")).matcher(result);
148+
"\\s*\\Q" + key + "\\E\\s*")).matcher(result);
147149
isFound |= matcher.find();
148150
result = matcher.replaceAll(escape(String.valueOf(((Map.Entry) element).getValue())));
149151
matcher = java.util.regex.Pattern.compile(evaluate.replace(ALL_SYMBOLS,
150-
"\\s*\\Q" + ((Map.Entry) element).getKey() + "\\E\\s*")).matcher(result);
152+
"\\s*\\Q" + key + "\\E\\s*")).matcher(result);
151153
isFound |= matcher.find();
152154
result = matcher.replaceAll(String.valueOf(((Map.Entry) element).getValue()));
153155
if (!isFound) {
154-
notFound.add("" + ((Map.Entry) element).getKey());
156+
notFound.add(key);
155157
}
158+
valueKeys.add(key);
156159
}
160+
final List<String> templateVars = new ArrayList<String>();
161+
java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(interpolate).matcher(result);
162+
while (matcher.find()) {
163+
templateVars.add(matcher.group(1).trim());
164+
}
165+
result = matcher.replaceAll("");
166+
matcher = java.util.regex.Pattern.compile(escape).matcher(result);
167+
while (matcher.find()) {
168+
templateVars.add(matcher.group(1).trim());
169+
}
170+
result = matcher.replaceAll("");
171+
matcher = java.util.regex.Pattern.compile(evaluate).matcher(result);
172+
while (matcher.find()) {
173+
templateVars.add(matcher.group(1).trim());
174+
}
175+
notFound.addAll(difference(templateVars, valueKeys));
157176
return notFound;
158177
}
159178
}

src/test/java/com/github/underscore/UtilityTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,20 @@ public void templateCheck() {
266266
assertEquals("name2", compiled.check(new LinkedHashMap<String, Object>() { {
267267
put("name2", "moe"); } }).get(0));
268268
}
269+
270+
@Test
271+
public void templateCheck2() {
272+
Template<Map<String, Object>> compiled = $.template("hello: <%= name %> <%= name2 %>");
273+
assertEquals("name2", compiled.check(new LinkedHashMap<String, Object>() { {
274+
put("name", "moe"); } }).get(0));
275+
Template<Map<String, Object>> compiled2 = $.template("hello: <%- name %> <%- name2 %>");
276+
assertEquals("name2", compiled2.check(new LinkedHashMap<String, Object>() { {
277+
put("name", "moe"); } }).get(0));
278+
Template<Map<String, Object>> compiled3 = $.template("hello: <% name %> <% name2 %>");
279+
assertEquals("name2", compiled3.check(new LinkedHashMap<String, Object>() { {
280+
put("name", "moe"); } }).get(0));
281+
}
282+
269283
/*
270284
var fortmatted = _.format("hello: {}", "moe");
271285
=> "hello: moe"

0 commit comments

Comments
 (0)