From 5171d4348062a388054039054fc62b764ed6510f Mon Sep 17 00:00:00 2001 From: Gianfranco Alongi Date: Sun, 19 Feb 2012 16:16:47 +0100 Subject: [PATCH] Added negative test for when the input does not contain the total even though there are rows. --- One/Solution/src/ros_parser.erl | 17 +++++++++++------ One/Solution/test/ros_parser_tests.erl | 5 +++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/One/Solution/src/ros_parser.erl b/One/Solution/src/ros_parser.erl index f747fe2..18567c9 100644 --- a/One/Solution/src/ros_parser.erl +++ b/One/Solution/src/ros_parser.erl @@ -6,16 +6,21 @@ parse("")-> {error,no_total}; parse(Input) -> - [Total|Lines] = lists:reverse(string:tokens(Input,"\n")), - Entries = lists:sort([ parse_row(Line) || Line <- Lines]), - {ok,#ros{total = list_to_integer(Total), - entries = Entries}}. + [Total|Lines] = lists:reverse(string:tokens(Input,"\n")), + case (catch list_to_integer(Total)) of + {'EXIT',_ } -> + {error,no_total}; + IntTotal -> + Entries = lists:sort([ parse_row(Line) || Line <- Lines]), + {ok,#ros{total = IntTotal, + entries = Entries}} + end. parse_row(Line) -> [Type,Sold,Projected] = string:tokens(Line,","), #entry{type = Type, - sold = list_to_integer(Sold), - projected = list_to_integer(Projected)}. + sold = list_to_integer(Sold), + projected = list_to_integer(Projected)}. diff --git a/One/Solution/test/ros_parser_tests.erl b/One/Solution/test/ros_parser_tests.erl index f00dea0..dfafc7d 100644 --- a/One/Solution/test/ros_parser_tests.erl +++ b/One/Solution/test/ros_parser_tests.erl @@ -18,4 +18,9 @@ ros_parse_basic_test() -> ], total = 3}}, 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)). +