tests/testthat/test-json_lengths.R

test_that("works for arrays", {
    
    json <- c('[]', '[1]', '[1, 2]')
    expect_identical(
      json %>% json_lengths %>% `$`(length),
      c(0L, 1L, 2L)
    )
    
  }
)

test_that("works for objects", {
    
    json <- c('{}', '{"k":"v"}', '{"k1":"v1", "k2":"v2"}') 
    expect_identical(
      json %>% json_lengths %>% `$`(length),
      c(0L, 1L, 2L)
    )
    
  }
)

test_that("works for scalars", {
    
    json <- c('[1, "a", true]') 
    expect_identical(
      json %>% gather_array %>% json_lengths %>% `$`(length),
      rep(1L, 3)
    )
     
  }
)

test_that("works for emtpy objects", {
    
    json <- character(0) 
    expect_identical(
      json %>% json_lengths %>% `$`(length),
      integer(0)
    )
    
    json <- c('[null, [], {}]') 
    expect_identical(
      json %>% gather_array %>% json_lengths %>% `$`(length),
      rep(0L, 3)
    )
     
  }
)

Try the tidyjson package in your browser

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

tidyjson documentation built on Jan. 7, 2023, 1:14 a.m.