@@ -170,3 +170,104 @@ testthat::test_that(
170170 )
171171 }
172172)
173+
174+
175+
176+ # comments --------------------------------------------------------------------------------------------------------
177+
178+
179+ # comments --------------------------------------------------------------------------------------------------------
180+
181+ testthat :: test_that(" comments fall into proper calls" , {
182+ # If comment is on top, it gets moved to the first call.
183+ # Any other comment gets moved to the call above.
184+ code <- "
185+ # initial comment
186+ a <- 1
187+ b <- 2 # inline comment
188+ c <- 3
189+ # inbetween comment
190+ d <- 4
191+ # finishing comment
192+ "
193+
194+ q <- eval_code(qenv(), code )
195+ testthat :: expect_identical(
196+ get_code(q ),
197+ code
198+ )
199+ })
200+
201+ testthat :: test_that(" comments get pasted when they fall into calls" , {
202+ # If comment is on top, it gets moved to the first call.
203+ # Any other comment gets moved to the call above.
204+ # Comments get pasted if there are two assigned to the same call.
205+ code <- "
206+ # initial comment
207+ a <- 1 # A comment
208+ b <- 2 # inline comment
209+ c <- 3 # C comment
210+ # inbetween comment
211+ d <- 4
212+ # finishing comment
213+ "
214+
215+ q <- eval_code(qenv(), code )
216+ testthat :: expect_identical(
217+ get_code(q ),
218+ code
219+ )
220+ })
221+
222+ testthat :: test_that(" comments alone are pasted to the next/following call element" ,{
223+ code <- c(" x <- 5" , " # comment" , " y <- 6" )
224+ q <- eval_code(qenv(), code )
225+ testthat :: expect_identical(
226+ unlist(q @ code )[2 ],
227+ pasten(code [2 : 3 ])
228+ )
229+ testthat :: expect_identical(
230+ get_code(q ),
231+ pasten(code )
232+ )
233+ })
234+
235+ testthat :: test_that(" comments at the end of src are added to the previous call element" ,{
236+ code <- c(" x <- 5" , " # comment" )
237+ q <- eval_code(qenv(), code )
238+ testthat :: expect_identical(
239+ unlist(q @ code ),
240+ pasten(code [1 : 2 ])
241+ )
242+ testthat :: expect_identical(
243+ get_code(q ),
244+ pasten(code )
245+ )
246+ })
247+
248+ testthat :: test_that(" comments from the same line are associated with it's call" ,{
249+ code <- c(" x <- 5" , " y <- 4 # comment" , " z <- 5" )
250+ q <- eval_code(qenv(), code )
251+ testthat :: expect_identical(
252+ unlist(q @ code )[2 ],
253+ paste0(code [2 ], " \n " )
254+ )
255+ testthat :: expect_identical(
256+ get_code(q ),
257+ pasten(code )
258+ )
259+ })
260+
261+ testthat :: test_that(" comments alone passed to eval_code are skipped" ,{
262+ code <- c(" x <- 5" , " # comment" )
263+ q <- eval_code(eval_code(qenv(), code [1 ]), code [2 ])
264+ testthat :: expect_identical(
265+ unlist(q @ code ),
266+ pasten(code [1 : 2 ])
267+ )
268+ testthat :: expect_identical(
269+ get_code(q ),
270+ pasten(code )
271+ )
272+ })
273+
0 commit comments