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 下午3:42
 */
public class StringRangeExtractorTest {
    private String html;
    StringRangeExtractor stringRangeExtractor;

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

    @Test
    public void testExtract() throws Exception {
        String demoHtml = "<td>www.baidu.com</td>";
        stringRangeExtractor = new StringRangeExtractor("<td>", "</td>");
        String result = stringRangeExtractor.extract(demoHtml);
        Assert.assertEquals(result, "www.baidu.com");

        stringRangeExtractor = new StringRangeExtractor("x", "xx");
        result = stringRangeExtractor.extract(demoHtml);
        Assert.assertEquals(result, "");

        stringRangeExtractor = new StringRangeExtractor("<td>", "</td>", "true");
        result = stringRangeExtractor.extract(demoHtml);
        Assert.assertEquals(result, "<td>www.baidu.com</td>");

        stringRangeExtractor = new StringRangeExtractor("<td>", "</td>", "false");
        result = stringRangeExtractor.extract(demoHtml);
        Assert.assertEquals(result, "www.baidu.com");
    }

    @Test
    public void testExtractList() throws Exception {
        stringRangeExtractor = new StringRangeExtractor("<td style", "</td>", "true");
        List<String> stringList = stringRangeExtractor.extractList(html);
        Assert.assertNotNull(stringList);
        Assert.assertEquals(stringList.size(), 5);
        Assert.assertEquals(stringList.get(3), "<td style=\"text-align:center;\">4</td>");
        stringRangeExtractor = new StringRangeExtractor("<td style", "</td>", "false");
        stringList = stringRangeExtractor.extractList(html);
        Assert.assertNotNull(stringList);
        Assert.assertEquals(stringList.size(), 5);
        Assert.assertEquals(stringList.get(3), "=\"text-align:center;\">4");

    }
}
@@ -605,17 +605,16 @@
 ce(%22list
-2
 .html%22),
@@ -1708,39 +1708,49 @@
 xtractor(%22%3Ct
-d style
+r class=%5C%22item%5C%22%3E
 %22, %22%3C/t
-d
+r
 %3E%22, %22true%22);
@@ -1905,33 +1905,33 @@
 ingList.size(), 
-5
+3
 );%0A        Asser
@@ -1949,68 +1949,280 @@
 als(
-stringList.get(3), %22%3Ctd style=%5C%22text-align:center;%5C%22%3E4%3C/td%3E%22
+%22%3Ctr class=%5C%22item%5C%22%3E%5Cn%22 +%0A                %22        %3Ctd class=%5C%22type%5C%22%3Edynamic%3C/td%3E%5Cn%22 +%0A                %22        %3Ctd class=%5C%22name%5C%22%3EPython%3C/td%3E%5Cn%22 +%0A                %22        %3Ctd class=%5C%22url%5C%22%3E%3Ehttps://www.python.org%3C/td%3E%5Cn%22 +%0A                %22    %3C/tr%3E%22, stringList.get(2)
 );%0A 
@@ -2283,23 +2283,33 @@
 (%22%3Ct
-d style
+r class=%5C%22item%5C%22%3E
 %22, %22%3C/t
-d
+r
 %3E%22, 
@@ -2468,17 +2468,17 @@
 size(), 
-5
+3
 );%0A     
@@ -2504,54 +2504,256 @@
 als(
-stringList.get(3), %22=%5C%22text-align:center;%5C%22%3E4%22
+%22%5Cn%22 +%0A                %22        %3Ctd class=%5C%22type%5C%22%3Edynamic%3C/td%3E%5Cn%22 +%0A                %22        %3Ctd class=%5C%22name%5C%22%3EPython%3C/td%3E%5Cn%22 +%0A                %22        %3Ctd class=%5C%22url%5C%22%3E%3Ehttps://www.python.org%3C/td%3E%5Cn%22 +%0A                %22    %22, stringList.get(2)
 );%0A%0A

fix String Range test