Skip to content

Commit

Permalink
Merged origin/main into blog-post
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Oct 13, 2023
2 parents 214d71a + 507cc9d commit 10dec43
Show file tree
Hide file tree
Showing 16 changed files with 306 additions and 204 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Suggests:
knitr,
methods,
rmarkdown,
testthat (>= 3.0.0),
testthat (>= 3.2.0),
tibble
VignetteBuilder:
knitr
Expand Down
25 changes: 15 additions & 10 deletions tests/testthat/_snaps/S3.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,39 @@

Code
foo2("a")
Error <simpleError>
<foo2> object is invalid:
Condition
Error:
! <foo2> object is invalid:
- Underlying data must be a double

# new_S3_class() checks its inputs

Code
new_S3_class(1)
Error <simpleError>
`class` must be a character vector
Condition
Error:
! `class` must be a character vector

---

Code
new_S3_class("foo", function(x) { })
Error <simpleError>
First argument to `constructor` must be .data
Condition
Error:
! First argument to `constructor` must be .data
Code
new_S3_class("foo", function(.data, ...) { })
Error <simpleError>
`constructor` can not use `...`
Condition
Error:
! `constructor` can not use `...`

# default new_S3_class constructor errors

Code
class_construct(new_S3_class("foo"), 1)
Error <simpleError>
S3 class <foo> doesn't have a constructor
Condition
Error:
! S3 class <foo> doesn't have a constructor

# catches invalid factors

Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/_snaps/S4.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Code
S4_to_S7_class(1)
Error <simpleError>
Unsupported S4 object: must be a class generator or a class definition, not a <double>.
Condition
Error:
! Unsupported S4 object: must be a class generator or a class definition, not a <double>.

10 changes: 6 additions & 4 deletions tests/testthat/_snaps/class-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

Code
as_class("foo")
Error <simpleError>
Can't convert `"foo"` to a valid class. Class specification must be an S7 class object, the result of `new_S3_class()`, an S4 class object, or a base class, not a <character>.
Condition
Error:
! Can't convert `"foo"` to a valid class. Class specification must be an S7 class object, the result of `new_S3_class()`, an S4 class object, or a base class, not a <character>.
Code
as_class(TRUE)
Error <simpleError>
Can't convert `TRUE` to a valid class. Class specification must be an S7 class object, the result of `new_S3_class()`, an S4 class object, or a base class, not a <logical>.
Condition
Error:
! Can't convert `TRUE` to a valid class. Class specification must be an S7 class object, the result of `new_S3_class()`, an S4 class object, or a base class, not a <logical>.

80 changes: 48 additions & 32 deletions tests/testthat/_snaps/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,89 +56,104 @@

Code
new_class(1)
Error <simpleError>
`name` must be a single string
Condition
Error:
! `name` must be a single string
Code
new_class("foo", 1)
Error <simpleError>
Can't convert `parent` to a valid class. Class specification must be an S7 class object, the result of `new_S3_class()`, an S4 class object, or a base class, not a <double>.
Condition
Error:
! Can't convert `parent` to a valid class. Class specification must be an S7 class object, the result of `new_S3_class()`, an S4 class object, or a base class, not a <double>.
Code
new_class("foo", package = 1)
Error <simpleError>
`package` must be a single string
Condition
Error:
! `package` must be a single string
Code
new_class("foo", constructor = 1)
Error <simpleError>
`constructor` must be a function
Condition
Error:
! `constructor` must be a function
Code
new_class("foo", constructor = function() { })
Error <simpleError>
`constructor` must contain a call to `new_object()`
Condition
Error:
! `constructor` must contain a call to `new_object()`
Code
new_class("foo", validator = function() { })
Error <simpleError>
`validator` must be function(self), not function()
Condition
Error:
! `validator` must be function(self), not function()

# S7 classes: can't inherit from S4 or class unions

Code
new_class("test", parent = parentS4)
Error <simpleError>
`parent` must be an S7 class, S3 class, or base type, not an S4 class.
Condition
Error:
! `parent` must be an S7 class, S3 class, or base type, not an S4 class.
Code
new_class("test", parent = new_union("character"))
Error <simpleError>
Can't convert `X[[i]]` to a valid class. Class specification must be an S7 class object, the result of `new_S3_class()`, an S4 class object, or a base class, not a <character>.
Condition
Error:
! Can't convert `X[[i]]` to a valid class. Class specification must be an S7 class object, the result of `new_S3_class()`, an S4 class object, or a base class, not a <character>.

# S7 classes: can't inherit from an environment

Code
new_class("test", parent = class_environment)
Error <simpleError>
Can't inherit from an environment.
Condition
Error:
! Can't inherit from an environment.

# abstract classes: can't be instantiated

Code
foo <- new_class("foo", abstract = TRUE)
foo()
Error <simpleError>
Can't construct an object from abstract class <foo>
Condition
Error in `new_object()`:
! Can't construct an object from abstract class <foo>

# abstract classes: can't inherit from concrete class

Code
foo1 <- new_class("foo1")
new_class("foo2", parent = foo1, abstract = TRUE)
Error <simpleError>
Abstract classes must have abstract parents
Condition
Error in `new_class()`:
! Abstract classes must have abstract parents

