Tables of values

davidmfoley edited this page Sep 13, 2010 · 11 revisions

Tabular data is supported by StorEvil, with a number of different options for parsing it.

Tables are delimited with | (pipe character).

Parsing as an array of string arrays:

Create a method with a parameter that is an array of string arrays:


Example of a scenario with a table
Scenario: Example

Given the following data
|one|two|
|three|four|
|five|six|

public void Given_the_following_data(string[][] tableData) 
{
     ...
}

Parsing as an array of arrays of other types

You can also create a method that takes an array of arrays of any simple type of data that StorEvil supports (currently: int, decimal, or a custom enum)


Example of a scenario with a table
Scenario: Example

Given the following matrix
|1|2|3|
|5|6|7|
|9|10|11|

public void Given_the_following_matrix(int[][] tableData) 
{
     ... tableData[0][0] == 1, tableData[0][1] ==2, etc...
}

Mapping to an array of any custom type

You can also create a method that takes an array of any type, and storevil will match the values in the table to the properties on the type.

The first row of the table holds the names of the properties on the type:


Example of a scenario with a typed array
Scenario: Example

Given the following data
|StringParam|IntParam|
|one|1|
|three|3|
|five|5|

public void Given_the_following_data(MyTestCase[] cases) 
{
    ... will get an array with 3 instances of MyTestCase, with the properties set to the values in the list...
    ... cases[0].StringParam == "one"
    ... cases[2].IntParam == 5
    ... etc. ...
}

public class MyTestCase {
    public string StringParam { get; set; }
    public int IntParam {get; set;}
}

Mapping to a single instance of a type

You can also create a method that takes a single instance of any type.
This works a little bit differently: each row is a single property name/value pair:


Example of a scenario with a single instance
Scenario: Example

Given the following object
|StringParam|string|
|IntParam|42|

public void Given_the_following_object(MyTestCase case) 
{
     ... case.StringParam = "string",  case.IntParam = 42 ...
}

public class MyTestCase {
    public string StringParam { get; set; }
    public int IntParam {get; set;}
}

Mapping to a Hashtable

You can also create a method that takes a Hashtable or Dictionary
Each row is a single key/value pair:


Example of a scenario with a hashtable
Scenario: Example

Given the following hashtable
|foo|bar|
|answer|42|

public void Given_the_following_hashtable(Hashtable table) 
{
     ... table["foo"] == bar, table["answer"] == 42 ...
}