File: test-r.R

package info (click to toggle)
r-cran-usethis 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,228 kB
  • sloc: sh: 26; makefile: 17; cpp: 6; ansic: 3
file content (106 lines) | stat: -rw-r--r-- 2,902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
test_that("use_r() creates a .R file below R/", {
  create_local_package()
  use_r("foo")
  expect_proj_file("R/foo.R")
})

test_that("use_test() creates a test file", {
  create_local_package()
  use_test("foo", open = FALSE)
  expect_proj_file("tests", "testthat", "test-foo.R")
})

test_that("use_test_helper() creates a helper file", {
  create_local_package()

  expect_snapshot(
    error = TRUE,
    use_test_helper(open = FALSE)
  )
  use_testthat()

  use_test_helper(open = FALSE)
  withr::local_options(list(usethis.quiet = FALSE))
  expect_snapshot(
    use_test_helper("foo", open = FALSE)
  )

  expect_proj_file("tests", "testthat", "helper.R")
  expect_proj_file("tests", "testthat", "helper-foo.R")
})

test_that("can use use_test() in a project", {
  create_local_project()
  expect_no_error(use_test("foofy"))
})

# helpers -----------------------------------------------------------------

test_that("compute_name() errors if no RStudio", {
  local_rstudio_available(FALSE)
  expect_snapshot(compute_name(), error = TRUE)
})

test_that("compute_name() sets extension if missing", {
  expect_equal(compute_name("foo"), "foo.R")
})

test_that("compute_name() validates its inputs", {
  expect_snapshot(error = TRUE, {
    compute_name("foo.c")
    compute_name("R/foo.c")
    compute_name(c("a", "b"))
    compute_name("")
    compute_name("****")
  })
})

test_that("compute_active_name() errors if no files open", {
  expect_snapshot(compute_active_name(NULL), error = TRUE)
})

test_that("compute_active_name() checks directory", {
  expect_snapshot(compute_active_name("foo/bar.R"), error = TRUE)
})

test_that("compute_active_name() standardises name", {
  dir <- create_local_project()

  expect_equal(
    compute_active_name(path(dir, "R/bar.R"), "c"),
    "bar.c"
  )
  expect_equal(
    compute_active_name(path(dir, "src/bar.cpp"), "R"),
    "bar.R"
  )
  expect_equal(
    compute_active_name(path(dir, "tests/testthat/test-bar.R"), "R"),
    "bar.R"
  )

  expect_equal(
    compute_active_name(path(dir, "tests/testthat/_snaps/bar.md"), "R"),
    "bar.R"
  )
  # https://github.com/r-lib/usethis/issues/1690
  expect_equal(
    compute_active_name(path(dir, "R/data.frame.R"), "R"),
    "data.frame.R"
  )
})

# https://github.com/r-lib/usethis/issues/1863
test_that("compute_name() accepts the declared extension", {
  expect_equal(compute_name("foo.cpp", ext = "cpp"), "foo.cpp")
})

test_that("as_test_helper_file() works", {
  expect_equal(as_test_helper_file(), "helper.R")
  expect_equal(as_test_helper_file("helper"), "helper.R")
  expect_equal(as_test_helper_file("helper.R"), "helper.R")
  expect_equal(as_test_helper_file("stuff"), "helper-stuff.R")
  expect_equal(as_test_helper_file("helper-stuff"), "helper-stuff.R")
  expect_equal(as_test_helper_file("stuff.R"), "helper-stuff.R")
  expect_equal(as_test_helper_file("helper-stuff.R"), "helper-stuff.R")
})