/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.spockframework.runtime.extension.builtin;

import org.spockframework.runtime.AbstractRunListener;
import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension;
import org.spockframework.runtime.model.*;

import java.lang.annotation.Annotation;
import java.util.List;

public class StepwiseExtension extends AbstractAnnotationDrivenExtension {
  @Override
  public void visitSpecAnnotation(Annotation annotation, final SpecInfo spec) {
    sortFeaturesInDeclarationOrder(spec);
    includeFeaturesBeforeLastIncludedFeature(spec);
    skipFeaturesAfterFirstFailingFeature(spec);
  }

  private void sortFeaturesInDeclarationOrder(SpecInfo spec) {
    for (FeatureInfo feature : spec.getFeatures())
      feature.setExecutionOrder(feature.getDeclarationOrder());
  }

  private void includeFeaturesBeforeLastIncludedFeature(SpecInfo spec) {
    List<FeatureInfo> features = spec.getFeatures();
    boolean includeRemaining = false;

    for (int i = features.size() - 1; i >= 0; i--) {
      FeatureInfo feature = features.get(i);
      if (includeRemaining) feature.setExcluded(false);
      else if (!feature.isExcluded()) includeRemaining = true;
    }
  }

  private void skipFeaturesAfterFirstFailingFeature(final SpecInfo spec) {
    spec.getBottomSpec().addListener(new AbstractRunListener() {
      @Override
      public void error(ErrorInfo error) {
        // @Stepwise only affects class that carries the annotation,
        // but not sub- and super classes
        if (!error.getMethod().getParent().equals(spec)) return;

        // we can just set skip flag on all features, even though
        // some of them might already have run
        for (FeatureInfo feature : spec.getFeatures())
          feature.setSkipped(true);
      }
    });
  }
}

@@ -2181,164 +2181,264 @@
  // 
-we can just set skip flag on all features, even though%0A        // some of them might already have run%0A        for (FeatureInfo feature : spec.getFeatures())
+mark all subsequent features as skipped%0A        List%3CFeatureInfo%3E features = spec.getFeatures();%0A        int indexOfFailedFeature = features.indexOf(error.getMethod().getFeature());%0A        for (int i = indexOfFailedFeature + 1; i %3C features.size(); i++) %7B
 %0A   
@@ -2447,24 +2447,32 @@
      feature
+s.get(i)
 .setSkipped(
@@ -2474,24 +2474,34 @@
 pped(true);%0A
+        %7D%0A
       %7D%0A    

Mark only subsequent features as skipped in case of failure (#893)