Skip to content

Commit 2f93714

Browse files
committed
release 2.0: the library straight up didn't work. now it does
1 parent 54ca61f commit 2f93714

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

init.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local min, max = math.min, math.max
99
---@field public maxY number
1010
---@overload fun(minX: number, minY: number, maxX: number, maxY: number): Rect
1111
---@overload fun(r: {minX: number, minY: number, maxX: number, maxY: number}): Rect
12-
local Rect = { _version = "1.0" }
12+
local Rect = { _version = "2.0" }
1313
Rect.__index = Rect
1414

1515
---Creates a new Rect. Equivalent to calling the class: `Rect(...)`.
@@ -29,7 +29,7 @@ function Rect.new(minX, minY, maxX, maxY)
2929
minY = minY,
3030
maxX = maxX,
3131
maxY = maxY,
32-
}, Rect)
32+
}, Rect --[[@as table]])
3333
end
3434

3535
---Creates a new Rect from XYWH parameters.
@@ -49,7 +49,7 @@ function Rect.fromXYWH(x, y, width, height)
4949
minY = y,
5050
maxX = x + width,
5151
maxY = y + height,
52-
}, Rect)
52+
}, Rect --[[@as table]])
5353
end
5454

5555
---Returns all components of this Rect.
@@ -157,7 +157,7 @@ end
157157
---@return Rect
158158
function Rect:cut_left(a)
159159
local left = self:get_left(a)
160-
self.minX = left.minX
160+
self.minX = left.maxX
161161
return left
162162
end
163163

@@ -172,7 +172,7 @@ function Rect:cutLeft(a) return Rect:cut_left(a) end
172172
---@return Rect
173173
function Rect:cut_right(a)
174174
local right = self:get_right(a)
175-
self.maxX = right.maxX
175+
self.maxX = right.minX
176176
return right
177177
end
178178

@@ -187,7 +187,7 @@ function Rect:cutRight(a) return Rect:cut_right(a) end
187187
---@return Rect
188188
function Rect:cut_top(a)
189189
local top = self:get_top(a)
190-
self.minY = top.minY
190+
self.minY = top.maxY
191191
return top
192192
end
193193

@@ -202,7 +202,7 @@ function Rect:cutTop(a) return Rect:cut_top(a) end
202202
---@return Rect
203203
function Rect:cut_bottom(a)
204204
local bottom = self:get_bottom(a)
205-
self.maxY = bottom.maxY
205+
self.maxY = bottom.minY
206206
return bottom
207207
end
208208

@@ -212,7 +212,7 @@ end
212212
---@see Rect.cut_bottom
213213
function Rect:cutBottom(a) return Rect:cut_bottom(a) end
214214

