Skip to content

Commit 2ccb993

Browse files
authored
Merge pull request #4 from Dich0tomy/feat/add-iter-spec
feat(lib.iter): add basic iter lib
2 parents ed9925b + 4d21993 commit 2ccb993

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

lua/cpp-tools/lib/iter.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
local M = {}
2+
3+
---Counts the lines in a string
4+
---@param str string the string to count lines for
5+
---@return number line count # The number of lines
6+
function M.line_count(str)
7+
local count = 1
8+
local length = #str
9+
for i = 1, length - 1 do
10+
local c = str:sub(i, i)
11+
if c == '\n' then
12+
if (i ~= (length - 2)) then
13+
count = count + 1
14+
end
15+
end
16+
end
17+
18+
return count
19+
end
20+
21+
---Splits the string into lines
22+
---@param str string the string to split
23+
---@return string[] lines # An array of lines
24+
function M.lines(str)
25+
return vim.split(str, '\n')
26+
end
27+
28+
---Checks if two array tables have equal values
29+
---@generic T
30+
---@param arr1 `T`[] first array
31+
---@param arr2 T[] second array
32+
---@return boolean # are the arrays equal
33+
function M.arrays_equal(arr1, arr2)
34+
if arr1 == arr2 then
35+
return true
36+
elseif #arr1 ~= #arr2 then
37+
return false
38+
end
39+
40+
for i = 1, #arr1 do
41+
if arr1[i] ~= arr2[i] then
42+
return false
43+
end
44+
end
45+
46+
return true
47+
end
48+
49+
return M

spec/iter_spec.lua

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
describe('array_equals', function()
2+
local iter = require('cpp-tools.lib.iter')
3+
4+
it('Two same references to empty are equal', function()
5+
local empty = {}
6+
local a = empty
7+
local b = empty
8+
9+
assert.is.truthy(iter.arrays_equal(a, b))
10+
end)
11+
12+
it('Two different references to empty are equal', function()
13+
local a = {}
14+
local b = {}
15+
16+
assert.is.truthy(iter.arrays_equal(a, b))
17+
end)
18+
19+
it('Two same values are equal', function()
20+
local a = { 1, 2 }
21+
local b = { 1, 2 }
22+
23+
assert.is.truthy(iter.arrays_equal(a, b))
24+
end)
25+
26+
it('Two same values with different order are not equal', function()
27+
local a = { 1, 2 }
28+
local b = { 2, 1 }
29+
30+
assert.is.falsy(iter.arrays_equal(a, b))
31+
end)
32+
33+
it('Two same values with different values', function()
34+
local a = { '' }
35+
local b = { 1 }
36+
37+
assert.is.falsy(iter.arrays_equal(a, b))
38+
end)
39+
end)
40+
41+
describe('line_count', function()
42+
local iter = require('cpp-tools.lib.iter')
43+
44+
it('Returns 1 for an empty string', function()
45+
assert.are.equal(iter.line_count(''), 1)
46+
end)
47+
48+
it('Returns 1 for a non empty string with one line', function()
49+
assert.are.equal(iter.line_count('Hello there'), 1)
50+
end)
51+
52+
it('Returns 1 for a string with a trailing newlien', function()
53+
assert.are.equal(iter.line_count('Foo\n'), 1)
54+
end)
55+
56+
it('Returns 2 for a string with a trailing newline and content after it', function()
57+
assert.are.equal(iter.line_count('Foo\nBar'), 2)
58+
end)
59+
60+
it('Returns X for a string with X lines (ignoring last newline)', function()
61+
local three_lined_string = [[
62+
first line
63+
second line
64+
third line]]
65+
66+
assert.are.equal(iter.line_count(three_lined_string), 3)
67+
end)
68+
end)
69+
70+
describe('lines', function()
71+
local iter = require('cpp-tools.lib.iter')
72+
73+
it('Returns one empty line untouched', function()
74+
local empty_line = ''
75+
local lines = iter.lines(empty_line)
76+
77+
assert.are.equal(#lines, 1)
78+
assert.are.equal(lines[1], empty_line)
79+
end)
80+
81+
it('Returns one non empty line untouched', function()
82+
local empty_line = 'asdasdasdasdasd'
83+
local lines = iter.lines(empty_line)
84+
85+
assert.are.equal(#lines, 1)
86+
assert.are.equal(lines[1], empty_line)
87+
end)
88+
89+
it('Returns X lines for a string with X lines', function()
90+
local lines_arr = { 'line1', 'line2', 'line3' }
91+
local lines_str = vim.fn.join(lines_arr, '\n')
92+
93+
assert.is.truthy(iter.arrays_equal(iter.lines(lines_str), lines_arr))
94+
end)
95+
end)

0 commit comments

Comments
 (0)