package cucumber.runtime.java;

import java.util.List;
import java.util.regex.Pattern;

import cucumber.runtime.CucumberException;
import org.junit.Assert;
import org.junit.Test;

import cucumber.api.java8.StepdefBody;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class Java8StepDefinitionTest {

    private Java8StepDefinition java8StepDefinition;

    @Test
    public void should_calculate_parameters_count_from_body_with_one_param() throws Exception {
        java8StepDefinition = new Java8StepDefinition(Pattern.compile("^I have (\\d) some step (.*)$"), 0, oneParamStep(), null);
        assertEquals(new Integer(1), java8StepDefinition.getParameterCount());
    }

    @Test
    public void should_calculate_parameters_count_from_body_with_two_params() throws Exception {
        java8StepDefinition = new Java8StepDefinition(Pattern.compile("^I have some step $"), 0, twoParamStep(), null);
        assertEquals(new Integer(2), java8StepDefinition.getParameterCount());
    }

    @Test
    public void should_fail_for_param_with_generic_list() throws Exception {
        try {
            new Java8StepDefinition(Pattern.compile("^I have (\\d) some step (.*)$"), 0, genericListStep(), null);
            fail();
        } catch(CucumberException expected) {
            assertEquals("Can't use java.util.List in lambda step definition. Declare a DataTable argument instead and convert manually with asList/asLists/asMap/asMaps", expected.getMessage());
        }
    }

    private StepdefBody oneParamStep() {
        return new StepdefBody.A1<String>() {
            @Override
            public void accept(String p1) {
            }
        };
    }

    private StepdefBody twoParamStep() {
        return new StepdefBody.A2<String, String>() {
            @Override
            public void accept(String p1, String p2) {
            }
        };
    }

    private StepdefBody genericListStep() {
        return new StepdefBody.A1<List>() {
            @Override
            public void accept(List p1) {
                throw new UnsupportedOperationException();
            }
        };
    }
}

@@ -36,56 +36,39 @@
 ort 
-java.util.List;%0Aimport java.util.regex.Pattern;%0A
+cucumber.api.java8.StepdefBody;
 %0Aimp
@@ -128,77 +128,68 @@
 nit.
-Asser
+Tes
 t;%0A
+%0A
 import 
-org.junit.Te
+java.util.Li
 st;%0A
-%0A
 import 
-cucumber.api.java8.StepdefBody
+java.util.regex.Pattern
 ;%0A%0Ai
@@ -314,62 +314,8 @@
  %7B%0A%0A
-    private Java8StepDefinition java8StepDefinition;%0A%0A
     
@@ -416,32 +416,52 @@
 ption %7B%0A        
+Java8StepDefinition 
 java8StepDefinit
@@ -767,16 +767,36 @@
         
+Java8StepDefinition 
 java8Ste
@@ -1038,16 +1038,20 @@
 am_with_
+non_
 generic_
@@ -1179,17 +1179,20 @@
 $%22), 0, 
-g
+nonG
 enericLi
@@ -1243,16 +1243,17 @@
  %7D catch
+ 
 (Cucumbe
@@ -1479,32 +1479,358 @@
        %7D%0A    %7D%0A%0A
+    @Test%0A    public void should_pass_for_param_with_generic_list() throws Exception %7B%0A        Java8StepDefinition java8StepDefinition = new Java8StepDefinition(Pattern.compile(%22%5EI have (%5C%5Cd) some step (.*)$%22), 0, genericListStep(), null);%0A        assertEquals(new Integer(1), java8StepDefinition.getParameterCount());%0A    %7D%0A%0A
     private Step
@@ -2238,32 +2238,294 @@
 ricListStep() %7B%0A
+        return new StepdefBody.A1%3CList%3CString%3E%3E() %7B%0A            @Override%0A            public void accept(List%3CString%3E p1) %7B%0A                throw new UnsupportedOperationException();%0A            %7D%0A        %7D;%0A    %7D%0A%0A    private StepdefBody nonGenericListStep() %7B%0A
         return n

Improve Java8 stepdef tests