package com.wizzardo.tools.xml;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @author: moxa
 * Date: 2/11/13
 */
public class NodeTest {

    @Test
    public void parse() {
        String s;
        Node xml;

        s = "I say: '${hello}'";
        Assert.assertEquals("I say: '${hello}'", new XmlParser().parse(s).textOwn());

        s = "<xml><xml>";
        Assert.assertEquals("xml", new XmlParser().parse(s).name());
        s = "<xml/>";
        Assert.assertEquals("xml", new XmlParser().parse(s).name());

        s = "<xml attr><xml>";
        Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr"));
        s = "<xml attr ><xml>";
        Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr"));
        s = "<xml attr />";
        Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr"));
        s = "<xml attr/>";
        Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr"));
        s = "<xml attr attr2/>";
        Assert.assertEquals(true, new XmlParser().parse(s).hasAttr("attr"));

        s = "<xml attr=\"qwerty\"/>";
        Assert.assertEquals("qwerty", new XmlParser().parse(s).attr("attr"));
        s = "<xml attr=\"qwerty\" attr2/>";
        Assert.assertEquals("qwerty", new XmlParser().parse(s).attr("attr"));
        s = "<xml attr2 attr=\"qwerty\"/>";
        Assert.assertEquals("qwerty", new XmlParser().parse(s).attr("attr"));

        s = "<xml><child></child></xml>";
        Assert.assertEquals(1, new XmlParser().parse(s).size());
        s = "<xml><child/></xml>";
        Assert.assertEquals(1, new XmlParser().parse(s).size());
        s = "<xml><child attr=\"qwerty\"/></xml>";
        Assert.assertEquals(1, new XmlParser().parse(s).size());
        Assert.assertEquals("qwerty", new XmlParser().parse(s).first().attr("attr"));


        s = "<xml><child/><child/><child/>ololo</xml>";
        Assert.assertEquals(4, new XmlParser().parse(s).size());
        Assert.assertEquals("ololo", new XmlParser().parse(s).text());
        Assert.assertEquals("ololo", new XmlParser().parse(s).textOwn());
        s = "<xml><child/><child/><child>ololo</child></xml>";
        Assert.assertEquals(3, new XmlParser().parse(s).size());
        Assert.assertEquals("ololo", new XmlParser().parse(s).text());
        Assert.assertEquals("", new XmlParser().parse(s).textOwn());
        s = "<xml><child/><child/><child>ololo</child>lo</xml>";
        Assert.assertEquals(4, new XmlParser().parse(s).size());
        Assert.assertEquals("ololo lo", new XmlParser().parse(s).text());
        Assert.assertEquals("lo", new XmlParser().parse(s).textOwn());


        s = "<xml>\n\t\tololo\n\t\t</xml>";
        Assert.assertEquals("ololo", new XmlParser().parse(s).text());
    }

    @Test
    public void html_1() throws IOException {
        String s = "";
        for (File f : new File("src/test/resources/xml").listFiles()) {
            System.out.println("parsing: " + f);
            new HtmlParser().parse(f);
        }
    }

    @Test
    public void html_2() throws IOException {
        String s = "<div width=100px></div>";
        Node root = new HtmlParser().parse(s);
        Node div = root.children().get(0);
        Assert.assertEquals(1, div.attributes().size());
        Assert.assertEquals(0, div.children().size());
        Assert.assertEquals("100px", div.attr("width"));

        s = "<div width=100px height=50px></div>";
        root = new HtmlParser().parse(s);
        div = root.children().get(0);
        Assert.assertEquals(2, div.attributes().size());
        Assert.assertEquals(0, div.children().size());
        Assert.assertEquals("100px", div.attr("width"));
        Assert.assertEquals("50px", div.attr("height"));
    }

    @Test
    public void gsp_1() throws IOException {
        String s = "<div><g:textField name=\"${it.key}\" placeholder=\"${[].collect({it})}\"/></div>";
        Node root = new GspParser().parse(s);
        Node div = root.children().get(0);
        Assert.assertEquals("div", div.name());
        Assert.assertEquals(1, div.children().size());

        Node textField = div.children().get(0);
        Assert.assertEquals("g:textField", textField.name());
        Assert.assertEquals(0, textField.children().size());
        Assert.assertEquals(2, textField.attributes().size());
        Assert.assertEquals("${it.key}", textField.attr("name"));
        Assert.assertEquals("${[].collect({it})}", textField.attr("placeholder"));
    }

    @Test
    public void gsp_2() throws IOException {
        String s = "<div><g:textField name=\"${it.key}\" placeholder=\"${String.valueOf(it.getValue()).replace(\"\\\"\", \"\")}\"/></div>";
        Node root = new GspParser().parse(s);
        Node div = root.children().get(0);
        Assert.assertEquals("div", div.name());
        Assert.assertEquals(1, div.children().size());

        Node textField = div.children().get(0);
        Assert.assertEquals("g:textField", textField.name());
        Assert.assertEquals(0, textField.children().size());
        Assert.assertEquals(2, textField.attributes().size());
        Assert.assertEquals("${it.key}", textField.attr("name"));
        Assert.assertEquals("${String.valueOf(it.getValue()).replace(\"\\\"\", \"\")}", textField.attr("placeholder"));
    }

    @Test
    public void gsp_3() throws IOException {
        String s = "<div id=\"${id}\"><span>foo:</span>${foo}</div>";
        Node root = new GspParser().parse(s);
        Node div = root.children().get(0);
        Assert.assertEquals("div", div.name());
        Assert.assertEquals(2, div.children().size());
        Assert.assertEquals(1, div.attributes().size());
        Assert.assertEquals("${id}", div.attr("id"));

        Node span = div.children().get(0);
        Assert.assertEquals("span", span.name());
        Assert.assertEquals(1, span.children().size());
        Assert.assertEquals("foo:", span.text());

        Node foo = div.children().get(1);
        Assert.assertEquals("${foo}", foo.text());
    }
}

@@ -2806,32 +2806,391 @@
 text());%0A    %7D%0A%0A
+    @Test%0A    public void xml_1() throws IOException %7B%0A        String s = %22%3Cdiv%3E%3C!--   %3Ccomment%3E   --%3E%3C/div%3E%22;%0A        Node div = new XmlParser().parse(s);%0A        Assert.assertEquals(0, div.attributes().size());%0A        Assert.assertEquals(1, div.children().size());%0A        Assert.assertEquals(%22%3C!-- %3Ccomment%3E --%3E%22, div.children().get(0).ownText());%0A    %7D%0A%0A
     @Test%0A    pu

add test for xml comment