package com.wizzardo.http.framework.template.taglib;

import com.wizzardo.http.framework.template.*;
import com.wizzardo.http.framework.template.taglib.g.Each;
import com.wizzardo.tools.xml.Node;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

/**
 * Created by wizzardo on 26.04.15.
 */
public class EachTest {

    @Before
    public void setup() {
        TagLib.findTags(Collections.singletonList(Each.class));
    }

    @Test
    public void test_1() {
        Node n = Node.parse("<div><g:each in=\"[1,2,3]\" var=\"i\">$i<br/></g:each></div>", true);

        RenderableList l = new RenderableList();
        ViewRenderer.prepare(n.children(), l, "", "");

        RenderResult result = l.get(new Model());

        Assert.assertEquals("" +
                "<div>\n" +
                "        1\n" +
                "        <br/>\n" +
                "        2\n" +
                "        <br/>\n" +
                "        3\n" +
                "        <br/>\n" +
                "</div>\n", result.toString());
    }

    @Test
    public void test_2() {
        Node n = Node.parse("<div><g:each in=\"$list\">$it<br/></g:each></div>", true);

        RenderableList l = new RenderableList();
        ViewRenderer.prepare(n.children(), l, "", "");

        Model model = new Model();
        model.put("list", Arrays.asList(1, 2, 3));
        RenderResult result = l.get(model);

        Assert.assertEquals("" +
                "<div>\n" +
                "        1\n" +
                "        <br/>\n" +
                "        2\n" +
                "        <br/>\n" +
                "        3\n" +
                "        <br/>\n" +
                "</div>\n", result.toString());
    }
}

@@ -1790,11 +1790,745 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void test_3() %7B%0A        Node n = Node.parse(%22%3Cdiv%3E%3Cg:each in=%5C%22$list%5C%22 status=%5C%22i%5C%22%3E$%7Bi+1%7D: $it%3Cbr/%3E%3C/g:each%3E%3C/div%3E%22, true);%0A%0A        RenderableList l = new RenderableList();%0A        ViewRenderer.prepare(n.children(), l, %22%22, %22%22);%0A%0A        Model model = new Model();%0A        model.put(%22list%22, Arrays.asList(%22one%22, %22two%22, %22three%22));%0A        RenderResult result = l.get(model);%0A%0A        Assert.assertEquals(%22%22 +%0A                %22%3Cdiv%3E%5Cn%22 +%0A                %22        1: one%5Cn%22 +%0A                %22        %3Cbr/%3E%5Cn%22 +%0A                %22        2: two%5Cn%22 +%0A                %22        %3Cbr/%3E%5Cn%22 +%0A                %22        3: three%5Cn%22 +%0A                %22        %3Cbr/%3E%5Cn%22 +%0A                %22%3C/div%3E%5Cn%22, result.toString());%0A    %7D
 %0A%7D%0A

improve tests