vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
cursor.lua
(2702B)
1
2 local win = vis.win
3
4 -- check that selection position remains unchanged after an invalid adjustment
5 local invalid_pos = function(place)
6 local pos = win.selection.pos
7 local line = win.selection.line
8 local col = win.selection.col
9 win.selection:to(line, col)
10 assert.has_error(place)
11 assert.are.equal(pos, win.selection.pos)
12 assert.are.equal(line, win.selection.line)
13 assert.are.equal(col, win.selection.col)
14 end
15
16 describe("win.selection", function()
17
18 it("initial position", function()
19 assert.are.equal(0, win.selection.pos)
20 end)
21
22 it("initial line", function()
23 assert.are.equal(1, win.selection.line)
24 end)
25
26 it("initial column", function()
27 assert.are.equal(1, win.selection.col)
28 end)
29 end)
30
31 describe("win.selection.pos", function()
32
33 it("= 0", function()
34 win.selection.pos = 0
35 assert.are.equal(0, win.selection.pos)
36 assert.are.equal(1, win.selection.line)
37 assert.are.equal(1, win.selection.col)
38 end)
39
40 it("= beyond end of file", function()
41 win.selection.pos = win.file.size
42 local pos = win.selection.pos
43 local line = win.selection.line
44 local col = win.selection.col
45 win.selection.pos = 0
46 -- selection is placed on last valid position
47 win.selection.pos = win.file.size+1
48 assert.are.equal(pos, win.selection.pos)
49 assert.are.equal(line, win.selection.line)
50 assert.are.equal(col, win.selection.col)
51 end)
52
53 end)
54
55 describe("win.selection.to", function()
56
57 it("(5, 3)", function()
58 win.selection:to(5, 3)
59 assert.are.equal(30, win.selection.pos)
60 assert.are.equal(5, win.selection.line)
61 assert.are.equal(3, win.selection.col)
62 end)
63
64 it("(0, 0) invalid position", function()
65 -- is that what we want?
66 win.selection:to(0, 0)
67 assert.are.equal(0, win.selection.pos)
68 assert.are.equal(1, win.selection.line)
69 assert.are.equal(1, win.selection.col)
70 end)
71
72 it("invalid position, negative line", function()
73 invalid_pos(function() win.selection:to(-1, 0) end)
74 end)
75
76 it("invalid position, negative column", function()
77 invalid_pos(function() win.selection:to(0, -1) end)
78 end)
79
80 it("invalid position, non-integer line", function()
81 invalid_pos(function() win.selection:to(1.5, 1) end)
82 end)
83
84 it("invalid position, non-integer column", function()
85 invalid_pos(function() win.selection:to(1, 1.5) end)
86 end)
87
88 --[[
89 it("move beyond end of file", function()
90 win.selection.pos = win.file.size
91 local pos = win.selection.pos
92 local line = win.selection.line
93 local col = win.selection.col
94 win.selection.pos = 0
95 -- selection is placed on last valid position
96 win.selection:to(#win.file.lines+2, 1000)
97 assert.are.equal(pos, win.selection.pos)
98 assert.are.equal(line, win.selection.line)
99 assert.are.equal(col, win.selection.col)
100 end)
101 --]]
102
103 end)
104