Commits

Stephen McKamey  committed baa7d78

fix for missing packages; added units tests

  • Participants
  • Parent commits 9a0d9ee

Comments (0)

Files changed (5)

File duel-compiler/src/main/java/org/duelengine/duel/codegen/JavaCodeGen.java

 
 	private void writeAccessModifier(Appendable output, AccessModifierType access)
 		throws IOException {
-		
+
 		switch (access) {
 			case PRIVATE:
 				output.append("private ");
 			case PUBLIC:
 				output.append("public ");
 				break;
+			case DEFAULT:
+				break;
 		}
 	}
 
 
 		List<CodeExpression> args = expression.getInitializers();
 		if (args.size() < 1) {
-			output.append("new ArrayList<");
+			output.append("new java.util.ArrayList<");
 			this.writeTypeName(output, expression.getType());
 			output.append(">(");
 			if (expression.getSize() > 0) {
 			return;
 		}
 
-		output.append("Arrays.asList(");
+		output.append("java.util.Arrays.asList(");
 		boolean needsDelim = false;
 		boolean singleArg = (args.size() == 1);
 		for (CodeExpression arg : args) {

File duel-compiler/src/test/java/org/duelengine/duel/codegen/CodeDOMBuilderTest.java

 	}
 
 	@Test
+	public void callLiteralTest() throws IOException {
+		VIEWCommandNode input = new VIEWCommandNode(
+			new AttributePair[] {
+				new AttributePair("name", new LiteralNode("foo.bar.Blah"))
+			},
+			new CALLCommandNode(
+				new AttributePair[] {
+					new AttributePair("view", new ExpressionNode("foo.bar.Yada")),
+					new AttributePair("data", new ExpressionNode(" { name: 'bar', items: [ 1, 'too' ] } "))
+				}));
+
+		CodeTypeDeclaration expected = CodeDOMUtility.createViewType(
+			"foo.bar",
+			"Blah",
+			new CodeMethod(
+				AccessModifierType.PROTECTED,
+				Void.class,
+				"render",
+				new CodeParameterDeclarationExpression[] {
+					new CodeParameterDeclarationExpression(DuelContext.class, "context"),
+					new CodeParameterDeclarationExpression(Object.class, "data"),
+					new CodeParameterDeclarationExpression(int.class, "index"),
+					new CodeParameterDeclarationExpression(int.class, "count"),
+					new CodeParameterDeclarationExpression(String.class, "key")
+				},
+				new CodeExpressionStatement(
+					new CodeMethodInvokeExpression(
+						Void.class,
+						new CodeThisReferenceExpression(),
+						"renderView",
+						new CodeVariableReferenceExpression(DuelContext.class, "context"),
+						new CodeFieldReferenceExpression(
+							new CodeThisReferenceExpression(),
+							org.duelengine.duel.DuelView.class,
+							"view_2"),
+						new CodeMethodInvokeExpression(
+							Map.class,
+							new CodeTypeReferenceExpression(DuelData.class),
+							"asMap",
+							new CodePrimitiveExpression("name"),
+							new CodePrimitiveExpression("bar"),
+							new CodePrimitiveExpression("items"),
+							new CodeArrayCreateExpression(
+								Object.class,
+								new CodePrimitiveExpression(1),
+								new CodePrimitiveExpression("too"))).withParens(),
+						new CodeVariableReferenceExpression(int.class, "index"),
+						new CodeVariableReferenceExpression(int.class, "count"),
+						new CodeVariableReferenceExpression(String.class, "key")))
+				).withOverride().withThrows(IOException.class),
+			new CodeField(
+				AccessModifierType.PRIVATE,
+				org.duelengine.duel.DuelView.class,
+				"view_2"),
+			new CodeMethod(
+				AccessModifierType.PROTECTED,
+				Void.class,
+				"init",
+				null,
+				new CodeExpressionStatement(
+					new CodeBinaryOperatorExpression(
+						CodeBinaryOperatorType.ASSIGN,
+						new CodeFieldReferenceExpression(
+							new CodeThisReferenceExpression(),
+							org.duelengine.duel.DuelView.class,
+							"view_2"),
+						new CodeObjectCreateExpression("foo.bar.Yada")))
+				).withOverride());
+
+		CodeTypeDeclaration actual = new CodeDOMBuilder().buildView(input);
+
+		assertEquals(expected, actual);
+	}
+
+	@Test
 	public void callWrapperTest() throws IOException {
 		VIEWCommandNode input = new VIEWCommandNode(
 			new AttributePair[] {

File duel-compiler/src/test/java/org/duelengine/duel/codegen/JavaCodeGenTest.java

 	}
 
 	@Test
+	public void callLiteralTest() throws IOException {
+
+		CodeTypeDeclaration input = CodeDOMUtility.createViewType(
+			"foo.bar",
+			"Blah",
+			new CodeMethod(
+				AccessModifierType.PROTECTED,
+				Void.class,
+				"render",
+				new CodeParameterDeclarationExpression[] {
+					new CodeParameterDeclarationExpression(DuelContext.class, "context"),
+					new CodeParameterDeclarationExpression(Object.class, "data"),
+					new CodeParameterDeclarationExpression(int.class, "index"),
+					new CodeParameterDeclarationExpression(int.class, "count"),
+					new CodeParameterDeclarationExpression(String.class, "key")
+				},
+				new CodeExpressionStatement(
+					new CodeMethodInvokeExpression(
+						Void.class,
+						new CodeThisReferenceExpression(),
+						"renderView",
+						new CodeVariableReferenceExpression(DuelContext.class, "context"),
+						new CodeFieldReferenceExpression(
+							new CodeThisReferenceExpression(),
+							org.duelengine.duel.DuelView.class,
+							"view_2"),
+						new CodeMethodInvokeExpression(
+							Map.class,
+							new CodeTypeReferenceExpression(DuelData.class),
+							"asMap",
+							new CodePrimitiveExpression("name"),
+							new CodePrimitiveExpression("bar"),
+							new CodePrimitiveExpression("items"),
+							new CodeArrayCreateExpression(
+								Object.class,
+								new CodePrimitiveExpression(1),
+								new CodePrimitiveExpression("too"))).withParens(),
+						new CodeVariableReferenceExpression(int.class, "index"),
+						new CodeVariableReferenceExpression(int.class, "count"),
+						new CodeVariableReferenceExpression(String.class, "key")))
+				).withOverride().withThrows(IOException.class),
+			new CodeField(
+				AccessModifierType.PRIVATE,
+				org.duelengine.duel.DuelView.class,
+				"view_2"),
+			new CodeMethod(
+				AccessModifierType.PROTECTED,
+				Void.class,
+				"init",
+				null,
+				new CodeExpressionStatement(
+					new CodeBinaryOperatorExpression(
+						CodeBinaryOperatorType.ASSIGN,
+						new CodeFieldReferenceExpression(
+							new CodeThisReferenceExpression(),
+							org.duelengine.duel.DuelView.class,
+							"view_2"),
+						new CodeObjectCreateExpression("foo.bar.Yada")))
+				).withOverride());
+
+		String expected =
+			"package foo.bar;\n\n"+
+			"import java.io.*;\n"+
+			"import org.duelengine.duel.*;\n\n"+
+			"public class Blah extends DuelView {\n\n"+
+			"\tpublic Blah() {\n"+
+			"\t}\n\n"+
+			"\tpublic Blah(DuelPart... parts) {\n"+
+			"\t\tsuper(parts);\n"+
+			"\t}\n\n"+
+			"\t@Override\n"+
+			"\tprotected void render(DuelContext context, Object data, int index, int count, String key) throws IOException {\n"+
+			"\t\tthis.renderView(context, this.view_2, DuelData.asMap(\"name\", \"bar\", \"items\", java.util.Arrays.asList(1, \"too\")), index, count, key);\n"+
+			"\t}\n\n"+
+			"\tprivate DuelView view_2;\n\n"+
+			"\t@Override\n"+
+			"\tprotected void init() {\n"+
+			"\t\tthis.view_2 = new foo.bar.Yada();\n"+
+			"\t}\n"+
+			"}\n";
+
+		StringBuilder output = new StringBuilder();
+		new JavaCodeGen().writeCode(output, input);
+		String actual = output.toString();
+
+		assertEquals(expected, actual);
+	}
+
+	@Test
 	public void callWrapperTest() throws IOException {
 
 		CodeTypeDeclaration input = CodeDOMUtility.createViewType(

File duel-compiler/src/test/java/org/duelengine/duel/parsing/DuelLexerTest.java

 		assertArrayEquals(expected, actual);
 	}
 
+	@Test
+	public void callLiteralTest() {
+
+		String input =
+				"<view name=\"foo\">\n"+
+				"<call view=\"bar\" data=\"<%= { name: 'bar', items: [ 1, 'too' ] } %>\" />";
+
+		Object[] expected = {
+				DuelToken.elemBegin("view"),
+				DuelToken.attrName("name"),
+				DuelToken.attrValue("foo"),
+				DuelToken.literal("\n"),
+				DuelToken.elemBegin("call"),
+				DuelToken.attrName("view"),
+				DuelToken.attrValue("bar"),
+				DuelToken.attrName("data"),
+				DuelToken.attrValue(new BlockValue("<%=", "%>", " { name: 'bar', items: [ 1, 'too' ] } ")),
+				DuelToken.elemEnd("call")
+			};
+
+		Object[] actual = new DuelLexer(input).toList().toArray();
+
+		assertArrayEquals(expected, actual);
+	}
+
+	@Test
+	public void callLiteralAltTest() {
+
+		String input =
+				"<view name=\"foo\">"+
+				"<call view=\"bar\" data=\" { name: 'bar', items: [ 1, 'too' ] } \" />";
+
+		Object[] expected = {
+				DuelToken.elemBegin("view"),
+				DuelToken.attrName("name"),
+				DuelToken.attrValue("foo"),
+				DuelToken.elemBegin("call"),
+				DuelToken.attrName("view"),
+				DuelToken.attrValue("bar"),
+				DuelToken.attrName("data"),
+				DuelToken.attrValue(" { name: 'bar', items: [ 1, 'too' ] } "),
+				DuelToken.elemEnd("call")
+			};
+
+		Object[] actual = new DuelLexer(input).toList().toArray();
+
+		assertArrayEquals(expected, actual);
+	}
+
 	@SuppressWarnings("unused")
 	private void dumpLists(Object[] expected, Object[] actual) {
 

File duel-compiler/src/test/java/org/duelengine/duel/parsing/DuelParserTest.java

 	}
 
 	@Test
+	public void callLiteralTest() throws Exception {
+
+		VIEWCommandNode expected = new VIEWCommandNode(
+			new AttributePair[] {
+				new AttributePair("name", new LiteralNode("foo"))
+			},
+			new CALLCommandNode(
+				new AttributePair[] {
+					new AttributePair("view", new ExpressionNode("bar")),
+					new AttributePair("data", new ExpressionNode(" { name: 'bar', items: [ 1, 'too' ] } "))
+				}));
+
+		Iterable<VIEWCommandNode> actual = new DuelParser().parse(
+			DuelToken.elemBegin("view"),
+			DuelToken.attrName("name"),
+			DuelToken.attrValue("foo"),
+			DuelToken.elemBegin("call"),
+			DuelToken.attrName("view"),
+			DuelToken.attrValue("bar"),
+			DuelToken.attrName("data"),
+			DuelToken.attrValue(new BlockValue("<%=", "%>", " { name: 'bar', items: [ 1, 'too' ] } ")),
+			DuelToken.elemEnd("call")
+		);
+
+		Iterator<VIEWCommandNode> iterator = actual.iterator();
+		assertTrue(iterator.hasNext());
+		assertEquals(expected, iterator.next());
+		assertFalse(iterator.hasNext());
+	}
+
+	@Test
 	public void callPartTest() throws Exception {
 
 		VIEWCommandNode expected = new VIEWCommandNode(