tests/testthat/test-batch-gsub.R

context("batch-gsub")

test_that("batch_gsub", {
  teardown(unlink(file.path(tempdir(), "batchr")))

  path <- file.path(tempdir(), "batchr")
  unlink(path, recursive = TRUE)
  dir.create(path)
  file <- file.path(path, "file.r")
  lines <- c("some zz", "and some more zzzz zzz zz z")
  writeLines(lines, file)
  file2 <- file.path(path, "file2.R")
  lines2 <- c("text")
  writeLines(lines2, file2)
  expect_error(
    batch_gsub(
      pattern = "zz", replacement = "z", path = path,
      regexp = "[.]txt$"
    ),
    "^Directory '.*batchr' does not contain any files matching '[[][.][]]txt[$]'[.]$"
  )
  expect_true(batch_gsub(
    pattern = "zz", replacement = "z", path = path,
    ask = FALSE
  ))

  expect_identical(readLines(file), c("some z", "and some more zz zz z z"))
  expect_identical(readLines(file2), c("text"))
  expect_true(batch_gsub(
    pattern = "zz", replacement = "z", path = path,
    ask = FALSE
  ))
  expect_identical(readLines(file), c("some z", "and some more z z z z"))
  expect_identical(readLines(file2), c("text"))
  expect_true(batch_gsub(
    pattern = "zz", replacement = "z", path = path,
    ask = FALSE
  ))
  expect_true(batch_gsub(
    pattern = "Z", replacement = "a", path = path,
    ask = FALSE
  ))
  expect_identical(readLines(file), c("some z", "and some more z z z z"))
  
  expect_true(batch_gsub(
    pattern = "Z", replacement = "a", path = path, ignore.case = TRUE,
    ask = FALSE
  ))
  expect_identical(readLines(file), c("some a", "and some more a a a a"))
  
  expect_true(batch_gsub(
    pattern = ".", replacement = "b", path = path, fixed = TRUE,
    ask = FALSE
  ))
  expect_identical(readLines(file), c("some a", "and some more a a a a"))
  
  expect_true(batch_gsub(
    pattern = ".", replacement = "b", path = path,
    ask = FALSE
  ))
  expect_identical(readLines(file), c("bbbbbb", "bbbbbbbbbbbbbbbbbbbbb"))
})
poissonconsulting/batchgsub documentation built on Feb. 13, 2021, 6:20 p.m.