215-
---Returns this rect, with `a` added to its left side.
215+
---Returns a copy of this rect, with `a` added to its left side.
216216
---@param a number
217217
---@return Rect
218218
function Rect:add_left(a) return Rect.new(self.minX - a, self.minY, self.maxX, self.maxY) end
@@ -223,7 +223,7 @@ function Rect:add_left(a) return Rect.new(self.minX - a, self.minY, self.maxX, s
223223
---@see Rect.add_left
224224
function Rect:addLeft(a) return Rect:add_left(a) end
225225

226-
---Returns this rect, with `a` added to its right side.
226+
---Returns a copy of this rect, with `a` added to its right side.
227227
---@param a number
228228
---@return Rect
229229
function Rect:add_right(a) return Rect.new(self.minX, self.minY, self.maxX + a, self.maxY) end
@@ -234,7 +234,7 @@ function Rect:add_right(a) return Rect.new(self.minX, self.minY, self.maxX + a,
234234
---@see Rect.add_right
235235
function Rect:addRight(a) return Rect:add_right(a) end
236236

237-
---Returns this rect, with `a` added to its top side.
237+
---Returns a copy of this rect, with `a` added to its top side.
238238
---@param a number
239239
---@return Rect
240240
function Rect:add_top(a) return Rect.new(self.minX, self.minY - a, self.maxX, self.maxY) end
@@ -245,7 +245,7 @@ function Rect:add_top(a) return Rect.new(self.minX, self.minY - a, self.maxX, se
245245
---@see Rect.add_top
246246
function Rect:addTop(a) return Rect:add_top(a) end
247247

248-
---Returns this rect, with `a` added to its bottom side.
248+
---Returns a copy of this rect, with `a` added to its bottom side.
249249
---@param a number
250250
---@return Rect
251251
function Rect:add_bottom(a) return Rect.new(self.minX, self.minY, self.maxX, self.maxY + a) end
@@ -278,9 +278,9 @@ function Rect:contract(a) return Rect.new(self.minX + a, self.minY + a, self.max
278278

279279
---@return string
280280
function Rect:__tostring()
281-
return string.format("Rect: X(%f-%f), Y(%f-%f)", self:unpack())
281+
return string.format("Rect: min(%g,%g), max(%g,%g)", self:unpack())
282282
end
283283

284-
return setmetatable(Rect, {
284+
return setmetatable(Rect --[[@as table]], {
285285
__call = function(cls, ...) return cls.new(...) end,
286-
})
286+
}) --[[@as Rect]]

tests.lua

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ local Rect = require 'init'
22

33
local fail = false
44

5-
local function testEqualRects(name, a, b)
5+
local function test(name, a, b)
66
if a.minX ~= b.minX or a.minY ~= b.minY
7-
or a.maxX ~= b.maxX or a.maxY ~= b.maxY then
7+
or a.maxX ~= b.maxX or a.maxY ~= b.maxY then
88
io.write(name .. " failed: expected " .. tostring(b) .. ", got " .. tostring(a) .. "\n")
99
fail = true
1010
return false
@@ -14,6 +14,43 @@ local function testEqualRects(name, a, b)
1414
end
1515
end
1616

17-
-- TODO: actually write tests lmfao
17+
--- tests taken from the Rust implementation, thanks and sorry
1818

19-
if fail then io.write("\nsome tests have failed, please check results\n") end
19+
local base = Rect(0, 0, 10, 10)
20+
local rect = base:copy()
21+
test("copy call", rect, base)
22+
23+
test("get_left call", rect:get_left(1), Rect(0, rect.minY, 1, rect.maxY))
24+
test("get_right call", rect:get_right(1), Rect(9, rect.minY, 10, rect.maxY))
25+
test("get_bottom call", rect:get_bottom(1), Rect(rect.minX, 9, rect.maxX, 10))
26+
test("get_top call", rect:get_top(1), Rect(rect.minX, 0, rect.maxX, 1))
27+
28+
test("cut_left call", rect:cut_left(1), Rect(0, rect.minY, 1, rect.maxY))
29+
test("cut_left result", rect, Rect(1, 0, 10, 10))
30+
test("add_left call", rect:add_left(1), base)
31+
32+
rect = base:copy()
33+
test("cut_right call", rect:cut_right(1), Rect(9, rect.minY, 10, rect.maxY))
34+
test("cut_right result", rect, Rect(0, 0, 9, 10))
35+
test("add_right result", rect:add_right(1), base)
36+
37+
rect = base:copy()
38+
test("cut_right call", rect:cut_right(1), Rect(9, rect.minY, 10, rect.maxY))
39+
test("cut_right result", rect, Rect(0, 0, 9, 10))
40+
test("add_right result", rect:add_right(1), base)
41+
42+
rect = base:copy()
43+
test("cut_bottom call", rect:cut_bottom(1), Rect(rect.minX, 9, rect.maxX, 10))
44+
test("cut_bottom result", rect, Rect(0, 0, 10, 9))
45+
test("add_bottom result", rect:add_bottom(1), base)
46+
47+
rect = base:copy()
48+
test("cut_top call", rect:cut_top(1), Rect(rect.minX, 0, rect.maxX, 1))
49+
test("cut_top result", rect, Rect(0, 1, 10, 10))
50+
test("add_top result", rect:add_top(1), base)
51+
52+
test("contract call", base:contract(1), Rect(1, 1, 9, 9))
53+
test("extend call", base:extend(1), Rect(-1, -1, 11, 11))
54+
55+
io.write "\n"
56+
if fail then io.write "some tests have failed, please check results\n" else io.write "all tests pass :D\n" end

0 commit comments

Comments
 (0)