Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Mock parametrized modules #55

Merged
merged 2 commits into from

2 participants

Shunichi Shinohara Adam Lindberg
Shunichi Shinohara

This patch enables mocking of parametrized modules.

Change is made in backup of original modules.
Parametrized modules should have module variables in their module attributes.
Without these variables, we can not compile original forms.

Adam Lindberg
Owner

Great work! Perfect pull request with nice code and good tests. :-D

Adam Lindberg eproxus merged commit 850fbff into from
Adam Lindberg
Owner

Will close #4 once the tests pass on TravisCI (they fail right now because of some connectivity issue towards GitHub).

Shunichi Shinohara

Thank you for rapid merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 21, 2012
  1. Handle module paramter in converting module attribute

    Shunichi Shinohara authored
Commits on Jan 22, 2012
  1. Add unit tests of parametrized module mocking

    Shunichi Shinohara authored
This page is out of date. Refresh to see the latest.
9 src/meck_mod.erl
View
@@ -80,8 +80,13 @@ compile_options(Module) ->
filter_options(proplists:get_value(options, Module:module_info(compile))).
-spec rename_module(erlang_form(), module()) -> erlang_form().
-rename_module([{attribute, Line, module, _OldName}|T], NewName) ->
- [{attribute, Line, module, NewName}|T];
+rename_module([{attribute, Line, module, OldAttribute}|T], NewName) ->
+ case OldAttribute of
+ {_OldName, Variables} ->
+ [{attribute, Line, module, {NewName, Variables}}|T];
+ _OldName ->
+ [{attribute, Line, module, NewName}|T]
+ end;
rename_module([H|T], NewName) ->
[H|rename_module(T, NewName)].
7 test/meck_test_parametrized_module.erl
View
@@ -0,0 +1,7 @@
+-module(meck_test_parametrized_module, [Var1, Var2]).
+-export([which/0, var1/0, var2/0]).
+
+which() -> original.
+
+var1() -> {original, Var1}.
+var2() -> {original, Var2}.
26 test/meck_tests.erl
View
@@ -807,6 +807,32 @@ cannot_expect_bif_or_autogenerated_test() ->
meck:expect(unicode, module_info, 0, doh)),
?assertEqual(ok, meck:unload(unicode)).
+meck_parametrized_module_test() ->
+ ?assertEqual(ok, meck:new(meck_test_parametrized_module)),
+ ?assertEqual(ok, meck:expect(meck_test_parametrized_module, new,
+ fun(V1, V2) ->
+ {meck_test_parametrized_module, V1, V2}
+ end)),
+ ?assertEqual(ok, meck:expect(meck_test_parametrized_module, which, 1, mecked)),
+ Object = meck_test_parametrized_module:new(var1, var2),
+ ?assertEqual(mecked, Object:which()),
+ ?assertEqual(ok, meck:unload(meck_test_parametrized_module)).
+
+meck_parametrized_module_passthrough_test() ->
+ ?assertEqual(ok, meck:new(meck_test_parametrized_module, [passthrough])),
+ ?assertEqual(ok, meck:expect(meck_test_parametrized_module, new,
+ fun(V1, V2) ->
+ {meck_test_parametrized_module, V1, V2}
+ end)),
+ ?assertEqual(ok, meck:expect(meck_test_parametrized_module, var2,
+ fun({_, _Var1, Var2} = _This) ->
+ {mecked, Var2}
+ end)),
+ Object = meck_test_parametrized_module:new(var1, var2),
+ ?assertEqual({original, var1}, Object:var1()),
+ ?assertEqual({mecked, var2}, Object:var2()),
+ ?assertEqual(ok, meck:unload(meck_test_parametrized_module)).
+
%%==============================================================================
%% Internal Functions
%%==============================================================================
Something went wrong with that request. Please try again.