Skip to content
Permalink
Browse files
Isolate hamcrest elements from static analysis issues
  • Loading branch information
paulo-ferraz-oliveira authored and eproxus committed Jan 20, 2021
1 parent 1b63047 commit f6fa0e38f7bd5521ce4f1d54c30a23e001571313
Showing 2 changed files with 10 additions and 5 deletions.
@@ -679,7 +679,7 @@ exec(Fun) -> meck_ret_spec:exec(Fun).
-spec is(MatcherImpl) -> matcher() when
MatcherImpl :: Predicate | HamcrestMatcher,
Predicate :: fun((any()) -> any()),
HamcrestMatcher :: hamcrest:matchspec().
HamcrestMatcher :: meck_matcher:hamcrest_matchspec().
is(MatcherImpl) ->
meck_matcher:new(MatcherImpl).

@@ -27,25 +27,30 @@
-export([is_matcher/1]).
-export([match_ignore/2]).

-ignore_xref({hamcrest, is_matcher, 1}).
-ignore_xref({hamcrest, assert_that, 2}).

%%%============================================================================
%%% Definitions
%%%============================================================================

-record('$meck.matcher', {type :: predicate | hamcrest,
impl :: predicate() | hamcrest:matchspec()}).
impl :: predicate() | hamcrest_matchspec()}).

%%%============================================================================
%%% Types
%%%============================================================================

-type predicate() :: fun((X::any()) -> any()).
-type matcher() :: #'$meck.matcher'{}.
-type hamcrest_matchspec() :: tuple(). %% #'hamcrest.matchspec'{}
-export_type([hamcrest_matchspec/0]).

%%%============================================================================
%%% API
%%%============================================================================

-spec new(predicate() | hamcrest:matchspec()) -> matcher().
-spec new(predicate() | hamcrest_matchspec()) -> matcher().
new(Predicate) when is_function(Predicate) ->
{arity, 1} = erlang:fun_info(Predicate, arity),
#'$meck.matcher'{type = predicate, impl = Predicate};
@@ -68,7 +73,7 @@ is_matcher(_Other) -> false.
match_ignore(Value, #'$meck.matcher'{type = predicate, impl = Predicate}) ->
Predicate(Value) == true;
match_ignore(Value, #'$meck.matcher'{type = hamcrest, impl = HamcrestMatcher}) ->
(catch hamcrest:assert_that(Value, HamcrestMatcher)) == true;
(catch erlang:apply(hamcrest, assert_that, [Value, HamcrestMatcher])) == true;
match_ignore(_Value, _NotMatcher) ->
true.

@@ -78,6 +83,6 @@ match_ignore(_Value, _NotMatcher) ->

-spec is_hamcrest_matcher(any()) -> boolean().
is_hamcrest_matcher(Something) ->
try hamcrest:is_matcher(Something)
try erlang:apply(hamcrest, is_matcher, [Something])
catch _Class:_Reason -> false
end.

0 comments on commit f6fa0e3

Please sign in to comment.