Skip to content

Commit

Permalink
use non-syntatic class names in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kalinowski committed Nov 4, 2024
1 parent 0b21f03 commit 44ce813
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 30 deletions.
22 changes: 11 additions & 11 deletions tests/testthat/_snaps/external-generic.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@
Code
args(Foo)
Output
function (bar = t0::AnS7Class())
function (bar = t0::`An S7 Class`())
NULL
Code
args(t2::AnS7Class2)
args(t2::`An S7 Class 2`)
Output
function (bar = t0::AnS7Class())
function (bar = t0::`An S7 Class`())
NULL
Code
args(t2:::AnInternalClass)
args(t2:::`An Internal Class`)
Output
function (foo = t0::AnS7Class(), bar = AnS7Class2())
function (foo = t0::`An S7 Class`(), bar = `An S7 Class 2`())
NULL

---

Code
new_class("Foo", properties = list(bar = new_class("MadeUpClass", package = "t0")))
new_class("Foo", properties = list(bar = new_class("Made Up Class", package = "t0")))
Condition
Error:
! 'MadeUpClass' is not an exported object from 'namespace:t0'
! 'Made Up Class' is not an exported object from 'namespace:t0'
Code
new_class("Foo", properties = list(bar = new_class("MadeUpClass", package = "MadeUpPackage")))
new_class("Foo", properties = list(bar = new_class("Made Up Class", package = "Made Up Package")))
Condition
Error in `loadNamespace()`:
! there is no package called 'MadeUpPackage'
! there is no package called 'Made Up Package'
Code
modified_class <- t0::AnS7Class
modified_class <- t0::`An S7 Class`
attr(modified_class, "xyz") <- "abc"
new_class("Foo", properties = list(bar = modified_class))
Condition
Error:
! `t0::AnS7Class` is not identical to the class with the same @package and @name properties
! `t0::An S7 Class` is not identical to the class with the same @package and @name properties

2 changes: 1 addition & 1 deletion tests/testthat/t0/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(AnS7Class)
export("An S7 Class")
export(an_s3_generic)
export(an_s7_generic)
2 changes: 1 addition & 1 deletion tests/testthat/t0/R/t0.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ an_s7_generic <- S7::new_generic("an_s7_generic", "x")
an_s3_generic <- function(x) UseMethod("an_s3_generic")

#' @export
AnS7Class <- S7::new_class("AnS7Class")
`An S7 Class` <- S7::new_class("An S7 Class")
4 changes: 2 additions & 2 deletions tests/testthat/t2/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(AnS7Class2)
export("An S7 Class 2")
export(an_s7_class)
importFrom(t0,AnS7Class)
importFrom(t0, `An S7 Class`)
importFrom(t0,an_s3_generic)
importFrom(t0,an_s7_generic)
11 changes: 6 additions & 5 deletions tests/testthat/t2/R/t2.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ S7::method(an_s7_generic, an_s7_class) <- function(x) "foo"
S7::method(an_s3_generic, an_s7_class) <- function(x) "foo"


#' @importFrom t0 AnS7Class
#' @rawNamespace importFrom(t0, `An S7 Class`)
#' @export
AnS7Class2 <- S7::new_class("AnS7Class2", properties = list(bar = AnS7Class))
`An S7 Class 2` <- S7::new_class("An S7 Class 2", properties = list(bar = `An S7 Class`))
NULL

AnInternalClass <- S7::new_class("AnInternalClass", properties = list(
foo = AnS7Class,
bar = AnS7Class2
`An Internal Class` <- S7::new_class("An Internal Class", properties = list(
foo = `An S7 Class`,
bar = `An S7 Class 2`
))


Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-external-generic.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,31 @@ test_that("new_method works with both hard and soft dependencies", {
# to t0::AnS7Class() (and not inline the full class object).
# As these tests grow, consider splitting this into a separate context like:
# test_that("package exported classes are not inlined in constructor formals", {...})
Foo <- new_class("Foo", properties = list(bar = t0::AnS7Class))
expect_identical(formals(Foo) , as.pairlist(alist(bar = t0::AnS7Class())))
expect_identical(formals(t2::AnS7Class2), as.pairlist(alist(bar = t0::AnS7Class())))
expect_identical(formals(t2:::AnInternalClass), as.pairlist(alist(
foo = t0::AnS7Class(), bar = AnS7Class2()
Foo <- new_class("Foo", properties = list(bar = t0::`An S7 Class`))
expect_identical(formals(Foo) , as.pairlist(alist(bar = t0::`An S7 Class`())))
expect_identical(formals(t2::`An S7 Class 2`), as.pairlist(alist(bar = t0::`An S7 Class`())))
expect_identical(formals(t2:::`An Internal Class`), as.pairlist(alist(
foo = t0::`An S7 Class`(), bar = `An S7 Class 2`()
)))

expect_snapshot({
args(Foo)
args(t2::AnS7Class2)
args(t2:::AnInternalClass)
args(t2::`An S7 Class 2`)
args(t2:::`An Internal Class`)
})

# test we emit informative error messages if a new_class() call with an
# external class dependency is malformed.
# https://github.com/RConsortium/S7/issues/477
expect_snapshot(error = TRUE, {
new_class("Foo", properties = list(
bar = new_class("MadeUpClass", package = "t0")
bar = new_class("Made Up Class", package = "t0")
))
new_class("Foo", properties = list(
bar = new_class("MadeUpClass", package = "MadeUpPackage")
bar = new_class("Made Up Class", package = "Made Up Package")
))

modified_class <- t0::AnS7Class
modified_class <- t0::`An S7 Class`
attr(modified_class, "xyz") <- "abc"
new_class("Foo", properties = list(bar = modified_class))
})
Expand Down

0 comments on commit 44ce813

Please sign in to comment.