local obj1 = {
x = 10,
getX = function(self) return self.x end
}
local r1 = obj1:getX()
print("Test 1 - Basic method call: " .. tostring(r1 == 10))
local obj2 = {
value = 5,
add = function(self, n) return self.value + n end
}
local r2 = obj2:add(3)
print("Test 2 - Method with args: " .. tostring(r2 == 8))
local obj3 = {
base = 100,
compute = function(self, a, b) return self.base + a * b end
}
local r3 = obj3:compute(3, 4)
print("Test 3 - Method with multiple args: " .. tostring(r3 == 112))
local counter = {
value = 0,
inc = function(self)
self.value = self.value + 1
return self
end,
get = function(self) return self.value end
}
local r4 = counter:inc():inc():inc():get()
print("Test 4 - Chained method calls: " .. tostring(r4 == 3))
local obj5 = {
x = 1,
y = 2,
getXY = function(self) return self.x, self.y end
}
local x, y = obj5:getXY()
print("Test 5 - Method returning multiple values: " .. tostring(x == 1 and y == 2))
local outer = {
inner = {
val = 42,
getVal = function(self) return self.val end
}
}
local r6 = outer.inner:getVal()
print("Test 6 - Nested object method: " .. tostring(r6 == 42))
local obj7 = {
x = 20,
getX = function(self) return self.x end
}
local r7a = obj7:getX()
local r7b = obj7.getX(obj7)
print("Test 7 - Colon vs dot equivalence: " .. tostring(r7a == r7b and r7a == 20))
print("All method call tests complete!")