### Question:
TemplateParamsExtractUtil { static void walk(String content, int start, int end, String pTag, Map<String, Object> paramMap, Set<String> nestedParameters) { int tagStart = content.indexOf("{{", start); while(tagStart != -1 && tagStart < end) { int tagStartOffset = content.indexOf("}}", tagStart); String var = content.substring(tagStart+2, tagStartOffset); if (var.startsWith("#") || var.startsWith("^")) { var = var.substring(1); int tagEnd = findEndTag(content, var, tagStartOffset); if (tagEnd == -1) { setValue(paramMap, nestedParameters, "错误:" + var, null); return; } if ("toJson".equals(var)) { String tagNameToBeListed = content.substring(tagStartOffset+2, tagEnd); setValue(paramMap, nestedParameters, tagNameToBeListed, new Object[0]); } else { setValue(paramMap, nestedParameters, var, null); walk(content, tagStartOffset + 2, tagEnd, var, paramMap, nestedParameters); } tagStart = content.indexOf("{{", tagEnd+3); } else if (var.startsWith("!") || var.startsWith(">")) { } else { setValue(paramMap, nestedParameters, var, null); tagStart = content.indexOf("{{", tagStartOffset); } } }  static Map<String, Object> getParams(String originContent, List<SearchTemplate> allFiles); static String renderESTemplateBody(String originContent, List<SearchTemplate> allFiles); static String renderMacrosThenFormat(String originContent, List<SearchTemplate> allFiles);  }### Answer:
@Test public void testNormal () { String s = "{{#a}}{{a}}{{/a}}{{#b}}{{/b}}{{^c}}{{/c}}{{null}}"; Map<String, Object> map = new HashMap<>(); walk(s, 0, s.length()-1, "", map, null); assertEquals(map.size(), 3); }
@Test public void testWithToJson () { String s = "{{#a}}{{#toJson}}a.list{{/toJson}}{{/a}}"; Map<String, Object> map = new HashMap<>(); walk(s, 0, s.length()-1, "", map, null); assertNotNull(((Map<String, Object>)map.get("a")).get("list")); }
@Test public void testWithWrongEndTag () { String s = "{{#a}}{{#toJson}}a.list{{/toJson}}"; Map<String, Object> map = new HashMap<>(); walk(s, 0, s.length()-1, "", map, null); assertTrue(map.containsKey("错误:a")); }
@Test public void testListComplexObjects () { String s = "{{#a}}{{#a.list}}{{a.list.b}},{{a.list.c}}{{/a.list}}{{/a}}"; Map<String, Object> map = new HashMap<>(); Set<String> nestedObjects = new HashSet<>(); walk(s, 0, s.length()-1, "", map, nestedObjects); assertEquals(2, ((Map<String, List<Map>>)map.get("a")).get("list").get(0).size()); assertEquals(2, nestedObjects.size()); }
@Test public void testMultiSameTags () { String s = "{{#a}}{{#a}}{{/a}}{{#c}}{{/c}}{{/a}}"; Map<String, Object> map = new HashMap<>(); walk(s, 0, s.length()-1, "", map, null); assertEquals(map.size(), 2); }