tests/pass/demo_account.lua

Wed, 24 Nov 2010 02:52:37 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 24 Nov 2010 02:52:37 +0000
changeset 138
f9bb0e212d28
parent 69
26ee626eda90
permissions
-rw-r--r--

Replace error reporting code with calls to LVM.traceback()

57
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- account.lua
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- from PiL 1, Chapter 16
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
69
26ee626eda90 Move the demo_account.lua test from fail to pass - yay!
Matthew Wild <mwild1@gmail.com>
parents: 57
diff changeset
4 Account = {balance = 0, name = "base"}
26ee626eda90 Move the demo_account.lua test from fail to pass - yay!
Matthew Wild <mwild1@gmail.com>
parents: 57
diff changeset
5 Account.__index = Account;
57
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 function Account:new (o, name)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 o = o or {name=name}
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 setmetatable(o, self)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 return o
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 function Account:deposit (v)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 self.balance = self.balance + v
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 function Account:withdraw (v)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 if v > self.balance then error("insufficient funds on account "..self.name) end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 self.balance = self.balance - v
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 function Account:show (title)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 print(title or "", self.name, self.balance)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 a = Account:new(nil,"demo")
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 a:show("after creation")
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 a:deposit(1000.00)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 a:show("after deposit")
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 a:withdraw(100.00)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 a:show("after withdraw")
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 -- this would raise an error
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 --[[
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 b = Account:new(nil,"DEMO")
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 b:withdraw(100.00)
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 --]]
3e148db7f933 Add tests/ to the repo
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38

mercurial