• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    Java
  • License
    MIT License
  • Created about 3 years ago
  • Updated 29 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Human friendly alternative to Regular Expressions

test-go test-java test-javascript test-python test-ruby test-dotnet

Cucumber Expressions

Cucumber Expressions is an alternative to Regular Expressions with a more intuitive syntax.

Try Cucumber Expressions in your browser.

Cucumber supports both Cucumber Expressions and Regular Expressions for defining Step Definitions, but you cannot mix Cucumber Expression syntax with Regular Expression syntax in the same expression.

On platforms that don't have a literal syntax for regular expressions (such as Java), Cucumber will create a Cucumber Expression by default. To use Regular Expressions, add anchors (starting with ^ and ending with $) or forward slashes (/). For more information, see Cucumber Expression - Java Heuristics.

Introduction

Let's write a Cucumber Expression that matches the following Gherkin step (the Given keyword has been removed here, as it's not part of the match).

I have 42 cucumbers in my belly

The simplest Cucumber Expression that matches that text would be the text itself, but we can also write a more generic expression, with an int output parameter:

I have {int} cucumbers in my belly

When the text is matched against that expression, the number 42 is extracted from the {int} output parameter and passed as an argument to the step definition.

The following text would not match the expression:

I have 42.5 cucumbers in my belly

This is because 42.5 has a decimal part, and doesn't fit into an int. Let's change the output parameter to float instead:

I have {float} cucumbers in my belly

Now the expression will match the text, and the float 42.5 is extracted.

Parameter types

Text between curly braces reference a parameter type. Cucumber comes with the following built-in parameter types:

Parameter Type Description
{int} Matches integers, for example 71 or -19. Converts to a 32-bit signed integer if the platform supports it.
{float} Matches floats, for example 3.6, .8 or -9.2. Converts to a 32 bit float if the platform supports it.
{word} Matches words without whitespace, for example banana (but not banana split).
{string} Matches single-quoted or double-quoted strings, for example "banana split" or 'banana split' (but not banana split). Only the text between the quotes will be extracted. The quotes themselves are discarded. Empty pairs of quotes are valid and will be matched and passed to step code as empty strings.
{} anonymous Matches anything (/.*/).
{bigdecimal} Matches the same as {float}, but converts to a BigDecimal if the platform supports it.
{double} Matches the same as {float}, but converts to a 64 bit float if the platform supports it.
{biginteger} Matches the same as {int}, but converts to a BigInteger if the platform supports it.
{byte} Matches the same as {int}, but converts to an 8 bit signed integer if the platform supports it.
{short} Matches the same as {int}, but converts to a 16 bit signed integer if the platform supports it.
{long} Matches the same as {int}, but converts to a 64 bit signed integer if the platform supports it.

Cucumber-JVM

The anonymous parameter type will be converted to the parameter type of the step definition using an object mapper. Cucumber comes with a built-in object mapper that can handle all numeric types as well as. Enum.

To automatically convert to other types it is recommended to install an object mapper. See configuration to learn how.

Custom Parameter types

Cucumber Expressions can be extended so they automatically convert output parameters to your own types. Consider this Cucumber Expression:

I have a {color} ball

If we want the {color} output parameter to be converted to a Color object, we can define a custom parameter type in Cucumber's configuration.

The table below explains the various arguments you can pass when defining a parameter type.

Argument Description
name The name the parameter type will be recognised by in output parameters.
regexp A regexp that will match the parameter. May include capture groups.
type The return type of the transformer.
transformer A function or method that transforms the match from the regexp. Must have arity 1 if the regexp doesn't have any capture groups. Otherwise the arity must match the number of capture groups in regexp.
useForSnippets / use_for_snippets Defaults to true. That means this parameter type will be used to generate snippets for undefined steps. If the regexp frequently matches text you don't intend to be used as arguments, disable its use for snippets with false.
preferForRegexpMatch / prefer_for_regexp_match Defaults to false. Set to true if you have step definitions that use regular expressions, and you want this parameter type to take precedence over others during a match.

