diff --git a/ghokin/fixtures/json.expected.feature b/ghokin/fixtures/json.expected.feature new file mode 100644 index 0000000..835362e --- /dev/null +++ b/ghokin/fixtures/json.expected.feature @@ -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" + } + """ + diff --git a/ghokin/fixtures/json.input.feature b/ghokin/fixtures/json.input.feature new file mode 100644 index 0000000..e99c416 --- /dev/null +++ b/ghokin/fixtures/json.input.feature @@ -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"} + """ + diff --git a/ghokin/transformer.go b/ghokin/transformer.go index 85fb8dd..88b88b7 100644 --- a/ghokin/transformer.go +++ b/ghokin/transformer.go @@ -2,6 +2,7 @@ package ghokin import ( "bytes" + "encoding/json" "fmt" "os/exec" "regexp" @@ -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 } diff --git a/ghokin/transformer_test.go b/ghokin/transformer_test.go index 21c13a0..b1127e0 100644 --- a/ghokin/transformer_test.go +++ b/ghokin/transformer_test.go @@ -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",