Search in sources :

Example 1 with FeatureParserException

use of io.cucumber.core.gherkin.FeatureParserException in project cucumber-jvm by cucumber.

the class CucumberTest method testThatParsingErrorsIsNicelyReported.

@Test
void testThatParsingErrorsIsNicelyReported() {
    Executable testMethod = () -> new Cucumber(LexerErrorFeature.class);
    FeatureParserException actualThrown = assertThrows(FeatureParserException.class, testMethod);
    assertThat(actualThrown.getMessage(), equalTo("" + "Failed to parse resource at: classpath:io/cucumber/error/lexer_error.feature\n" + "(1:1): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Feature  FA'\n" + "(3:3): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Scenario SA'\n" + "(4:5): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Given GA'\n" + "(5:5): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'When GA'\n" + "(6:5): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Then TA'"));
}
Also used : FeatureParserException(io.cucumber.core.gherkin.FeatureParserException) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 2 with FeatureParserException

use of io.cucumber.core.gherkin.FeatureParserException in project cucumber-jvm by cucumber.

the class GherkinMessagesFeatureParser method parse.

@Override
public Optional<Feature> parse(URI path, String source, Supplier<UUID> idGenerator) {
    List<Envelope> sources = singletonList(makeSourceEnvelope(source, path.toString()));
    List<Envelope> envelopes = Gherkin.fromSources(sources, true, true, true, () -> idGenerator.get().toString()).collect(toList());
    GherkinDocument gherkinDocument = envelopes.stream().map(Envelope::getGherkinDocument).filter(Objects::nonNull).findFirst().orElse(null);
    if (gherkinDocument == null || gherkinDocument.getFeature() == null) {
        List<String> errors = envelopes.stream().map(Envelope::getParseError).filter(Objects::nonNull).map(ParseError::getMessage).collect(toList());
        if (!errors.isEmpty()) {
            throw new FeatureParserException("Failed to parse resource at: " + path + "\n" + String.join("\n", errors));
        }
        return Optional.empty();
    }
    CucumberQuery cucumberQuery = new CucumberQuery();
    cucumberQuery.update(gherkinDocument);
    GherkinDialectProvider dialectProvider = new GherkinDialectProvider();
    io.cucumber.messages.types.Feature feature = gherkinDocument.getFeature();
    String language = feature.getLanguage();
    GherkinDialect dialect = dialectProvider.getDialect(language, null);
    List<io.cucumber.messages.types.Pickle> pickleMessages = envelopes.stream().map(Envelope::getPickle).filter(Objects::nonNull).collect(toList());
    List<Pickle> pickles = pickleMessages.stream().map(pickle -> new GherkinMessagesPickle(pickle, path, dialect, cucumberQuery)).collect(toList());
    GherkinMessagesFeature messagesFeature = new GherkinMessagesFeature(feature, path, source, pickles, envelopes);
    return Optional.of(messagesFeature);
}
Also used : Pickle(io.cucumber.core.gherkin.Pickle) FeatureParser(io.cucumber.core.gherkin.FeatureParser) GherkinDialect(io.cucumber.gherkin.GherkinDialect) Envelope(io.cucumber.messages.types.Envelope) Gherkin(io.cucumber.gherkin.Gherkin) UUID(java.util.UUID) Gherkin.makeSourceEnvelope(io.cucumber.gherkin.Gherkin.makeSourceEnvelope) Supplier(java.util.function.Supplier) Collections.singletonList(java.util.Collections.singletonList) Objects(java.util.Objects) GherkinDocument(io.cucumber.messages.types.GherkinDocument) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Feature(io.cucumber.core.gherkin.Feature) FeatureParserException(io.cucumber.core.gherkin.FeatureParserException) Optional(java.util.Optional) ParseError(io.cucumber.messages.types.ParseError) URI(java.net.URI) GherkinDialectProvider(io.cucumber.gherkin.GherkinDialectProvider) Pickle(io.cucumber.core.gherkin.Pickle) FeatureParserException(io.cucumber.core.gherkin.FeatureParserException) GherkinDialectProvider(io.cucumber.gherkin.GherkinDialectProvider) GherkinDialect(io.cucumber.gherkin.GherkinDialect) GherkinDocument(io.cucumber.messages.types.GherkinDocument) Envelope(io.cucumber.messages.types.Envelope) Gherkin.makeSourceEnvelope(io.cucumber.gherkin.Gherkin.makeSourceEnvelope) Objects(java.util.Objects)

Example 3 with FeatureParserException

use of io.cucumber.core.gherkin.FeatureParserException in project cucumber-jvm by cucumber.

the class FeatureParserTest method lexer_error_throws_exception.

@Test
void lexer_error_throws_exception() throws IOException {
    URI uri = URI.create("classpath:com/example.feature");
    String source = new String(readAllBytes(Paths.get("src/test/resources/io/cucumber/core/gherkin/messages/lexer-error.feature")));
    FeatureParserException exception = assertThrows(FeatureParserException.class, () -> parser.parse(uri, source, UUID::randomUUID));
    assertEquals("" + "Failed to parse resource at: classpath:com/example.feature\n" + "(1:1): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Feature  FA'\n" + "(3:3): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Scenario SA'\n" + "(4:5): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Given GA'\n" + "(5:5): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'When GA'\n" + "(6:5): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Then TA'", exception.getMessage());
}
Also used : FeatureParserException(io.cucumber.core.gherkin.FeatureParserException) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

FeatureParserException (io.cucumber.core.gherkin.FeatureParserException)3 URI (java.net.URI)2 Test (org.junit.jupiter.api.Test)2 Feature (io.cucumber.core.gherkin.Feature)1 FeatureParser (io.cucumber.core.gherkin.FeatureParser)1 Pickle (io.cucumber.core.gherkin.Pickle)1 Gherkin (io.cucumber.gherkin.Gherkin)1 Gherkin.makeSourceEnvelope (io.cucumber.gherkin.Gherkin.makeSourceEnvelope)1 GherkinDialect (io.cucumber.gherkin.GherkinDialect)1 GherkinDialectProvider (io.cucumber.gherkin.GherkinDialectProvider)1 Envelope (io.cucumber.messages.types.Envelope)1 GherkinDocument (io.cucumber.messages.types.GherkinDocument)1 ParseError (io.cucumber.messages.types.ParseError)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Supplier (java.util.function.Supplier)1 Collectors.toList (java.util.stream.Collectors.toList)1