Expectation predicate for testing the names of an object

expect_named(
  current,
  target,
  ...,
  ignore.order = FALSE,
  ignore.case = FALSE,
  info = NA_character_
)

Arguments

current

[R object or expression] Object or expression to be tested

target

[missing, character, or NULL] One of the following:

  • Left missing to test that current has names

  • A character vector giving the expected names of current

  • NULL to test that current is unnamed

...

Ignored

ignore.order

[logical(1L)] Ignore the order of names(current) with respect to target

ignore.case

[logical(1L)] Ignore upper- vs lower-case discrepancies between names(current) and target

info

[character(1L)] Optional user-defined message; must be a single string value

Value

A tinytest object. A tinytest object is a logical with attributes holding information about the test that was run

See also

Examples

(x <- c(a = 1L, b = 2L, c = 3L))
#> a b c 
#> 1 2 3 
(y <- c(4L, 5L, 6L))
#> [1] 4 5 6

expect_named(x) # Pass
#> ----- PASSED      : <-->
#>  call| expect_named(x) 
expect_named(y) # Fail
#> ----- FAILED[attr]: <-->
#>  call| expect_named(y)
#>  diff| Expected 'current' to be named 

expect_named(x, target = NULL) # Fail
#> ----- FAILED[attr]: <-->
#>  call| expect_named(x, target = NULL)
#>  diff| Expected 'current' to be unnamed 
expect_named(y, target = NULL) # Pass
#> ----- PASSED      : <-->
#>  call| expect_named(y, target = NULL) 

expect_named(x, target = c("a", "b", "c")) # Pass
#> ----- PASSED      : <-->
#>  call| expect_named(x, target = c("a", "b", "c")) 
expect_named(y, target = c("a", "b", "c")) # Fail
#> ----- FAILED[attr]: <-->
#>  call| expect_named(y, target = c("a", "b", "c"))
#>  diff| Expected 'names(current)' to match 'target' 

expect_named(x, target = c("b", "a", "c")) # Fail
#> ----- FAILED[attr]: <-->
#>  call| expect_named(x, target = c("b", "a", "c"))
#>  diff| Expected 'names(current)' to match 'target' 
expect_named(x, target = c("b", "a", "c"), ignore.order = TRUE) # Pass
#> ----- PASSED      : <-->
#>  call| expect_named(x, target = c("b", "a", "c"), ignore.order = TRUE) 

expect_named(x, target = c("A", "B", "C")) # Fail
#> ----- FAILED[attr]: <-->
#>  call| expect_named(x, target = c("A", "B", "C"))
#>  diff| Expected 'names(current)' to match 'target' 
expect_named(x, target = c("A", "B", "C"), ignore.case = TRUE) # Pass
#> ----- PASSED      : <-->
#>  call| expect_named(x, target = c("A", "B", "C"), ignore.case = TRUE)