gherkin.ast.GherkinDocument Java Examples

The following examples show how to use gherkin.ast.GherkinDocument. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestSourcesModel.java    From extentreports-cucumber4-adapter with Apache License 2.0 6 votes vote down vote up
private void parseGherkinSource(String path) {
    if (!pathToReadEventMap.containsKey(path)) {
        return;
    }
    Parser<GherkinDocument> parser = new Parser<GherkinDocument>(new AstBuilder());
    TokenMatcher matcher = new TokenMatcher();
    try {
        GherkinDocument gherkinDocument = parser.parse(pathToReadEventMap.get(path).source, matcher);
        pathToAstMap.put(path, gherkinDocument);
        Map<Integer, AstNode> nodeMap = new HashMap<Integer, AstNode>();
        AstNode currentParent = new AstNode(gherkinDocument.getFeature(), null);
        for (ScenarioDefinition child : gherkinDocument.getFeature().getChildren()) {
            processScenarioDefinition(nodeMap, child, currentParent);
        }
        pathToNodeMap.put(path, nodeMap);
    } catch (ParserException e) {
        // Ignore exceptions
    }
}
 
Example #2
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 6 votes vote down vote up
private void parseGherkinSource(final String path) {
    if (!pathToReadEventMap.containsKey(path)) {
        return;
    }
    final Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
    final TokenMatcher matcher = new TokenMatcher();
    try {
        final GherkinDocument gherkinDocument = parser.parse(pathToReadEventMap.get(path).source, matcher);
        pathToAstMap.put(path, gherkinDocument);
        final Map<Integer, AstNode> nodeMap = new HashMap<>();
        final AstNode currentParent = new AstNode(gherkinDocument.getFeature(), null);
        for (ScenarioDefinition child : gherkinDocument.getFeature().getChildren()) {
            processScenarioDefinition(nodeMap, child, currentParent);
        }
        pathToNodeMap.put(path, nodeMap);
    } catch (ParserException e) {
        LOGGER.trace(e.getMessage(), e);
    }
}
 
Example #3
Source File: TestSourcesModel.java    From allure-java with Apache License 2.0 6 votes vote down vote up
private void parseGherkinSource(final URI path) {
    if (!pathToReadEventMap.containsKey(path)) {
        return;
    }
    final Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
    final TokenMatcher matcher = new TokenMatcher();
    try {
        final GherkinDocument gherkinDocument = parser.parse(pathToReadEventMap.get(path).getSource(),
                matcher);
        pathToAstMap.put(path, gherkinDocument);
        final Map<Integer, AstNode> nodeMap = new HashMap<>();
        final AstNode currentParent = new AstNode(gherkinDocument.getFeature(), null);
        for (ScenarioDefinition child : gherkinDocument.getFeature().getChildren()) {
            processScenarioDefinition(nodeMap, child, currentParent);
        }
        pathToNodeMap.put(path, nodeMap);
    } catch (ParserException e) {
        throw new IllegalStateException("You are using a plugin that only supports till Gherkin 5.\n"
                + "Please check if the Gherkin provided follows the standard of Gherkin 5\n", e
        );
    }
}
 
