package org.zwobble.mammoth.tests.xml;

import org.junit.Test;
import org.zwobble.mammoth.xml.XmlElement;
import org.zwobble.mammoth.xml.XmlParser;
import org.zwobble.mammoth.xml.XmlTextNode;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class XmlParserTests {
    @Test
    public void canParseSelfClosingElement() {
        assertThat(
            XmlParser.parseString("<body/>"),
            is(new XmlElement("body")));
        assertThat(
            XmlParser.parseString("<values/>"),
            is(new XmlElement("values")));
    }
    
    @Test
    public void canParseEmptyElementWithSeparateClosingTag() {
        assertThat(
            XmlParser.parseString("<body></body>"),
            is(new XmlElement("body")));
    }
    
    @Test
    public void canParseAttributesOfTag() {
        assertThat(
            XmlParser.parseString("<body name='bob'></body>"),
            is(new XmlElement("body", ImmutableMap.of("name", "bob"))));
    }
    
    @Test
    public void canParseElementWithDescendantElements() {
        assertThat(
            XmlParser.parseString("<body><a><b/></a><a/></body>"),
            is(new XmlElement("body", ImmutableMap.of(), ImmutableList.of(
                new XmlElement("a", ImmutableMap.of(), ImmutableList.of(
                    new XmlElement("b")
                )),
                new XmlElement("a")
            ))));
    }
    
    @Test
    public void canParseTextNodes() {
        assertThat(
            XmlParser.parseString("<body>Hello!</body>"),
            is(new XmlElement("body", ImmutableMap.of(), ImmutableList.of(
                new XmlTextNode("Hello!")
            ))));
    }
    
    @Test
    public void unmappedNamespaceUrisAreIncludedInBracesAsPrefix() {
        assertThat(
            XmlParser.parseString("<w:body xmlns:w='word'/>"),
            is(new XmlElement("{word}body")));
    }
}

@@ -1846,16 +1846,30 @@
 paceUris
+InElementNames
 AreInclu
@@ -2028,10 +2028,295 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void unmappedNamespaceUrisInAttributeNamesAreIncludedInBracesAsPrefix() %7B%0A        assertThat(%0A            XmlParser.parseString(%22%3Cbody xmlns:w='word' w:name='bob'%3E%3C/body%3E%22),%0A            is(new XmlElement(%22body%22, ImmutableMap.of(%22%7Bword%7Dname%22, %22bob%22))));%0A    %7D%0A
 %7D%0A

Add test for attribute names with URIs