@@ -152,4 +152,93 @@ describe('Headline', function()
152152 assert_range (second_headline_dates [3 ], { 5 , 3 , 5 , 18 })
153153 end )
154154 end )
155+
156+ describe (' toggle_tag' , 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+ it (' adds a tag where there is none' , function ()
185+ file :get_headlines ()[1 ]:toggle_tag (' ARCHIVE' )
186+ local expected = {
187+ ' * Headline 1 :ARCHIVE:' ,
188+ ' * Headline 2 :other:' ,
189+ ' * Headline 3 :other:more:ARCHIVE:' ,
190+ }
191+ assert .are .same (expected , file :reload_sync ().lines )
192+ end )
193+
194+ it (' adds a tag if another already exists' , function ()
195+ file :get_headlines ()[2 ]:toggle_tag (' ARCHIVE' )
196+ local expected = {
197+ ' * Headline 1' ,
198+ ' * Headline 2 :other:ARCHIVE:' ,
199+ ' * Headline 3 :other:more:ARCHIVE:' ,
200+ }
201+ assert .are .same (expected , file :reload_sync ().lines )
202+ end )
203+
204+ it (' does not add the same tag twice' , function ()
205+ file :get_headlines ()[2 ]:toggle_tag (' other' , true )
206+ local expected = {
207+ ' * Headline 1' ,
208+ ' * Headline 2 :other:' ,
209+ ' * Headline 3 :other:more:ARCHIVE:' ,
210+ }
211+ assert .are .same (expected , file :reload_sync ().lines )
212+ end )
213+
214+ it (' removes an existing tag' , function ()
215+ file :get_headlines ()[2 ]:toggle_tag (' other' )
216+ local expected = {
217+ ' * Headline 1' ,
218+ ' * Headline 2' ,
219+ ' * Headline 3 :other:more:ARCHIVE:' ,
220+ }
221+ assert .are .same (expected , file :reload_sync ().lines )
222+ end )
223+
224+ it (' keeps other tags when removing one' , function ()
225+ file :get_headlines ()[3 ]:toggle_tag (' more' )
226+ local expected = {
227+ ' * Headline 1' ,
228+ ' * Headline 2 :other:' ,
229+ ' * Headline 3 :other:ARCHIVE:' ,
230+ }
231+ assert .are .same (expected , file :reload_sync ().lines )
232+ end )
233+
234+ it (' does nothing when removing a non-existent tag' , function ()
235+ file :get_headlines ()[1 ]:toggle_tag (' other' , false )
236+ local expected = {
237+ ' * Headline 1' ,
238+ ' * Headline 2 :other:' ,
239+ ' * Headline 3 :other:more:ARCHIVE:' ,
240+ }
241+ assert .are .same (expected , file :reload_sync ().lines )
242+ end )
243+ end )
155244end )
0 commit comments