Use Gherkin for Java
mainGherkinParserTest class in the source repository.repository·main·Indexed 18 days ago
https://github.com/cucumber/gherkinA parser and compiler for the Gherkin language that transforms feature files into Abstract Syntax Trees (AST) and Pickles. It is available as a library for multiple languages including C, C++, Dart, .NET, Elixir, Go, Java, JavaScript, Perl, PHP, and Python, and supports output via the Cucumber Messages protocol.
GherkinParserTest class in the source repository.@cucumber/gherkin package provides a parser and compiler for Gherkin language files in JavaScript environments. It allows you to parse Gherkin feature files into an Abstract Syntax Tree (AST) or into 'Pickles' (an intermediate representation used for execution).Markdown with Gherkin (MDG) is a dialect of Markdown that is a strict superset of GitHub Flavored Markdown (GFM). It allows you to embed Gherkin scenarios directly within Markdown documents, enabling the creation of rich, living documentation that can be rendered by any GFM-compliant tool.
Requirements:
.feature.md extension.@cucumber/gherkin (JavaScript) implementation, version 19.0.0 and above.MDG is parsed using the same Parser class as Gherkin Classic, but utilizes a GherkinInMarkdownTokenMatcher.
Key Parsing Behaviors:
GherkinInMarkdownTokenMatcher treats all lines not recognized as special Gherkin tokens as Empty. This allows Markdown prose to exist alongside Gherkin without causing errors.Empty, the resulting GherkinDocument AST will have empty description properties. Consequently, the JSON formatter will not include a description property for scenarios.Gherkin processing follows a three-stage pipeline:
.feature file) and produces a stream of Tokens. If the scanner encounters a #language header, it dynamically reconfigures itself to use keywords defined in gherkin-languages.json for that specific language.The AST is a tree of simple data objects representing the Gherkin document. Every node in the AST includes a Location (1-indexed line and column) indicating its position in the source file.
Key AST Nodes and Fields:
Feature, optional Comments, and a language.name, description, keyword, tags, Background, Rules, and ScenarioDefinitions.name, description, keyword, tags, and ScenarioDefinitions.Scenario and ScenarioOutline): Contains keyword, name, description, tags, and steps.Examples.keyword, name, description, tags, a header (TableRow), and multiple rows (TableRows).keyword, text, and optionally one DataTable and one DocString.rows (TableRows).cells (TableCells).value.content and contentType.name.text.Note: All fields are strings except for Location.line and Location.column. In JSON representations, every node includes a type property matching its node type.
IncrementingIdGenerator which generates IDs as strings like "1", "2", etc. If your application requires UUIDs, you must provide your own implementation of the id_generator.h interface that generates UUID strings.Pickles are a simplified, flattened version of the Gherkin AST. While the AST represents the full structure of the document, Pickles are designed for execution by Cucumber.
Compilation Logic:
Scenario is compiled into a Pickle.Examples row in a Scenario Outline is compiled into a Pickle.Background steps are compiled into a Pickle.Tag (e.g., @a) is compiled into a Pickle, inheriting tags from its parent elements.Benefits:
Pickle contains the path to the original source file, enabling accurate reporting and stack traces during failures.If Gherkin is installed on your system, you can use find_package to integrate it into your C++ project.
cmake_minimum_required(VERSION 3.0)
project(gherkincsample)
list(APPEND CMAKE_PREFIX_PATH "INSTALLATION_DIRECTORY")
set(CMAKE_CXX_STANDARD 11)
find_package(gherkin REQUIRED)
add_executable(gherkincsample main.cpp)
target_link_libraries(gherkincsample gherkin::gherkin)cmake_minimum_required(VERSION 3.0)
project(gherkincsample)
list(APPEND CMAKE_PREFIX_PATH "INSTALLATION_DIRECTORY")
set(CMAKE_CXX_STANDARD 11)
find_package(gherkin REQUIRED)
add_executable(gherkincsample main.cpp)
target_link_libraries(gherkincsample gherkin::gherkin)