diff options
Diffstat (limited to 'testes/locals.lua')
-rw-r--r-- | testes/locals.lua | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/testes/locals.lua b/testes/locals.lua index ccea0a14..02f41980 100644 --- a/testes/locals.lua +++ b/testes/locals.lua | |||
@@ -1,6 +1,8 @@ | |||
1 | -- $Id: testes/locals.lua $ | 1 | -- $Id: testes/locals.lua $ |
2 | -- See Copyright Notice in file lua.h | 2 | -- See Copyright Notice in file lua.h |
3 | 3 | ||
4 | global <const> * | ||
5 | |||
4 | print('testing local variables and environments') | 6 | print('testing local variables and environments') |
5 | 7 | ||
6 | local debug = require"debug" | 8 | local debug = require"debug" |
@@ -39,9 +41,11 @@ f = nil | |||
39 | local f | 41 | local f |
40 | local x = 1 | 42 | local x = 1 |
41 | 43 | ||
42 | a = nil | 44 | do |
43 | load('local a = {}')() | 45 | global a; a = nil |
44 | assert(a == nil) | 46 | load('local a = {}')() |
47 | assert(a == nil) | ||
48 | end | ||
45 | 49 | ||
46 | function f (a) | 50 | function f (a) |
47 | local _1, _2, _3, _4, _5 | 51 | local _1, _2, _3, _4, _5 |
@@ -154,7 +158,7 @@ local _ENV = (function (...) return ... end)(_G, dummy) -- { | |||
154 | do local _ENV = {assert=assert}; assert(true) end | 158 | do local _ENV = {assert=assert}; assert(true) end |
155 | local mt = {_G = _G} | 159 | local mt = {_G = _G} |
156 | local foo,x | 160 | local foo,x |
157 | A = false -- "declare" A | 161 | global A; A = false -- "declare" A |
158 | do local _ENV = mt | 162 | do local _ENV = mt |
159 | function foo (x) | 163 | function foo (x) |
160 | A = x | 164 | A = x |
@@ -177,20 +181,27 @@ assert(x==20) | |||
177 | A = nil | 181 | A = nil |
178 | 182 | ||
179 | 183 | ||
180 | do -- constants | 184 | do print("testing local constants") |
185 | global assert<const>, load, string, X | ||
186 | X = 1 -- not a constant | ||
181 | local a<const>, b, c<const> = 10, 20, 30 | 187 | local a<const>, b, c<const> = 10, 20, 30 |
182 | b = a + c + b -- 'b' is not constant | 188 | b = a + c + b -- 'b' is not constant |
183 | assert(a == 10 and b == 60 and c == 30) | 189 | assert(a == 10 and b == 60 and c == 30) |
190 | |||
184 | local function checkro (name, code) | 191 | local function checkro (name, code) |
185 | local st, msg = load(code) | 192 | local st, msg = load(code) |
186 | local gab = string.format("attempt to assign to const variable '%s'", name) | 193 | local gab = string.format("attempt to assign to const variable '%s'", name) |
187 | assert(not st and string.find(msg, gab)) | 194 | assert(not st and string.find(msg, gab)) |
188 | end | 195 | end |
196 | |||
189 | checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12") | 197 | checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12") |
190 | checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11") | 198 | checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11") |
191 | checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11") | 199 | checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11") |
192 | checkro("foo", "local foo <const> = 10; function foo() end") | 200 | checkro("foo", "local<const> foo = 10; function foo() end") |
193 | checkro("foo", "local foo <const> = {}; function foo() end") | 201 | checkro("foo", "local<const> foo <const> = {}; function foo() end") |
202 | checkro("foo", "global<const> foo <const>; function foo() end") | ||
203 | checkro("XX", "global XX <const>; XX = 10") | ||
204 | checkro("XX", "local _ENV; global XX <const>; XX = 10") | ||
194 | 205 | ||
195 | checkro("z", [[ | 206 | checkro("z", [[ |
196 | local a, z <const>, b = 10; | 207 | local a, z <const>, b = 10; |
@@ -201,11 +212,26 @@ do -- constants | |||
201 | local a, var1 <const> = 10; | 212 | local a, var1 <const> = 10; |
202 | function foo() a = 20; z = function () var1 = 12; end end | 213 | function foo() a = 20; z = function () var1 = 12; end end |
203 | ]]) | 214 | ]]) |
215 | |||
216 | checkro("var1", [[ | ||
217 | global a, var1 <const>, z; | ||
218 | local function foo() a = 20; z = function () var1 = 12; end end | ||
219 | ]]) | ||
204 | end | 220 | end |
205 | 221 | ||
206 | 222 | ||
223 | |||
207 | print"testing to-be-closed variables" | 224 | print"testing to-be-closed variables" |
208 | 225 | ||
226 | |||
227 | do | ||
228 | local st, msg = load("local <close> a, b") | ||
229 | assert(not st and string.find(msg, "multiple")) | ||
230 | |||
231 | local st, msg = load("local a<close>, b<close>") | ||
232 | assert(not st and string.find(msg, "multiple")) | ||
233 | end | ||
234 | |||
209 | local function stack(n) n = ((n == 0) or stack(n - 1)) end | 235 | local function stack(n) n = ((n == 0) or stack(n - 1)) end |
210 | 236 | ||
211 | local function func2close (f, x, y) | 237 | local function func2close (f, x, y) |
@@ -1162,7 +1188,7 @@ do | |||
1162 | local function open (x) | 1188 | local function open (x) |
1163 | numopen = numopen + 1 | 1189 | numopen = numopen + 1 |
1164 | return | 1190 | return |
1165 | function () -- iteraction function | 1191 | function () -- iteration function |
1166 | x = x - 1 | 1192 | x = x - 1 |
1167 | if x > 0 then return x end | 1193 | if x > 0 then return x end |
1168 | end, | 1194 | end, |