运行测试用例时,元组列表不匹配

时间:2016-09-09 18:51:53

标签: erlang eunit

我有一段非常简单的代码:

-module(tuples_from_file).

-export([parse/0]).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

%%%==================================================================
%%% Export
%%%==================================================================
parse() ->
  {ok, File} = file:read_file("assets/tests/input/tuples_from_file.txt"),
  %%io:format("File >> ~p~n", [File]),
  List = binary:split(File, [<<" ">>, <<"\t">>, <<"\n">>], [global, trim_all]),
  %%io:format("List >> ~p~n", [List]),
  %%io:format("build_tuples >> ~p~n", [build_tuples(List, [])]),
  build_tuples(List, []).

-ifdef(TEST).
parse_test() ->
  R1 = [{<<"1">>, <<"7">>}, {<<"11">>, <<"0">>}, {<<"1">>, <<"3">>}, {<<"5">>, <<"0">>}, {<<"7">>, <<"0">>},
    {<<"1">>, <<"8">>}, {<<"10">>, <<"0">>}, {<<"1">>, <<"11">>}, {<<"99">>, <<"0">>}],
  ?assertEqual(R1, tuples_from_file:parse()).
-endif.

%%%==================================================================
%%% Internal
%%%==================================================================
build_tuples([X, Y | T], Acc) -> build_tuples(T, [{X, Y} | Acc]);
build_tuples([X | T], Acc) -> build_tuples(T, [{X, undefined} | Acc]);
build_tuples([], Acc) -> lists:reverse(Acc).

...这是tuples_from_file.txt

的内容
1 7 11 0
1 3 5 0 7 0
1 8 10 0 1 11
99 0

当我运行示例(不是单元测试)时,我得到的输出与R1上的输出完全相同。

为什么assertEqual总是失败,说:

...
in function tuples_from_file:parse/0 (src/unclassified/tuples_from_file.erl, line 20)
in call from tuples_from_file:'-parse_test/0-fun-0-'/1 (src/unclassified/tuples_from_file.erl, line 31)
**error:{badmatch,{error,enoent}}

ERROR: One or more eunit tests failed.
ERROR: eunit failed while processing /Users/vvera/Workshop/Erlang/erlang_contests: rebar_abort

这是项目结构:

   erlang_contests
   |-assets
   |---tests
   |-----input
   |-----output
   |-doc
   |-ebin
   |-include
   |-priv
   |-src
   |---leetcode
   |-----algorithms
   |---unclassified
   |-test
   |---leetcode
   |-----algorithms
   |---unclassified

0 个答案:

没有答案