Search code examples
javacucumbergherkincucumber-java

GherkinDocument to Gherkin Raw Text


I would like to store all the gherkin feature files created by a user on the front end as GherkinDocuments on the back end using the gherkin parser. Once saved, I would also like to be able to display the raw gherkin document on the front end. I have read through the documentation and cannot find anything built-in that converts the GherkinDocument back to a raw text. The toString() method is also not overloaded to print out. Is there a way to convert a GherkinDocument object to raw text within the gherkin parser?

I want to be able to keep as much of the original formatting as possible. Normally I would just write my own utility to perform this, however the structure of the GherkinDocument object renders it tedious. I would prefer to use existing capabilities if they exist.


Solution

  • I talked to Aslak, Cucumber developer, on the cucumber help gitter. He told me:

    Hi @tramstheman have you considered storing it as text instead of serialising the GherkinDocument AST? It is very quick to parse that text back into an AST when you need to. There isn't currently a renderer/prettifier that will turn an AST back to source as @mattwynne suggested. The tests don't do roundtrips, they just perform approval testing on various outputs (parser tokens, ASTs as JSON, pickles as JSON)

    What I have done instead is extended the GherkinDocument object and set it to store the raw text inside it, as similarly suggested by Aslak.