summaryrefslogtreecommitdiff
path: root/Data/Libraries/Penlight/tests/lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-30 11:32:16 +0800
committerchai <chaifix@163.com>2021-10-30 11:32:16 +0800
commit42ec7286b2d36a9ba22925f816a17cb1cc2aa5ce (patch)
tree24bc7009457a8d7500f264e89946dc20d069294f /Data/Libraries/Penlight/tests/lua
parent164885fd98d48703bd771f802d79557b7db97431 (diff)
+ Penlight
Diffstat (limited to 'Data/Libraries/Penlight/tests/lua')
-rw-r--r--Data/Libraries/Penlight/tests/lua/animal.lua54
-rw-r--r--Data/Libraries/Penlight/tests/lua/bar.lua10
-rw-r--r--Data/Libraries/Penlight/tests/lua/foo/args.lua9
-rw-r--r--Data/Libraries/Penlight/tests/lua/mod52.lua30
-rw-r--r--Data/Libraries/Penlight/tests/lua/mymod.lua19
5 files changed, 122 insertions, 0 deletions
diff --git a/Data/Libraries/Penlight/tests/lua/animal.lua b/Data/Libraries/Penlight/tests/lua/animal.lua
new file mode 100644
index 0000000..9366db9
--- /dev/null
+++ b/Data/Libraries/Penlight/tests/lua/animal.lua
@@ -0,0 +1,54 @@
+-- Module containing classes
+local class = require 'pl.class'
+local utils = require 'pl.utils'
+local error = error
+if utils.lua51 then
+ module 'animal'
+else
+ _ENV = {}
+end
+
+class.Animal()
+
+function Animal:_init(name)
+ self.name = name
+end
+
+function Animal:__tostring()
+ return self.name..': '..self:speak()
+end
+
+class.Dog(Animal)
+
+function Dog:speak()
+ return 'bark'
+end
+
+class.Cat(Animal)
+
+function Cat:_init(name,breed)
+ self:super(name) -- must init base!
+ self.breed = breed
+end
+
+function Cat:speak()
+ return 'meow'
+end
+
+-- you may declare the methods in-line like so;
+-- note the meaning of `_base`!
+class.Lion {
+ _base = Cat;
+ speak = function(self)
+ return 'roar'
+ end
+}
+
+-- a class may handle unknown methods with `catch`:
+Lion:catch(function(self,name)
+ return function() error("no such method "..name,2) end
+end)
+
+if not utils.lua51 then
+ return _ENV
+end
diff --git a/Data/Libraries/Penlight/tests/lua/bar.lua b/Data/Libraries/Penlight/tests/lua/bar.lua
new file mode 100644
index 0000000..191cb70
--- /dev/null
+++ b/Data/Libraries/Penlight/tests/lua/bar.lua
@@ -0,0 +1,10 @@
+--- test module for demonstrating app.require_here()
+local bar = {}
+
+function bar.name ()
+ return 'bar'
+end
+
+return bar
+
+
diff --git a/Data/Libraries/Penlight/tests/lua/foo/args.lua b/Data/Libraries/Penlight/tests/lua/foo/args.lua
new file mode 100644
index 0000000..8a53130
--- /dev/null
+++ b/Data/Libraries/Penlight/tests/lua/foo/args.lua
@@ -0,0 +1,9 @@
+--- test module for demonstrating app.require_here()
+local args = {}
+
+function args.answer ()
+ return 42
+end
+
+return args
+
diff --git a/Data/Libraries/Penlight/tests/lua/mod52.lua b/Data/Libraries/Penlight/tests/lua/mod52.lua
new file mode 100644
index 0000000..3dd4e46
--- /dev/null
+++ b/Data/Libraries/Penlight/tests/lua/mod52.lua
@@ -0,0 +1,30 @@
+local test = require 'pl.test'
+local LUA_VERSION = _VERSION
+print(LUA_VERSION)
+
+-- if STRICT is true, then M is distinct from _ENV, and ONLY contains
+-- the exported functions!
+
+local _ENV,M = require 'pl.import_into' (rawget(_G,'STRICT'))
+
+function answer ()
+ -- of course, you don't have the usual global environment available
+ -- so define it as a local up above, or use utils.import(_G).
+
+ local versioned_errors = {
+ ["1"] = "attempt to call global 'print'",
+ ["2"] = "attempt to call global 'print'",
+ ["3"] = "attempt to call a nil value",
+ ["4"] = "a nil value",
+ }
+ local expected = versioned_errors[LUA_VERSION:match("Lua 5.(%d)")]
+ test.assertraise(function()
+ print 'hello'
+ end, expected)
+
+ -- but all the Penlight modules are available
+ return pretty.write(utils.split '10 20 30', '')
+end
+
+return M
+
diff --git a/Data/Libraries/Penlight/tests/lua/mymod.lua b/Data/Libraries/Penlight/tests/lua/mymod.lua
new file mode 100644
index 0000000..e7d73a7
--- /dev/null
+++ b/Data/Libraries/Penlight/tests/lua/mymod.lua
@@ -0,0 +1,19 @@
+local strict = require 'pl.strict'
+local test = require 'pl.test'
+local M = strict.module (...)
+
+function M.answer ()
+ Boo = false -- fine, it's a declared global
+ -- in strict mode, you cannot assign to globals if you aren't in main
+ test.assertraise(function()
+ Foo = true
+ end," assign to undeclared global 'Foo'")
+ return 42
+end
+
+function M.question ()
+ return 'what is the answer to Life, the Universe and Everything?'
+end
+
+return M
+