diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-17 14:57:29 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-17 14:57:29 -0300 |
commit | ed2872cd3bf6d352f36bbd34529738a60b0b51eb (patch) | |
tree | a4476cca4a43deeb8eaa0d52f0f02b15acc0db09 /testes | |
parent | 2d3f09544895b422eeecf89e0d108da8b8fcdfca (diff) | |
download | lua-ed2872cd3bf6d352f36bbd34529738a60b0b51eb.tar.gz lua-ed2872cd3bf6d352f36bbd34529738a60b0b51eb.tar.bz2 lua-ed2872cd3bf6d352f36bbd34529738a60b0b51eb.zip |
'require' returns where module was found
The function 'require' returns the *loader data* as a second result.
For file searchers, this data is the path where they found the module.
Diffstat (limited to 'testes')
-rw-r--r-- | testes/attrib.lua | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/testes/attrib.lua b/testes/attrib.lua index dcafd634..4adb42e0 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua | |||
@@ -122,12 +122,13 @@ local oldpath = package.path | |||
122 | 122 | ||
123 | package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR) | 123 | package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR) |
124 | 124 | ||
125 | local try = function (p, n, r) | 125 | local try = function (p, n, r, ext) |
126 | NAME = nil | 126 | NAME = nil |
127 | local rr = require(p) | 127 | local rr, x = require(p) |
128 | assert(NAME == n) | 128 | assert(NAME == n) |
129 | assert(REQUIRED == p) | 129 | assert(REQUIRED == p) |
130 | assert(rr == r) | 130 | assert(rr == r) |
131 | assert(ext == x) | ||
131 | end | 132 | end |
132 | 133 | ||
133 | a = require"names" | 134 | a = require"names" |
@@ -143,27 +144,27 @@ assert(package.searchpath("C", package.path) == D"C.lua") | |||
143 | assert(require"C" == 25) | 144 | assert(require"C" == 25) |
144 | assert(require"C" == 25) | 145 | assert(require"C" == 25) |
145 | AA = nil | 146 | AA = nil |
146 | try('B', 'B.lua', true) | 147 | try('B', 'B.lua', true, "libs/B.lua") |
147 | assert(package.loaded.B) | 148 | assert(package.loaded.B) |
148 | assert(require"B" == true) | 149 | assert(require"B" == true) |
149 | assert(package.loaded.A) | 150 | assert(package.loaded.A) |
150 | assert(require"C" == 25) | 151 | assert(require"C" == 25) |
151 | package.loaded.A = nil | 152 | package.loaded.A = nil |
152 | try('B', nil, true) -- should not reload package | 153 | try('B', nil, true, nil) -- should not reload package |
153 | try('A', 'A.lua', true) | 154 | try('A', 'A.lua', true, "libs/A.lua") |
154 | package.loaded.A = nil | 155 | package.loaded.A = nil |
155 | os.remove(D'A.lua') | 156 | os.remove(D'A.lua') |
156 | AA = {} | 157 | AA = {} |
157 | try('A', 'A.lc', AA) -- now must find second option | 158 | try('A', 'A.lc', AA, "libs/A.lc") -- now must find second option |
158 | assert(package.searchpath("A", package.path) == D"A.lc") | 159 | assert(package.searchpath("A", package.path) == D"A.lc") |
159 | assert(require("A") == AA) | 160 | assert(require("A") == AA) |
160 | AA = false | 161 | AA = false |
161 | try('K', 'L', false) -- default option | 162 | try('K', 'L', false, "libs/L") -- default option |
162 | try('K', 'L', false) -- default option (should reload it) | 163 | try('K', 'L', false, "libs/L") -- default option (should reload it) |
163 | assert(rawget(_G, "_REQUIREDNAME") == nil) | 164 | assert(rawget(_G, "_REQUIREDNAME") == nil) |
164 | 165 | ||
165 | AA = "x" | 166 | AA = "x" |
166 | try("X", "XXxX", AA) | 167 | try("X", "XXxX", AA, "libs/XXxX") |
167 | 168 | ||
168 | 169 | ||
169 | removefiles(files) | 170 | removefiles(files) |
@@ -183,14 +184,16 @@ files = { | |||
183 | createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n") | 184 | createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n") |
184 | AA = 0 | 185 | AA = 0 |
185 | 186 | ||
186 | local m = assert(require"P1") | 187 | local m, ext = assert(require"P1") |
188 | assert(ext == "libs/P1/init.lua") | ||
187 | assert(AA == 0 and m.AA == 10) | 189 | assert(AA == 0 and m.AA == 10) |
188 | assert(require"P1" == m) | 190 | assert(require"P1" == m) |
189 | assert(require"P1" == m) | 191 | assert(require"P1" == m) |
190 | 192 | ||
191 | assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua") | 193 | assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua") |
192 | m.xuxu = assert(require"P1.xuxu") | 194 | m.xuxu, ext = assert(require"P1.xuxu") |
193 | assert(AA == 0 and m.xuxu.AA == 20) | 195 | assert(AA == 0 and m.xuxu.AA == 20) |
196 | assert(ext == "libs/P1/xuxu.lua") | ||
194 | assert(require"P1.xuxu" == m.xuxu) | 197 | assert(require"P1.xuxu" == m.xuxu) |
195 | assert(require"P1.xuxu" == m.xuxu) | 198 | assert(require"P1.xuxu" == m.xuxu) |
196 | assert(require"P1" == m and m.AA == 10) | 199 | assert(require"P1" == m and m.AA == 10) |
@@ -267,15 +270,17 @@ else | |||
267 | 270 | ||
268 | -- test C modules with prefixes in names | 271 | -- test C modules with prefixes in names |
269 | package.cpath = DC"?" | 272 | package.cpath = DC"?" |
270 | local lib2 = require"lib2-v2" | 273 | local lib2, ext = require"lib2-v2" |
274 | assert(string.find(ext, "libs/lib2-v2", 1, true)) | ||
271 | -- check correct access to global environment and correct | 275 | -- check correct access to global environment and correct |
272 | -- parameters | 276 | -- parameters |
273 | assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") | 277 | assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") |
274 | assert(lib2.id("x") == "x") | 278 | assert(lib2.id("x") == "x") |
275 | 279 | ||
276 | -- test C submodules | 280 | -- test C submodules |
277 | local fs = require"lib1.sub" | 281 | local fs, ext = require"lib1.sub" |
278 | assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") | 282 | assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") |
283 | assert(string.find(ext, "libs/lib1", 1, true)) | ||
279 | assert(fs.id(45) == 45) | 284 | assert(fs.id(45) == 45) |
280 | end | 285 | end |
281 | 286 | ||
@@ -293,10 +298,10 @@ do | |||
293 | return _ENV | 298 | return _ENV |
294 | end | 299 | end |
295 | 300 | ||
296 | local pl = require"pl" | 301 | local pl, ext = require"pl" |
297 | assert(require"pl" == pl) | 302 | assert(require"pl" == pl) |
298 | assert(pl.xuxu(10) == 30) | 303 | assert(pl.xuxu(10) == 30) |
299 | assert(pl[1] == "pl" and pl[2] == nil) | 304 | assert(pl[1] == "pl" and pl[2] == ":preload:" and ext == ":preload:") |
300 | 305 | ||
301 | package = p | 306 | package = p |
302 | assert(type(package.path) == "string") | 307 | assert(type(package.path) == "string") |