package cucumber.junit;

import cucumber.runtime.PendingException;
import gherkin.formatter.Formatter;
import gherkin.formatter.Reporter;
import gherkin.formatter.model.*;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;

import java.util.ArrayList;
import java.util.List;

class JUnitReporter implements Reporter, Formatter {
    private final List<Step> steps = new ArrayList<Step>();

    private final Reporter reporter;
    private final Formatter formatter;

    private EachTestNotifier eachTestNotifier;
    private ExecutionUnitRunner executionUnitRunner;
    private RunNotifier notifier;

    public JUnitReporter(Reporter reporter, Formatter formatter) {
        this.reporter = reporter;
        this.formatter = formatter;
    }

    public void match(Match match) {
        Description description = executionUnitRunner.describeChild(steps.remove(0));
        eachTestNotifier = new EachTestNotifier(notifier, description);
        eachTestNotifier.fireTestStarted();
        reporter.match(match);
    }

    public void embedding(String mimeType, byte[] data) {
        reporter.embedding(mimeType, data);
    }

    public void result(Result result) {
        Throwable error = result.getError();
        if (Result.SKIPPED == result || Result.UNDEFINED == result || error instanceof PendingException) {
            eachTestNotifier.fireTestIgnored();
        } else {
            if (error != null) {
                eachTestNotifier.addFailure(error);
            }
        }
        eachTestNotifier.fireTestFinished();
        reporter.result(result);
    }

    @Override
    public void uri(String uri) {
        formatter.uri(uri);
    }

    @Override
    public void feature(gherkin.formatter.model.Feature feature) {
        formatter.feature(feature);
    }

    @Override
    public void background(Background background) {
        formatter.background(background);
    }

    @Override
    public void scenario(Scenario scenario) {
        formatter.scenario(scenario);
    }

    @Override
    public void scenarioOutline(ScenarioOutline scenarioOutline) {
        formatter.scenarioOutline(scenarioOutline);
    }

    @Override
    public void examples(Examples examples) {
        formatter.examples(examples);
    }

    @Override
    public void step(Step step) {
        steps.add(step);
        formatter.step(step);
    }

    @Override
    public void eof() {
        formatter.eof();
    }

    @Override
    public void syntaxError(String state, String event, List<String> legalEvents, String uri, int line) {
        formatter.syntaxError(state, event, legalEvents, uri, line);
    }

    public void setStepParentRunner(ExecutionUnitRunner executionUnitRunner, RunNotifier notifier) {
        this.executionUnitRunner = executionUnitRunner;
        this.notifier = notifier;
    }

    public Formatter getFormatter() {
        return formatter;
    }

    public Reporter getReporter() {
        return reporter;
    }
}

@@ -583,24 +583,20 @@
 otifier 
-eachTe
 st
+ep
 Notifier
@@ -956,32 +956,28 @@
 ));%0A        
-eachTe
 st
+ep
 Notifier = n
@@ -1024,32 +1024,28 @@
 n);%0A        
-eachTe
 st
+ep
 Notifier.fir
@@ -1407,32 +1407,28 @@
             
-eachTe
 st
+ep
 Notifier.fir
@@ -1505,32 +1505,28 @@
             
-eachTe
 st
+ep
 Notifier.add
@@ -1577,16 +1577,12 @@
     
-eachTe
 st
+ep
 Noti

Rename variable