greghaskins / spectrum

A BDD-style test runner for Java 8. Inspired by Jasmine, RSpec, and Cucumber.
MIT License
145 stars 23 forks source link

how to create a collection types in example #116

Closed braghome closed 7 years ago

braghome commented 7 years ago

on my sample test, I see a compilation error on my IDE

import org.junit.runner.RunWith;

import com.greghaskins.spectrum.Spectrum;

import static com.greghaskins.spectrum.dsl.gherkin.Gherkin.feature;

import static com.greghaskins.spectrum.dsl.gherkin.Gherkin.scenarioOutline;

@RunWith(Spectrum.class)
public class MaxPairwiseJavaTest {
    {
        feature("find the max pair wise product", () -> {
            scenarioOutline("intialise evaluation with",
                    (inputCount, imputSample, resultOutput) -> {

                    },
                    withExamples(example(4, { 3, 4, 81, 1 }, 324L)));
        });
    }
}
braghome commented 7 years ago

Maybe I should further explain that withExamples function does not allow one to pass in a list of any type (primitive or regular Java collection)

greghaskins commented 7 years ago

@braghome Thanks for reaching out!

It looks like you might be missing a new int[] in your example, e.g. new int[]{ 3, 4, 81, 1}. Alternatively, you can use Arrays.asList(3, 4, 81, 1) or a similar collection factory method. The literal { 3, 4, 81, 1}, unfortunately, isn't valid Java 8 syntax (I wish it was!).

This works for me:

    feature("find the max pair wise product", () -> {
      scenarioOutline("intialise evaluation with",
          (inputSample, resultOutput) -> {

            given("some integers: " + asList(inputSample), () -> {

            });

            when ("computing the max pairwise product", () -> {

            });

            then("the result should be " + resultOutput, () -> {

            });

          },
          withExamples(
              example(new int[]{ 3, 4, 81, 1 }, 324L),
              example(new int[]{ 5, 7, 2, 8 }, 56L)
          )
      );
    });
braghome commented 7 years ago

@greghaskins sorry for the confusion as I was thinking of something different


public class AppTest {

    /**
     * @return the suite of tests being tested
     */
    public static int[] intdata() {
        final int[] start = { 3, 24, 12 };
        return start;
    }

    public static void main(final String[] args) {
        if (intdata()[0] == 3 && intdata()[1] == 24 && intdata()[2] == 12) {
            System.out.printf("the array values are: %d, %d, %d", intdata()[0], intdata()[1], intdata()[2]);
        } else {
            throw new AssertionError("array was null");
        }
    }
}