Skip to content

Commit 70a4627

Browse files
committed
refactor: ♻️ Seperate TurtleMock and TurtleEmulator
1 parent 04fc1ef commit 70a4627

File tree

4 files changed

+184
-163
lines changed

4 files changed

+184
-163
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/config
22
/.vscode
3-
/computer
3+
/computer
4+
/turtleFunctions.txt

tests/test_spec.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ describe("Complexer Movement", function()
212212
assert.are.equal(2, turtle.position.y)
213213
end)
214214
end)
215-
216215
describe("multiple Turtles", function()
217216
local turtle1
218217
local turtle2
@@ -252,7 +251,6 @@ describe("multiple Turtles", function()
252251
assert.are.equal(1, turtle2.facing)
253252
end)
254253
end)
255-
256254
describe("ProxyTests", function()
257255
local turtle
258256
setup(function()

turtleEmulator.lua

Lines changed: 5 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,12 @@
1-
---@alias direction "forward" | "back" | "up" | "down"
2-
---@alias facing
3-
---| 0 North
4-
---| 1 East
5-
---| 2 South
6-
---| 3 West
7-
---@alias north integer
8-
---@alias east integer
9-
---@alias height integer
10-
---@alias position {x: north, y: east, z: height}
11-
1+
local turtleM = require("./turtleMock")
122
---@class TurtleEmulator
133
local turtleEmulator = {
14-
---@type position
15-
position = nil,
16-
---@type facing
17-
facing = nil,
18-
canMoveToCheck = nil,
19-
canPrint = nil,
4+
turtles = {},
205
createTurtle = function(self)
21-
local turtle = {
22-
position = { x = 0, y = 0, z = 0 },
23-
facing = 0,
24-
canMoveToCheck = nil
25-
}
26-
setmetatable(turtle, { __index = self })
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
6+
local t = turtleM.createMock()
7+
table.insert(self.turtles, t)
8+
return t
529
end,
5310
}
5411

55-
56-
---@param direction string the direction to move to
57-
---@return boolean success if the turtle can move to the direction
58-
---@return string | nil errorReason the reason why the turtle can't move to the direction
59-
local function canMoveTo(self, direction)
60-
if self.canMoveToCheck ~= nil and type(self.canMoveToCheck) == "function" then
61-
if not self.canMoveToCheck(direction) then
62-
return false, "Can't move to " .. direction
63-
else
64-
return true
65-
end
66-
else
67-
--TODO implement a check for the world
68-
return false, "Move to " .. direction .. " is not implemented yet."
69-
end
70-
end
71-
72-
local function forward(self)
73-
if self.facing == 0 then
74-
self.position.x = self.position.x + 1
75-
elseif self.facing == 1 then
76-
self.position.z = self.position.z + 1
77-
elseif self.facing == 2 then
78-
self.position.x = self.position.x - 1
79-
elseif self.facing == 3 then
80-
self.position.z = self.position.z - 1
81-
end
82-
return true
83-
end
84-
85-
local function back(self)
86-
if self.facing == 0 then
87-
self.position.x = self.position.x - 1
88-
elseif self.facing == 1 then
89-
self.position.z = self.position.z - 1
90-
elseif self.facing == 2 then
91-
self.position.x = self.position.x + 1
92-
elseif self.facing == 3 then
93-
self.position.z = self.position.z + 1
94-
end
95-
return true
96-
end
97-
98-
local function up(self)
99-
self.position.y = self.position.y + 1
100-
return true
101-
end
102-
103-
local function down(self)
104-
self.position.y = self.position.y - 1
105-
return true
106-
end
107-
108-
function turtleEmulator:forward()
109-
self:print(self.canPrint, "Can print")
110-
local c, e = canMoveTo(self, "forward")
111-
if not c then
112-
return c, e
113-
end
114-
return forward(self)
115-
end
116-
117-
function turtleEmulator:back()
118-
local c, e = canMoveTo(self, "back")
119-
if not c then
120-
return c, e
121-
end
122-
return back(self)
123-
end
124-
125-
function turtleEmulator:up()
126-
local c, e = canMoveTo(self, "up")
127-
if not c then
128-
return c, e
129-
end
130-
return up(self)
131-
end
132-
133-
function turtleEmulator:down()
134-
local c, e = canMoveTo(self, "down")
135-
if not c then
136-
return c, e
137-
end
138-
return down(self)
139-
end
140-
141-
function turtleEmulator:turnLeft()
142-
self.facing = (self.facing - 1) % 4
143-
return true
144-
end
145-
146-
function turtleEmulator:turnRight()
147-
self.facing = (self.facing + 1) % 4
148-
return true
149-
end
150-
151-
local function functionNotFoundError(key)
152-
return nil
153-
end
154-
155-
local mt = {
156-
__index = function(table, key)
157-
return rawget(table, key) or functionNotFoundError(key)
158-
end
159-
}
160-
161-
function turtleEmulator:print(...)
162-
return self.canPrint ~= nil and print(...)
163-
end
164-
165-
setmetatable(turtleEmulator, mt)
166-
16712
return turtleEmulator

turtleMock.lua

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
---@alias direction "forward" | "back" | "up" | "down"
2+
---@alias facing
3+
---| 0 North
4+
---| 1 East
5+
---| 2 South
6+
---| 3 West
7+
---@alias north integer
8+
---@alias east integer
9+
---@alias height integer
10+
---@alias position {x: north, y: east, z: height}
11+
12+
13+
---@class TurtleMock
14+
local turtleMock = {
15+
---@type position
16+
position = nil,
17+
---@type facing
18+
facing = nil,
19+
canMoveToCheck = nil,
20+
canPrint = nil,
21+
fuelLevel = 0,
22+
}
23+
---@param self TurtleMock
24+
---@param direction string the direction to move to
25+
---@return boolean success if the turtle can move to the direction
26+
---@return string | nil errorReason the reason why the turtle can't move to the direction
27+
local function canMoveTo(self, direction)
28+
if self.canMoveToCheck ~= nil and type(self.canMoveToCheck) == "function" then
29+
if not self.canMoveToCheck(direction) then
30+
return false, "Can't move to " .. direction
31+
else
32+
return true
33+
end
34+
else
35+
--TODO implement a check for the world
36+
return false, "Move to " .. direction .. " is not implemented yet."
37+
end
38+
end
39+
40+
local function forward(self)
41+
if self.facing == 0 then
42+
self.position.x = self.position.x + 1
43+
elseif self.facing == 1 then
44+
self.position.z = self.position.z + 1
45+
elseif self.facing == 2 then
46+
self.position.x = self.position.x - 1
47+
elseif self.facing == 3 then
48+
self.position.z = self.position.z - 1
49+
end
50+
return true
51+
end
52+
53+
local function back(self)
54+
if self.facing == 0 then
55+
self.position.x = self.position.x - 1
56+
elseif self.facing == 1 then
57+
self.position.z = self.position.z - 1
58+
elseif self.facing == 2 then
59+
self.position.x = self.position.x + 1
60+
elseif self.facing == 3 then
61+
self.position.z = self.position.z + 1
62+
end
63+
return true
64+
end
65+
66+
local function up(self)
67+
self.position.y = self.position.y + 1
68+
return true
69+
end
70+
71+
local function down(self)
72+
self.position.y = self.position.y - 1
73+
return true
74+
end
75+
76+
77+
function turtleMock.createMock()
78+
---@class Turtle : TurtleMock
79+
local turtle = {
80+
position = { x = 0, y = 0, z = 0 },
81+
facing = 0,
82+
canMoveToCheck = nil,
83+
fuelLevel = 0,
84+
canPrint = true,
85+
}
86+
setmetatable(turtle, { __index = turtleMock })
87+
88+
---@class TurtleProxy : Turtle
89+
local proxy = {}
90+
local mt = {}
91+
mt.__index = function(_, key)
92+
local value = turtle[key]
93+
if type(value) == "function" then
94+
return function(...)
95+
local mightBeSelf = select(1, ...)
96+
if mightBeSelf == turtle then
97+
return value(...)
98+
elseif mightBeSelf == proxy then
99+
return value(turtle, select(2, ...))
100+
end
101+
return value(turtle, ...)
102+
end
103+
end
104+
return value
105+
end
106+
mt.__newindex = function(_, key, value)
107+
turtle[key] = value
108+
end
109+
mt.__metatable = mt
110+
111+
setmetatable(proxy, mt)
112+
return proxy
113+
end
114+
115+
function turtleMock:forward()
116+
local c, e = canMoveTo(self, "forward")
117+
if not c then
118+
return c, e
119+
end
120+
return forward(self)
121+
end
122+
123+
function turtleMock:back()
124+
local c, e = canMoveTo(self, "back")
125+
if not c then
126+
return c, e
127+
end
128+
return back(self)
129+
end
130+
131+
function turtleMock:up()
132+
local c, e = canMoveTo(self, "up")
133+
if not c then
134+
return c, e
135+
end
136+
return up(self)
137+
end
138+
139+
function turtleMock:down()
140+
local c, e = canMoveTo(self, "down")
141+
if not c then
142+
return c, e
143+
end
144+
return down(self)
145+
end
146+
147+
function turtleMock:turnLeft()
148+
self.facing = (self.facing - 1) % 4
149+
return true
150+
end
151+
152+
function turtleMock:turnRight()
153+
self.facing = (self.facing + 1) % 4
154+
return true
155+
end
156+
157+
function turtleMock:getFuelLevel()
158+
return self.fuelLevel
159+
end
160+
161+
local function functionNotFoundError(key)
162+
return error("Function " .. key .. " not found")
163+
end
164+
165+
local mt = {
166+
__index = function(table, key)
167+
return rawget(table, key) or functionNotFoundError(key)
168+
end
169+
}
170+
171+
function turtleMock:print(...)
172+
return self.canPrint == true and print(...)
173+
end
174+
175+
setmetatable(turtleMock, mt)
176+
177+
return turtleMock

0 commit comments

Comments
 (0)