Skip to content

Commit aa33f69

Browse files
committed
added tests for GetTextObject()
1 parent 33c307e commit aa33f69

File tree

2 files changed

+116
-8
lines changed

2 files changed

+116
-8
lines changed

lib/utils.vim

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ export def RemoveSurrounding(A: string, B: string, lead: number, trail: number)
153153
endif
154154
enddef
155155

156-
export def SurroundNew(open_delimiter: string, close_delimiter: string, text_object: string = '', keep_even: bool = false)
156+
export def SurroundNew(open_delimiter: string,
157+
close_delimiter: string,
158+
text_object: string = '',
159+
keep_even: bool = false)
157160
# Usage:
158161
# Select text and hit <leader> + e.g. parenthesis
159162
#
@@ -226,7 +229,8 @@ export def SurroundNew(open_delimiter: string, close_delimiter: string, text_obj
226229

227230
var toA = ''
228231
if !empty(found_delimiters_interval)
229-
toA = old_right_delimiter .. open_delimiter .. strcharpart(getline(xA), 0, yA)
232+
toA = old_right_delimiter .. open_delimiter
233+
.. strcharpart(getline(xA), 0, yA)
230234
else
231235
toA = open_delimiter .. strcharpart(getline(xA), 0, yA)
232236
endif
@@ -243,7 +247,8 @@ export def SurroundNew(open_delimiter: string, close_delimiter: string, text_obj
243247

244248
var fromB = ''
245249
if !empty(found_delimiters_interval)
246-
FromB = close_delimiter .. old_left_delimiter .. strcharpart(getline(xB), yB - 1)
250+
FromB = close_delimiter .. old_left_delimiter
251+
.. strcharpart(getline(xB), yB - 1)
247252
else
248253
FromB = close_delimiter .. strcharpart(getline(xB), yB - 1)
249254
endif
@@ -274,7 +279,8 @@ export def SurroundNew(open_delimiter: string, close_delimiter: string, text_obj
274279
# echom "l: " .. lead
275280
# echom "t: " .. trail
276281

277-
# TODO: This has to be done afterwardsRemove all existing delimiters between A and B
282+
# TODO: This has to be done afterwardsRemove all existing delimiters between
283+
# A and B
278284
# If there is a delimiter surrounded by white spaces, keep it as it is not a
279285
# valid text-style in markdown
280286
var cleaned_text = captured_text
@@ -358,7 +364,8 @@ export def InsertLinesAtMark(marker: string, lines: list<string>)
358364

359365
# If there's only one line in the list, insert it inline
360366
if len(lines) == 1
361-
var new_line = strcharpart(current_line, 0, col - 1) .. lines[0] .. strcharpart(current_line, col - 1)
367+
var new_line = strcharpart(current_line, 0, col - 1) .. lines[0]
368+
.. strcharpart(current_line, col - 1)
362369
setline(line_num, new_line)
363370
else
364371
# Modify the first line (before the marker)
@@ -433,7 +440,8 @@ export def GetDelimitersRanges(open_delimiter: string,
433440
else
434441
# Pick the open-interval
435442
open_delimiter_pos_short_final[0] = open_delimiter_pos_short[0]
436-
open_delimiter_pos_short_final[1] = open_delimiter_pos_short[1] + open_delimiter_length
443+
open_delimiter_pos_short_final[1] = open_delimiter_pos_short[1]
444+
+ open_delimiter_length
437445
endif
438446
open_delimiter_pos = [0] + open_delimiter_pos_short_final + [0]
439447

@@ -478,7 +486,8 @@ export def IsBetweenMarks(A: string, B: string): bool
478486
# comparison of floats.
479487
var lower_float = str2float($'{A_pos[1]}.{A_pos[2]}')
480488
var upper_float = str2float($'{B_pos[1]}.{B_pos[2]}')
481-
var cursor_pos_float = str2float($'{getcharpos(".")[1]}.{getcharpos(".")[2]}')
489+
var cursor_pos_float =
490+
str2float($'{getcharpos(".")[1]}.{getcharpos(".")[2]}')
482491

483492
# Debugging
484493
# echom "cur_pos: " .. cursor_pos_float
@@ -496,7 +505,8 @@ export def IsBetweenMarks(A: string, B: string): bool
496505

497506
enddef
498507

499-
export def IsInRange(open_delimiter: string, close_delimiter: string): list<list<number>>
508+
export def IsInRange(open_delimiter: string,
509+
close_delimiter: string): list<list<number>>
500510
# Return the range of the delimiters if the cursor is within such a range,
501511
# otherwise return an empty list.
502512
# Arguments must be regex.

test/test_utils.vim

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ def Generate_markdown_testfile()
4444
et quas molestias excepturi sint~~ occaecati cupiditate non provident,
4545
similique sunt in culpa qui officia ~~deserunt mollitia animi, id est
4646
~~laborum et dolorum fuga.
47+
48+
Et harum quidem rerum facilis est et expedita distinctio. Nam libero
49+
tempore, cum soluta nobis est (eligendi optio cumque nihil) impedit quo
50+
minus id quod maxime [placeat facere possimus, omnis] voluptas assumenda
51+
est, omnis "dolor repellendus". Temporibus autem quibusdam et aut
52+
officiis debitis aut {rerum necessitatibus} saepe eveniet ut et
53+
voluptates repudiandae sint et molestiae non recusandae. Itaque earum
54+
rerum hic tenetur 'a sapiente' delectus, ut aut reiciendis voluptatibus
55+
maiores alias consequatur aut perferendis doloribus asperiores
56+
repellat.
4757
END
4858
writefile(lines, src_name)
4959
enddef
@@ -154,3 +164,91 @@ def g:Test_IsInRange()
154164
:%bw!
155165
Cleanup_markdown_testfile()
156166
enddef
167+
168+
169+
170+
def g:Test_GetTextObject()
171+
Generate_markdown_testfile()
172+
exe $"edit {src_name}"
173+
174+
def AssertGetTextObject(expected_value: dict<any>, actual_value: dict<any>)
175+
assert_equal(expected_value.text, actual_value.text)
176+
assert_equal(expected_value.start_pos, getcharpos(actual_value.start_pos))
177+
assert_equal(expected_value.end_pos, getcharpos(actual_value.end_pos))
178+
enddef
179+
180+
# test 'iw'
181+
cursor(1, 8)
182+
var expected_value = {text: 'perspiciatis',
183+
start_pos: [0, 1, 8, 0],
184+
end_pos: [0, 1, 19, 0]}
185+
186+
var actual_value = utils.GetTextObject('iw')
187+
188+
# test 'iW'
189+
actual_value = utils.GetTextObject('iW')
190+
AssertGetTextObject(expected_value, actual_value)
191+
192+
# test 'aw'
193+
expected_value = {text: 'perspiciatis ',
194+
start_pos: [0, 1, 8, 0],
195+
end_pos: [0, 1, 20, 0]}
196+
197+
actual_value = utils.GetTextObject('aw')
198+
AssertGetTextObject(expected_value, actual_value)
199+
200+
# test 'aW'
201+
actual_value = utils.GetTextObject('aW')
202+
AssertGetTextObject(expected_value, actual_value)
203+
204+
# Test 'i('
205+
cursor(25, 33)
206+
expected_value = {text: 'eligendi optio cumque nihil',
207+
start_pos: [0, 25, 32, 0],
208+
end_pos: [0, 25, 58, 0]}
209+
210+
actual_value = utils.GetTextObject('i(')
211+
AssertGetTextObject(expected_value, actual_value)
212+
# Test 'yib'
213+
actual_value = utils.GetTextObject('ib')
214+
AssertGetTextObject(expected_value, actual_value)
215+
216+
# Test 'a('
217+
expected_value = {text: '(eligendi optio cumque nihil)',
218+
start_pos: [0, 25, 31, 0],
219+
end_pos: [0, 25, 59, 0]}
220+
221+
actual_value = utils.GetTextObject('a(')
222+
AssertGetTextObject(expected_value, actual_value)
223+
# Test 'ab'
224+
actual_value = utils.GetTextObject('ab')
225+
AssertGetTextObject(expected_value, actual_value)
226+
227+
# Test 'i{'
228+
cursor(28, 25)
229+
expected_value = {text: 'rerum necessitatibus',
230+
start_pos: [0, 28, 23, 0],
231+
end_pos: [0, 28, 42, 0]}
232+
actual_value = utils.GetTextObject('i{')
233+
AssertGetTextObject(expected_value, actual_value)
234+
235+
# Test 'a{'
236+
expected_value = {text: '{rerum necessitatibus}',
237+
start_pos: [0, 28, 22, 0],
238+
end_pos: [0, 28, 43, 0]}
239+
actual_value = utils.GetTextObject('a{')
240+
AssertGetTextObject(expected_value, actual_value)
241+
242+
# Test quoted text
243+
# TODO: it does not work due to a bug in vim, see:
244+
# https://github.com/vim/vim/issues/16679
245+
# cursor(27, 22)
246+
# expected_value = {text: 'dolor repellendus',
247+
# start_pos: [0, 27, 13, 0],
248+
# end_pos: [0, 27, 29, 0]}
249+
# actual_value = utils.GetTextObject('i"')
250+
# AssertGetTextObject(expected_value, actual_value)
251+
252+
# :%bw!
253+
# Cleanup_markdown_testfile()
254+
enddef

0 commit comments

Comments
 (0)