package com.pivovarit.function;

import com.pivovarit.function.exception.WrappedException;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

class ThrowingBiConsumerTest {

    @Test
    void shouldConsume() throws Exception {
        // given
        Integer[] input = {0};

        ThrowingBiConsumer<Integer, Integer, Exception> consumer = (i, j) -> input[0] = 2;

        // when
        consumer.accept(2, 3);

        // then
        assertThat(input[0]).isEqualTo(2);
    }

    @Test
    void shouldConsumeAfter() throws Exception {
        // given
        Integer[] input = {0};

        ThrowingBiConsumer<Integer, Integer, Exception> consumer = (i, j) -> input[0] = 2;
        ThrowingBiConsumer<Integer, Integer, Exception> after = (i, j) -> input[0] = 3;

        // when
        consumer.andThenConsume(after).accept(2, 3);

        // then
        assertThat(input[0]).isEqualTo(3);
    }

    @Test
    void shouldConsumeAsFunction() throws Exception {
        // given
        Integer[] input = {0};

        ThrowingBiConsumer<Integer, Integer, Exception> consumer = (i, j) -> input[0] = 2;

        // when
        consumer.asFunction().apply(42, 0);

        // then
        assertThat(input[0]).isEqualTo(2);
    }

    @Test
    void shouldConsumeAndThrowUnchecked() {
        IOException cause = new IOException("some message");

        // given
        ThrowingBiConsumer<Integer, Integer, IOException> consumer = (i, j) -> { throw cause; };

        // when
        assertThatThrownBy(() -> {
            ThrowingBiConsumer.unchecked(consumer).accept(3, 3);
        }).hasMessage(cause.getMessage())
          .isInstanceOf(WrappedException.class)
          .hasCauseInstanceOf(cause.getClass());
    }

    @Test
    void shouldConsumeUnchecked() {
        // given
        ThrowingBiConsumer<Integer, Integer, IOException> consumer = (i, j) -> {};

        // when
        ThrowingBiConsumer.unchecked(consumer).accept(3, 4);

        // then no exception thrown
    }
}
@@ -143,24 +143,70 @@
 IOException;
+%0Aimport java.util.concurrent.atomic.LongAdder;
 %0A%0Aimport sta
@@ -444,37 +444,49 @@
         
-Integer%5B%5D input = %7B0%7D
+LongAdder input = new LongAdder()
 ;%0A%0A     
@@ -554,39 +554,44 @@
  (i, j) -%3E input
-%5B0%5D = 2
+.increment()
 ;%0A%0A        // wh
@@ -657,35 +657,38 @@
 assertThat(input
-%5B0%5D
+.sum()
 ).isEqualTo(2);%0A
@@ -675,33 +675,33 @@
 um()).isEqualTo(
-2
+1
 );%0A    %7D%0A%0A    @T
@@ -778,37 +778,49 @@
         
-Integer%5B%5D input = %7B0%7D
+LongAdder input = new LongAdder()
 ;%0A%0A     
@@ -896,23 +896,28 @@
 -%3E input
-%5B0%5D = 2
+.increment()
 ;%0A      
@@ -993,15 +993,20 @@
 nput
-%5B0%5D = 3
+.increment()
 ;%0A%0A 
@@ -1106,35 +1106,38 @@
 assertThat(input
-%5B0%5D
+.sum()
 ).isEqualTo(3);%0A
@@ -1132,17 +1132,17 @@
 EqualTo(
-3
+2
 );%0A    %7D
@@ -1236,29 +1236,41 @@
     
-Integer%5B%5D input = %7B0%7D
+LongAdder input = new LongAdder()
 ;%0A%0A 
@@ -1354,15 +1354,20 @@
 nput
-%5B0%5D = 2
+.increment()
 ;%0A%0A 
@@ -1470,11 +1470,14 @@
 nput
-%5B0%5D
+.sum()
 ).is
@@ -1476,33 +1476,33 @@
 um()).isEqualTo(
-2
+1
 );%0A    %7D%0A%0A    @T

Refactor ThrowingBiConsumerTest