gherkin

package module
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2016 License: MIT Imports: 7 Imported by: 6

README

Go Gherkin package

Build Status

Package gherkin implements a parser for the Gherkin language, used for Story/Feature based Behavior Driven Development.

The parser is generated via @pointlander's awesome peg parser generator.

Read the Documentation over at godoc.org.

Install

Via Go Get
$ go get github.com/muhqu/go-gherkin
Via GNU Make
$ git clone https://github.com/muhqu/go-gherkin
...
$ cd go-gherkin/
$ make get-deps build test install

Usage Example

import (
  "github.com/muhqu/go-gherkin"
)

func main() {
    feature, _ := gherkin.ParseGherkinFeature(`
@wip
Feature: Hello World
  The world is a beautiful place
  So let people be nice to each other

  @nice @people
  Scenario: Nice people
    Given a nice person called "Bob"
      And a nice person called "Lisa"
     When "Bob" says to "Lisa": "Hello!"
     Then "Lisa" should reply to "Bob": "Hello!"

`)

    fmt.Printf("feature: %#v %#v\n", feature.Title(), feature.Tags())
    fmt.Printf("no. scenarios: %#v\n", len(feature.Scenarios()))
    for i, scenario := range feature.Scenarios() {
        fmt.Printf("scenario %d: %#v %#v\n", i+1, scenario.Title(), scenario.Tags())
        for i, step := range scenario.Steps() {
            fmt.Printf("  step %d: %#v %#v\n", i+1, step.StepType(), step.Text())
        }
    }
}

Output:

feature: "Hello World" []string{"wip"}
no. scenarios: 1
scenario 1: "Nice people" []string{"nice", "people"}
  step 1: "Given" "a nice person called \"Bob\""
  step 2: "And" "a nice person called \"Lisa\""
  step 3: "When" "\"Bob\" says to \"Lisa\": \"Hello!\""
  step 4: "Then" "\"Lisa\" should reply to \"Bob\": \"Hello!\""

Using Go-Gherkin

Projects using go-gherkin package:

  • gherkinfmt - Commandline tool to format Gherkin files.

Author

© 2014 by Mathias Leppich
github.com/muhqu, @muhqu

Documentation

Overview

Package gherkin implements a parser for the Gherkin language, used for Story/Feature based Behavior Driven Development.

Basic usage example:

feature, _ := gherkin.ParseGherkinFeature(`
@wip
Feature: Hello World
  The world is a beautiful place
  So let people be nice to each other

  @nice @people
  Scenario: Nice people
    Given a nice person called "Bob"
      And a nice person called "Lisa"
     When "Bob" says to "Lisa": "Hello!"
     Then "Lisa" should reply to "Bob": "Hello!"

`)

fmt.Printf("feature: %#v %#v\n", feature.Title(), feature.Tags())
fmt.Printf("no. scenarios: %#v\n", len(feature.Scenarios()))
for i, scenario := range feature.Scenarios() {
	fmt.Printf("scenario %d: %#v %#v\n", i+1, scenario.Title(), scenario.Tags())
	for i, step := range scenario.Steps() {
		fmt.Printf("  step %d: %#v %#v\n", i+1, step.StepType(), step.Text())
	}
}

Output:

feature: "Hello World" []string{"wip"}
no. scenarios: 1
scenario 1: "Nice people" []string{"nice", "people"}
  step 1: "Given" "a nice person called \"Bob\""
  step 2: "And" "a nice person called \"Lisa\""
  step 3: "When" "\"Bob\" says to \"Lisa\": \"Hello!\""
  step 4: "Then" "\"Lisa\" should reply to \"Bob\": \"Hello!\""

Index

Examples

Constants

View Source
const VERSION = "v0.1.7"

Variables

This section is empty.

Functions

func ParseGherkinFeature

func ParseGherkinFeature(content string) (FeatureNode, error)
Example
feature, _ := gherkin.ParseGherkinFeature(`
@wip
Feature: Hello World
  The world is a beautiful place
  So let people be nice to each other

  @nice @people
  Scenario: Nice people
    Given a nice person called "Bob"
      And a nice person called "Lisa"
     When "Bob" says to "Lisa": "Hello!"
     Then "Lisa" should reply to "Bob": "Hello!"

`)

fmt.Printf("feature: %#v %#v\n", feature.Title(), feature.Tags())
fmt.Printf("no. scenarios: %#v\n", len(feature.Scenarios()))
for i, scenario := range feature.Scenarios() {
	fmt.Printf("scenario %d: %#v %#v\n", i+1, scenario.Title(), scenario.Tags())
	for i, step := range scenario.Steps() {
		fmt.Printf("  step %d: %#v %#v\n", i+1, step.StepType(), step.Text())
	}
}
Output:

feature: "Hello World" []string{"wip"}
no. scenarios: 1
scenario 1: "Nice people" []string{"nice", "people"}
  step 1: "Given" "a nice person called \"Bob\""
  step 2: "And" "a nice person called \"Lisa\""
  step 3: "When" "\"Bob\" says to \"Lisa\": \"Hello!\""
  step 4: "Then" "\"Lisa\" should reply to \"Bob\": \"Hello!\""

