erlangerleunit

Why is Eunit insisting my function is returning {ok, value} when it's not?


I'm doing something very simple: Reversing a list in Erlang without using BIFs.

Here's my attempt:

%% -----------------------------------------------------------------------------
%% Reverses the specified list.
%% reverse(List) where:
%%   * List:list() is the list to be reversed.
%% Returns: A new List with the order of its elements reversed.
%% -----------------------------------------------------------------------------
reverse(List) ->
  reverse2(List, []).

%% -----------------------------------------------------------------------------
%% If there are no more elements to move, simply return the List
%% -----------------------------------------------------------------------------
reverse2([], List) -> 
  List;

%% -----------------------------------------------------------------------------
%% Taking the head of the List and placing it as the head of our new List
%% This will result in the first element becoming the last, the 2nd becoming
%% the second last, etc etc.
%% We repeat this until we arrive at the base case above
%% -----------------------------------------------------------------------------
reverse2([H|T], List) ->
  reverse2(T, [H|List]).

Now I believe this is correct, and testing it out on the shell with random lists gives out the right answer every time. This is the code I'm using to run the Eunit testing:

%% -----------------------------------------------------------------------------
%% Tests the reverse list functionality.
%% -----------------------------------------------------------------------------
reverse_test_() -> [
  {"Reverse empty list", ?_test(begin

    % Reverse empty list.
    ?assertEqual([], util:reverse([]))
  end)},
  {"Reverse non-empty list", ?_test(begin

    % Reverse non-empty list.
    ?assertEqual([5, 4, 3, 2, 1], util:reverse([1, 2, 3, 4, 5]))
  end)}
].

And this is what running the test tells me, in particular:

error:{assertEqual,[{module,util_test},
              {line,101},
              {expression,"util : reverse ( [ ] )"},
              {expected,[]},
              {value,{ok,[]}}]}
  output:<<"">>

This is what's confusing me, my function reverse never returns anything with ok, testing it in the terminal does not output anything with ok, but testing it using EUnit on the other hand, seems to return {ok, value}


Solution

  • Actually the code looks good. Maybe you have some other function reverse/1 in util.erl what trying return {ok, Value}. Try update your tests without util: indication:

    %% -----------------------------------------------------------------------------
    %% Tests the reverse list functionality.
    %% -----------------------------------------------------------------------------
    reverse_test_() -> [
      {"Reverse empty list", ?_test(begin
    
        % Reverse empty list.
        ?assertEqual([], reverse([]))
      end)},
      {"Reverse non-empty list", ?_test(begin
    
        % Reverse non-empty list.
        ?assertEqual([5, 4, 3, 2, 1], reverse([1, 2, 3, 4, 5]))
      end)}
    ].