|
| 1 | +---@class are |
| 2 | +---@field same function |
| 3 | +---@field equal function |
| 4 | +---@field equals function |
| 5 | + |
| 6 | +---@class is |
| 7 | +---@field truthy function |
| 8 | +---@field falsy function |
| 9 | +---@field not_true function |
| 10 | +---@field not_false function |
| 11 | + |
| 12 | +---@class has |
| 13 | +---@field error function |
| 14 | +---@field errors function |
| 15 | + |
| 16 | +---@class assert |
| 17 | +---@field are are |
| 18 | +---@field is is |
| 19 | +---@field are_not are |
| 20 | +---@field is_not is |
| 21 | +---@field has has |
| 22 | +---@field has_no has |
| 23 | +---@field True function |
| 24 | +---@field False function |
| 25 | +---@field has_error function |
| 26 | +---@field is_true function |
| 27 | +---@field equal function |
| 28 | +assert = assert |
| 29 | + |
| 30 | +-- functions provided by busted, only available in the global scope |
| 31 | +describe = describe |
| 32 | +it = it |
| 33 | +setup = setup |
| 34 | +before_each = before_each |
| 35 | + |
| 36 | +local turtleEmulator = require("../turtleEmulator") |
| 37 | +describe("Disabled Movement", function() |
| 38 | + local turtle |
| 39 | + setup(function() |
| 40 | + turtle = turtleEmulator:createTurtle(turtleEmulator) |
| 41 | + turtle.canMoveToCheck = function() |
| 42 | + return false |
| 43 | + end |
| 44 | + end) |
| 45 | + it("Can't move to forward", function() |
| 46 | + local c, e = turtle:forward() |
| 47 | + assert.is.falsy(c) |
| 48 | + assert.are.equal("Can't move to forward", e) |
| 49 | + end) |
| 50 | + it("Can't move to back", function() |
| 51 | + local c, e = turtle:back() |
| 52 | + assert.is.falsy(c) |
| 53 | + assert.are.equal("Can't move to back", e) |
| 54 | + end) |
| 55 | + it("Can't move to up", function() |
| 56 | + local c, e = turtle:up() |
| 57 | + assert.is.falsy(c) |
| 58 | + assert.are.equal("Can't move to up", e) |
| 59 | + end) |
| 60 | + it("Can't move to down", function() |
| 61 | + local c, e = turtle:down() |
| 62 | + assert.is.falsy(c) |
| 63 | + assert.are.equal("Can't move to down", e) |
| 64 | + end) |
| 65 | +end) |
| 66 | +describe("Enabled Movement", function() |
| 67 | + local turtle |
| 68 | + setup(function() |
| 69 | + turtle = turtleEmulator:createTurtle(turtleEmulator) |
| 70 | + turtle["canMoveToCheck"] = function() |
| 71 | + return true |
| 72 | + end |
| 73 | + end) |
| 74 | + it("Can move to forward", function() |
| 75 | + local c, e = turtle:forward() |
| 76 | + assert.is.truthy(c) |
| 77 | + assert.are.equal(nil, e) |
| 78 | + end) |
| 79 | + it("Can move to back", function() |
| 80 | + local c, e = turtle:back() |
| 81 | + assert.is.truthy(c) |
| 82 | + assert.are.equal(nil, e) |
| 83 | + end) |
| 84 | + it("Can move to up", function() |
| 85 | + local c, e = turtle:up() |
| 86 | + assert.is.truthy(c) |
| 87 | + assert.are.equal(nil, e) |
| 88 | + end) |
| 89 | + it("Can move to down", function() |
| 90 | + local c, e = turtle:down() |
| 91 | + assert.is.truthy(c) |
| 92 | + assert.are.equal(nil, e) |
| 93 | + end) |
| 94 | +end) |
| 95 | +describe("Track Movement", function() |
| 96 | + local turtle |
| 97 | + setup(function() |
| 98 | + turtle = turtleEmulator:createTurtle(turtleEmulator) |
| 99 | + turtle["canMoveToCheck"] = function() |
| 100 | + return true |
| 101 | + end |
| 102 | + end) |
| 103 | + before_each(function() |
| 104 | + turtle.position.x = 0 |
| 105 | + turtle.position.z = 0 |
| 106 | + turtle.position.y = 0 |
| 107 | + turtle.facing = 0 |
| 108 | + end) |
| 109 | + it("Move forward", function() |
| 110 | + local c, e = turtle:forward() |
| 111 | + assert.is.truthy(c) |
| 112 | + assert.are.equal(nil, e) |
| 113 | + assert.are.equal(1, turtle.position.x) |
| 114 | + assert.are.equal(0, turtle.position.z) |
| 115 | + assert.are.equal(0, turtle.position.y) |
| 116 | + end) |
| 117 | + it("Move back", function() |
| 118 | + local c, e = turtle:back() |
| 119 | + assert.is.truthy(c) |
| 120 | + assert.are.equal(nil, e) |
| 121 | + assert.are.equal(-1, turtle.position.x) |
| 122 | + assert.are.equal(0, turtle.position.z) |
| 123 | + assert.are.equal(0, turtle.position.y) |
| 124 | + end) |
| 125 | + it("Move up", function() |
| 126 | + local c, e = turtle:up() |
| 127 | + assert.is.truthy(c) |
| 128 | + assert.are.equal(nil, e) |
| 129 | + assert.are.equal(0, turtle.position.x) |
| 130 | + assert.are.equal(0, turtle.position.z) |
| 131 | + assert.are.equal(1, turtle.position.y) |
| 132 | + end) |
| 133 | + it("Move down", function() |
| 134 | + local c, e = turtle:down() |
| 135 | + assert.is.truthy(c) |
| 136 | + assert.are.equal(nil, e) |
| 137 | + assert.are.equal(0, turtle.position.x) |
| 138 | + assert.are.equal(0, turtle.position.z) |
| 139 | + assert.are.equal(-1, turtle.position.y) |
| 140 | + end) |
| 141 | +end) |
| 142 | +describe("Track Facing Direction", function() |
| 143 | + local turtle |
| 144 | + setup(function() |
| 145 | + turtle = turtleEmulator:createTurtle(turtleEmulator) |
| 146 | + turtle["canMoveToCheck"] = function() |
| 147 | + return true |
| 148 | + end |
| 149 | + end) |
| 150 | + before_each(function() |
| 151 | + turtle.position.x = 0 |
| 152 | + turtle.position.z = 0 |
| 153 | + turtle.position.y = 0 |
| 154 | + turtle.facing = 0 |
| 155 | + end) |
| 156 | + it("Turn right", function() |
| 157 | + turtle:turnRight() |
| 158 | + assert.are.equal(1, turtle.facing) |
| 159 | + end) |
| 160 | + it("Turn left", function() |
| 161 | + turtle:turnLeft() |
| 162 | + assert.are.equal(3, turtle.facing) |
| 163 | + end) |
| 164 | + it("Turn right twice", function() |
| 165 | + turtle:turnRight() |
| 166 | + turtle:turnRight() |
| 167 | + assert.are.equal(2, turtle.facing) |
| 168 | + end) |
| 169 | + it("Turn right four times", function() |
| 170 | + turtle:turnRight() |
| 171 | + turtle:turnRight() |
| 172 | + turtle:turnRight() |
| 173 | + turtle:turnRight() |
| 174 | + assert.are.equal(0, turtle.facing) |
| 175 | + end) |
| 176 | +end) |
| 177 | +describe("Complexer Movement", function() |
| 178 | + local turtle |
| 179 | + setup(function() |
| 180 | + turtle = turtleEmulator:createTurtle() |
| 181 | + turtle["canMoveToCheck"] = function() |
| 182 | + return true |
| 183 | + end |
| 184 | + end) |
| 185 | + before_each(function() |
| 186 | + turtle.position.x = 0 |
| 187 | + turtle.position.z = 0 |
| 188 | + turtle.position.y = 0 |
| 189 | + turtle.facing = 0 |
| 190 | + end) |
| 191 | + 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() |
| 195 | + assert.is.truthy(c) |
| 196 | + assert.are.equal(nil, e) |
| 197 | + assert.are.equal(1, turtle.facing) |
| 198 | + assert.are.equal(1, turtle.position.x) |
| 199 | + assert.are.equal(1, turtle.position.z) |
| 200 | + assert.are.equal(0, turtle.position.y) |
| 201 | + end) |
| 202 | + 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()) |
| 209 | + assert.are.equal(0, turtle.facing) |
| 210 | + assert.are.equal(-1, turtle.position.x) |
| 211 | + assert.are.equal(1, turtle.position.z) |
| 212 | + assert.are.equal(2, turtle.position.y) |
| 213 | + end) |
| 214 | +end) |
| 215 | + |
| 216 | +describe("multiple Turtles", function() |
| 217 | + local turtle1 |
| 218 | + local turtle2 |
| 219 | + setup(function() |
| 220 | + turtle1 = turtleEmulator:createTurtle() |
| 221 | + turtle2 = turtleEmulator:createTurtle() |
| 222 | + turtle1["canMoveToCheck"] = function() |
| 223 | + return true |
| 224 | + end |
| 225 | + turtle2["canMoveToCheck"] = function() |
| 226 | + return true |
| 227 | + end |
| 228 | + end) |
| 229 | + before_each(function() |
| 230 | + turtle1.position.x = 0 |
| 231 | + turtle1.position.z = 0 |
| 232 | + turtle1.position.y = 0 |
| 233 | + turtle1.facing = 0 |
| 234 | + turtle2.position.x = 0 |
| 235 | + turtle2.position.z = 0 |
| 236 | + turtle2.position.y = 0 |
| 237 | + turtle2.facing = 0 |
| 238 | + end) |
| 239 | + |
| 240 | + it("Move forward with both turtles and turn right with turtle2", function() |
| 241 | + assert.is_true(turtle1:forward()) |
| 242 | + assert.is_true(turtle2:forward()) |
| 243 | + assert.is_true(turtle2:turnRight()) |
| 244 | + assert.is_true(turtle2:forward()) |
| 245 | + assert.are.equal(1, turtle1.position.x) |
| 246 | + assert.are.equal(0, turtle1.position.z) |
| 247 | + assert.are.equal(0, turtle1.position.y) |
| 248 | + assert.are.equal(0, turtle1.facing) |
| 249 | + assert.are.equal(1, turtle2.position.x) |
| 250 | + assert.are.equal(1, turtle2.position.z) |
| 251 | + assert.are.equal(0, turtle2.position.y) |
| 252 | + assert.are.equal(1, turtle2.facing) |
| 253 | + end) |
| 254 | +end) |
0 commit comments