/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.xml.impl;

import java.util.Arrays;
import net.ranides.assira.TestFiles;
import net.ranides.assira.junit.NewAssert;
import net.ranides.assira.xml.XMLElement;
import net.ranides.assira.xml.XMLElements;
import net.ranides.assira.xml.XMLException;
import static net.ranides.assira.xml.XMLElement.$;
import net.ranides.assira.xml.XMLSelector;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class W3SelectorsTest {
    
    @Test
    public void testCompile() {
        XMLElement doc = $(TestFiles.XML_INPUT).normalize();
        
        XMLSelector xs1 = XMLSelector.css("user");
        XMLSelector xs2 = XMLSelector.xpath("//user");
        
        NewAssert.assertThrows(XMLException.class, ()->{
            XMLSelector.css("?user");
        });
        NewAssert.assertThrows(XMLException.class, ()->{
            XMLSelector.xpath("?//user");
        });
        
        assertEquals(doc.find(xs1).nodes(), doc.find(xs2).nodes());
    }
    
    @Test
    public void testXPath() {
        XMLElement doc = $(TestFiles.XML_INPUT).normalize();
        XMLElements users = doc.find("? //user").filter("? @public");
        
        assertEquals(Arrays.asList("ranides","saren"), users.children("login").texts());

    }
    
    @Test
    public void testName() {
        XMLElement user = $(TestFiles.XML_INPUT).normalize().find("user").first();
        
        XMLSelector sname = XMLSelector.compile("uid");
        
        assertEquals("5461", user.attrs().children(sname).text());
        assertEquals("", user.attrs().children(sname).find(sname).text());
    }
    
}
