eproxus/meck — pull request #73 by horkhe (2012-07-31 05:21:06+00:00)
Title: Extended expect syntax and more

With this patch I would like to suggest the following changes:

1. Previously sequences of return values could be defined through special **sequence** and **loop** functions. This patch introduces an alternative syntax that allows defining return specifications right in the **expect** function. Consider:

meck:expect(Mod, f, 1, meck:val(a)), % equivalent to meck:expect(Mod, f, 1, a)
?assertEqual(a, Mod:f(1001)),
?assertEqual(a, Mod:f(1001)),

meck:expect(Mod, f, 1, meck:seq([a, b])),
?assertEqual(a, Mod:f(1001)),
?assertEqual(b, Mod:f(1001)),
?assertEqual(b, Mod:f(1001)),

meck:expect(Mod, loop, 1, meck:loop([a, b])),
?assertEqual(a, Mod:loop(1001)),
?assertEqual(b, Mod:loop(1001)),
?assertEqual(a, Mod:loop(1001)),

2. Previously expectations can be defined either as func-name/arity or as func-name/anon-func. This patch introduces the third way which is defining an expectations through a list of arg pattern/values just like defining an ordiry function but shorter. Consider:

meck:expect(Mod, f, [{[1, 1], a},
{[1, '_'], b},
{['_', '_'], c}]),
?assertEqual(a, Mod:f(1, 1)),
?assertEqual(b, Mod:f(1, 2)),
?assertEqual(c, Mod:f(2, 2)).

3. Combining return specifications and argument patterns you can create quite sophisticated expectations. Consider:

meck:expect(Mod, f, [{[1, 1], meck:seq([a, b, c])},
{[1, '_'], meck:loop([d, e])},
{['_', '_'], meck:val(f)}]),
?assertEqual(d, Mod:f(1, 2)),
?assertEqual(f, Mod:f(2, 2)),
?assertEqual(e, Mod:f(1, 2)),
?assertEqual(a, Mod:f(1, 1)),
?assertEqual(d, Mod:f(1, 2)),
?assertEqual(b, Mod:f(1, 1)),
?assertEqual(c, Mod:f(1, 1)),
?assertEqual(f, Mod:f(2, 2)),
?assertEqual(c, Mod:f(1, 1)),
?assertEqual(e, Mod:f(1, 2)),
?assertEqual(c, Mod:f(1, 1)).

4. Besides internal data structure redesign made in the scope of this patch allows easier introduction of hamcrest patterns into the expectation definition.

Adam, please consider this changes and provide your comments.