eproxus / meck

A mocking library for Erlang
http://eproxus.github.io/meck
Apache License 2.0
811 stars 231 forks source link

One of the example shown on README page doesn't work #224

Closed intjelic closed 3 years ago

intjelic commented 3 years ago

One of the example shown on README page doesn't work.

Eshell V5.8.4  (abort with ^G)
1> meck:new(string, [unstick]).
ok
2> meck:expect(string, strip, fun(String) -> meck:passthrough([String]) end).
ok
3> string:strip("  test  ").
"test"
4> meck:unload(string).
ok
5> string:strip("  test  ").
"test"

As soon as I enter meck:expect(string, strip, fun(String) -> meck:passthrough([String]) end)., it will crash (output below). I didn't try the other examples to see if they work.

Meck version: master Erlang version: 22 OS: Ubuntu: 20.04

The output I get.

Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Eshell V10.6.4  (abort with ^G)
1> meck:new(string, [unstick]).
ok
2> *** ERROR: Shell process terminated! ***
2021-02-15T08:40:16.939964+07:00 error: FORMATTER CRASH: {"Error in process ~p with exit value:~n~p~n",[<0.64.0>,{undef,[{string,to_graphemes,["me"],[]},{edlin,do_op,4,[{file,"edlin.erl"},{line,286}]},{edlin,edit,5,[{file,"edlin.erl"},{line,146}]},{group,more_data,6,[{file,"group.erl"},{line,668}]},{group,get_chars_loop,9,[{file,"group.erl"},{line,471}]},{group,io_request,6,[{file,"group.erl"},{line,181}]},{group,server_loop,3,[{file,"group.erl"},{line,117}]}]}]}
Eshell V10.6.4  (abort with ^G)
1> =ERROR REPORT==== 15-Feb-2021::08:40:16.969572 ===
** Generic server string_meck terminating 
** Last message in was {'EXIT',<0.78.0>,killed}
** When Server state == {state,string,
                               [{is_empty,1},
                                {equal,2},
                                {equal,3},
                                {equal,4},
                                {slice,2},
                                {pad,2},
                                {pad,3}, 
                                {pad,4},
                                {trim,1},
                                {trim,2},
                                {chomp,1},
                                {take,2},
                                {take,3},
                                {take,4},
                                {titlecase,1},
                                {casefold,1},
                                {to_integer,1},
                                {to_float,1}, 
                                {prefix,2},
                                {split,2},
                                {split,3},
                                {replace,3},
                                {replace,4},
                                {nth_lexeme,3},
                                {find,3},
                                {next_codepoint,1},
                                {len,1},
                                {concat,2},
                                {chr,2},
                                {rchr,2},
                                {str,2},
                                {rstr,2},
                                {span,2},
                                {cspan,2},
                                {substr,2},
                                {substr,3},
                                {tokens,2},
                                {chars,2},
                                {chars,3},
                                {copies,2},
                                {words,1},
                                {words,2},
                                {sub_word,2},
                                {sub_word,3},
                                {strip,1},
                                {strip,2},
                                {strip,3},
                                {left,2},
                                {left,3},
                                {right,2},
                                {right,3},
                                {centre,2},
                                {centre,3},
                                {sub_string,2},
                                {sub_string,3},
                                {to_lower,1},
                                {to_upper,1},
                                {join,2},
                                {next_grapheme,1},
                                {length,1},
                                {to_graphemes,1},
                                {reverse,1},
                                {uppercase,1},
                                {lowercase,1},
                                {lexemes,2},
                                {slice,3},
                                {trim,3},
                                {find,2}], 
                               {dict,0,16,16,8,80,48,
                                     {[],[],[],[],[],[],[],[],[],[],[],[],[],
                                      [],[],[]},
                                     {{[],[],[],[],[],[],[],[],[],[],[],[],[],
                                       [],[],[]}}},
                               true,[],
                               {false,no_passthrough_cover},
                               true,false,false,undefined,[]}
** Reason for termination ==
** killed

=CRASH REPORT==== 15-Feb-2021::08:40:16.970630 ===
  crasher:
    initial call: meck_proc:init/1 
    pid: <0.80.0>
    registered_name: string_meck
    exception exit: killed
      in function  gen_server:decode_msg/9 (gen_server.erl, line 432)
    ancestors: [<0.78.0>]
    message_queue_len: 0
    messages: []
    links: []
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 75113
    stack_size: 27
    reductions: 138922
  neighbours:

1> 

Note that the command meck:expect(string, strip, fun(String) -> meck:passthrough([String]) end). seems to be erased from the terminal output as soon as I enter it.

paulo-ferraz-oliveira commented 3 years ago

This does not happen on OTP 19.3, though.

eproxus commented 3 years ago

The problem is, the example is missing the passthrough flag when creating the mock and it seems in newer versions of Erlang the string module is used more by the shell (hence the crash because the module doesn't have the to_graphemes/1 function anymore when mocked).

I'll update the docs.