package im.nll.data.extractor.impl;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/**
 * @author <a href="mailto:fivesmallq@gmail.com">fivesmallq</a>
 * @version Revision: 1.0
 * @date 15/12/30 上午10:49
 */
public class SelectorExtractorTest {
    private String html;
    SelectorExtractor selectorExtractor;

    @Before
    public void before() {
        try {
            html = Resources.toString(Resources.getResource("list.html"), Charsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testExtractEscaped() throws Exception {
        selectorExtractor = new SelectorExtractor("th:contains(hello,w):nth-child(2).html");
        String title = selectorExtractor.extract(html);
        Assert.assertEquals("hello,world", title);
    }
    @Test
    public void testExtract() throws Exception {
        selectorExtractor = new SelectorExtractor("th.title");
        String title = selectorExtractor.extract(html);
        Assert.assertEquals("languages", title);
    }

    @Test
    public void testExtractHtml() throws Exception {
        selectorExtractor = new SelectorExtractor("tr:nth-child(2).html");
        String title = selectorExtractor.extract(html);
        Assert.assertEquals("<th>type</th> \n" +
                "<th>name</th> \n" +
                "<th>website</th>", title);
    }

    @Test
    public void testExtractList() throws Exception {
        selectorExtractor = new SelectorExtractor("tr:has(td)");
        List<String> datas = selectorExtractor.extractList(html);
        Assert.assertEquals(3, datas.size());
        Assert.assertEquals("static Java https://www.java.com", datas.get(0));
    }
}

@@ -421,16 +421,42 @@
 g html;%0A
+    private String html2;%0A
     Sele
@@ -488,16 +488,16 @@
 ractor;%0A
-
 %0A    @Be
@@ -633,16 +633,109 @@
 UTF_8);%0A
+            html2 = Resources.toString(Resources.getResource(%22list2.html%22), Charsets.UTF_8);%0A
         
@@ -1078,24 +1078,25 @@
 tle);%0A    %7D%0A
+%0A
     @Test%0A  
@@ -1309,32 +1309,297 @@
  title);%0A    %7D%0A%0A
+    @Test%0A    public void testExtractAttr() throws Exception %7B%0A        selectorExtractor = new SelectorExtractor(%22a.attr(href)%22);%0A        String href = selectorExtractor.extract(%22%3Ca href=baidu.com %3Ehref%3C/a%3E%22);%0A        Assert.assertEquals(%22baidu.com%22, href);%0A    %7D%0A%0A
     @Test%0A    pu
@@ -2149,24 +2149,24 @@
 as.size());%0A
-
         Asse
@@ -2237,11 +2237,381 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testExtractList2() throws Exception %7B%0A        // .html mehtod only return html in the element%0A        selectorExtractor = new SelectorExtractor(%22a.h4.html%22);%0A        List%3CString%3E datas = selectorExtractor.extractList(html2);%0A        Assert.assertEquals(10, datas.size());%0A        Assert.assertEquals(%22%E3%80%8C%E5%92%B3%E5%92%B3%E5%92%B3%E3%80%8D%E6%98%A5%E5%AD%A3%E5%92%B3%E4%B8%8D%E5%81%9C %E8%BF%99%E4%BA%9B%E8%8D%AF%E7%89%A9%E8%A6%81%E8%AE%B0%E7%89%A2%22, datas.get(0));%0A    %7D
 %0A%7D%0A

add test