Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ghokin/fixtures/json.expected.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: A Feature

Background:
Given a thing
"""json
{
"this": "is a json doc string",
"but": "with a newline in the middle"
}
"""

9 changes: 9 additions & 0 deletions ghokin/fixtures/json.input.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: A Feature

Background:
Given a thing
"""json
{"this": "is a json doc string",
"but": "with a newline in the middle"}
"""

25 changes: 25 additions & 0 deletions ghokin/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ghokin

import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"regexp"
Expand Down Expand Up @@ -85,6 +86,30 @@ func transform(section *section, indent int, aliases aliases) ([]byte, error) {
continue
}

if sec.kind == gherkin.TokenTypeDocStringSeparator && len(sec.values) == 1 && sec.values[0].Text == "json" {
document = append(
document,
trimExtraTrailingSpace(indentStrings(paddings[gherkin.TokenTypeOther], []string{`"""json`}))...,
)

var jsonLines string
for sec.nex.kind != gherkin.TokenTypeDocStringSeparator {
sec = sec.nex
for _, value := range sec.values {
jsonLines += value.Text
}
}

var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(jsonLines), "", " "); err != nil {
return []byte{}, fmt.Errorf("failed to format json: %w", err)
}

document = append(document, trimExtraTrailingSpace(indentStrings(paddings[gherkin.TokenTypeOther], strings.Split(prettyJSON.String(), "\n")))...)

continue
}

if sec.kind == 0 {
continue
}
Expand Down
4 changes: 4 additions & 0 deletions ghokin/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ func TestTransform(t *testing.T) {
"fixtures/file1.feature",
"fixtures/file1.feature",
},
{
"fixtures/json.input.feature",
"fixtures/json.expected.feature",
},

{
"fixtures/cmd.input.feature",
Expand Down