BDD Development with Tcl 8.6

Presentation
EuroTcl2018


Munich

Version 1.7
July 2018

BDD Development with Tcl 8.6

Work in Progress

© 2018 Michael Kaelbling, Siemens AG. All rights reserved. Unrestricted

BDD Development

BDD – Behavior-Driven Development

brief background

BD Development Development … in Tcl 8.6

exploring extensions

BDD –
Behavior-Driven Development

Better software(-based) systems, through:

Gherkin

Gherkin

Basis of BDD

The Gherkin Language

See the Wikipedia section on the Gherkin language in the Cucumber (software) article.

It's a little-language — just features, scenarios, and steps.

Gherkin syntax

Does this all sound familiar?

Gherkin "Features"

Features are context for "scenarios".

  Feature: Register for EuroTcl 2018

    A user wants to register for
    EuroTcl 2018.

    Let them.

Gherkin "Scenarios"

Scenarios are a sequence of steps.

  Scenario: Scenario 1
      Given preconditions
       When actions
       Then results
  Scenario: Scenario 2
             ...

Example Scenario



  Scenario: A user wants to register
      Given they have a browser
       When they enter an email address
       Then they get a confirmation

Gherkin "Scenaro Outlines"

Scenario outlines are clone recipes.

  Scenario Outline: Banned users are rejected
      Given a denied-persons database
       When a user submits an <email>
       Then they get a <response>

  Examples:

Gherkin "Examples"

Examples fill scenario-outlines.

  Examples:
    | email     | ban? | response     |
    | foo@x.org | yes  | rejection    |
    | drk@x.org | no   | confirmation |
    | bar@x.org | yes  | rejection    |

Gherkin "Steps"

Conjuctions



Field Trip to the command line!

What is missing from Gherkin?

      Given travel is requested
       When the first-line manager approves
        And the accountant approves
        And the big boss approves
        ??? in any order
       Then the request is approved
      

[incr Gherkin]

Gherkin++

[incr Gherkin]

Test Area: Jolly Test Probes



J. Fröhlich et al. Reliable Statements About a Fault-Tolerant X-by-Wire eCar, 2017.

Extensions (to Be) Examined

BDD Development in Tcl 8.6

Why use Tcl?

Its size and power — and a fun idea.

Tcl-induced Quirks

A few gotchas.

Semicolon Quirk

  Scenario: cold start; Arctic
    Arctic conditions require special
    behavior; as well as certification
      

  Scenario: "cold start; Arctic"
    "Arctic conditions require special
     behavior; as well as certification"

Square-Bracket Quirk

  Given sufficient coolant [DIN 0]


      

  Given sufficient coolant \[DIN 0\]
    

Quotemark Quirk

  Feature: Avoiding unsightly layout
      It is important to avoid so called
      "widows" and "orphans" in layout.
      

  Feature: Avoiding unsightly layout
      It is important to avoid so called
      'widows' and 'orphans' in layout.

Initial-Command-Name Quirk

  Feature: Packaging
      Packages must accommodate a
      set of six; And be recyclable.
      

  Feature: Packaging
      Packages must accommodate a
  {}  set of six;{} And be recyclable.

Python Behave syntax Quirk

  Feature: Packaging
    """Packages must accommodate a
       set of six; and be "recyclable"."""
      

  Feature: Packaging
    {{{Packages must accommodate a
       set of six; and be "recyclable".}}}
      

Lets digress, shall we?

Remember when …

    {*}bar

was

    "extra characters after close-brace"

Thinking about the Dodecalogue?

Well,

    "foo"bar"baz"

is still

    "extra characters after close-quote"

Ready for a Triskadecalogue?

We could leapfrog both Python and Lua.

     """     pythonic string       """
     ""[ \, $, and [] replacements ]""
     ""{     no replacements       }""
  ""==="   trans-pythonic string   "===""
   ""==[    Tcl-ish long string    ]==""
    ""={    Lua-ish long braces    }=""

[incr Gherkin]

Gherkin++

[incr Gherkin]

Trailing clauses .

Avoids both long lines and line splicing.

  Feature: Enhancement: the "with" adverb
      Demonstrate the trivial enhancement: the "with" adverb.
      By the way: this feature file also shows that text can
      simply be inserted after the Feature line.

    Scenario: use With as a Given, When, or Then
        By the way: descriptive text can simply be added
        after the Scenario line, too.

Trailing clauses ..


      Given that the wheel speed is 1001 rpm
       With a confidence of 60%

       When the wheel speed is reported as 1000 rpm
        And the wheel speed is 1001 RPM
       With a confidence of 60%

       Then the wheel speed is 1000 rpm
       With a confidence of 80 percent

