Add tests for __index, __newindex

This commit is contained in:
James Roseborough
2010-08-18 23:05:20 +00:00
parent 2066e41ecd
commit 0d50184e8f
2 changed files with 23 additions and 4 deletions

View File

@@ -43,6 +43,8 @@ local mt = {
return 'mt.__tostring('..type(a)..','..type(b)..')'
end,
__metatable={},
__index=buildop('index'),
__newindex=buildop('newindex'),
}
-- pcall a function and check for a pattern in the error string
@@ -178,3 +180,21 @@ for i=1,#values do
print( debug.setmetatable( a, nil ) )
end
print( '---- __index, __newindex' )
values = { aboolean, anumber, afunction, athread }
for i=1,#values do
local a = values[i]
print( type(a), 'before', ecall( 'attempt to index', function() return a.foo end ) )
print( type(a), 'before', ecall( 'attempt to index', function() return a[123] end ) )
print( type(a), 'before', ecall( 'index', function() a.foo = 'bar' end ) )
print( type(a), 'before', ecall( 'index', function() a[123] = 'bar' end ) )
print( type(a), 'before', ecall( 'attempt to index', function() return a:foo() end ) )
print( debug.setmetatable( a, mt ) )
print( type(a), 'after', pcall( function() return a.foo end ) )
print( type(a), 'after', pcall( function() return a[123] end ) )
print( type(a), 'after', pcall( function() a.foo = 'bar' end ) )
print( type(a), 'after', pcall( function() a[123] = 'bar' end ) )
print( type(a), 'after', ecall( 'attempt to call', function() return a:foo() end ) )
print( debug.setmetatable( a, nil ) )
end