Skip to content

Commit 04fc1ef

Browse files
committed
fix(self): 🧐 Added self-reference to functioncalls when it is missing
The self reference does not exist in CraftOS-turtle-calls, therefore functioncalls without a self reference need to work here as well
1 parent 964e842 commit 04fc1ef

File tree

3 files changed

+109
-36
lines changed

3 files changed

+109
-36
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ This Emulator is exclusively ment to enable testing CC code with a testingframew
44

55
### WIP
66

7-
> This Libary is not finished at all at this point and <b><u> will not work yet.</b></u>
87
> Should increase in scope incrementally. For now it (will) only keep track of position and turtle-direction
98
109
### HOW TO USE
@@ -15,7 +14,16 @@ local turtleEmulator = require("<path>/turtleEmulator")
1514
local turtleOne = turtleEmulator:createTurtle()
1615
local turtleTwo = turtleEmulator:createTurtle()
1716

18-
--currently this will only work with a self reference, will work on removing this neccessity
19-
-- one possibilty is a Proxy table in the turtleEmulator #todo
17+
-- override the default behavior, skipping fuelcheck etc.
18+
turtleTwo.canMoveToCheck = function() return true end
2019

20+
turtleOne.forward()
21+
assert(turtleOne.position.x == 1)
22+
23+
--...
2124
```
25+
26+
### Restrictions
27+
28+
To allow the creation of multiple turtles within the same Emulator, the turtle returned by createTurtle is only a proxy, meaning that the metatable should not be modified.
29+
However, should the need ever arise, you can modify it by getmetatable(turtle).\_\_metatable = nil. But please be aware that overriding the \_\_index and \_\_newIndex will break the functionality of the turtle.

tests/test_spec.lua

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,22 @@ describe("Disabled Movement", function()
4343
end
4444
end)
4545
it("Can't move to forward", function()
46-
local c, e = turtle:forward()
46+
local c, e = turtle.forward()
4747
assert.is.falsy(c)
4848
assert.are.equal("Can't move to forward", e)
4949
end)
5050
it("Can't move to back", function()
51-
local c, e = turtle:back()
51+
local c, e = turtle.back()
5252
assert.is.falsy(c)
5353
assert.are.equal("Can't move to back", e)
5454
end)
5555
it("Can't move to up", function()
56-
local c, e = turtle:up()
56+
local c, e = turtle.up()
5757
assert.is.falsy(c)
5858
assert.are.equal("Can't move to up", e)
5959
end)
6060
it("Can't move to down", function()
61-
local c, e = turtle:down()
61+
local c, e = turtle.down()
6262
assert.is.falsy(c)
6363
assert.are.equal("Can't move to down", e)
6464
end)
@@ -72,22 +72,22 @@ describe("Enabled Movement", function()
7272
end
7373
end)
7474
it("Can move to forward", function()
75-
local c, e = turtle:forward()
75+
local c, e = turtle.forward()
7676
assert.is.truthy(c)
7777
assert.are.equal(nil, e)
7878
end)
7979
it("Can move to back", function()
80-
local c, e = turtle:back()
80+
local c, e = turtle.back()
8181
assert.is.truthy(c)
8282
assert.are.equal(nil, e)
8383
end)
8484
it("Can move to up", function()
85-
local c, e = turtle:up()
85+
local c, e = turtle.up()
8686
assert.is.truthy(c)
8787
assert.are.equal(nil, e)
8888
end)
8989
it("Can move to down", function()
90-
local c, e = turtle:down()
90+
local c, e = turtle.down()
9191
assert.is.truthy(c)
9292
assert.are.equal(nil, e)
9393
end)
@@ -107,31 +107,31 @@ describe("Track Movement", function()
107107
turtle.facing = 0
108108
end)
109109
it("Move forward", function()
110-
local c, e = turtle:forward()
110+
local c, e = turtle.forward()
111111
assert.is.truthy(c)
112112
assert.are.equal(nil, e)
113113
assert.are.equal(1, turtle.position.x)
114114
assert.are.equal(0, turtle.position.z)
115115
assert.are.equal(0, turtle.position.y)
116116
end)
117117
it("Move back", function()
118-
local c, e = turtle:back()
118+
local c, e = turtle.back()
119119
assert.is.truthy(c)
120120
assert.are.equal(nil, e)
121121
assert.are.equal(-1, turtle.position.x)
122122
assert.are.equal(0, turtle.position.z)
123123
assert.are.equal(0, turtle.position.y)
124124
end)
125125
it("Move up", function()
126-
local c, e = turtle:up()
126+
local c, e = turtle.up()
127127
assert.is.truthy(c)
128128
assert.are.equal(nil, e)
129129
assert.are.equal(0, turtle.position.x)
130130
assert.are.equal(0, turtle.position.z)
131131
assert.are.equal(1, turtle.position.y)
132132
end)
133133
it("Move down", function()
134-
local c, e = turtle:down()
134+
local c, e = turtle.down()
135135
assert.is.truthy(c)
136136
assert.are.equal(nil, e)
137137
assert.are.equal(0, turtle.position.x)
@@ -154,23 +154,23 @@ describe("Track Facing Direction", function()
154154
turtle.facing = 0
155155
end)
156156
it("Turn right", function()
157-
turtle:turnRight()
157+
turtle.turnRight()
158158
assert.are.equal(1, turtle.facing)
159159
end)
160160
it("Turn left", function()
161-
turtle:turnLeft()
161+
turtle.turnLeft()
162162
assert.are.equal(3, turtle.facing)
163163
end)
164164
it("Turn right twice", function()
165-
turtle:turnRight()
166-
turtle:turnRight()
165+
turtle.turnRight()
166+
turtle.turnRight()
167167
assert.are.equal(2, turtle.facing)
168168
end)
169169
it("Turn right four times", function()
170-
turtle:turnRight()
171-
turtle:turnRight()
172-
turtle:turnRight()
173-
turtle:turnRight()
170+
turtle.turnRight()
171+
turtle.turnRight()
172+
turtle.turnRight()
173+
turtle.turnRight()
174174
assert.are.equal(0, turtle.facing)
175175
end)
176176
end)
@@ -189,9 +189,9 @@ describe("Complexer Movement", function()
189189
turtle.facing = 0
190190
end)
191191
it("Move forward, turn right and turn forward", function()
192-
assert.is_true(turtle:forward())
193-
assert.is_true(turtle:turnRight())
194-
local c, e = turtle:forward()
192+
assert.is_true(turtle.forward())
193+
assert.is_true(turtle.turnRight())
194+
local c, e = turtle.forward()
195195
assert.is.truthy(c)
196196
assert.are.equal(nil, e)
197197
assert.are.equal(1, turtle.facing)
@@ -200,12 +200,12 @@ describe("Complexer Movement", function()
200200
assert.are.equal(0, turtle.position.y)
201201
end)
202202
it("Move back, up, right, up, forward and then left", function()
203-
assert.is_true(turtle:back())
204-
assert.is_true(turtle:up())
205-
assert.is_true(turtle:turnRight())
206-
assert.is_true(turtle:up())
207-
assert.is_true(turtle:forward())
208-
assert.is_true(turtle:turnLeft())
203+
assert.is_true(turtle.back())
204+
assert.is_true(turtle.up())
205+
assert.is_true(turtle.turnRight())
206+
assert.is_true(turtle.up())
207+
assert.is_true(turtle.forward())
208+
assert.is_true(turtle.turnLeft())
209209
assert.are.equal(0, turtle.facing)
210210
assert.are.equal(-1, turtle.position.x)
211211
assert.are.equal(1, turtle.position.z)
@@ -252,3 +252,43 @@ describe("multiple Turtles", function()
252252
assert.are.equal(1, turtle2.facing)
253253
end)
254254
end)
255+
256+
describe("ProxyTests", function()
257+
local turtle
258+
setup(function()
259+
turtle = turtleEmulator:createTurtle()
260+
turtle["canMoveToCheck"] = function()
261+
return true
262+
end
263+
end)
264+
265+
it("Create own key Value", function()
266+
local functionGotCalled = false
267+
turtle.canPrint = true
268+
function turtle:thisIsATest(RandomString)
269+
assert.are_not.equal(self, turtle, "The self reference is the Proxy instead of the turtle object")
270+
assert.are.equal("RandomString", RandomString, "Parameter 2 not correct, might be a self reference?")
271+
functionGotCalled = true
272+
end
273+
274+
turtle:thisIsATest("RandomString")
275+
assert.are.equal(true, functionGotCalled, "Function was not called")
276+
turtle.canPrint = false
277+
end)
278+
279+
it("Change Metatable", function()
280+
local mt = getmetatable(turtle)
281+
local newMt = {}
282+
local gotCalled = 0
283+
assert.has_error(function()
284+
gotCalled = gotCalled + 1
285+
setmetatable(turtle, newMt)
286+
end)
287+
mt.__metatable = nil
288+
assert.has_no.errors(function()
289+
gotCalled = gotCalled + 1
290+
setmetatable(turtle, newMt)
291+
end)
292+
assert.are.equal(2, gotCalled, "asserts did not run as expected")
293+
end)
294+
end)

turtleEmulator.lua

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,40 @@ local turtleEmulator = {
1616
---@type facing
1717
facing = nil,
1818
canMoveToCheck = nil,
19+
canPrint = nil,
1920
createTurtle = function(self)
2021
local turtle = {
2122
position = { x = 0, y = 0, z = 0 },
2223
facing = 0,
2324
canMoveToCheck = nil
2425
}
2526
setmetatable(turtle, { __index = self })
26-
return turtle
27+
28+
local proxy = {}
29+
local mt = {}
30+
mt.__index = function(_, key)
31+
local value = turtle[key]
32+
if type(value) == "function" then
33+
return function(...)
34+
local mightBeSelf = select(1, ...)
35+
if mightBeSelf == turtle then
36+
return value(...)
37+
elseif mightBeSelf == proxy then
38+
return value(turtle, select(2, ...))
39+
end
40+
return value(turtle, ...)
41+
end
42+
end
43+
return value
44+
end
45+
mt.__newindex = function(_, key, value)
46+
turtle[key] = value
47+
end
48+
mt.__metatable = mt
49+
50+
setmetatable(proxy, mt)
51+
return proxy
2752
end,
28-
canPrint = nil
2953
}
3054

3155

@@ -82,6 +106,7 @@ local function down(self)
82106
end
83107

84108
function turtleEmulator:forward()
109+
self:print(self.canPrint, "Can print")
85110
local c, e = canMoveTo(self, "forward")
86111
if not c then
87112
return c, e
@@ -133,8 +158,8 @@ local mt = {
133158
end
134159
}
135160

136-
function turtleEmulator:print(generic)
137-
return self.canPrint ~= nil and print(generic)
161+
function turtleEmulator:print(...)
162+
return self.canPrint ~= nil and print(...)
138163
end
139164

140165
setmetatable(turtleEmulator, mt)

0 commit comments

Comments
 (0)