tests/testthat/test-add_ins.R

# make_path_norm() -------------------------------------------------------------

test_that("make_path_norm() - passes normalized path to modifyRange", {
  modified <- NULL

  local_mocked_bindings(
    getActiveDocumentContext = function() list(
      id = "test_id",
      selection = list(list(range = list(), text = "a/./b"))
    ),
    modifyRange = function(range, text, id) modified <<- text,
    .package = "rstudioapi"
  )

  make_path_norm()
  expect_equal(modified, "a/b")
})

test_that("make_path_norm() - flips backslashes to forward slashes", {
  modified <- NULL

  local_mocked_bindings(
    getActiveDocumentContext = function() list(
      id = "test_id",
      selection = list(list(range = list(), text = "a\\b"))
    ),
    modifyRange = function(range, text, id) modified <<- text,
    .package = "rstudioapi"
  )

  make_path_norm()
  expect_equal(modified, "a/b")
})

test_that("make_path_norm() - normalizes multiple selections", {
  modified <- list()

  local_mocked_bindings(
    getActiveDocumentContext = function() list(
      id = "test_id",
      selection = list(
        list(range = list(), text = "a/./b"),
        list(range = list(), text = "c\\d")
      )
    ),
    modifyRange = function(range, text, id) modified[[length(modified) + 1]] <<- text,
    .package = "rstudioapi"
  )

  make_path_norm()
  expect_setequal(modified, c("a/b", "c/d"))
})

Try the rstudio.prefs package in your browser

Any scripts or data that you put into this service are public.

rstudio.prefs documentation built on Sept. 26, 2026, 5:06 p.m.