Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 73 lines (69 sloc) 2.501 kb
99c7036 Raphael Speyer added jospecl_server, further jocaml-ified jospecl_client, removed the -...
authored
1 #load "unix.cma"
5a9c1f4 Raphael Speyer added examples dir and compile target
authored
2
3 open Ospecl.Spec
4 open Ospecl.Matchers
5
c22c0ae Raphael Speyer added ospecl command line runner
authored
6 #load "account.cmo";;
7
8 let specs = [
9 describe "An account" begin
10 let empty = Account.create () in
11 [
12 it "initially has a balance of 0" begin
13 expect (Account.balance empty) (is equal_to_int 0)
14 end;
15 describe "when depositing an amount" [
16 it "is not changed in-place" begin
17 ignore (Account.deposit 100 empty);
5a9c1f4 Raphael Speyer added examples dir and compile target
authored
18 expect (Account.balance empty) (is equal_to_int 0)
298ccfe Raphael Speyer changed it function to accept an expectation rather than a function
authored
19 end;
c22c0ae Raphael Speyer added ospecl command line runner
authored
20 describe "that is positive" [
21 it "increases the balance by that amount" begin
22 let to_deposit = 1 in
23 let after_deposit = Account.deposit to_deposit empty in
24 expect (Account.balance after_deposit) (is equal_to_int to_deposit)
298ccfe Raphael Speyer changed it function to accept an expectation rather than a function
authored
25 end;
c22c0ae Raphael Speyer added ospecl command line runner
authored
26 describe "multiple times" [
27 it "increases the balance by the sum of those amounts" begin
28 let to_deposit = [1;2;3;4] in
29 let total_deposits = List.fold_left (+) 0 to_deposit in
30 let after_deposits = List.fold_left (fun acct amt -> Account.deposit amt acct) empty to_deposit in
31 expect (Account.balance after_deposits) (is equal_to_int total_deposits)
32 end
33 ]
34 ];
35 describe "that is negative" [
36 it "fails" begin
37 expect
38 (fun _ -> Account.deposit (-1) empty)
39 (raise_exn (Invalid_argument "Cannot deposit a negative amount: -1"))
40 end
41 ];
42 ];
43 describe "when withdrawing an amount" [
44 describe "that is positive" [
45 describe "but less than the current balance" [
46 it "decreases the balance by that amount" begin
47 let before = Account.deposit 100 empty in
48 let after = Account.withdraw 50 before in
49 expect
50 (Account.balance after)
51 (is equal_to_int (Account.balance before - 50))
298ccfe Raphael Speyer changed it function to accept an expectation rather than a function
authored
52 end;
5a9c1f4 Raphael Speyer added examples dir and compile target
authored
53 ];
c22c0ae Raphael Speyer added ospecl command line runner
authored
54 describe "and more than the current balance" [
298ccfe Raphael Speyer changed it function to accept an expectation rather than a function
authored
55 it "fails" begin
5a9c1f4 Raphael Speyer added examples dir and compile target
authored
56 expect
c22c0ae Raphael Speyer added ospecl command line runner
authored
57 (fun _ -> Account.withdraw 50 empty)
58 (raise_exn (Account.Insufficient_funds 0))
59 end;
5a9c1f4 Raphael Speyer added examples dir and compile target
authored
60 ];
61 ];
c22c0ae Raphael Speyer added ospecl command line runner
authored
62 describe "that is negative" [
63 it "fails" begin
64 expect
65 (fun _ -> Account.withdraw (-1) empty)
66 (raise_exn (Invalid_argument "Cannot withdraw a negative amount: -1"))
67 end
5a9c1f4 Raphael Speyer added examples dir and compile target
authored
68 ]
69 ]
c22c0ae Raphael Speyer added ospecl command line runner
authored
70 ]
71 end
72 ]
Something went wrong with that request. Please try again.