I spend a lot of time fixing my typos and other mistakes. I have to use tests to ease it up.
So I remembered my java coding time and I came with the idea of twtest (same idea as jtest).
Here is what I achieved in two days:
This is only a tool for testing filter runs, especially for testing functions. So far, it is meant for functions returning a single elements. (*void^*)
is my convention to tell there is no tiddler in the result.
This is made by a small CSS sheet and a single template. Here is that one.
\parameters(label:"no label" filter:"[[no filter]]", value:"no value")
\function .wrong(label, awaited)
[dump[input for .wrong]]
+[first[]]
:map[[<dl class="wrong"><dt>$(label)$</dt><dd>ERROR</dd><dd>GOT: <samp class="got"><$text text="""$(currentTiddler)$"""/></samp></dd><dd>AWAITED: <samp class="awaited"><$text text="""$(awaited)$"""/></samp></dd></dl>]substitute[]]
\end .wrong
\function .wrongVoid(label, awaited)
[[<dl class="wrong"><dt>$(label)$</dt><dd>ERROR</dd><dd>GOT: <samp class="got">(* void! *)</samp></dd><dd>AWAITED: <samp class="awaited"><$text text="""$(awaited)$"""/></samp></dd></dl>]substitute[]dump[.wrongVoid result]]
\end .wrongVoid
\function .correct(label)
+[first[]]
[[<dl class="correct"><dt>$(label)$</dt><dd>SUCCESS</dd></dl>]substitute[]]
\end .correct
\function .verdict(label, value)
[all[]match<value>]
:then[.correct<label>]
:else[.wrong<label>,<value>]
\end .verdict
\function .verdictIfVoid(label, value)
[<value>first[]]
:then[.wrongVoid<label>,<value>]
:else[.correct<label>]
\end .verdictIfVoid
\function .test(label, filter, value)
[subfilter<filter>]
:map[.verdict<label>,<value>]
else[.verdictIfVoid<label>,<value>]
\end .test
<$let res = {{{ [.test<label>,<filter>,<value>] }}}
real = {{{ [subfilter<filter>dump[real value]] }}}
>
<!--
* filter: <code><<filter>></code>
* awaited value: <samp><$text text=<<value>>/></samp>
* real value: <samp><$text text=<<real>>/></samp>
* verdict text: <samp><$text text=<<res>>/></samp>
-->
<<res>>
</$let>
And an example of use, corresponding to the image shown.
<$let project=bdmsda model=phpdat>
<$transclude $tiddler="$:/user/twtest/templates/ftest.template"
label="`.getLocation` standard success case"
filter="[.getLocation[holes]]"
value="$:/user/data/pcdgen/holes/$(model)$"
/>
<$transclude $tiddler="$:/user/twtest/templates/ftest.template"
label="`.getLocation` with unknown reference"
filter="[.getLocation[dummy]]"
value=""
/>
<$transclude $tiddler="$:/user/twtest/templates/ftest.template"
label="`.getLocation` test filter that is broken (to test .wrong)"
filter="[.getLocation[holes]]"
value="$:/user/data/pcdgen/hole/$(model)$"
/>
<$transclude $tiddler="$:/user/twtest/templates/ftest.template"
label="`.getLocation` test filter that can't work as intended (to test .wrongVoid)"
filter="[.getLocation[hole]]"
value="$:/user/data/pcdgen/hole/$(model)$"
/>
<$transclude $tiddler="$:/user/twtest/templates/ftest.template"
label="`.getLocation` improper input"
filter="[.getLocation[hole]]"
value=""
/>
</$let>
What do you think of it? I know these are a lot of problem that will arise. Especially, mocking might be tricky. Here I am using the real stuff for testing, which not what should be done. My first task was to see how I could test a filter run.That is done. So, how would you try to handle this mocking issue?