Java

@ParameterType("red|blue|yellow")  // regexp
public Color color(String color){  // type, name (from method)
    return new Color(color);       // transformer function
}

Kotlin

@ParameterType("red|blue|yellow")   // regexp
fun color(color: String): Color {   // name (from method), type
    return Color(color)             // transformer function
}                                    

Scala

ParameterType("color", "red|blue|yellow") { color: String => // name, regexp
    Color(color)                                             // transformer function, type
}                                    

JavaScript / TypeScript

import { defineParameterType } from '@cucumber/cucumber'

defineParameterType({
    name: 'color',
    regexp: /red|blue|yellow/,
    transformer: s => new Color(s)
})

The transformer function may return a Promise.

Ruby

ParameterType(
  name:        'color',
  regexp:      /red|blue|yellow/,
  type:        Color,
  transformer: ->(s) { Color.new(s) }
)

.NET / SpecFlow

[StepArgumentTransformation("(red|blue|yellow)")]
public Color ConvertColor(string colorValue)
{
    return new Color(colorValue);
}

Python

ParameterType(
  name=        'color',
  regexp=      "red|blue|yellow",
  type=  Color,
  transformer= lambda s: Color(),
)

Note: Currently the parameter name cannot be customized, so the custom parameters can only be used with the type name, e.g. {Color}.

Optional text

It's grammatically incorrect to say 1 cucumbers, so we should make the plural s optional. That can be done by surrounding the optional text with parentheses:

I have {int} cucumber(s) in my belly

That expression would match this text:

I have 1 cucumber in my belly

It would also match this text:

I have 42 cucumbers in my belly

In Regular Expressions, parentheses indicate a capture group, but in Cucumber Expressions they mean optional text.

Alternative text

Sometimes you want to relax your language, to make it flow better. For example:

I have {int} cucumber(s) in my belly/stomach

This would match either of those texts:

I have 1 cucumber in my belly
I have 1 cucumber in my stomach
I have 42 cucumbers in my stomach
I have 42 cucumbers in my belly

Alternative text only works when there is no whitespace between the alternative parts.

Escaping

