-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.ml
76 lines (63 loc) · 2.05 KB
/
command.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
open OUnit2
type direction =
| Right | Left | Up | Down
type command =
| Go of direction
| Map
| Stats
| Quit
type choice = Yes | No
exception Empty
exception Malformed
let rec remove_blanks lst =
match lst with
| [] -> []
| h::t -> begin
match h with
| "" -> remove_blanks t
| _ -> h::(remove_blanks t)
end
let parse str =
let temp = str |> String.trim |> String.escaped |> String.split_on_char ' ' |> remove_blanks in
match (temp) with
| [] -> raise (Empty)
| h::t -> begin
match h with
| "right" | "east" -> Go Right
| "left" | "west" -> Go Left
| "up" | "north" -> Go Up
| "down" | "south" -> Go Down
| "map" | "board" | "where" -> Map
| "stats" | "status" | "check" | "well-being" | "me" -> Stats
| "quit" | "exit" | "end" -> if (t == []) then Quit else raise (Malformed)
| _ -> raise (Malformed) end
let eff_parse str =
let temp = str |> String.trim |> String.escaped |> String.split_on_char ' ' |> remove_blanks in
match (temp) with
| [] -> raise (Empty)
| h::t -> begin
match h with
| "yes" | "Yes" | "YES" -> Yes
| "no" | "No" | "NO" -> No
| _ -> raise (Malformed) end
(* ------------------------------------------------- *)
(* CODE FOR TESTING *)
(** [make_parse_test name input expected_output] constructs an
OUnit test named [name] that asserts the quality of [expected_output] with [parse input]. *)
let make_parse_test
(name : string)
(input: string)
(expected_output : command) : test =
name >:: (fun _ -> assert_equal expected_output (parse input))
let parse_test = [
make_parse_test "Going Right" "right" (Go Right);
make_parse_test "Going Left" "west" (Go Left);
make_parse_test "Going Up" "up and down and all around" (Go Up);
make_parse_test "Going Down_with Spaces" " down " (Go Down);
make_parse_test "Viewing the map" "board game" (Map);
make_parse_test "Check Stats" "well-being" (Stats);
make_parse_test "Quitting" "quit" (Quit);
]
let tests = List.flatten [
parse_test
]