package com.codepoetics.ambivalence;

import org.junit.Test;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;

public class EithersTest {

    @Test
    public void splitLeftsFromRights() {
        EitherFactory<String, Integer> factory = new EitherFactory<>();

        final EitherSplit<String, Integer> result = Stream.of(
                factory.makeLeft("foo"),
                factory.makeRight(12),
                factory.makeRight(23),
                factory.makeLeft("bar")
        ).collect(Eithers.split());

        assertThat(result.getLefts(), contains("foo", "bar"));
        assertThat(result.getRights(), contains(12, 23));
    }

    @Test
    public void splitAndCollect() {
        EitherFactory<String, Integer> factory = new EitherFactory<>();

        final SplitResult<String, Integer> result = Stream.of(
                factory.makeLeft("foo"),
                factory.makeRight(12),
                factory.makeRight(23),
                factory.makeLeft("bar")
        ).collect(Eithers.splitAndCollectLeft(Collectors.joining(",")));

        assertThat(result.getLeft(), equalTo("foo,bar"));
        assertThat(result.getRights(), contains(12, 23));
    }
}

@@ -55,16 +55,44 @@
 .Test;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -152,16 +152,87 @@
 tream;%0A%0A
+import static com.codepoetics.ambivalence.Eithers.splitAndCollectLeft;%0A
 import s
@@ -366,16 +366,61 @@
 equalTo;
+%0Aimport static org.hamcrest.Matchers.hasSize;
 %0A%0Apublic
@@ -1307,32 +1307,24 @@
   ).collect(
-Eithers.
 splitAndColl
@@ -1480,11 +1480,590 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void splitAndCollectWithTryable() %7B%0A        SplitResult%3CString, IOException%3E result = Stream.of(%22a%22, %22bee%22, %22cee%22, %22d%22)%0A                .map(Tryable.of(c -%3E %7B%0A                    if (c.length() ==1) %7B%0A                        return c;%0A                    %7D else %7B%0A                        throw new IOException(%22String too long%22);%0A                    %7D%0A                %7D))%0A                .collect(splitAndCollectLeft(Collectors.joining(%22,%22)));%0A%0A        assertThat(result.getLeft(), equalTo(%22a,d%22));%0A        assertThat(result.getRights(), hasSize(2));%0A    %7D
 %0A%7D%0A

Split and collect with tryable