|
1 | 1 | local Template = require('orgmode.capture.template') |
2 | 2 | local Date = require('orgmode.objects.date') |
| 3 | +local helpers = require('tests.plenary.helpers') |
| 4 | +local Input = require('orgmode.ui.input') |
| 5 | +local Promise = require('orgmode.utils.promise') |
3 | 6 |
|
4 | 7 | describe('Capture template', function() |
5 | 8 | it('should compile expression', function() |
@@ -87,4 +90,105 @@ describe('Capture template', function() |
87 | 90 | end) |
88 | 91 | assert.is.Nil(template:compile():wait()) |
89 | 92 | end) |
| 93 | + |
| 94 | + it('should prompt for single tag with %^g', function() |
| 95 | + helpers.with_var(Input, 'open', function(_prompt, _default, _completion) |
| 96 | + return Promise.resolve('mytag') |
| 97 | + end, function() |
| 98 | + local template = Template:new({ |
| 99 | + template = '* TODO %^g', |
| 100 | + }) |
| 101 | + assert.are.same({ '* TODO :mytag:' }, template:compile():wait()) |
| 102 | + end) |
| 103 | + end) |
| 104 | + |
| 105 | + it('should prompt for multiple tags with %^G', function() |
| 106 | + helpers.with_var(Input, 'open', function(_prompt, _default, _completion) |
| 107 | + return Promise.resolve('tag1:tag2') |
| 108 | + end, function() |
| 109 | + local template = Template:new({ |
| 110 | + template = '* TODO %^G', |
| 111 | + }) |
| 112 | + assert.are.same({ '* TODO :tag1:tag2:' }, template:compile():wait()) |
| 113 | + end) |
| 114 | + end) |
| 115 | + |
| 116 | + it('should prompt for restricted tags with %^{tag1|tag2}G', function() |
| 117 | + helpers.with_var(Input, 'open', function(_prompt, _default, _completion) |
| 118 | + return Promise.resolve('tag1') |
| 119 | + end, function() |
| 120 | + local template = Template:new({ |
| 121 | + template = '* TODO %^{tag1|tag2}G', |
| 122 | + }) |
| 123 | + assert.are.same({ '* TODO :tag1:' }, template:compile():wait()) |
| 124 | + end) |
| 125 | + end) |
| 126 | + |
| 127 | + it('should not cancel capture when %^g input is empty', function() |
| 128 | + helpers.with_var(Input, 'open', function(_prompt, _default, _completion) |
| 129 | + return Promise.resolve('') |
| 130 | + end, function() |
| 131 | + local template = Template:new({ |
| 132 | + template = '* TODO %^g', |
| 133 | + }) |
| 134 | + assert.are.same({ '* TODO ' }, template:compile():wait()) |
| 135 | + end) |
| 136 | + end) |
| 137 | + |
| 138 | + it('should complete %^g from target file tags only', function() |
| 139 | + local files = helpers.create_agenda_files({ |
| 140 | + { |
| 141 | + filename = 'target.org', |
| 142 | + content = { |
| 143 | + '#+FILETAGS: :target_file:', |
| 144 | + '* TODO target item :target_headline:', |
| 145 | + }, |
| 146 | + }, |
| 147 | + { |
| 148 | + filename = 'other.org', |
| 149 | + content = { |
| 150 | + '* TODO other item :other_headline:', |
| 151 | + }, |
| 152 | + }, |
| 153 | + }) |
| 154 | + |
| 155 | + helpers.with_var(Input, 'open', function(_prompt, _default, completion) |
| 156 | + assert.are.same({ 'target_file', 'target_headline' }, completion('')) |
| 157 | + return Promise.resolve('target_headline') |
| 158 | + end, function() |
| 159 | + local template = Template:new({ |
| 160 | + template = '* TODO %^g', |
| 161 | + target = files['target.org'], |
| 162 | + }) |
| 163 | + assert.are.same({ '* TODO :target_headline:' }, template:compile():wait()) |
| 164 | + end) |
| 165 | + end) |
| 166 | + |
| 167 | + it('should complete %^G from all loaded agenda file tags', function() |
| 168 | + helpers.create_agenda_files({ |
| 169 | + { |
| 170 | + filename = 'target.org', |
| 171 | + content = { |
| 172 | + '#+FILETAGS: :target_file:', |
| 173 | + '* TODO target item :target_headline:', |
| 174 | + }, |
| 175 | + }, |
| 176 | + { |
| 177 | + filename = 'other.org', |
| 178 | + content = { |
| 179 | + '* TODO other item :other_headline:', |
| 180 | + }, |
| 181 | + }, |
| 182 | + }) |
| 183 | + |
| 184 | + helpers.with_var(Input, 'open', function(_prompt, _default, completion) |
| 185 | + assert.are.same({ 'other_headline', 'target_file', 'target_headline' }, completion('')) |
| 186 | + return Promise.resolve('target_headline:other_headline') |
| 187 | + end, function() |
| 188 | + local template = Template:new({ |
| 189 | + template = '* TODO %^G', |
| 190 | + }) |
| 191 | + assert.are.same({ '* TODO :target_headline:other_headline:' }, template:compile():wait()) |
| 192 | + end) |
| 193 | + end) |
90 | 194 | end) |
0 commit comments