Skip to content

Commit

Permalink
Added negative test for when the input does not contain
Browse files Browse the repository at this point in the history
the total even though there are rows.
  • Loading branch information
Gianfranco Alongi committed Feb 19, 2012
1 parent d1f12c1 commit 5171d43
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
17 changes: 11 additions & 6 deletions One/Solution/src/ros_parser.erl
Expand Up @@ -6,16 +6,21 @@
parse("")-> {error,no_total}; parse("")-> {error,no_total};


parse(Input) -> parse(Input) ->
[Total|Lines] = lists:reverse(string:tokens(Input,"\n")), [Total|Lines] = lists:reverse(string:tokens(Input,"\n")),
Entries = lists:sort([ parse_row(Line) || Line <- Lines]), case (catch list_to_integer(Total)) of
{ok,#ros{total = list_to_integer(Total), {'EXIT',_ } ->
entries = Entries}}. {error,no_total};
IntTotal ->
Entries = lists:sort([ parse_row(Line) || Line <- Lines]),
{ok,#ros{total = IntTotal,
entries = Entries}}
end.


parse_row(Line) -> parse_row(Line) ->
[Type,Sold,Projected] = string:tokens(Line,","), [Type,Sold,Projected] = string:tokens(Line,","),
#entry{type = Type, #entry{type = Type,
sold = list_to_integer(Sold), sold = list_to_integer(Sold),
projected = list_to_integer(Projected)}. projected = list_to_integer(Projected)}.






5 changes: 5 additions & 0 deletions One/Solution/test/ros_parser_tests.erl
Expand Up @@ -18,4 +18,9 @@ ros_parse_basic_test() ->
], ],
total = 3}}, total = 3}},
ros_parser:parse(Input)). ros_parser:parse(Input)).

ros_parse_negative_no_total_but_rows_test() ->
Input = "a,1,1\nb,2,2",
?assertMatch({error,no_total},ros_parser:parse(Input)).



0 comments on commit 5171d43

Please sign in to comment.