test_that("needs_roxygenize returns FALSE when all docs are current", {
  path <- local_package_copy(test_path("up-to-date"))
  suppressMessages(roxygenize(path))

  expect_false(needs_roxygenize(path))
})

test_that("needs_roxygenize returns TRUE when source is newer", {
  path <- local_package_copy(test_path("up-to-date"))
  suppressMessages(roxygenize(path))

  # Backdate the Rd file so source appears newer
  Sys.setFileTime(file.path(path, "man", "foo.Rd"), as.POSIXct("2000-01-01"))

  expect_snapshot(out <- needs_roxygenize(path))
  expect_true(out)
})

# rd_outdated ------------------------------------------------------------------

test_that("rd_outdated returns TRUE when source file is deleted", {
  path <- local_package_copy(test_path("up-to-date"))
  suppressMessages(roxygenize(path))

  file.remove(file.path(path, "R", "foo.R"))
  expect_true(rd_outdated(file.path(path, "man", "foo.Rd"), path))
})

# rd_backref_sources -----------------------------------------------------------

test_that("rd_backref_sources parses single source file", {
  lines <- c(
    "% Generated by roxygen2: do not edit by hand",
    "% Please edit documentation in R/foo.R",
    "\\name{foo}"
  )
  expect_equal(rd_backref_sources(lines, "/base"), "/base/R/foo.R")
})

test_that("rd_backref_sources parses multiple source files", {
  lines <- c(
    "% Generated by roxygen2: do not edit by hand",
    "% Please edit documentation in R/foo.R, R/bar.R",
    "\\name{foo}"
  )
  expect_equal(
    rd_backref_sources(lines, "/base"),
    c("/base/R/foo.R", "/base/R/bar.R")
  )
})

test_that("rd_backref_sources handles wrapped backref lines", {
  lines <- c(
    "% Generated by roxygen2: do not edit by hand",
    "% Please edit documentation in R/foo.R,",
    "%   R/bar.R",
    "\\name{foo}"
  )
  expect_equal(
    rd_backref_sources(lines, "/base"),
    c("/base/R/foo.R", "/base/R/bar.R")
  )
})

test_that("rd_backref_sources handles missing source file", {
  lines <- c(
    "% Generated by roxygen2: do not edit by hand",
    "\\name{foo}"
  )
  expect_equal(rd_backref_sources(lines, "/base"), character())
})
