gwen-interpreter / gwen

Core Gwen interpreter
https://gweninterpreter.org
Apache License 2.0
36 stars 8 forks source link

Feature doc strings #33

Closed bjuric closed 7 years ago

bjuric commented 7 years ago

Doc Strings

This PR adds Doc Strings to Gwen. So every step in the Gwen DSL that includes a string literal parameter surrounded by quotes at the end of the step expression can now accept a Doc String value.

This is handy when you want to specify a multiline string literal as the last parameter to a step.

Some examples follow..

Multiline Text

One use case is multiline strings. In the absence of Doc Strings there was no way to pass in multiline strings or paragraphs of text. You can now do this as follows:

Given my paragraph is
     """
     Gwen is a Gherkin interpreter that turns Given-When-Then steps into automation instructions and executes
     them for you so you don't have to do all the programming work. It has an abstracted evaluation engine
     allowing any type of automation capability to be built and mixed in. Meta specifications (also expressed
     in Gherkin) are used to capture automation bindings and allow you to compose step definitions by mapping
     'declarative' steps in features to 'imperative' steps in engines that perform operations.
     """

JavaScript Function Bindings

Another use case is JavaScript bindings. In Gwen, they are limited to one-liner expressions. A work around for working with multiline javascript functions is to wrap the entire script in the body of an anonymous function and invoke it inline.

For example, consider the following script that returns the current date in yyyy-mm-dd format.

(function() {
  var d = new Date();
  var year = d.getFullYear();
  var month = ('0' + (d.getMonth() + 1)).slice(-2);
  var day = ('0' + d.getDate()).slice(-2);
  return year + '-' + month + '-' + day;
})();

In the absence of Doc Strings, you would have to compact this to a one-liner expression and use it as follows (which is inelegant and hard to read):

Given the current date is defined by javascript "(function() {var d = new Date(); var year = d.getFullYear(); var month = ('0' + (d.getMonth() + 1)).slice(-2); var day = ('0' + d.getDate()).slice(-2); return year + '-' + month + '-' + day; })();"

With Doc Strings, you can now do this as follows instead:

Given the current date is defined by javascript
     """
     (function() {
       var d = new Date();
       var year = d.getFullYear();
       var month = ('0' + (d.getMonth() + 1)).slice(-2);
       var day = ('0' + d.getDate()).slice(-2);
       return year + '-' + month + '-' + day;
     })();
     """

Sample Reports

The javascript Doc String in this report includes the optional JavaScript content type annotation at the opening triple quotes""". Currently, Gwen just renders this literal in grey, but could in future use it for syntax highlighting.

docstring