If you ever need to match () or {} literally, you can escape the opening ( or { with a backslash:

I have {int} \{what} cucumber(s) in my belly \(amazing!)

This expression would match the following examples:

I have 1 {what} cucumber in my belly (amazing!)
I have 42 {what} cucumbers in my belly (amazing!)

You may have to escape the \ character itself with another \, depending on your programming language. For example, in Java, you have to use escape character \ with another backslash.

I have {int} \\{what} cucumber(s) in my belly \\(amazing!)

Then this expression would match the following example:

I have 1 \{what} cucumber in my belly \(amazing!)
I have 42 \{what} cucumbers in my belly \(amazing!)

There is currently no way to escape a / character - it will always be interpreted as alternative text.

Architecture

See ARCHITECTURE.md

Acknowledgements

The Cucumber Expression syntax is inspired by similar expression syntaxes in other BDD tools, such as Turnip, Behat and Behave.

Big thanks to Jonas Nicklas, Konstantin Kudryashov and Jens Engel for implementing those libraries.

The Tiny-Compiler-Parser tutorial by Yehonathan Sharvit inspired the design of the Cucumber expression parser.

More Repositories

1

cucumber-ruby

Cucumber for Ruby. It's amazing!
Ruby
5,178
star
2

cucumber-js

Cucumber for JavaScript
TypeScript
5,053
star
3

common

A home for issues that are common to multiple cucumber repositories
3,363
star
4

cucumber-jvm

Cucumber for the JVM
Java
2,702
star
5

godog

Cucumber for golang
Go
2,307
star
6

cucumber-rails

Rails Generators for Cucumber with special support for Capybara and DatabaseCleaner
Ruby
1,021
star
7

aruba

Test command-line applications with Cucumber-Ruby, RSpec or Minitest.
Ruby
948
star
8

cucumber-java-skeleton

This is the simplest possible setup for Cucumber-JVM using Java.
Java
461
star
9

cucumber-cpp

Support for writing Cucumber step definitions in C++
C++
308
star
10

cucumber-eclipse

Eclipse plugin for Cucumber
Java
192
star
11

gherkin

A parser and compiler for the Gherkin language.
C
182
star
12

docs

Cucumber user documentation
CSS
151
star
13

cucumber-android

Android support for Cucumber-JVM
Kotlin
135
star
14

cucumber-electron

Run cucumber.js in electron
JavaScript
118
star
15

gherkin-go

[READ-ONLY] Gherkin for Go - subtree of https://github.com/cucumber/gherkin -- moved to https://github.com/cucumber/gherkin
Go
84
star
16

gherkin-javascript

[READ-ONLY] Gherkin for JavaScript - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
TypeScript
79
star
17

gherkin-python

[READ-ONLY] Gherkin for Python - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Python
76
star
18

vscode

Official Visual Studio Code Extension for Cucumber
TypeScript
66
star
19

screenplay.js

Library to ease implementation of the Screenplay pattern with CucumberJS
TypeScript
56
star
20

gherkin-java

[READ-ONLY] Gherkin for Java - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Java
49
star
21

cucumber-jvm-scala

Cucumber Scala
Scala
48
star
22

gherkin-dotnet

[READ-ONLY] Gherkin for Dotnet - subtree of monorepo https://github.com/cucumber/cucumber Gherkin parser/compiler for .NET
C#
46
star
23

language-server

Cucumber Language Server
TypeScript
36
star
24

cucumber-ruby-core

Core library for the Ruby flavour of Cucumber
Ruby
35
star
25

react-components

React components for Cucumber
TypeScript
34
star
26

cucumber-lua

A cucumber wire protocol implementation for Lua step definitions
Lua
29
star
27

cucumber.ml

Cucumber for OCaml
OCaml
27
star
28

cucumber-js-examples

Examples of using Cucumber-JS
Makefile
25
star
29

blockly

Gherkin Editor based on Blockly
TypeScript
24
star
30

cucumber-jvm-groovy

Cucumber Groovy
Java
23
star
31

microdata

Extract WHATWG microdata from a DOM
TypeScript
22
star
32

json-formatter

Provides a language-agnostic command-line tool to convert cucumber messages into a JSON document.
Go
21
star
33

messages

A message protocol for representing results and other information from Cucumber
C#
19
star
34

language-service

Cucumber Language Service
TypeScript
18
star
35

cucumber-js-pretty-formatter

Cucumber.js pretty formatter
TypeScript
17
star
36

monaco

Configure Monaco editor to use cucumber-language-service
TypeScript
16
star
37

html-formatter

HTML formatter for reporting Cucumber results
Java
14
star
38

gherkin-utils

API for working with Gherkin documents
TypeScript
12
star
39

ci-environment

Detect CI Environment from environment variables
Java
10
star
40

gherkin-c

[READ-ONLY] Gherkin for C - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
C
10
star
41

tag-expressions

Cucumber tag expression parser
Python
9
star
42

screenplay.js.examples

Examples using @cucumber/screenplay
TypeScript
7
star
43

cucumber-eclipse-update-site-snapshot

Cucumber Eclipse Update Site Snapshots
CSS
7
star
44

gherkin-objective-c

[READ-ONLY] Gherkin for Objective C - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Objective-C
7
star
45

gherkin-ruby

[READ-ONLY] Gherkin for Ruby - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Ruby
7
star
46

cucumber-ruby-wire

Wire protocol plugin for Cucumber
Gherkin
7
star
47

cucumber-json-converter

Parse Cucumber JSON from most Cucumber implementations and versions
TypeScript
6
star
48

polyglot-release

Make polyglot releases with a single command
Shell
5
star
49

todo-react-typescript-subsecond

Tiny Todo app in React and TypeScript demonstrating sub-second test feedback
TypeScript
4
star
50

action-retire-inactive-contributors

Retire inactive contributors from one team to another
TypeScript
4
star
51

cucumber-json-schema

JSON Schemas for Cucumber JSON output
JavaScript
4
star
52

messages-go

[READ ONLY] Cucumber Messages for Go - subtree of monorepo https://github.com/cucumber/messages -- moved to https://github.com/cucumber/messages
Go
3
star
53

gherkin-streams

Stream utilities to read Gherkin parser output.
TypeScript
3
star
54

try-cucumber-expressions

Try Cucumber Expressions in your browser
TypeScript
3
star
55

cucumber-eclipse-update-site

Cucumber Eclipse Update Site
3
star
56

github-settings

Pulumi scripts to automatically configure our GitHub org/repo settings
TypeScript
2
star
57

gherkin-perl

[READ-ONLY] Gherkin for Perl - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/gherkin
Perl
2
star
58

gherkin-php

[READ ONLY] Cucumber Gherkin for PHP - subtree of https://github.com/cucumber/gherkin
PHP
2
star
59

junit-xml-formatter

JUnit XML formatter for reporting Cucumber results
Java
2
star
60

oselvar-github-metrics

Oselvar GitHub Metrics for the Cucumber Organisation
Shell
2
star
61

build

Docker image used to build the Cucumber Project
Shell
2
star
62

fake-cucumber

Tool to generate test data for cucumber
TypeScript
2
star
63

messages-javascript

[READ ONLY] Cucumber Messages for JavaScript (Protocol Buffers) - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/messages
TypeScript
2
star
64

commitbit

Microservice that hands out commit bit to everyone who gets a pull request merged
JavaScript
2
star
65

query

A query API for https://github.com/cucumber/messages
Java
2
star
66

aruba-getting-started

Getting started with aruba
Ruby
2
star
67

action-publish-cpan

GitHub Action to publish a Perl module to CPAN
Perl
1
star
68

community-calendar

Public calendar for community calls and events
1
star
69

action-changelog

GitHub Action for changelog tool
Shell
1
star
70

release-announcement-banner

For blog posts where we announce a new version of a Cucumber tool
JavaScript
1
star
71

split-java

A Cucumber plugin to toggle Split features from Cucumber scenarios
Java
1
star
72

cucumber-js-package-upgrade

package to point users to the new @cucumber/cucumber
JavaScript
1
star
73

messages-java

[READ ONLY] Cucumber Messages for Java (Protocol Buffers) - subtree of monorepo https://github.com/cucumber/cucumber
Java
1
star
74

action-publish-subrepo-test-monorepo-a-subfolder

target for tests for https://github.com/cucumber/action-publish-subrepo
1
star
75

cucumber-parent

Parent `pom.xml` for all Cucumber Java modules
1
star
76

action-publish-rubygem

GitHub Action to publish a Ruby Gem
Ruby
1
star
77

compatibility-kit

Platform-agnostic set of acceptance tests for validating cucumber implementations
TypeScript
1
star
78

action-publish-subrepo-test-monorepo

Test repo for testing the action-publish-subrepo GitHub Action
1
star
79

messages-ruby

[READ ONLY] Cucumber Messages for Ruby (Protocol Buffers) - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/messages
Ruby
1
star
80

renovate-config

Shareable Config Presets for Renovate in the Cucumber org
1
star
81

action-publish-mvn

GitHub Action to publish Maven artefacts
Java
1
star
82

message-streams

Stream utilities to read and write Cucumber Message objects to/from streams.
TypeScript
1
star
83

action-publish-npm

GitHub Action to publish an NPM module
1
star
84

messages-dotnet

[READ ONLY] Cucumber Messages for .NET (Protocol Buffers) - subtree of monorepo https://github.com/cucumber/cucumber -- moved to https://github.com/cucumber/messages
Makefile
1
star
85

.github

👩‍⚕️ Default community health files for the Cucumber organisation on GitHub.
Shell
1
star