Git Product home page Git Product logo

gherkin-utils's Introduction

gherkin-utils

This library is a set of utilities to work with Gherkin documents and AST.

Command line

The command-line tool can be used to format .feature files or translate .feature files into .feature.md files.

Example usage:

# Translate all `.feature` files to `.feature.md` files and delete the `.feature` files.
# See https://github.com/cucumber/common/blob/main/gherkin/MARKDOWN_WITH_GHERKIN.md
# Note that the globs must be quoted to prevent the shell from expanding the globs.
npx @cucumber/gherkin-utils format --move "features/**/*.feature" "features/**/*.feature.md"

More details:

npx @cucumber/gherkin-utils --help

As a library

This module can also be used as a library. It provides two main utilities, pretty and gherkinDocumentWalker.

pretty(gherkinDocument: messages.GherkinDocument, syntax: 'gherkin' | 'markdown')

This function takes a GherkinDocument as input and returns a pretty-printed representation in Gherkin or Markdown.

GherkinDocumentWalker class

The GherkinDocumentWalker is a class for walking and filtering the AST produced by Gherkin after parsing a feature file. When running walkGherkinDocument on a GherkinDocument, it will produce a deep copy of the object.

It takes two arguments upon creation:

  • filters: set of functions used to know if the walked elements are kept in the result. By default, all elements are kept.
  • handlers: set of function that can be used to alter the produced elements.

Filtering keeps the meaning of the original GherkinDocument, which means:

  • if a Background was present, it will always be in the Feature (or Rule)
  • the kept scenarios will have the same steps and examples than the original

By default, all elements are accepted, which means that if you want to do filtering you should reject all other elements. To ease this, we also provide the rejectAllFilters.

Here's an example:

import { GherkinDocumentWalker, rejectAllFilters } from '@cucumber/gherkin-utils'

// Only keeps scenarios which name include 'magic'
const filter = new GherkinDocumentWalker({
  ...rejectAllFilters,
  ...{ acceptScenario: (scenario) => scenario.name.includes('magic') },
})

// Makes a list with all the scenario names
const allScenarioNames: string[] = []
const scenarioNameFinder = new GherkinDocumentWalker({}, {
  handleScenario: (scenario) => allScenarioNames.push(scenario.name),
})

gherkin-utils's People

Contributors

renovate[bot] avatar aslakhellesoy avatar renovate-bot avatar cukebot avatar mpkorstanje avatar mattwynne avatar aurelien-reeves avatar xeger avatar vincent-psarga avatar davidjgoss avatar blacelle avatar gasparnagy avatar cbliard avatar osiux avatar ehuelsmann avatar just95 avatar kieran-ryan avatar timgates42 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.