David Garamond <lists / zara.6.isreserved.com> wrote:
> For deterministic functions/methods, the same set of inputs should 
> always produces the same output. What about adding something like:
> 
>   assert_follows_spec(method_or_class_name, spec)
> 
> Where spec is an array of input and output pairs (and optionally an 
> exception, if the input should not be accepted):
> 
>   [{input => [1, 1, 1], output => 1},
>    {input => [8, 1, 2], output => 4},
>    {input => [8, 3, 4], output => 6},
>    {input => [4, 3, 2], output => 6},
>    {input => [1, 2, nil], exception => ArgumentError},
>    {input => [1, 2, 0], exception => ZeroDivisionError}]
> 
> This can make unit tests shorter and more readable. We can transform the 
> spec into an HTML table, include it into the API documentation, etc.
> 
> The assert_follows_spec() could even perform several iterations of tests 
> if wanted, to test deterministic-ness.

Good idea..  for ages I have been using

io = [
  ['inputx', 'outputx'],
  ['bla', 'outputbla'],
  ['ruby', 'outputruby'],
]
input, expected = io.transpose
actual = input.map{|i| stuff(i) }
assert_equal(expected, actual)

If there are too many input/output entries, then its difficult to 
quickly identify where the problem are located.

A more well-crafted assertion method, which are able to output
more precisely where the problems are located, would be greak.

-- 
Simon Strandgaard