|
| 1 | +local config = require('orgmode.config') |
| 2 | +local Indent = require('orgmode.org.indent') |
| 3 | +local helpers = require('tests.plenary.ui.helpers') |
| 4 | + |
| 5 | +-- Helper assert function. |
| 6 | +local function expect_whole_buffer(expected) |
| 7 | + assert.are.same(expected, vim.api.nvim_buf_get_lines(0, 0, -1, false)) |
| 8 | +end |
| 9 | + |
| 10 | +-- We want to run all tests under both values for `org_indent_mode`: "indent" |
| 11 | +-- and "noindent". So it is easier to put all tests into test functions and |
| 12 | +-- check the indent mode, then run them under two different `describe()`. |
| 13 | + |
| 14 | +local function test_full_reindent() |
| 15 | + local unformatted_file = { |
| 16 | + '* TODO First task', |
| 17 | + 'SCHEDULED: <1970-01-01 Thu>', |
| 18 | + '', |
| 19 | + '1. Ordered list', |
| 20 | + ' a) nested list', |
| 21 | + ' over-indented', |
| 22 | + ' over-indented', |
| 23 | + ' b) nested list', |
| 24 | + ' under-indented', |
| 25 | + '2. Ordered list', |
| 26 | + 'Not part of the list', |
| 27 | + '', |
| 28 | + '** Second task', |
| 29 | + ' DEADLINE: <1970-01-01 Thu>', |
| 30 | + '', |
| 31 | + '- Unordered list', |
| 32 | + ' + nested list', |
| 33 | + ' over-indented', |
| 34 | + ' over-indented', |
| 35 | + ' + nested list', |
| 36 | + ' under-indented', |
| 37 | + '- unordered list', |
| 38 | + ' + nested list', |
| 39 | + ' * triple nested list', |
| 40 | + ' continuation', |
| 41 | + ' part of the first-level list', |
| 42 | + 'Not part of the list', |
| 43 | + } |
| 44 | + helpers.load_file_content(unformatted_file) |
| 45 | + vim.cmd([[silent norm 0gg=G]]) |
| 46 | + local expected |
| 47 | + if config.org_indent_mode == 'indent' then |
| 48 | + expected = { |
| 49 | + '* TODO First task', |
| 50 | + ' SCHEDULED: <1970-01-01 Thu>', |
| 51 | + '', |
| 52 | + ' 1. Ordered list', |
| 53 | + ' a) nested list', |
| 54 | + ' over-indented', |
| 55 | + ' over-indented', |
| 56 | + ' b) nested list', |
| 57 | + ' under-indented', |
| 58 | + ' 2. Ordered list', |
| 59 | + ' Not part of the list', |
| 60 | + '', |
| 61 | + '** Second task', |
| 62 | + ' DEADLINE: <1970-01-01 Thu>', |
| 63 | + '', |
| 64 | + ' - Unordered list', |
| 65 | + ' + nested list', |
| 66 | + ' over-indented', |
| 67 | + ' over-indented', |
| 68 | + ' + nested list', |
| 69 | + ' under-indented', |
| 70 | + ' - unordered list', |
| 71 | + ' + nested list', |
| 72 | + ' * triple nested list', |
| 73 | + ' continuation', |
| 74 | + ' part of the first-level list', |
| 75 | + ' Not part of the list', |
| 76 | + } |
| 77 | + elseif config.org_indent_mode == 'noindent' then |
| 78 | + expected = { |
| 79 | + '* TODO First task', |
| 80 | + 'SCHEDULED: <1970-01-01 Thu>', |
| 81 | + '', |
| 82 | + '1. Ordered list', |
| 83 | + ' a) nested list', |
| 84 | + ' over-indented', |
| 85 | + ' over-indented', |
| 86 | + ' b) nested list', |
| 87 | + ' under-indented', |
| 88 | + '2. Ordered list', |
| 89 | + 'Not part of the list', |
| 90 | + '', |
| 91 | + '** Second task', |
| 92 | + 'DEADLINE: <1970-01-01 Thu>', |
| 93 | + '', |
| 94 | + '- Unordered list', |
| 95 | + ' + nested list', |
| 96 | + ' over-indented', |
| 97 | + ' over-indented', |
| 98 | + ' + nested list', |
| 99 | + ' under-indented', |
| 100 | + '- unordered list', |
| 101 | + ' + nested list', |
| 102 | + ' * triple nested list', |
| 103 | + ' continuation', |
| 104 | + ' part of the first-level list', |
| 105 | + 'Not part of the list', |
| 106 | + } |
| 107 | + end |
| 108 | + expect_whole_buffer(expected) |
| 109 | +end |
| 110 | + |
| 111 | +local function test_newly_written_list() |
| 112 | + helpers.load_file_content({}) |
| 113 | + local user_input = vim.api.nvim_replace_termcodes('i- new item<CR>second line<CR>third line<Esc>', true, true, true) |
| 114 | + vim.api.nvim_feedkeys(user_input, 'ntix', false) |
| 115 | + local expected |
| 116 | + if config.org_indent_mode == 'indent' then |
| 117 | + expected = { |
| 118 | + '- new item', |
| 119 | + ' second line', |
| 120 | + ' third line', |
| 121 | + } |
| 122 | + elseif config.org_indent_mode == 'noindent' then |
| 123 | + expected = { |
| 124 | + '- new item', |
| 125 | + ' second line', |
| 126 | + ' third line', |
| 127 | + } |
| 128 | + end |
| 129 | + expect_whole_buffer(expected) |
| 130 | +end |
| 131 | + |
| 132 | +local function test_insertion_to_an_existing_list() |
| 133 | + helpers.load_file_content({ '- first item', '- third item' }) |
| 134 | + vim.cmd([[normal! o]]) |
| 135 | + local user_input = vim.api.nvim_replace_termcodes('i- new item<CR>second line<CR>third line<Esc>', true, true, true) |
| 136 | + vim.api.nvim_feedkeys(user_input, 'ntix', false) |
| 137 | + local expected |
| 138 | + if config.org_indent_mode == 'indent' then |
| 139 | + expected = { |
| 140 | + '- first item', |
| 141 | + '- new item', |
| 142 | + ' second line', |
| 143 | + ' third line', |
| 144 | + '- third item', |
| 145 | + } |
| 146 | + elseif config.org_indent_mode == 'noindent' then |
| 147 | + expected = { |
| 148 | + '- first item', |
| 149 | + '- new item', |
| 150 | + ' second line', |
| 151 | + ' third line', |
| 152 | + '- third item', |
| 153 | + } |
| 154 | + end |
| 155 | + expect_whole_buffer(expected) |
| 156 | +end |
| 157 | + |
| 158 | +local function test_add_line_breaks_to_existing_file() |
| 159 | + helpers.load_file_content({ '- first item', '- second item' }) |
| 160 | + local user_input = vim.api.nvim_replace_termcodes('wwi<CR><Esc><Down><Right>i<CR><Esc>', true, true, true) |
| 161 | + vim.api.nvim_feedkeys(user_input, 'ntix', false) |
| 162 | + local expected = { |
| 163 | + '- first ', |
| 164 | + ' item', |
| 165 | + '- ', |
| 166 | + ' second item', |
| 167 | + } |
| 168 | + expect_whole_buffer(expected) |
| 169 | +end |
| 170 | + |
| 171 | +-- The actual tests are here. |
| 172 | + |
| 173 | +describe('with "indent",', function() |
| 174 | + before_each(function() |
| 175 | + config:extend({ org_indent_mode = 'indent' }) |
| 176 | + end) |
| 177 | + |
| 178 | + it('"0gg=G" reindents the whole file', function() |
| 179 | + test_full_reindent() |
| 180 | + end) |
| 181 | + |
| 182 | + it('a newly written list is well indented', function() |
| 183 | + test_newly_written_list() |
| 184 | + end) |
| 185 | + |
| 186 | + it('insertion to an existing list is well indented', function() |
| 187 | + test_insertion_to_an_existing_list() |
| 188 | + end) |
| 189 | + |
| 190 | + it('adding line breaks to list items maintains indent', function() |
| 191 | + test_add_line_breaks_to_existing_file() |
| 192 | + end) |
| 193 | +end) |
| 194 | + |
| 195 | +describe('with "noindent",', function() |
| 196 | + before_each(function() |
| 197 | + config:extend({ org_indent_mode = 'noindent' }) |
| 198 | + end) |
| 199 | + |
| 200 | + it('"0gg=G" reindents the whole file', function() |
| 201 | + test_full_reindent() |
| 202 | + end) |
| 203 | + |
| 204 | + it('a newly written list is well indented', function() |
| 205 | + test_newly_written_list() |
| 206 | + end) |
| 207 | + |
| 208 | + it('insertion into an existing list is well indented', function() |
| 209 | + test_insertion_to_an_existing_list() |
| 210 | + end) |
| 211 | + |
| 212 | + it('adding line breaks to list items maintains indent', function() |
| 213 | + test_add_line_breaks_to_existing_file() |
| 214 | + end) |
| 215 | +end) |
0 commit comments