Commits

Stephen McKamey committed 46a86e1

enabling conditional attr on $FOR and $CALL

  • Participants
  • Parent commits 16fe6c0

Comments (0)

Files changed (4)

File duel-compiler/src/main/java/org/duelengine/duel/ast/CALLCommandNode.java

 			!name.equalsIgnoreCase(DATA) &&
 			!name.equalsIgnoreCase(INDEX) &&
 			!name.equalsIgnoreCase(COUNT) &&
-			!name.equalsIgnoreCase(KEY)) {
+			!name.equalsIgnoreCase(KEY) &&
+			!name.equalsIgnoreCase(IFCommandNode.IF_ATTR)) {
 
 			throw new InvalidNodeException("Attribute invalid on CALL command: "+name, value);
 		}

File duel-compiler/src/main/java/org/duelengine/duel/ast/IFCommandNode.java

  */
 public class IFCommandNode extends CommandNode {
 
+	public static final String IF_ATTR = "if";
 	public static final String EXT_NAME = "else";
 	private static final String NAME = "$if";
 	private static final CommandName CMD = CommandName.IF;
-	private static final String TEST = "test";
+	public static final String TEST = "test";
 
 	public IFCommandNode(int index, int line, int column) {
 		super(CMD, NAME, true, index, line, column);

File duel-compiler/src/main/java/org/duelengine/duel/parsing/DuelParser.java

 	}
 
 	private void rewriteConditionalAttr(ContainerNode parent, ElementNode elem) {
-		if (elem instanceof CommandNode) {
-			// only process normal HTML elements
+		if (elem instanceof CommandNode && !(elem instanceof CALLCommandNode) && !(elem instanceof FORCommandNode)) {
+			// only process normal CALL, FOR, and HTML elements
 			return;
 		}
 
-		DuelNode attr = elem.removeAttribute("if");
+		DuelNode attr = elem.removeAttribute(IFCommandNode.IF_ATTR);
 		if (attr == null) {
 			// nothing to do
 			return;
 		// create a conditional wrapper and
 		// move attr over to the conditional
 		IFCommandNode conditional = new IFCommandNode(attr.getIndex(), attr.getLine(), attr.getColumn());
-		conditional.setAttribute("test", attr);
+		conditional.setAttribute(IFCommandNode.TEST, attr);
 
 		// wrap element in parent
 		if (!parent.replaceChild(conditional, elem)) {

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

 	}
 
 	@Test
+	public void conditionalAttrCallTest() throws Exception {
+
+		VIEWCommandNode expected = new VIEWCommandNode(
+			new AttributePair[] {
+				new AttributePair("name", new LiteralNode("Foo"))
+			},
+			new IFCommandNode(
+				new AttributePair[] {
+					new AttributePair("test", new ExpressionNode("data"))
+				},
+				new CALLCommandNode(
+					new AttributePair[] {
+						new AttributePair("view", new ExpressionNode("Bar"))
+					})));
+
+		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("if"),
+			DuelToken.attrValue("data"),
+			DuelToken.elemEnd("call")
+		);
+
+		Iterator<VIEWCommandNode> iterator = actual.iterator();
+		assertTrue(iterator.hasNext());
+		assertEquals(expected, iterator.next());
+		assertFalse(iterator.hasNext());
+	}
+
+	@Test
 	public void callTest() throws Exception {
 
 		VIEWCommandNode expected = new VIEWCommandNode(