package io.github.benas.unixstream;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;

import static io.github.benas.unixstream.UnixStream.file;
import static io.github.benas.unixstream.components.Grep.grep;
import static io.github.benas.unixstream.components.NumberLines.nl;
import static io.github.benas.unixstream.components.Sort.sort;
import static io.github.benas.unixstream.components.Uniq.uniq;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;

public class UnixStreamImplTest {

    private Stream<String> stream;

    @Before
    public void setUp() {
        stream = Stream.of("id,name", "1,foo", "2,bar");
    }

    @Test
    public void integrationTest1() throws IOException {
        List<String> strings = UnixStream.unixify(stream)
                .grep("bar")
                .cut(",", 2)
                .sort()
                .uniq()
                .nl()
                .collect(toList());

        assertThat(strings).isNotEmpty().isEqualTo(singletonList("1 bar"));
    }

    @Test
    public void integrationTest2() throws IOException {
        // cat input.txt | grep a | sort | uniq | nl > output.txt
        UnixStream.cat("src/test/resources/input.txt")
                .pipe(grep("a"))
                .pipe(sort())
                .pipe(uniq())
                .pipe(nl())
                .to(file("target/output.txt"));

        assertThat(new File("target/output.txt")).exists().hasContent("1 2,bar");
    }

    @Test
    public void integrationTest3() throws IOException {
        List<String> items = stream
                .filter(Predicates.grep("a"))
                .map(Functions.cut(",", 2))
                .collect(toList());

        assertThat(items).isNotNull().isNotEmpty().hasSize(2).containsExactly("name", "bar");
    }

    @Test
    public void getShouldReturnTheOriginalStream() throws Exception {
        assertThat(UnixStream.unixify(stream).get()).isEqualTo(stream);
    }
}

@@ -730,16 +730,60 @@
 tream;%0A%0A
+    private UnixStream%3CString%3E unixStream;%0A%0A
     @Bef
@@ -786,16 +786,16 @@
 @Before%0A
-
     publ
@@ -868,32 +868,1106 @@
 2,bar%22);%0A    %7D%0A%0A
+    @Test%0A    public void compact() throws Exception %7B%0A        stream = Stream.of(%22 f o o       %22);%0A        unixStream = new UnixStreamImpl%3C%3E(stream);%0A%0A        assertThat(unixStream.compact()).containsExactly(%22foo%22);%0A    %7D%0A%0A    @Test%0A    public void concat() throws Exception %7B%0A        stream = Stream.of(%22foo%22);%0A        unixStream = new UnixStreamImpl%3C%3E(stream);%0A%0A        assertThat(unixStream.concat(Stream.of(%22bar%22))).containsExactly(%22foo%22, %22bar%22);%0A    %7D%0A%0A    @Test%0A    public void cut() throws Exception %7B%0A        stream = Stream.of(%221;foo%22);%0A        unixStream = new UnixStreamImpl%3C%3E(stream);%0A%0A        assertThat(unixStream.cut(%22;%22, 2)).containsExactly(%22foo%22);%0A    %7D%0A%0A    @Test%0A    public void dos2unix() throws Exception %7B%0A        stream = Stream.of(%22a%5Cr%5Cn%22, %22b%5Cr%5Cnc%5Cr%5Cn%22);%0A        unixStream = new UnixStreamImpl%3C%3E(stream);%0A%0A        assertThat(unixStream.dos2unix()).containsExactly(%22a%5Cn%22, %22b%5Cnc%5Cn%22);%0A    %7D%0A%0A    @Test%0A    public void getShouldReturnTheOriginalStream() throws Exception %7B%0A        assertThat(UnixStream.unixify(stream).get()).isEqualTo(stream);%0A    %7D%0A%0A
     @Test%0A    pu
@@ -3102,164 +3102,6 @@
  %7D%0A%0A
-    @Test%0A    public void getShouldReturnTheOriginalStream() throws Exception %7B%0A        assertThat(UnixStream.unixify(stream).get()).isEqualTo(stream);%0A    %7D%0A
 %7D%0A

add tests for UnixStreamImpl