summaryrefslogtreecommitdiff
path: root/lua/99/test/marks_spec.lua
blob: 0b810f0607200d5c482c4ed116149b1dfc48a481 (plain)
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
-- luacheck: globals describe it assert before_each after_each
local Mark = require("99.ops.marks")
local geo = require("99.geo")
local Point = geo.Point
local Range = geo.Range
local test_utils = require("99.test.test_utils")
local eq = assert.are.same

describe("Mark", function()
  local buffer

  before_each(function()
    buffer = test_utils.create_file({
      "function foo()",
      "  local x = 1",
      "  return x",
      "end",
      "",
      "function bar()",
      "  return 42",
      "end",
    }, "lua", 1, 0)
  end)

  after_each(function()
    test_utils.clean_files()
  end)

  it("should create a mark at a specific point", function()
    local point = Point:new(2, 3)
    local mark = Mark.mark_point(buffer, point)
    local mark_point = Point.from_mark(mark)

    eq(point, mark_point)

    mark:delete()
  end)

  it("marks range", function()
    local start_point = Point:new(2, 3)
    local end_point = Point:new(3, 10)
    local range = Range:new(buffer, start_point, end_point)
    local mark_start, mark_end = Mark.mark_range(range)
    local actual_start = Point.from_mark(mark_start)
    local actual_end = Point.from_mark(mark_end)

    eq(start_point, actual_start)
    eq(end_point, actual_end)

    mark_start:delete()
    mark_end:delete()
  end)

  it("should handle single-line ranges", function()
    local start_point = Point:new(2, 3)
    local end_point = Point:new(2, 10)
    local range = Range:new(buffer, start_point, end_point)
    local mark_start, mark_end = Mark.mark_range(range)
    local actual_start = Point.from_mark(mark_start)
    local actual_end = Point.from_mark(mark_end)

    eq(start_point, actual_start)
    eq(end_point, actual_end)

    mark_start:delete()
    mark_end:delete()
  end)

  it("should create mark one line above the range start", function()
    local above_point = Point:new(2, 14)
    local start_point = Point:new(3, 5)
    local end_point = Point:new(4, 3)
    local range = Range:new(buffer, start_point, end_point)
    local mark = Mark.mark_above_range(range)
    local mark_point = Point.from_mark(mark)

    eq(above_point, mark_point)

    mark:delete()
  end)

  it("should create mark at beginning when range starts at line 1", function()
    local start_point = Point:new(1, 5)
    local end_point = Point:new(2, 3)
    local range = Range:new(buffer, start_point, end_point)
    local mark = Mark.mark_above_range(range)
    local mark_point = Point.from_mark(mark)

    local beginning_point = Point:new(1, 1)
    eq(beginning_point, mark_point)
    mark:delete()
  end)

  it("should create mark at the end of the range", function()
    local start_point = Point:new(2, 3)
    local end_point = Point:new(3, 8)
    local range = Range:new(buffer, start_point, end_point)
    local mark = Mark.mark_end_of_range(buffer, range)
    local mark_point = Point.from_mark(mark)
    local expected_end_point = end_point:add(Point:new(0, 1))

    eq(expected_end_point, mark_point)

    mark:delete()
  end)

  it("should create mark above a function", function()
    local func_start = Point:new(6, 1)
    local func_end = Point:new(8, 4)
    local func_range = Range:new(buffer, func_start, func_end)
    local mock_func = {
      function_range = func_range,
    }

    local mark = Mark.mark_above_func(buffer, mock_func)
    local mark_point = Point.from_mark(mark)
    local expected_mark_point = func_start:sub(Point:new(1, 0))

    eq(expected_mark_point, mark_point)
    mark:delete()
  end)

  it("should create mark at function body start", function()
    local func_start = Point:new(6, 1)
    local func_end = Point:new(8, 4)
    local func_range = Range:new(buffer, func_start, func_end)
    local mock_func = {
      function_range = func_range,
    }
    local mark = Mark.mark_func_body(buffer, mock_func)
    local mark_point = Point.from_mark(mark)

    eq(func_start, mark_point)

    mark:delete()
  end)

  it("should delete the extmark", function()
    local point = Point:new(2, 3)
    local mark = Mark.mark_point(buffer, point)
    local mark_pos = Point.from_mark(mark)
    eq(point, mark_pos)
    mark:delete()

    local deleted_pos =
      vim.api.nvim_buf_get_extmark_by_id(mark.buffer, mark.nsid, mark.id, {})
    eq(0, #deleted_pos)
  end)
end)