This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
forked from ewmailing/lua-TestMore
-
Notifications
You must be signed in to change notification settings - Fork 2
/
303-package.t
executable file
·217 lines (174 loc) · 4.44 KB
/
303-package.t
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#! /usr/bin/lua
--
-- lua-TestMore : <http://fperrad.github.com/lua-TestMore/>
--
-- Copyright (C) 2009-2011, Perrad Francois
--
-- This code is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--
--[[
=head1 Lua Package Library
=head2 Synopsis
% prove 303-package.t
=head2 Description
Tests Lua Package Library
See "Lua 5.2 Reference Manual", section 6.3 "Modules",
L<http://www.lua.org/manual/5.2/manual.html#6.3>.
=cut
--]]
require 'Test.More'
plan(33)
ok(package.loaded._G, "table package.loaded")
ok(package.loaded.coroutine)
ok(package.loaded.io)
ok(package.loaded.math)
ok(package.loaded.os)
ok(package.loaded.package)
ok(package.loaded.string)
ok(package.loaded.table)
type_ok(package.path, 'string')
type_ok(package.preload, 'table', "table package.preload")
is(# package.preload, 0)
if (platform and platform.compat)
or (arg[-1] == 'luajit') then
type_ok(package.loaders, 'table', "table package.loaders")
if arg[-1] == 'luajit' then
todo("LuaJIT TODO. package.searchers", 1)
end
is(package.searchers, package.loaders, "alias")
else
type_ok(package.searchers, 'table', "table package.searchers")
is(package.loaders, nil)
end
m = {}
if (platform and platform.compat)
or (arg[-1] == 'luajit') then
package.seeall(m)
m.pass("function package.seeall")
else
is(package.seeall, nil, "package.seeall (removed)")
end
local m = require 'Test.More'
m.ok(true, "function require")
is(m, package.loaded['Test.More'])
p = package.searchpath('Test.More', package.path)
type_ok(p, 'string', "searchpath")
p = package.searchpath('Test.More', 'bad path')
is(p, nil)
f = io.open('complex.lua', 'w')
f:write [[
complex = {}
function complex.new (r, i) return {r=r, i=i} end
--defines a constant 'i'
complex.i = complex.new(0, 1)
function complex.add (c1, c2)
return complex.new(c1.r + c2.r, c1.i + c2.i)
end
function complex.sub (c1, c2)
return complex.new(c1.r - c2.r, c1.i - c2.i)
end
function complex.mul (c1, c2)
return complex.new(c1.r*c2.r - c1.i*c2.i,
c1.r*c2.i + c1.i*c2.r)
end
local function inv (c)
local n = c.r^2 + c.i^2
return complex.new(c.r/n, -c.i/n)
end
function complex.div (c1, c2)
return complex.mul(c1, inv(c2))
end
return complex
]]
f:close()
m = require 'complex'
is(m, complex, "function require")
is(complex.i.r, 0)
is(complex.i.i, 1)
os.remove('complex.lua') -- clean up
error_like(function () require('no_module') end,
"^[^:]+:%d+: module 'no_module' not found:",
"function require (no module)")
f = io.open('foo.lua', 'w')
f:write [[?syntax error?]]
f:close()
error_like(function () require('foo') end,
"^error loading module 'foo' from file '%.[/\\]foo%.lua':",
"function require (syntax error)")
os.remove('foo.lua') -- clean up
foo = {}
foo.bar = 1234
function foo_loader ()
return foo
end
package.preload.foo = foo_loader
m = require 'foo'
assert(m == foo)
is(m.bar, 1234, "function require & package.preload")
f = io.open('bar.lua', 'w')
f:write [[
print(" in bar.lua", ...)
a = ...
]]
f:close()
a = nil
require 'bar'
is(a, 'bar', "function require (arg)")
os.remove('bar.lua') -- clean up
f = io.open('cplx.lua', 'w')
f:write [[
-- print('cplx.lua', ...)
local _G = _G
_ENV = nil
local cplx = {}
local function new (r, i) return {r=r, i=i} end
cplx.new = new
--defines a constant 'i'
cplx.i = new(0, 1)
function cplx.add (c1, c2)
return new(c1.r + c2.r, c1.i + c2.i)
end
function cplx.sub (c1, c2)
return new(c1.r - c2.r, c1.i - c2.i)
end
function cplx.mul (c1, c2)
return new(c1.r*c2.r - c1.i*c2.i,
c1.r*c2.i + c1.i*c2.r)
end
local function inv (c)
local n = c.r^2 + c.i^2
return new(c.r/n, -c.i/n)
end
function cplx.div (c1, c2)
return mul(c1, inv(c2))
end
_G.cplx = cplx
return cplx
]]
f:close()
require 'cplx'
is(cplx.i.r, 0, "function require & module")
is(cplx.i.i, 1)
os.remove('cplx.lua') -- clean up
if (platform and platform.compat)
or (arg[-1] == 'luajit') then
is(mod, nil, "function module & seeall")
module('mod', package.seeall)
type_ok(mod, 'table')
is(mod, package.loaded.mod)
is(modz, nil, "function module")
local _G = _G
module('modz')
_G.type_ok(_G.modz, 'table')
_G.is(_G.modz, _G.package.loaded.modz)
else
is(module, nil, "module (removed)")
skip("module (removed)", 5)
end
-- Local Variables:
-- mode: lua
-- lua-indent-level: 4
-- fill-column: 100
-- End:
-- vim: ft=lua expandtab shiftwidth=4: