【问题标题】:Why is Eunit insisting my function is returning {ok, value} when it's not?为什么 Eunit 坚持我的函数返回 {ok, value} 而不是?
【发布时间】:2020-05-18 03:16:33
【问题描述】:

我正在做一些非常简单的事情:在不使用 BIF 的情况下在 Erlang 中反转列表。

这是我的尝试:

%% -----------------------------------------------------------------------------
%% 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]).

现在我相信这是正确的,并且使用随机列表在 shell 上对其进行测试,每次都会给出正确的答案。这是我用来运行 Eunit 测试的代码:

%% -----------------------------------------------------------------------------
%% 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)}
].

这是运行测试告诉我的,特别是:

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

这让我感到困惑,我的函数reverse 永远不会用ok 返回任何东西,在终端中测试它不会用ok 输出任何东西,但另一方面使用EUnit 测试它,似乎返回@ 987654327@

【问题讨论】:

    标签: erlang erl eunit


    【解决方案1】:

    实际上代码看起来不错。也许你在util.erl 中有一些其他功能reverse/1 尝试返回{ok, Value}。尝试在没有util: 指示的情况下更新您的测试:

    %% -----------------------------------------------------------------------------
    %% 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)}
    ].
    

    【讨论】:

    • 这其实很尴尬,但问题是我忘记编译代码了。以前的版本确实返回了ok。一次只使用一个 erl 实例应该给我一个教训。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2021-01-06
    • 1970-01-01
    相关资源
    最近更新 更多