1
exercism fetch ocaml hangman

test.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
77
78
79
80
81
82
83
84
85
86
87
open OUnit2
open React

open Hangman

(* Assert Equals Int *)
let aei exp got =
  assert_equal exp got ~printer:string_of_int

(* Assert Equals String *)
let aes exp got =
  assert_equal exp got ~printer:(fun s -> s)

(* Assert Equals Progress *)
let aep exp got =
  let string_of_progress = function
      | Win -> "Win"
      | Lose -> "Lose"
      | Busy n -> "Busy " ^ string_of_int n in
  assert_equal exp got ~printer:string_of_progress

let tests = [
    "initially 9 failures are allowed">::(fun _ ->
        let hm = create "foo" in
        aep (Busy 9) (S.value (progress hm))
    );
    "initially no letters are guessed">::(fun _ ->
        let hm = create "foo" in
        aes "___" (S.value (masked_word hm))
    );
    "after 10 failures the game is over">::(fun _ ->
        let hm = create "foo" in
        begin
            for i = 1 to 9 do
                feed 'x' hm;
                aep (Busy (9-i)) (S.value (progress hm))
            done;
            feed 'x' hm;
            aep Lose (S.value (progress hm))
        end;
    );
    "feeding a correct letter removes underscores">::(fun _ ->
        let hm = create "foobar" in
        begin
            feed 'b' hm;
            aep (Busy 9) (S.value (progress hm));
            aes "___b__" (S.value (masked_word hm));
            feed 'o' hm;
            aep (Busy 9) (S.value (progress hm));
            aes "_oob__" (S.value (masked_word hm));
        end;
    );
    "feeding a correct letter twice counts as a failure">::(fun _ ->
        let hm = create "foobar" in
        begin
            feed 'b' hm;
            aep (Busy 9) (S.value (progress hm));
            aes "___b__" (S.value (masked_word hm));
            feed 'b' hm;
            aep (Busy 8) (S.value (progress hm));
            aes "___b__" (S.value (masked_word hm));
        end;
    );
    "getting all the letters right makes for a win">::(fun _ ->
        let hm = create "hello" in
        begin
            feed 'b' hm;
            aep (Busy 8) (S.value (progress hm));
            aes "_____" (S.value (masked_word hm));
            feed 'e' hm;
            aep (Busy 8) (S.value (progress hm));
            aes "_e___" (S.value (masked_word hm));
            feed 'l' hm;
            aep (Busy 8) (S.value (progress hm));
            aes "_ell_" (S.value (masked_word hm));
            feed 'o' hm;
            aep (Busy 8) (S.value (progress hm));
            aes "_ello" (S.value (masked_word hm));
            feed 'h' hm;
            aep Win (S.value (progress hm));
            aes "hello" (S.value (masked_word hm));
        end;
    );
]

let () =
  run_test_tt_main ("hangman tests" >::: tests)