|
| 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