Example #4
Source File: AllureCucumber4JvmTest.java    From allure-java with Apache License 2.0 6 votes vote down vote up
private byte runFeature(final AllureResultsWriterStub writer,
                        final String featureResource,
                        final String... moreOptions) {

    final AllureLifecycle lifecycle = new AllureLifecycle(writer);
    final AllureCucumber4Jvm cucumber4Jvm = new AllureCucumber4Jvm(lifecycle);
    final ClassLoader classLoader = currentThread().getContextClassLoader();
    final ResourceLoader resourceLoader = new MultiLoader(classLoader);
    final ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
    final List<String> opts = new ArrayList<>(Arrays.asList(
            "--glue", "io.qameta.allure.cucumber4jvm.samples",
            "--plugin", "null_summary"
    ));
    opts.addAll(Arrays.asList(moreOptions));
    final RuntimeOptions options = new CommandlineOptionsParser().parse(opts).build();
    boolean mt = options.isMultiThreaded();

    FeatureSupplier featureSupplier = () -> {
        try {
            final String gherkin = readResource(featureResource);
            Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
            TokenMatcher matcher = new TokenMatcher();
            GherkinDocument gherkinDocument = parser.parse(gherkin, matcher);
            List<PickleEvent> pickleEvents = compilePickles(gherkinDocument, featureResource);
            CucumberFeature feature = new CucumberFeature(gherkinDocument, URI.create(featureResource), gherkin, pickleEvents);

            return Collections.singletonList(feature);
        } catch (IOException e) {
            return Collections.EMPTY_LIST;
        }
    };
    final Runtime runtime = Runtime.builder()
            .withResourceLoader(resourceLoader)
            .withClassFinder(classFinder)
            .withClassLoader(classLoader)
            .withRuntimeOptions(options)
            .withAdditionalPlugins(cucumber4Jvm)
            .withFeatureSupplier(featureSupplier)
            .build();

    runtime.run();
    return runtime.exitStatus();
}
 
Example #5
Source File: CucumberSourceUtils.java    From allure-java with Apache License 2.0 6 votes vote down vote up
private void parseGherkinSource(final String path) {
    if (!pathToReadEventMap.containsKey(path)) {
        return;
    }
    final Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
    final TokenMatcher matcher = new TokenMatcher();
    try {
        final GherkinDocument gherkinDocument = parser.parse(pathToReadEventMap.get(path).source, matcher);
        pathToAstMap.put(path, gherkinDocument);
        final Map<Integer, AstNode> nodeMap = new HashMap<>();
        final AstNode currentParent = new AstNode(gherkinDocument.getFeature(), null);
        for (ScenarioDefinition child : gherkinDocument.getFeature().getChildren()) {
            processScenarioDefinition(nodeMap, child, currentParent);
        }
        pathToNodeMap.put(path, nodeMap);
    } catch (ParserException e) {
        LOGGER.trace(e.getMessage(), e);
    }
}
 
Example #6
Source File: AllureCucumber2JvmTest.java    From allure-java with Apache License 2.0 5 votes vote down vote up
private AllureResults runFeature(final String featureResource,
                                 final String... moreOptions) {
    final AllureResultsWriterStub writer = new AllureResultsWriterStub();
    final AllureLifecycle lifecycle = new AllureLifecycle(writer);
    final AllureCucumber2Jvm cucumber2Jvm = new AllureCucumber2Jvm(lifecycle);
    final ClassLoader classLoader = currentThread().getContextClassLoader();
    final ResourceLoader resourceLoader = new MultiLoader(classLoader);
    final ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
    final List<String> opts = new ArrayList<>(Arrays.asList(
            "--glue", "io.qameta.allure.cucumber2jvm.samples",
            "--plugin", "null"
    ));
    opts.addAll(Arrays.asList(moreOptions));
    final RuntimeOptions options = new RuntimeOptions(opts);
    final Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, options);

    options.addPlugin(cucumber2Jvm);

    final String gherkin = readResource(featureResource);
    Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
    TokenMatcher matcher = new TokenMatcher();
    GherkinDocument gherkinDocument = parser.parse(gherkin, matcher);
    CucumberFeature feature = new CucumberFeature(gherkinDocument, featureResource, gherkin);

    feature.sendTestSourceRead(runtime.getEventBus());
    runtime.runFeature(feature);
    return writer;
}
 
