Repository navigation
Expand file tree
/
Copy pathplacement_spec.lua
More file actions
226 lines (209 loc) · 9.08 KB
/
Copy pathplacement_spec.lua
File metadata and controls
226 lines (209 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require("spec.helper")
local placement = require("sagefs.placement")
-- Where a cell's result is drawn. The old rule was "at the cell's last line",
-- which put the result off screen whenever the cell was taller than the
-- window (a lemming pressed <A-CR> and saw nothing happen). The rule now is a
-- pure function of the cell range, the window and the result height, and the
-- property below is that the result is always inside the visible window.
--- Tiny seeded generator (busted has no FsCheck). LCG so runs are reproducible.
local function rng(seed)
local state = seed
return function(lo, hi)
state = (state * 1103515245 + 12345) % 2147483648
return lo + (state % (hi - lo + 1))
end
end
local function case_from(next_int)
local rows = next_int(2, 60)
local top = next_int(1, 400)
local bot = top + rows - 1
local cell_start = next_int(1, 500)
local cell_end = cell_start + next_int(0, 200)
local anchor = next_int(cell_start, cell_end)
return {
rows = rows,
top = top,
bot = bot,
cell_start = cell_start,
cell_end = cell_end,
anchor = anchor,
height = next_int(1, 80),
max_lines = next_int(3, 25),
}
end
local function intersects(c)
return math.max(c.cell_start, c.top) <= math.min(c.cell_end, c.bot)
end
describe("sagefs.placement.place", function()
describe("preserved behaviour", function()
it("puts a short result on the cell's last line when it is visible and fits", function()
local p = placement.place({
cell_start = 10, cell_end = 14, anchor = 11,
top = 1, bot = 40, rows = 40, height = 3, max_lines = 12,
})
assert.are.equal(14, p.line)
assert.are.equal(3, p.shown)
assert.are.equal(0, p.hidden)
assert.is_false(p.footer)
assert.is_true(p.visible)
end)
it("single-line result needs no virtual lines beyond its one row", function()
local p = placement.place({
cell_start = 3, cell_end = 3, anchor = 3,
top = 1, bot = 20, rows = 20, height = 1, max_lines = 12,
})
assert.are.equal(3, p.line)
assert.are.equal(1, p.shown)
assert.is_false(p.footer)
end)
end)
describe("a cell taller than the window", function()
it("anchors on the line the user evaluated from, not the off-screen end", function()
-- cell 1..300, window shows 100..139, cursor was on 120
local p = placement.place({
cell_start = 1, cell_end = 300, anchor = 120,
top = 100, bot = 139, rows = 40, height = 5, max_lines = 12,
})
assert.are.equal(120, p.line)
assert.is_true(p.line >= 100 and p.line <= 139)
assert.are.equal(5, p.shown)
end)
it("falls back to the last visible line of the cell when the anchor scrolled away", function()
local p = placement.place({
cell_start = 1, cell_end = 300, anchor = 5,
top = 100, bot = 139, rows = 40, height = 2, max_lines = 12,
})
assert.is_true(p.line >= 100 and p.line <= 139)
assert.is_true(p.visible)
end)
it("starts the cell above the window: the visible part is what counts", function()
local p = placement.place({
cell_start = 50, cell_end = 90, anchor = 60,
top = 80, bot = 99, rows = 20, height = 4, max_lines = 12,
})
assert.are.equal(90, p.line) -- the cell's end is visible, so keep it
assert.is_true(p.line >= p.line and p.line <= 99)
end)
end)
describe("a tall result", function()
it("truncates with a footer and reports how many lines are hidden", function()
local p = placement.place({
cell_start = 1, cell_end = 5, anchor = 5,
top = 1, bot = 40, rows = 40, height = 50, max_lines = 12,
})
assert.are.equal(12, p.shown)
assert.are.equal(38, p.hidden)
assert.is_true(p.footer)
end)
it("never reports hidden lines without the footer that says so", function()
local p = placement.place({
cell_start = 1, cell_end = 5, anchor = 5,
top = 1, bot = 10, rows = 10, height = 30, max_lines = 12,
})
assert.is_true(p.hidden > 0)
assert.is_true(p.footer)
end)
it("keeps the result at the cell's end, truncated, when there is room for a few lines (does not split the code)", function()
-- cell 1..30 ends 7 rows above the window's bottom; the result needs 40 rows
local p = placement.place({
cell_start = 1, cell_end = 30, anchor = 25,
top = 1, bot = 37, rows = 37, height = 40, max_lines = 12,
})
assert.are.equal(30, p.line)
assert.is_true(p.footer)
assert.is_true(p.shown >= 4)
assert.is_true(30 + p.shown + 1 <= 37)
end)
it("moves up inside the cell when the cell end is at the very bottom of the window", function()
-- the cell end is the last visible row: nothing would fit beneath it
local p = placement.place({
cell_start = 1, cell_end = 40, anchor = 40,
top = 1, bot = 40, rows = 40, height = 6, max_lines = 12,
})
assert.is_true(p.line < 40)
-- everything we draw fits in the window
assert.is_true(p.line - 1 + 1 + p.shown + (p.footer and 1 or 0) <= 40)
end)
end)
describe("a cell that is not on screen", function()
it("says so instead of inventing a position inside the window", function()
local p = placement.place({
cell_start = 1, cell_end = 10, anchor = 5,
top = 100, bot = 139, rows = 40, height = 3, max_lines = 12,
})
assert.is_false(p.visible)
end)
end)
describe("rows_through", function()
it("counts wrapped lines and virtual lines when the caller knows them", function()
-- every buffer line costs two rows (e.g. all wrap): fewer rows are left
local p = placement.place({
cell_start = 1, cell_end = 8, anchor = 8,
top = 1, bot = 10, rows = 20, height = 10, max_lines = 12,
rows_through = function(line) return (line) * 2 end,
})
-- at line 8 only 20 - 16 = 4 rows remain; the result must not exceed them
local used = (p.line * 2) + p.shown + (p.footer and 1 or 0)
assert.is_true(used <= 20, "used " .. used .. " rows of 20")
end)
end)
describe("property: the result is always inside the visible window", function()
it("holds for 5000 generated windows, cells and result heights", function()
local next_int = rng(20261002)
local checked = 0
for _ = 1, 5000 do
local c = case_from(next_int)
local p = placement.place(c)
if intersects(c) then
checked = checked + 1
local lo = math.max(c.cell_start, c.top)
local hi = math.min(c.cell_end, c.bot)
local ctx = string.format("case rows=%d top=%d cell=%d..%d anchor=%d height=%d max=%d -> line=%s shown=%s hidden=%s footer=%s",
c.rows, c.top, c.cell_start, c.cell_end, c.anchor, c.height, c.max_lines,
tostring(p.line), tostring(p.shown), tostring(p.hidden), tostring(p.footer))
assert.is_true(p.visible, ctx)
-- anchored on a line of the cell that is on screen
assert.is_true(p.line >= lo and p.line <= hi, "line out of range: " .. ctx)
-- every row we draw lies inside the window
local drawn = p.shown + (p.footer and 1 or 0)
assert.is_true((p.line - c.top + 1) + drawn <= c.rows, "overflows window: " .. ctx)
-- accounting: nothing is lost silently
assert.are.equal(math.min(c.height, c.height), p.shown + p.hidden, "lost lines: " .. ctx)
-- the one case with no room beneath the line at all (the cell's visible part is the
-- window's last row): the inline text on the line itself is what the user sees
local room = c.rows - (p.line - c.top + 1)
if p.hidden > 0 and room >= 1 then assert.is_true(p.footer, "hidden without footer: " .. ctx) end
-- something is always on screen for the user to see
assert.is_true(drawn >= 1 or room < 1, "nothing drawn: " .. ctx)
else
assert.is_false(p.visible)
end
end
assert.is_true(checked > 500, "generator produced too few intersecting cases: " .. checked)
end)
it("holds when rows are not one per line (random wrap and virtual line costs)", function()
local next_int = rng(7)
for _ = 1, 3000 do
local c = case_from(next_int)
-- cumulative row cost per line, 1..4 rows each, from `top`
local cost = {}
local acc = 0
for l = c.top, c.top + c.rows + 5 do
acc = acc + next_int(1, 4)
cost[l] = acc
end
c.rows_through = function(line) return cost[line] or (acc + (line - (c.top + c.rows + 5))) end
local p = placement.place(c)
if intersects(c) and p.visible then
local lo = math.max(c.cell_start, c.top)
local hi = math.min(c.cell_end, c.bot)
assert.is_true(p.line >= lo and p.line <= hi)
local drawn = p.shown + (p.footer and 1 or 0)
local avail = c.rows - c.rows_through(p.line)
-- either what we draw fits, or there was no room at all on the topmost line
assert.is_true(drawn <= math.max(avail, 0), string.format("drawn %d > avail %d", drawn, avail))
end
end
end)
end)
end)