package cucumber.runtime.junit;

import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.platform.engine.TestDescriptor;

import static cucumber.runtime.junit.CucumberFeatureMother.anyFeatureFile;
import static cucumber.runtime.junit.UniqueIdMatcher.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.platform.engine.UniqueId.forEngine;

public class CucumberEngine_IdAssignmentTest implements CucumberEngineTestSugar {

    private final CucumberEngineFixture fixture = new CucumberEngineFixture();
    private final CucumberFeatureBuilder cucumberFeatureBuilder = anyFeatureFile();

    @After
    public void allTestDescriptorsAreInAProperState() throws Exception {
        fixture.engineExecutionListener.ensureAllInProperEndState();
    }

    @Test
    public void topLevelCucumberEngine() throws Exception {
        assertThat(engineDescriptor().getUniqueId(), equalTo(forEngine("cucumber-jvm")));
    }

    @Test
    public void pathToFeatureFile() throws Exception {
        cucumberFeatureBuilder.Feature("first line of feature description\nextended description");

        assertThat(featureDescriptor().getUniqueId(), endsWith("feature", "first-line-of-feature-description"));
    }

    @Test
    public void scenarioId() throws Exception {
        cucumberFeatureBuilder.Scenario("first line of the scenario\nextended description");

        assertThat(scenarioDescriptor().getUniqueId(), endsWith("scenario", "feature-name;first-line-of-the-scenario"));
    }

    @Test
    public void scenarioStepId() throws Exception {
        cucumberFeatureBuilder.Scenario("first line of the scenario\nextended description").AStep("step text");

        assertThat(scenarioDescriptor().getChildren().iterator().next().getUniqueId(), endsWith("step", "step text"));
    }

    @Test
    public void scenarioOutlineId() throws Exception {
        cucumberFeatureBuilder.ScenarioOutline("first line of the scenario outline\nextended description");

        assertThat(scenarioOutlineDescriptor().getUniqueId(), endsWith("scenario-outline", "feature-name;first-line-of-the-scenario-outline"));
    }

    @Test
    @Ignore
    public void name() throws Exception {
        CucumberEngineDescriptor engineDescriptor = discoveredDescriptorsFor(
                anyFeatureFile().ScenarioOutline("text take\nExtended discription")
                        .Given("<arg1> <arg2>")
                        .Example("one", "two")
                        .Example("A", "B"));

        System.out.println(engineDescriptor);
    }

    private TestDescriptor scenarioOutlineDescriptor() {
        return featureDescriptor().getChildren().iterator().next();
    }

    private TestDescriptor scenarioDescriptor() {
        return featureDescriptor().getChildren().iterator().next();
    }

    private TestDescriptor featureDescriptor() {
        return engineDescriptor().getChildren().iterator().next();
    }

    private CucumberEngineDescriptor engineDescriptor() {
        return discoveredDescriptorsFor(cucumberFeatureBuilder);
    }

    @Override
    public CucumberEngineFixture fixture() {
        return fixture;
    }
}
@@ -54,33 +54,8 @@
 er;%0A
-import org.junit.Ignore;%0A
 impo
@@ -627,38 +627,23 @@
 Builder 
-cucumberFeatureBuilder
+feature
  = anyFe
@@ -1056,38 +1056,23 @@
         
-cucumberFeatureBuilder
+feature
 .Feature
@@ -1319,38 +1319,23 @@
         
-cucumberFeatureBuilder
+feature
 .Scenari
@@ -1588,38 +1588,23 @@
         
-cucumberFeatureBuilder
+feature
 .Scenari
@@ -1877,38 +1877,23 @@
         
-cucumberFeatureBuilder
+feature
 .Scenari
@@ -2132,226 +2132,115 @@
     
-@Ignore%0A    public void name() throws Exception %7B%0A        CucumberEngineDescriptor engineDescriptor = discoveredDescriptorsFor(%0A                anyFeatureFile().ScenarioOutline(%22text take%5CnExtended discription%22)%0A      
+public void scenarioInScenarioOutlineId() throws Exception %7B%0A        feature.ScenarioOutline(%22first-line%22)%0A
     
@@ -2255,16 +2255,14 @@
     
-  .Given
+.AStep
 (%22%3Ca
@@ -2287,32 +2287,24 @@
             
-   
-     
 .Example(%22on
@@ -2317,98 +2317,263 @@
 wo%22)
-%0A                        .Example(%22A%22, %22B
+;%0A%0A        assertThat(scenarioInScenarioOutline().getUniqueId(), endsWith(%22scenario%22, %22feature-name;first-line;;2
 %22));%0A
-%0A
     
+%7D%0A%0A
     
-System.out.println(engineDescriptor
+private TestDescriptor scenarioInScenarioOutline() %7B%0A        return scenarioOutlineDescriptor().getChildren().iterator().next(
 );%0A 
@@ -3060,30 +3060,15 @@
 For(
-cucumberFeatureBuilder
+feature
 );%0A 

test for scenario in scenario outline