Example #7
Source File: AllureCucumber3JvmTest.java    From allure-java with Apache License 2.0 5 votes vote down vote up
private AllureResults runFeature(final String featureResource,
                                 final String... moreOptions) {
    final AllureResultsWriterStub writer = new AllureResultsWriterStub();
    final AllureLifecycle lifecycle = new AllureLifecycle(writer);
    final AllureCucumber3Jvm cucumber3Jvm = new AllureCucumber3Jvm(lifecycle);
    final ClassLoader classLoader = currentThread().getContextClassLoader();
    final ResourceLoader resourceLoader = new MultiLoader(classLoader);
    final ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
    final List<String> opts = new ArrayList<>(Arrays.asList(
            "--glue", "io.qameta.allure.cucumber3jvm.samples",
            "--plugin", "null"
    ));
    opts.addAll(Arrays.asList(moreOptions));
    final RuntimeOptions options = new RuntimeOptions(opts);
    final Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, options);

    options.addPlugin(cucumber3Jvm);
    options.noSummaryPrinter();

    final String gherkin = readResource(featureResource);
    Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
    TokenMatcher matcher = new TokenMatcher();
    GherkinDocument gherkinDocument = parser.parse(gherkin, matcher);
    CucumberFeature feature = new CucumberFeature(gherkinDocument, featureResource, gherkin);

    feature.sendTestSourceRead(runtime.getEventBus());
    runtime.runFeature(feature);
    return writer;
}
 
Example #8
Source File: CucumberITGeneratorByScenario.java    From cucumber-jvm-parallel-plugin with Apache License 2.0 5 votes vote down vote up
private ScenarioDefinition findScenarioDefinitionViaLocation(Location location, GherkinDocument gherkinDocument) {
    List<ScenarioDefinition> scenarioDefinitions = gherkinDocument.getFeature().getChildren();
    for (ScenarioDefinition definition : scenarioDefinitions) {
        if (isLocationSame(definition.getLocation(), location)) {
            return definition;
        }
    }
    return null;
}
 
Example #9
Source File: AllureCucumber4JvmTest.java    From allure-java with Apache License 2.0 4 votes vote down vote up
private static List<PickleEvent> compilePickles(GherkinDocument gherkinDocument, String resource) {
    if (gherkinDocument.getFeature() == null) {
        return Collections.emptyList();
    }
    List<PickleEvent> pickleEvents = new ArrayList<>();
    for (Pickle pickle : new Compiler().compile(gherkinDocument)) {
        pickleEvents.add(new PickleEvent(resource, pickle));
    }
    return pickleEvents;
}
 
Example #10
Source File: CucumberITGeneratorByFeature.java    From cucumber-jvm-parallel-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a single Cucumber runner for each separate feature file.
 *
 * @param outputDirectory the output directory to place generated files
 * @param featureFiles    The feature files to create runners for
 * @throws MojoExecutionException if something goes wrong
 */
public void generateCucumberITFiles(final File outputDirectory,
                                    final Collection<File> featureFiles) throws MojoExecutionException {
    Parser<GherkinDocument> parser = new Parser<GherkinDocument>(new AstBuilder());
    TagPredicate tagPredicate = new TagPredicate(overriddenParameters.getTags());

    TokenMatcher matcher = new TokenMatcher();

    for (final File file : featureFiles) {
        GherkinDocument gherkinDocument = null;
        final List<Pickle> acceptedPickles = new ArrayList<Pickle>();
        try {
            String source = FileUtils.readFileToString(file);
            gherkinDocument = parser.parse(source, matcher);
            Compiler compiler = new Compiler();
            List<Pickle> pickles = compiler.compile(gherkinDocument);

            for (Pickle pickle : pickles) {
                if (tagPredicate.apply(pickle.getTags())) {
                    acceptedPickles.add(pickle);
                    continue;
                }
            }

        } catch (final IOException e) {
            // should never happen
            // TODO - proper logging
            System.out.println(format("WARNING: Failed to parse '%s'...IGNORING",
                    file.getName()));
        }

        if (acceptedPickles.isEmpty()) {
            continue;
        }

        outputFileName = classNamingScheme.generate(file.getName());
        setFeatureFileLocation(file);
        setParsedFeature(gherkinDocument.getFeature());
        writeFile(outputDirectory);

    }
}