2 Calculations And String Manipulations - essenius/FitNesseFitSharpSupportFunctions GitHub Wiki

Calculate

A function that people frequently ask for is simple calculation. As said in the previous section, you need to be careful with this kind of functions since before you know it you rebuild the logic you want to test in your test cases, and then you are not really testing anything. But in some cases, it can be really useful. Here is a test page with some simple calculations:

|script|common functions      |
|check |calculate|1/3  |~=0.33|
|$a=   |calculate|3+2         |
|$b=   |calculate|20 % 13     |
|check |calculate|$a*$b|35    |

This is what you should see when running it:

Calculations

The Calculate function delivers a double result, but it can handle integer functions as the modulo (%). Other supported functions are +, -, * and /.

Is True

There is a similar function IsTrue which delivers a Boolean result. Let’s add the following line:

|ensure|is true  |$b>$a       |

We then should see as a result:

Is True

Evaluate [As]

In many cases this will be sufficient, but there are times you want a bit more control about which data type is being returned. This is when you can use the Evaluate As function. Here is an example showing when this matters:

|script|common functions                                                                   |
|check |evaluate|9999999999999999999999999998. + 1|as|decimal|9999999999999999999999999999 |
|check |evaluate|9999999999999999999999999998 + 1 |as|decimal|10000000000000000000000000000|
|check |evaluate|9999999999999999999999999998. + 1|as|double |1E+28                        |

The evaluate parser considers numbers with a decimal point to be decimal type. So, the first row evaluates in decimal, and therefore keeps its decimal precision. Numbers without decimal points are first tried as integer, then long and if that doesn't work double. So the first argument in the second example is evaluated as double (too long for a long), which makes the addition return a double, which loses precision. Then the double result is cast back to decimal. So you will get a slightly unexpected result.

The third row is evaluated as decimal, and then the result of the addition (which was still precise) is converted to a double (so loses its precision).

Precision

The supported data types are bool, Date, decimal, double, int, long and string. Here are a few of the data types we didn’t show yet in action:

|script|common functions                                  |
|$var= |echo    |abcdef                                   |
|check |evaluate|'$var'+'g'           |abcdefg            |
|check |evaluate|substring('$var',1,2)|ab                 |
|$date=|evaluate|#9-May-1995#         |as |Date           |
|check |echo    |$date                |1995-05-09T00:00:00|

Running it should give this result:

Other Data Types

For strings, a few simple functions are supported: concatenate, substring, and trim (removing leading and trailing spaces). Notice how the evaluate parser uses the hash (#) symbol to indicate a date. And we already saw the Echo function before. Furthermore, the functions Regex Escape and Regex Unescape are available, which escape characters in a string that need escaping for a regular expression and the other way around.

Using C# Reflection

If you need more calculation or string handling power than these simple fixtures can provide, you can also us a fixture that utilizes reflection to call C# functions. You are responsible yourself that you are using existing functions with the right number of parameters and the correct casing. Below table shows a few examples. As said before, use this with care. Do not recreate the logic that your tests are intended to verify this way.

!|script|Reflection Functions                              |
|check  |get|Substring(3,2)|of         |abcdef|de          |
|check  |get|Int.MaxValue  |2147483647                     |
|check  |get|Math.Sqrt(49) |7                              |
|check  |get|Math.Sqrt     |of         |16.0  |4           |
|check  |get|Substring     |with params|[3, 2]|of|abcdef|de|
|check  |get|Math.Sqrt     |with params|[64]  |8           |

Reflection Functions