package es.sandbox.spikes.java8.lambda;

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

import java.util.Arrays;
import java.util.List;

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

/**
 * Created by jeslopalo on 29/12/15.
 */

public class LambdaSpecs {

    private static final String[] ARRAY_OF_STRINGS = new String[]{"a", "b", "c"};

    private StringBuilder result;

    @Before
    public void setup() {
        this.result = new StringBuilder();
    }

    private void assertThatTheResultIs(String expected) {
        assertThat(this.result.toString()).isEqualTo(expected);
    }

    @Test
    public void the_arrow_is_the_body_separator() {
        Arrays.asList(ARRAY_OF_STRINGS)
                .forEach((String e) -> this.result.append(e));

        assertThatTheResultIs("abc");
    }

    @Test
    public void the_argument_type_can_be_inferred() {
        Arrays.asList(ARRAY_OF_STRINGS)
                .forEach(e -> this.result.append(e));

        assertThatTheResultIs("abc");
    }

    @Test
    public void complex_bodies_can_be_wrapped_into_curly_brackets() {
        Arrays.asList(ARRAY_OF_STRINGS)
                .forEach(e -> {
                    final String upper = e.toUpperCase();
                    this.result.append(upper);
                });

        assertThatTheResultIs("ABC");
    }

    @Test
    public void it_may_reference_to_local_variables() {
        final String separator = ",";

        Arrays.asList(ARRAY_OF_STRINGS)
                .forEach(e -> this.result.append(e).append(separator));

        assertThatTheResultIs("a,b,c,");
    }

    @Test
    public void it_may_return_a_value() {
        final List<String> elements = Arrays.asList("c", "a", "b");

        elements.sort((e1, e2) -> {
            int result = e1.compareTo(e2);
            return result;
        });

        assertThat(elements).containsExactly("a", "b", "c");
    }

    @Test
    public void return_statements_is_not_required_if_body_is_oneliner() {
        final List<String> elements = Arrays.asList("c", "a", "b");

        elements.sort((e1, e2) -> e1.compareTo(e2));

        assertThat(elements).containsExactly("a", "b", "c");
    }
}

@@ -79,24 +79,52 @@
 unit.Test;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -388,29 +388,28 @@
 private 
+List%3C
 String
-Builder
+%3E
  result;
@@ -478,151 +478,20 @@
 new 
-StringBuilder();%0A    %7D%0A%0A    private void assertThatTheResultIs(String expected) %7B%0A        assertThat(this.result.toString()).isEqualTo(expected
+ArrayList%3C%3E(
 );%0A 
@@ -488,32 +488,33 @@
 ist%3C%3E();%0A    %7D%0A%0A
+%0A
     @Test%0A    pu
@@ -643,36 +643,33 @@
 -%3E this.result.a
-ppen
+d
 d(e));%0A%0A        
@@ -674,39 +674,65 @@
   assertThat
-TheResultIs(%22ab
+(this.result).containsExactly(%22a%22, %22b%22, %22
 c%22);%0A    %7D%0A%0A
@@ -874,28 +874,25 @@
 his.result.a
-ppen
+d
 d(e));%0A%0A    
@@ -909,23 +909,49 @@
 That
-TheResultIs(%22ab
+(this.result).containsExactly(%22a%22, %22b%22, %22
 c%22);
@@ -1201,20 +1201,17 @@
 result.a
-ppen
+d
 d(upper)
@@ -1255,23 +1255,49 @@
 That
-TheResultIs(%22AB
+(this.result).containsExactly(%22A%22, %22B%22, %22
 C%22);
@@ -1395,22 +1395,21 @@
 ing 
-separator
+addition
  = %22
-,
++
 %22;%0A%0A
@@ -1495,75 +1495,91 @@
 lt.a
-ppend(e).append(separator));%0A%0A        assertThatTheResultIs(%22a,b,c,
+dd(e + addition));%0A%0A        assertThat(this.result).containsExactly(%22a+%22, %22b+%22, %22c+
 %22);%0A

Refactor to simplify lambda's test