# abstract classes: can use inherited validator from abstract class

Code
foo2(x = 2)
Error <simpleError>
<foo2> object is invalid:
Condition
Error:
! <foo2> object is invalid:
- @x has bad value

# new_object(): gives useful error if called directly

Code
new_object()
Error <simpleError>
`new_object()` must be called from within a constructor
Condition
Error in `new_object()`:
! `new_object()` must be called from within a constructor

# new_object(): validates object

Code
foo("x")
Error <simpleError>
<foo> object properties are invalid:
Condition
Error:
! <foo> object properties are invalid:
- @x must be <double>, not <character>
Code
foo(-1)
Error <simpleError>
<foo> object is invalid:
Condition
Error:
! <foo> object is invalid:
- x must be positive

# new_object(): runs each parent validator exactly once
Expand Down Expand Up @@ -205,6 +220,7 @@

Code
c(foo1, foo1)
Error <simpleError>
Can not combine S7 class objects
Condition
Error:
! Can not combine S7 class objects

5 changes: 3 additions & 2 deletions tests/testthat/_snaps/convert.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

Code
convert(obj, to = class_double)
Error <simpleError>
Can't find method for generic `convert()` with dispatch classes:
Condition
Error:
! Can't find method for generic `convert()` with dispatch classes:
- from: <converttest>
- to : <double>

10 changes: 6 additions & 4 deletions tests/testthat/_snaps/generic-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

Code
as_generic(function() { })
Error <simpleError>
`generic` is a function, but not an S3 generic function
Condition
Error:
! `generic` is a function, but not an S3 generic function

---

Code
as_generic(1)
Error <simpleError>
`generic` must be a function, not a <double>
Condition
Error:
! `generic` must be a function, not a <double>

65 changes: 39 additions & 26 deletions tests/testthat/_snaps/generic.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,62 @@

Code
new_generic(1)
Error <simpleError>
`name` must be a single string
Condition
Error:
! `name` must be a single string
Code
new_generic("")
Error <simpleError>
`name` must not be "" or NA
Condition
Error:
! `name` must not be "" or NA
Code
new_generic("foo", 1)
Error <simpleError>
`dispatch_args` must be a character vector
Condition
Error:
! `dispatch_args` must be a character vector
Code
new_generic("foo", "x", function(x) { })
Error <simpleError>
`fun` must contain a call to `S7_dispatch()`
Condition
Error:
! `fun` must contain a call to `S7_dispatch()`

# check_dispatch_args() produces informative errors

Code
check_dispatch_args(1)
Error <simpleError>
`dispatch_args` must be a character vector
Condition
Error:
! `dispatch_args` must be a character vector
Code
check_dispatch_args(character())
Error <simpleError>
`dispatch_args` must have at least one component
Condition
Error:
! `dispatch_args` must have at least one component
Code
check_dispatch_args("")
Error <simpleError>
`dispatch_args` must not be missing or the empty string
Condition
Error in `check_dispatch_args()`:
! `dispatch_args` must not be missing or the empty string
Code
check_dispatch_args(NA_character_)
Error <simpleError>
`dispatch_args` must not be missing or the empty string
Condition
Error in `check_dispatch_args()`:
! `dispatch_args` must not be missing or the empty string
Code
check_dispatch_args(c("x", "x"))
Error <simpleError>
`dispatch_args` must be unique
Condition
Error:
! `dispatch_args` must be unique
Code
check_dispatch_args("...")
Error <simpleError>
Can't dispatch on `...`
Condition
Error:
! Can't dispatch on `...`
Code
check_dispatch_args("y", function(x, ..., y) { })
Error <simpleError>
`dispatch_args` must be a prefix of the generic arguments
Condition
Error:
! `dispatch_args` must be a prefix of the generic arguments

# S7_generic printing

Expand Down Expand Up @@ -75,10 +86,12 @@

Code
check_generic("x")
Error <simpleError>
`fun` must be a function
Condition
Error:
! `fun` must be a function
Code
check_generic(function() { })
Error <simpleError>
`fun` must contain a call to `S7_dispatch()`
Condition
Error:
! `fun` must contain a call to `S7_dispatch()`

15 changes: 9 additions & 6 deletions tests/testthat/_snaps/inherits.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

Code
S7_inherits(1:10, "x")
Error <simpleError>
`class` must be an <S7_class> or NULL
Condition
Error in `S7_inherits()`:
! `class` must be an <S7_class> or NULL

# throws informative error

Code
foo1 <- new_class("foo1")
foo2 <- new_class("foo2")
check_is_S7(foo1(), foo2)
Error <simpleError>
`foo1()` must be a <foo2>, not a <foo1>
Condition
Error:
! `foo1()` must be a <foo2>, not a <foo1>

---

Code
check_is_S7("a")
Error <simpleError>
`"a"` must be an <S7_object>, not a <character>
Condition
Error:
! `"a"` must be an <S7_object>, not a <character>

Loading

0 comments on commit 10dec43

Please sign in to comment.