Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 50d0b893bb
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 124 lines (101 sloc) 1.902 kb
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
#! /usr/bin/lua
--
-- lua-TestMore : <http://fperrad.github.com/lua-TestMore/>
--
-- Copyright (C) 2009-2010, Perrad Francois
--
-- This code is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--

--[[

=head1 Lua assignment

=head2 Synopsis

% prove 201-assign.t

=head2 Description

See "Lua 5.2 Reference Manual", section 3.3.3 "Assignment",
L<http://www.lua.org/manual/5.2/manual.html#3.3.3>.

See "Programming in Lua", section 4.1 "Assignment".

=cut

--]]

require 'Test.More'

plan(38)

is(b, nil, "global variable")
b = 10
is(b, 10)
if arg[-1] == 'luajit' then
    skip("LuaJIT intentional. _ENV.", 3)
else
    is(_ENV.b, 10, "_ENV")
    is(_G, _ENV, "_G")
    error_like([[ _ENV = nil; b = 20 ]],
               "^[^:]+:%d+: attempt to index upvalue '_ENV' %(a nil value%)")
end
b = nil
is(b, nil)

a = {}
i = 3
i, a[i] = i+1, 20
is(i, 4, "check eval")
is(a[3], 20)

x = 1.
y = 2.
x, y = y, x -- swap
is(x, 2, "check swap")
is(y, 1)

a, b, c = 0, 1
is(a, 0, "check padding")
is(b, 1)
is(c, nil)
a, b = a+1, b+1, a+b
is(a, 1)
is(b, 2)
a, b, c = 0
is(a, 0)
is(b, nil)
is(c, nil)

function f() return 1, 2 end
a, b, c, d = f()
is(a, 1, "adjust with function")
is(b, 2)
is(c, nil)
is(d, nil)

function f() print('# f') end
a = 2
a, b, c = f(), 3
is(a, nil, "padding with function")
is(b, 3)
is(c, nil)

local my_i = 1
is(my_i, 1, "local variable")
local my_i = 2
is(my_i, 2)

local i = 1
local j = i
is(i, 1, "local variable")
is(j, 1)
j = 2
is(i, 1)
is(j, 2)

local function f(x) return 2*x end
is(f(2), 4, "param & result of function")
a = 2
a = f(a)
is(a, 4)
local b = 2
b = f(b)
is(b, 4)

local n1 = 1
local n2 = 2
local n3 = 3
local n4 = 4
n1,n2,n3,n4 = n4,n3,n2,n1
is(n1, 4, "assignment list swap values")
is(n2, 3)
is(n3, 2)
is(n4, 1)

-- Local Variables:
-- mode: lua
-- lua-indent-level: 4
-- fill-column: 100
-- End:
-- vim: ft=lua expandtab shiftwidth=4:
Something went wrong with that request. Please try again.