@@ -152,4 +152,139 @@ describe('Headline', function()
152152 assert_range (second_headline_dates [3 ], { 5 , 3 , 5 , 18 })
153153 end )
154154 end )
155+
156+ describe (' tags' , function ()
157+ --- @type OrgFile
158+ local file
159+ local orig_tags_column
160+
161+ before_each (function ()
162+ -- Put tags flush to headlines for shorter tests.
163+ if not orig_tags_column then
164+ orig_tags_column = config .org_tags_column
165+ end
166+ config :extend ({ org_tags_column = 0 })
167+ -- Reinitialize test file to same state.
168+ if not file then
169+ file = helpers .load_file (vim .fn .tempname () .. ' .org' )
170+ end
171+ local bufnr = file :get_valid_bufnr ()
172+ vim .api .nvim_buf_set_lines (bufnr , 0 , - 1 , true , {
173+ ' * Headline 1' ,
174+ ' * Headline 2 :other:' ,
175+ ' * Headline 3 :other:more:ARCHIVE:' ,
176+ })
177+ file :reload_sync ()
178+ end )
179+
180+ after_each (function ()
181+ config :extend ({ org_tags_column = orig_tags_column })
182+ end )
183+
184+ describe (' toggling' , function ()
185+ it (' adds a tag where there is none' , function ()
186+ file :get_headlines ()[1 ]:toggle_tag (' ARCHIVE' )
187+ local expected = {
188+ ' * Headline 1 :ARCHIVE:' ,
189+ ' * Headline 2 :other:' ,
190+ ' * Headline 3 :other:more:ARCHIVE:' ,
191+ }
192+ assert .are .same (expected , file :reload_sync ().lines )
193+ end )
194+
195+ it (' adds a tag if another already exists' , function ()
196+ file :get_headlines ()[2 ]:toggle_tag (' ARCHIVE' )
197+ local expected = {
198+ ' * Headline 1' ,
199+ ' * Headline 2 :other:ARCHIVE:' ,
200+ ' * Headline 3 :other:more:ARCHIVE:' ,
201+ }
202+ assert .are .same (expected , file :reload_sync ().lines )
203+ end )
204+
205+ it (' removes an existing tag' , function ()
206+ file :get_headlines ()[2 ]:toggle_tag (' other' )
207+ local expected = {
208+ ' * Headline 1' ,
209+ ' * Headline 2' ,
210+ ' * Headline 3 :other:more:ARCHIVE:' ,
211+ }
212+ assert .are .same (expected , file :reload_sync ().lines )
213+ end )
214+
215+ it (' keeps other tags when removing one' , function ()
216+ file :get_headlines ()[3 ]:toggle_tag (' more' )
217+ local expected = {
218+ ' * Headline 1' ,
219+ ' * Headline 2 :other:' ,
220+ ' * Headline 3 :other:ARCHIVE:' ,
221+ }
222+ assert .are .same (expected , file :reload_sync ().lines )
223+ end )
224+ end )
225+
226+ describe (' addition' , function ()
227+ it (' adds a tag where there is none' , function ()
228+ file :get_headlines ()[1 ]:add_tag (' ARCHIVE' )
229+ local expected = {
230+ ' * Headline 1 :ARCHIVE:' ,
231+ ' * Headline 2 :other:' ,
232+ ' * Headline 3 :other:more:ARCHIVE:' ,
233+ }
234+ assert .are .same (expected , file :reload_sync ().lines )
235+ end )
236+
237+ it (' adds a tag if another already exists' , function ()
238+ file :get_headlines ()[2 ]:add_tag (' ARCHIVE' )
239+ local expected = {
240+ ' * Headline 1' ,
241+ ' * Headline 2 :other:ARCHIVE:' ,
242+ ' * Headline 3 :other:more:ARCHIVE:' ,
243+ }
244+ assert .are .same (expected , file :reload_sync ().lines )
245+ end )
246+
247+ it (' does not add the same tag twice' , function ()
248+ file :get_headlines ()[2 ]:add_tag (' other' )
249+ local expected = {
250+ ' * Headline 1' ,
251+ ' * Headline 2 :other:' ,
252+ ' * Headline 3 :other:more:ARCHIVE:' ,
253+ }
254+ assert .are .same (expected , file :reload_sync ().lines )
255+ end )
256+ end )
257+
258+ describe (' removal' , function ()
259+ it (' removes an existing tag' , function ()
260+ file :get_headlines ()[2 ]:remove_tag (' other' )
261+ local expected = {
262+ ' * Headline 1' ,
263+ ' * Headline 2' ,
264+ ' * Headline 3 :other:more:ARCHIVE:' ,
265+ }
266+ assert .are .same (expected , file :reload_sync ().lines )
267+ end )
268+
269+ it (' keeps other tags when removing one' , function ()
270+ file :get_headlines ()[3 ]:remove_tag (' more' )
271+ local expected = {
272+ ' * Headline 1' ,
273+ ' * Headline 2 :other:' ,
274+ ' * Headline 3 :other:ARCHIVE:' ,
275+ }
276+ assert .are .same (expected , file :reload_sync ().lines )
277+ end )
278+
279+ it (' does nothing when removing a non-existent tag' , function ()
280+ file :get_headlines ()[1 ]:remove_tag (' other' )
281+ local expected = {
282+ ' * Headline 1' ,
283+ ' * Headline 2 :other:' ,
284+ ' * Headline 3 :other:more:ARCHIVE:' ,
285+ }
286+ assert .are .same (expected , file :reload_sync ().lines )
287+ end )
288+ end )
289+ end )
155290end )
0 commit comments