Inline alternatives


  Feature: Enhancement/Sugar: the A, B, etc. option
      Demonstrate the automatic handling of single steps
      that imply multiple tests.

    Scenario: automatic unfolding of scenarios
      Given the system mode is commissioning or running
       When 10, 20, or 30 ms pass
        And 4, 6 ms pass
       Then the system mode is stopped

cascading information .


Feature: Enhancement: info cascades downward
    Demonstrate effect on scenarios.
    | mode           | acceleration allowed |
    |----------------+----------------------|
    | failsafe       | no                   |
    | normal         | yes                  |
    | fire           | no                   |
    | fire in tunnel | yes                  |
    | commissioning  | no                   |
    | shakedown      | yes                  |

cascading information ..


  Scenario: No power to wheels during an emergency
    Given the mode is not normal or shakedown
    # this fails because mode "fire in tunnel" allows motion
      And throttle 0% or not 0%
     Then power to wheels is at most 0 W
  Scenario: Power to wheels is possible when not in trouble
    Given mode is neither fire nor failsafe
    # this fails because mode "fire in tunnel" allows motion
      And throttle is greater than 0%
     Then power to wheels is greater than 0 W

Spreadsheet Integration .


  #wheel-motion-indication.feature
  Scenario Outline: Calculation of confidence

      Given that initially the wheel is turning at w_0 rpm
       With a confidence of c_0 percent

       When the wheel speed is measured as m_1... rpm
        And {ms} ms pass
       Then its computed velocity is w_1... rpm
       With a confidence of c_1... percent

Spreadsheet Integration ..


@supplier_UGE
Examples:
{{{
SELECT ms, m_*, w_*, c_*, title
FROM d:/alfha/bdd/features/dat/wheel-motion-sensor-kennlinien.txt;
}}}
# | ms  | w_0 | c_0 | m_1 | w_1 | c_1 | m_2 | w_2 | c_2 | title |
# | ms  | rpm | %   | rpm | rpm | %   | rpm | rpm | %   |       |

Lines of Code

Lines of Code

Framework LOC .


lines   LOC    #s  file
----- ----- -----  ----------------------------
  576   472    75  src/behave.tcl
  149    56    76  src/cmd_auxiliaries.tcl
   62    25    35  src/cmd_ifnoproc.tcl
   58    30    24  src/cmd_perform.tcl
  104    74    28  src/cmd_prequel.tcl
   97    35    55  src/mix_addin.tcl
  473   229   220  src/mix_gherkin.tcl
  336   143   175  src/mix_oblist.tcl

Framework LOC ..


  141    50    81  src/moc_environment.tcl
  154    55    88  src/moc_step.tcl
   35     8    25  src/mod_background.tcl
  439   165   254  src/mod_behave.tcl
  175    92    75  src/mod_context.tcl
   27     6    19  src/mod_description.tcl
  116    16    90  src/mod_environment.tcl
   77    39    34  src/mod_examples.tcl
  182   119    56  src/mod_feature.tcl
  210   112    85  src/mod_log.tcl

Framework LOC ...


  185   116    61  src/mod_scenario.tcl
   34     7    25  src/mod_scenario_outline.tcl
  141    63    72  src/mod_step.tcl
  175    97    67  src/mod_table.tcl
  144    71    67  src/mod_tags.tcl
----- ----- -----  ----------------------------
 4090  2080  1787  23

Framework LOTestC .

Tcltest test written to test the framework.

lines   LOC    #s file
----- ----- ----- ------------------------
   42    16    20 tst/all.tcl
  365   301    13 tst/behave.test
   80    57    11 tst/cmd_prequel.test
   23     8    11 tst/mix_addin.test
  258   210    16 tst/mix_gherkin.test
  337   280    15 tst/mix_oblist.test
   76    54    12 tst/mix_perform.test
  600   546    11 tst/mod_behave.test

Framework LOTestC ..


   49    28    13 tst/mod_context.test
  117    89    14 tst/mod_description.test
  206   167    11 tst/mod_examples.test
  132    99    17 tst/mod_feature.test
  155   118    15 tst/mod_log.test
  180   149    11 tst/mod_scenario.test
   55    35    11 tst/mod_step.test
  224   187    11 tst/mod_table.test
  233   184    14 tst/mod_tags.test
 3132  2528   226 17 --------------------



Questions?

Summary

There are useful extensions to Gherkin

Tcl is a good platform for enhanced Gherkin