Types

type EventProcessor

type EventProcessor interface {
	ProcessEvent(GherkinEvent)
}

type EventProcessorFn

type EventProcessorFn func(GherkinEvent)

func (EventProcessorFn) ProcessEvent

func (fn EventProcessorFn) ProcessEvent(e GherkinEvent)

type GherkinDOM

type GherkinDOM interface {
	Feature() FeatureNode
}

type GherkinDOMParser

type GherkinDOMParser interface {
	GherkinParser
	GherkinDOM
	ParseDOM() (GherkinDOM, error)
	ParseFeature() (FeatureNode, error)
}

func NewGherkinDOMParser

func NewGherkinDOMParser(content string) GherkinDOMParser
Example
gp := gherkin.NewGherkinDOMParser(`
@wip
Feature: Hello World
  The world is a beautiful place
  So let people be nice to each other

  @nice @people
  Scenario: Nice people
    Given a nice person called "Bob"
      And a nice person called "Lisa"
     When "Bob" says to "Lisa": "Hello!"
     Then "Lisa" should reply to "Bob": "Hello!"

`)
feature := gp.Feature()

fmt.Printf("feature: %#v %#v\n", feature.Title(), feature.Tags())
fmt.Printf("no. scenarios: %#v\n", len(feature.Scenarios()))
scenario1 := feature.Scenarios()[0]
fmt.Printf("scenario 1: %#v %#v\n", scenario1.Title(), scenario1.Tags())
Output:

feature: "Hello World" []string{"wip"}
no. scenarios: 1
scenario 1: "Nice people" []string{"nice", "people"}

type GherkinEvent

type GherkinEvent events.Event

type GherkinParser

type GherkinParser interface {
	WithLogFn(LogFn)
	WithEventProcessor(EventProcessor)
	Init()
	Parse() error
	Execute()
}

func NewGherkinParser

func NewGherkinParser(content string) GherkinParser
Example
gp := gherkin.NewGherkinParser(`
@wip
Feature: Hello World
  The world is a beautiful place
  So let people be nice to each other

  @nice @people
  Scenario: Nice people
    Given a nice person called "Bob"
      And a nice person called "Lisa"
     When "Bob" says to "Lisa": "Hello!"
     Then "Lisa" should reply to "Bob": "Hello!"
`)
gp.WithEventProcessor(gherkin.EventProcessorFn(func(e gherkin.GherkinEvent) {
	fmt.Println(e)
}))

gp.Init()
err := gp.Parse()
if err != nil {
	panic(err)
}
gp.Execute()
Output:


FeatureEvent("Hello World","The world is a beautiful place\nSo let people be nice to each other",["wip"])
ScenarioEvent("Nice people","",["nice" "people"])
StepEvent("Given","a nice person called \"Bob\"")
StepEndEvent()
StepEvent("And","a nice person called \"Lisa\"")
StepEndEvent()
StepEvent("When","\"Bob\" says to \"Lisa\": \"Hello!\"")
StepEndEvent()
StepEvent("Then","\"Lisa\" should reply to \"Bob\": \"Hello!\"")
StepEndEvent()
ScenarioEndEvent()
FeatureEndEvent()

type LogFn

type LogFn func(msg string, args ...interface{})
Example
gp := gherkin.NewGherkinParser(`
@wip
Feature: Hello World
  The world is a beautiful place
  So let people be nice to each other

  @nice @people
  Scenario: Nice people
    Given a nice person called "Bob"
      And a nice person called "Lisa"
     When "Bob" says to "Lisa": "Hello!"
     Then "Lisa" should reply to "Bob": "Hello!"
`)

gp.WithLogFn(func(msg string, args ...interface{}) {
	fmt.Printf(msg+"\n", args...)
})

gp.Init()
err := gp.Parse()
if err != nil {
	panic(err)
}
gp.Execute()
Output:

BeginFeature: "Hello World": "The world is a beautiful place\nSo let people be nice to each other" tags:[wip]
BeginScenario: "Nice people": "" tags:[nice people]
BeginStep: "Given": "a nice person called \"Bob\""
EndStep
BeginStep: "And": "a nice person called \"Lisa\""
EndStep
BeginStep: "When": "\"Bob\" says to \"Lisa\": \"Hello!\""
EndStep
BeginStep: "Then": "\"Lisa\" should reply to \"Bob\": \"Hello!\""
EndStep
EndScenario
EndFeature

Directories

Path Synopsis
Sub-Package gherkin/events provides the data-structure types for the evented gherkin parser.
Sub-Package gherkin/events provides the data-structure types for the evented gherkin parser.
Sub-Package gherkin/formater provides everything to pretty-print a Gherkin DOM.
Sub-Package gherkin/formater provides everything to pretty-print a Gherkin DOM.
Sub-Package gherkin/nodes provides the data-structure types for the gherkin DOM parser.
Sub-Package gherkin/nodes provides the data-structure types for the gherkin DOM parser.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL