summaryrefslogtreecommitdiffstats
path: root/tests/Smoke/account.lua
diff options
context:
space:
mode:
authorYevhenii Shapovalov <yshapova@redhat.com>2017-11-09 13:39:24 +0200
committerYevhenii Shapovalov <yshapova@redhat.com>2017-11-09 13:39:24 +0200
commit7b824e0268575e4c13c624b54b2a1280aa295f7f (patch)
tree4ae2e36e9e6b708845fb15fdd002f0e20c384421 /tests/Smoke/account.lua
parent8a0d5153089bcc081fd088027b52b4c90c76b769 (diff)
downloadlua-add_test.tar.gz
lua-add_test.tar.xz
lua-add_test.zip
add testsHEADadd_test
Diffstat (limited to 'tests/Smoke/account.lua')
-rw-r--r--tests/Smoke/account.lua35
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/Smoke/account.lua b/tests/Smoke/account.lua
new file mode 100644
index 0000000..e783c01
--- /dev/null
+++ b/tests/Smoke/account.lua
@@ -0,0 +1,35 @@
+-- account.lua
+-- from PiL 1, Chapter 16
+
+Account = {balance = 0}
+
+function Account:new (o, name)
+ o = o or {name=name}
+ setmetatable(o, self)
+ self.__index = self
+ return o
+end
+
+function Account:deposit (v)
+ self.balance = self.balance + v
+end
+
+function Account:withdraw (v)
+ if v > self.balance then error("insufficient funds on account "..self.name) end
+ self.balance = self.balance - v
+end
+
+function Account:show (title)
+ print(title or "", self.name, self.balance)
+end
+
+a = Account:new(nil,"demo")
+a:show("after creation")
+a:deposit(1000.00)
+a:show("after deposit")
+a:withdraw(100.00)
+a:show("after withdraw")
+
+-- this would raise an error
+b = Account:new(nil,"DEMO")
+b:withdraw(100.00)