📄 parsertest.java
字号:
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Jamon code, released February, 2003. * * The Initial Developer of the Original Code is Ian Robertson. Portions * created by Ian Robertson are Copyright (C) 2005 Ian Robertson. All Rights * Reserved. * * Contributor(s): */package org.jamon.parser;import static org.junit.Assert.*;import java.io.IOException;import java.io.StringReader;import org.jamon.codegen.AnnotationType;import org.jamon.node.*;import org.junit.Test;/** * @author ian **/public class ParserTest extends AbstractParserTest{ private static final String JAVA_SNIPPET_START = "<%java "; private static final String JAVA_END = "</%java>"; private static final String JAVA_START = "<%java>"; private static final String LITERAL_START = "<%LITERAL>"; private static final String LITERAL_END = "</%LITERAL>"; private static final String EMIT_START = "<% "; private static final String EMIT_END = "%>"; private static final String BEFORE = "before"; private static final String AFTER = "after"; private static final String SAMPLE_ANNOTATION = "@SuppressWarnings(\"%> #impl\""; private void checkParseText(final String p_text) throws Exception { assertEquals( topNode().addSubNode(new TextNode(START_LOC, p_text)), new TopLevelParser( TEMPLATE_LOC, new StringReader(p_text)).parse().getRootNode()); } @Test public void testParseText() throws Exception { checkParseText("Hello, world"); checkParseText("Hello < world"); checkParseText("Hello << world"); checkParseText("Hello <"); } @Test public void testParseJavaLine() throws Exception { String java = "int c = 4;\n"; assertEquals( topNode().addSubNode(new JavaNode(START_LOC, java)), parse("%" + java)); } @Test public void testParseTextAndJavaLine() throws Exception { String text = "hello, world\n"; String java = "int c = 4;"; assertEquals( topNode().addSubNode( new TextNode(START_LOC, text)).addSubNode( new JavaNode(location(2, 1), java)), new TopLevelParser( TEMPLATE_LOC, new StringReader(text + "%" + java)) .parse() .getRootNode()); } @Test public void testParseJavaLineAndText() throws Exception { String text = "hello, world\n"; String java = "int c = 4;\n"; assertEquals( topNode().addSubNode( new JavaNode(START_LOC, java)).addSubNode( new TextNode(location(2, 1), text)), parse("%" + java + text)); } @Test public void testParseJavaSnippet() throws Exception { String java = "int c = 4;"; assertEquals( topNode().addSubNode(new JavaNode(START_LOC, java)), parse(JAVA_SNIPPET_START + java + EMIT_END)); } @Test public void testParseJavaSnippetInTextWithQuotes() throws Exception { String text1 = "hello\n", text2 = "goodbye", java = "String c = \"a%%>\";"; assertEquals( topNode() .addSubNode(new TextNode(START_LOC, text1)) .addSubNode(new JavaNode(location(2,1), java)) .addSubNode(new TextNode( location(2, java.length() + JAVA_SNIPPET_START.length() + EMIT_END.length() + 1), text2)), parse(text1 + JAVA_SNIPPET_START + java + EMIT_END + text2)); } @Test public void testParseEscapedNewline() throws Exception { String line1 = "line\\1", line2 = "line2\\"; assertEquals( topNode().addSubNode( new TextNode(location(1,1), line1 + line2)), parse(line1 + "\\\n" + line2)); } @Test public void testJavaTag() throws Exception { String java = "int c = 4;"; String javaTag = JAVA_START + java + JAVA_END; assertEquals( topNode() .addSubNode(new TextNode(START_LOC, BEFORE)) .addSubNode( new JavaNode(location(1, 1 + BEFORE.length()), java)) .addSubNode( new TextNode( location(1, 1 + (BEFORE + javaTag).length()), AFTER)), parse(BEFORE + javaTag + AFTER)); } private void checkJavaString(String p_java) throws Exception { assertEquals( topNode().addSubNode(new JavaNode(START_LOC, p_java)), parse(JAVA_START + p_java + JAVA_END)); } @Test public void testJavaTagWithQuotes() throws Exception { checkJavaString("String s = \" " + JAVA_END + "\"; "); checkJavaString("String s = \" \\\" " + JAVA_END + "\"; "); checkJavaString("String s = \"'" + JAVA_END + "\"; "); checkJavaString("String s = ' " + JAVA_END + " '; "); checkJavaString("int a = 3; \\"); } @Test public void testLiteral() throws Exception { String literal = LITERAL_START + JAVA_START + LITERAL_END; assertEquals( topNode() .addSubNode(new TextNode(START_LOC, BEFORE)) .addSubNode( new LiteralNode( location(1, 1 + BEFORE.length()), JAVA_START)) .addSubNode( new TextNode( location(1, 1 + (BEFORE + literal).length()), AFTER)), parse(BEFORE + literal + AFTER)); } @Test public void testFalseStartForJavaTagEnd() throws Exception { checkJavaString("a </% b"); } private interface SubcomponentBuilder { SubcomponentNode makeNode(Location p_location, String p_name); } private void doSubcomponentTest( String p_prefix, String p_suffix, SubcomponentBuilder p_builder) throws IOException { String during = "during"; String name = "foo"; String start = p_prefix + " " + name + ">"; String subcomponent = start + during + p_suffix; assertEquals( topNode() .addSubNode(new TextNode(START_LOC, BEFORE)) .addSubNode( p_builder .makeNode(location(1, 1 + BEFORE.length()), name) .addSubNode(new TextNode( location(1, 1 + (BEFORE + start).length()), during))) .addSubNode( new TextNode( location(1, 1 + (BEFORE + subcomponent).length()), AFTER)), parse(BEFORE + subcomponent + AFTER)); } @Test public void testDef() throws Exception { doSubcomponentTest( "<%def", "</%def>", new SubcomponentBuilder() { public SubcomponentNode makeNode(Location p_location, String p_name) { return new DefNode(p_location, p_name); } }); } @Test public void testMethod() throws Exception { doSubcomponentTest( "<%method", "</%method>", new SubcomponentBuilder() { public SubcomponentNode makeNode(Location p_location, String p_name) { return new MethodNode(p_location, p_name); } }); } @Test public void testAbsMethod() throws Exception { assertEquals( topNode() .addSubNode( new AbsMethodNode(START_LOC, "foo") .addArgsBlock( new ArgsNode(location(2,1)) .addArg( new ArgNode(location(3,1), new ArgTypeNode(location(3,1), "int"), new ArgNameNode(location(3, 5), "i")))) .addArgsBlock( new FragmentArgsNode(location(5,1), "f"))), parse("<%absmeth foo>\n<%args>\nint i;\n</%args>\n<%frag f/></%absmeth>")); } @Test public void testOverride() throws Exception { doSubcomponentTest( "<%override", "</%override>", new SubcomponentBuilder() { public SubcomponentNode makeNode(Location p_location, String p_name) { return new OverrideNode(p_location, p_name); } }); } @Test public void testCall() throws Exception { String call = "<& foo &>"; Location pathStart = location(1, (BEFORE + "<& f").length()); assertEquals( topNode() .addSubNode(new TextNode(START_LOC, BEFORE)) .addSubNode( new SimpleCallNode( location(1, BEFORE.length() + 1), new RelativePathNode(pathStart).addPathElement( new PathElementNode(pathStart, "foo")), new NoParamsNode( location(1, (BEFORE + "<& foo &").length())))) .addSubNode( new TextNode( location(1, (BEFORE + call).length() + 1), AFTER)), parse(BEFORE + call + AFTER)); } @Test public void testEmit() throws Exception { String emitExpr = "foo"; String emit = EMIT_START + emitExpr + EMIT_END; assertEquals( topNode() .addSubNode(new TextNode(START_LOC, BEFORE)) .addSubNode( new EmitNode( location(1, 1 + BEFORE.length()), emitExpr, new DefaultEscapeNode( location(1, (BEFORE + emit).length())))) .addSubNode( new TextNode( location(1, 1 + (BEFORE + emit).length()), AFTER)), parse(BEFORE + emit + AFTER)); String escapedEmit = EMIT_START + emitExpr + "#h" + EMIT_END; assertEquals( topNode().addSubNode( new TextNode(START_LOC, BEFORE)).addSubNode( new EmitNode( location(1, 1 + BEFORE.length()), emitExpr, new EscapeNode( location( 1, 1 + (BEFORE + EMIT_START + emitExpr).length()), "h"))).addSubNode( new TextNode( location(1, 1 + (BEFORE + escapedEmit).length()),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -