gherkin

package module
v5.2.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 21, 2018 License: MIT Imports: 9 Imported by: 9

README

Build Status

Gherkin parser/compiler for Go. Please see Gherkin for details.

Documentation

Index

Examples

Constants

View Source
const (
	DEFAULT_DIALECT                 = "en"
	COMMENT_PREFIX                  = "#"
	TAG_PREFIX                      = "@"
	TITLE_KEYWORD_SEPARATOR         = ":"
	TABLE_CELL_SEPARATOR            = '|'
	ESCAPE_CHAR                     = '\\'
	ESCAPED_NEWLINE                 = 'n'
	DOCSTRING_SEPARATOR             = "\"\"\""
	DOCSTRING_ALTERNATIVE_SEPARATOR = "```"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Argument

type Argument interface{}

type AstBuilder

type AstBuilder interface {
	Builder
	GetGherkinDocument() *GherkinDocument
}

func NewAstBuilder

func NewAstBuilder() AstBuilder

type AttachmentEvent

type AttachmentEvent struct {
	Source SourceReference
	Data   string
	Media  MediaType
}

func (*AttachmentEvent) MarshalJSON

func (ae *AttachmentEvent) MarshalJSON() ([]byte, error)

type Background

type Background struct {
	ScenarioDefinition
}

type Builder

type Builder interface {
	Build(*Token) (bool, error)
	StartRule(RuleType) (bool, error)
	EndRule(RuleType) (bool, error)
	Reset()
}

type Comment

type Comment struct {
	Node
	Location *Location `json:"location,omitempty"`
	Text     string    `json:"text"`
}

type CucumberEvent

type CucumberEvent interface{}

func GherkinEvents

func GherkinEvents(paths ...string) ([]CucumberEvent, error)

func GherkinEventsForLanguage

func GherkinEventsForLanguage(paths []string, language string) ([]CucumberEvent, error)

type DataTable

type DataTable struct {
	Node
	Rows []*TableRow `json:"rows"`
}

type DocString

type DocString struct {
	Node
	ContentType string `json:"contentType,omitempty"`
	Content     string `json:"content"`
	Delimitter  string `json:"-"`
}

type Examples

type Examples struct {
	Node
	Tags        []*Tag      `json:"tags"`
	Keyword     string      `json:"keyword"`
	Name        string      `json:"name"`
	Description string      `json:"description,omitempty"`
	TableHeader *TableRow   `json:"tableHeader,omitempty"`
	TableBody   []*TableRow `json:"tableBody"`
}

func (*Examples) MarshalJSON

func (ex *Examples) MarshalJSON() ([]byte, error)

To be consistent with the Gherkin parsers implemented in other languages the TableBody attribute of an Examples struct should only be omitted if it is null, not if it an empty list of TableRow:s.

type Feature

type Feature struct {
	Node
	Tags        []*Tag        `json:"tags"`
	Language    string        `json:"language,omitempty"`
	Keyword     string        `json:"keyword"`
	Name        string        `json:"name"`
	Description string        `json:"description,omitempty"`
	Children    []interface{} `json:"children"`
}

type GherkinDialect

type GherkinDialect struct {
	Language string
	Name     string
	Native   string
	Keywords map[string][]string
}

func (*GherkinDialect) BackgroundKeywords

func (g *GherkinDialect) BackgroundKeywords() []string

func (*GherkinDialect) ExamplesKeywords

func (g *GherkinDialect) ExamplesKeywords() []string

func (*GherkinDialect) FeatureKeywords

func (g *GherkinDialect) FeatureKeywords() []string

func (*GherkinDialect) ScenarioKeywords

func (g *GherkinDialect) ScenarioKeywords() []string

func (*GherkinDialect) ScenarioOutlineKeywords

func (g *GherkinDialect) ScenarioOutlineKeywords() []string

func (*GherkinDialect) StepKeywords

func (g *GherkinDialect) StepKeywords() []string

type GherkinDialectProvider

type GherkinDialectProvider interface {
	GetDialect(language string) *GherkinDialect
}

func GherkinDialectsBuildin

func GherkinDialectsBuildin() GherkinDialectProvider

Builtin dialects for af (Afrikaans), am (Armenian), an (Aragonese), ar (Arabic), ast (Asturian), az (Azerbaijani), bg (Bulgarian), bm (Malay), bs (Bosnian), ca (Catalan), cs (Czech), cy-GB (Welsh), da (Danish), de (German), el (Greek), em (Emoji), en (English), en-Scouse (Scouse), en-au (Australian), en-lol (LOLCAT), en-old (Old English), en-pirate (Pirate), eo (Esperanto), es (Spanish), et (Estonian), fa (Persian), fi (Finnish), fr (French), ga (Irish), gj (Gujarati), gl (Galician), he (Hebrew), hi (Hindi), hr (Croatian), ht (Creole), hu (Hungarian), id (Indonesian), is (Icelandic), it (Italian), ja (Japanese), jv (Javanese), ka (Georgian), kn (Kannada), ko (Korean), lt (Lithuanian), lu (Luxemburgish), lv (Latvian), mk-Cyrl (Macedonian), mk-Latn (Macedonian (Latin)), mn (Mongolian), nl (Dutch), no (Norwegian), pa (Panjabi), pl (Polish), pt (Portuguese), ro (Romanian), ru (Russian), sk (Slovak), sl (Slovenian), sr-Cyrl (Serbian), sr-Latn (Serbian (Latin)), sv (Swedish), ta (Tamil), th (Thai), tl (Telugu), tlh (Klingon), tr (Turkish), tt (Tatar), uk (Ukrainian), ur (Urdu), uz (Uzbek), vi (Vietnamese), zh-CN (Chinese simplified), zh-TW (Chinese traditional)

type GherkinDocument

type GherkinDocument struct {
	Type     string     `json:"type"`
	Feature  *Feature   `json:"feature,omitempty"`
	Comments []*Comment `json:"comments"`
}

func ParseGherkinDocument

func ParseGherkinDocument(in io.Reader) (gherkinDocument *GherkinDocument, err error)
Example
input := `Feature: Tagged Examples

  Scenario Outline: minimalistic
    Given the <what>

    @foo
    Examples:
      | what |
      | foo  |

    @bar
    Examples:
      | what |
      | bar  |

  @zap
  Scenario: ha ok
`
r := strings.NewReader(input)

gherkinDocument, err := ParseGherkinDocument(r)
if err != nil {
	fmt.Fprintf(os.Stdout, "%s\n", err)
	return
}
feature := gherkinDocument.Feature
fmt.Fprintf(os.Stdout, "Location: %+v\n", feature.Location)
fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature.Keyword)
fmt.Fprintf(os.Stdout, "Name: %+v\n", feature.Name)
fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature.Children))

scenario1, _ := feature.Children[0].(*ScenarioOutline)
fmt.Fprintf(os.Stdout, " 1: Location: %+v\n", scenario1.Location)
fmt.Fprintf(os.Stdout, "    Keyword: %+v\n", scenario1.Keyword)
fmt.Fprintf(os.Stdout, "    Name: %+v\n", scenario1.Name)
fmt.Fprintf(os.Stdout, "    Steps: length: %+v\n", len(scenario1.Steps))

scenario2, _ := feature.Children[1].(*Scenario)
fmt.Fprintf(os.Stdout, " 2: Location: %+v\n", scenario2.Location)
fmt.Fprintf(os.Stdout, "    Keyword: %+v\n", scenario2.Keyword)
fmt.Fprintf(os.Stdout, "    Name: %+v\n", scenario2.Name)
fmt.Fprintf(os.Stdout, "    Steps: length: %+v\n", len(scenario2.Steps))
Output:


Location: &{Line:1 Column:1}
Keyword: Feature
Name: Tagged Examples
Children: length: 2
 1: Location: &{Line:3 Column:3}
    Keyword: Scenario Outline
    Name: minimalistic
    Steps: length: 1
 2: Location: &{Line:17 Column:3}
    Keyword: Scenario
    Name: ha ok
    Steps: length: 0
Example (Dialect)
input := "Egenskap: i18n support"
r := strings.NewReader(input)

gherkinDocument, err := ParseGherkinDocumentForLanguage(r, "no")
if err != nil {
	fmt.Fprintf(os.Stdout, "%s\n", err)
	return
}
feature := gherkinDocument.Feature
fmt.Fprintf(os.Stdout, "Location: %+v\n", feature.Location)
fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature.Keyword)
fmt.Fprintf(os.Stdout, "Name: %+v\n", feature.Name)
fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature.Children))
Output:


Location: &{Line:1 Column:1}
Keyword: Egenskap
Name: i18n support
Children: length: 0
Example (Error)
builder := NewAstBuilder()
parser := NewParser(builder)
parser.StopAtFirstError(false)
matcher := NewMatcher(GherkinDialectsBuildin())

input1 := `# a comment
Feature: Foo
  Scenario: Bar
    Given x
` + "      ```" + `
      unclosed docstring`
r1 := strings.NewReader(input1)

err1 := parser.Parse(NewScanner(r1), matcher)
if err1 != nil {
	fmt.Fprintf(os.Stdout, "%s\n", err1)
}
fmt.Fprintf(os.Stdout, "\n")

input2 := `Feature: Foo
  Scenario: Bar
    Given x
      """
      closed docstring
      """`
r2 := strings.NewReader(input2)

err2 := parser.Parse(NewScanner(r2), matcher)
if err2 != nil {
	fmt.Fprintf(os.Stdout, "%s\n", err2)
	return
}
doc2 := builder.GetGherkinDocument()
fmt.Fprintf(os.Stdout, "Comments: length: %+v\n", len(doc2.Comments))

feature2 := doc2.Feature
fmt.Fprintf(os.Stdout, "Location: %+v\n", feature2.Location)
fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature2.Keyword)
fmt.Fprintf(os.Stdout, "Name: %+v\n", feature2.Name)
fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature2.Children))
scenario1, _ := feature2.Children[0].(*Scenario)
fmt.Fprintf(os.Stdout, " 1: Location: %+v\n", scenario1.Location)
fmt.Fprintf(os.Stdout, "    Keyword: %+v\n", scenario1.Keyword)
fmt.Fprintf(os.Stdout, "    Name: %+v\n", scenario1.Name)
fmt.Fprintf(os.Stdout, "    Steps: length: %+v\n", len(scenario1.Steps))
Output:


Parser errors:
(7:0): unexpected end of file, expected: #DocStringSeparator, #Other

Comments: length: 0
Location: &{Line:1 Column:1}
Keyword: Feature
Name: Foo
Children: length: 1
 1: Location: &{Line:2 Column:3}
    Keyword: Scenario
    Name: Bar
    Steps: length: 1
Example (Multiple)
builder := NewAstBuilder()
parser := NewParser(builder)
parser.StopAtFirstError(false)
matcher := NewMatcher(GherkinDialectsBuildin())

input1 := `Feature: Test`
r1 := strings.NewReader(input1)

err1 := parser.Parse(NewScanner(r1), matcher)
if err1 != nil {
	fmt.Fprintf(os.Stdout, "%s\n", err1)
	return
}
doc1 := builder.GetGherkinDocument()
feature1 := doc1.Feature
fmt.Fprintf(os.Stdout, "Location: %+v\n", feature1.Location)
fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature1.Keyword)
fmt.Fprintf(os.Stdout, "Name: %+v\n", feature1.Name)
fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature1.Children))
fmt.Fprintf(os.Stdout, "\n")

input2 := `Feature: Test2`
r2 := strings.NewReader(input2)

err2 := parser.Parse(NewScanner(r2), matcher)
if err2 != nil {
	fmt.Fprintf(os.Stdout, "%s\n", err2)
	return
}
doc2 := builder.GetGherkinDocument()
feature2 := doc2.Feature
fmt.Fprintf(os.Stdout, "Location: %+v\n", feature2.Location)
fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature2.Keyword)
fmt.Fprintf(os.Stdout, "Name: %+v\n", feature2.Name)
fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature2.Children))
Output:


Location: &{Line:1 Column:1}
Keyword: Feature
Name: Test
Children: length: 0

Location: &{Line:1 Column:1}
Keyword: Feature
Name: Test2
Children: length: 0

func ParseGherkinDocumentForLanguage

func ParseGherkinDocumentForLanguage(in io.Reader, language string) (gherkinDocument *GherkinDocument, err error)

func (*GherkinDocument) Pickles

func (gd *GherkinDocument) Pickles() []*Pickle

type GherkinDocumentEvent

type GherkinDocumentEvent struct {
	Document *GherkinDocument
	URI      string
}

func (*GherkinDocumentEvent) MarshalJSON

func (gde *GherkinDocumentEvent) MarshalJSON() ([]byte, error)

type Line

type Line struct {
	LineText        string
	LineNumber      int
	TrimmedLineText string
	AtEof           bool
}

func (*Line) Indent

func (g *Line) Indent() int

func (*Line) IsEmpty

func (g *Line) IsEmpty() bool

func (*Line) IsEof

func (g *Line) IsEof() bool

func (*Line) StartsWith

func (g *Line) StartsWith(prefix string) bool

type LineSpan

type LineSpan struct {
	Column int
	Text   string
}

func (*LineSpan) String

func (l *LineSpan) String() string

type Location

type Location struct {
	Line   int `json:"line"`
	Column int `json:"column"`
}

type Matcher

type Matcher interface {
	MatchEOF(line *Line) (bool, *Token, error)
	MatchEmpty(line *Line) (bool, *Token, error)
	MatchComment(line *Line) (bool, *Token, error)
	MatchTagLine(line *Line) (bool, *Token, error)
	MatchFeatureLine(line *Line) (bool, *Token, error)
	MatchBackgroundLine(line *Line) (bool, *Token, error)
	MatchScenarioLine(line *Line) (bool, *Token, error)
	MatchScenarioOutlineLine(line *Line) (bool, *Token, error)
	MatchExamplesLine(line *Line) (bool, *Token, error)
	MatchStepLine(line *Line) (bool, *Token, error)
	MatchDocStringSeparator(line *Line) (bool, *Token, error)
	MatchTableRow(line *Line) (bool, *Token, error)
	MatchLanguage(line *Line) (bool, *Token, error)
	MatchOther(line *Line) (bool, *Token, error)
	Reset()
}

func NewLanguageMatcher

func NewLanguageMatcher(gdp GherkinDialectProvider, language string) Matcher

func NewMatcher

func NewMatcher(gdp GherkinDialectProvider) Matcher

type MediaType

type MediaType struct {
	Encoding string `json:"encoding"`
	Type     string `json:"type"`
}

type Node

type Node struct {
	Location *Location `json:"location,omitempty"`
	Type     string    `json:"type"`
}

type Parser

type Parser interface {
	StopAtFirstError(b bool)
	Parse(s Scanner, m Matcher) (err error)
}

func NewParser

func NewParser(b Builder) Parser

type Pickle

type Pickle struct {
	Name      string        `json:"name"`
	Language  string        `json:"language"`
	Steps     []*PickleStep `json:"steps"`
	Tags      []*PickleTag  `json:"tags"`
	Locations []Location    `json:"locations"`
}

type PickleCell

type PickleCell struct {
	Location Location `json:"location"`
	Value    string   `json:"value"`
}

type PickleEvent

type PickleEvent struct {
	URI    string
	Pickle *Pickle
}

func (*PickleEvent) MarshalJSON

func (pe *PickleEvent) MarshalJSON() ([]byte, error)

type PickleRow

type PickleRow struct {
	Cells []*PickleCell `json:"cells"`
}

type PickleStep

type PickleStep struct {
	Text      string     `json:"text"`
	Arguments []Argument `json:"arguments"`
	Locations []Location `json:"locations"`
}

type PickleString

type PickleString struct {
	Location    Location `json:"location"`
	ContentType string   `json:"contentType,omitempty"`
	Content     string   `json:"content"`
}

type PickleTable

type PickleTable struct {
	Rows []*PickleRow `json:"rows"`
}

type PickleTag

type PickleTag struct {
	Location Location `json:"location"`
	Name     string   `json:"name"`
}

type RuleType

type RuleType int
const (
	RuleType_None RuleType = iota

	RuleType__EOF
	RuleType__Empty
	RuleType__Comment
	RuleType__TagLine
	RuleType__FeatureLine
	RuleType__BackgroundLine
	RuleType__ScenarioLine
	RuleType__ScenarioOutlineLine
	RuleType__ExamplesLine
	RuleType__StepLine
	RuleType__DocStringSeparator
	RuleType__TableRow
	RuleType__Language
	RuleType__Other
	RuleType_GherkinDocument
	RuleType_Feature
	RuleType_Feature_Header
	RuleType_Background
	RuleType_Scenario_Definition
	RuleType_Scenario
	RuleType_ScenarioOutline
	RuleType_Examples_Definition
	RuleType_Examples
	RuleType_Examples_Table
	RuleType_Step
	RuleType_Step_Arg
	RuleType_DataTable
	RuleType_DocString
	RuleType_Tags
	RuleType_Description_Helper
	RuleType_Description
)

func (RuleType) IsEOF

func (t RuleType) IsEOF() bool

func (RuleType) Name

func (t RuleType) Name() string

type Scanner

type Scanner interface {
	Scan() (line *Line, atEof bool, err error)
}

The scanner reads a gherkin doc (typically read from a .feature file) and creates a token for each line. The tokens are passed to the parser, which outputs an AST (Abstract Syntax Tree).

If the scanner sees a # language header, it will reconfigure itself dynamically to look for Gherkin keywords for the associated language. The keywords are defined in gherkin-languages.json.

func NewScanner

func NewScanner(r io.Reader) Scanner

type Scenario

type Scenario struct {
	ScenarioDefinition
	Tags []*Tag `json:"tags"`
}

type ScenarioDefinition

type ScenarioDefinition struct {
	Node
	Keyword     string  `json:"keyword"`
	Name        string  `json:"name"`
	Description string  `json:"description,omitempty"`
	Steps       []*Step `json:"steps"`
}

type ScenarioOutline

type ScenarioOutline struct {
	ScenarioDefinition
	Tags     []*Tag      `json:"tags"`
	Examples []*Examples `json:"examples"`
}

type SourceEvent

type SourceEvent struct {
	URI  string
	Data string
}

func (*SourceEvent) MarshalJSON

func (se *SourceEvent) MarshalJSON() ([]byte, error)

type SourceReference

type SourceReference struct {
	URI   string   `json:"uri"`
	Start Location `json:"start"` // from gherkin AST
}

type Step

type Step struct {
	Node
	Keyword  string      `json:"keyword"`
	Text     string      `json:"text"`
	Argument interface{} `json:"argument,omitempty"`
}

type TableCell

type TableCell struct {
	Node
	Value string `json:"value"`
}

type TableRow

type TableRow struct {
	Node
	Cells []*TableCell `json:"cells"`
}

type Tag

type Tag struct {
	Node
	Location *Location `json:"location,omitempty"`
	Name     string    `json:"name"`
}

type Token

type Token struct {
	Type           TokenType
	Keyword        string
	Text           string
	Items          []*LineSpan
	GherkinDialect string
	Indent         string
	Location       *Location
}

func (*Token) IsEOF

func (t *Token) IsEOF() bool

func (*Token) String

func (t *Token) String() string

type TokenType

type TokenType int
const (
	TokenType_None TokenType = iota
	TokenType_EOF
	TokenType_Empty
	TokenType_Comment
	TokenType_TagLine
	TokenType_FeatureLine
	TokenType_BackgroundLine
	TokenType_ScenarioLine
	TokenType_ScenarioOutlineLine
	TokenType_ExamplesLine
	TokenType_StepLine
	TokenType_DocStringSeparator
	TokenType_TableRow
	TokenType_Language
	TokenType_Other
)

func (TokenType) Name

func (t TokenType) Name() string

func (TokenType) RuleType

func (t TokenType) RuleType() RuleType

Directories

Path Synopsis
This is a gherkin console application used to produce gherkin event stream of ndjson based entries.
This is a gherkin console application used to produce gherkin event stream of ndjson based entries.

Jump to

Keyboard shortcuts

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