tests/testthat/tests.R

library(tweak)
context("all tests")

url <- "https://cran.r-project.org/Rlogo.svg"
pth <- convert(url,to="path")
arr <- convert(pth,to="array3")
lng <- convert(arr,to="long")
mat <- convert(lng,to="matrix")
rst <- convert(mat,to="raster")
# path, url, array, long, matrix, raster

test_that("upload to url works", {
  expect_false(httr::http_error(convert(url,to="url")))
  expect_false(httr::http_error(convert(pth,to="url")))
  expect_false(httr::http_error(convert(arr,to="url")))
  expect_false(httr::http_error(convert(lng,to="url")))
  expect_false(httr::http_error(convert(mat,to="url")))
  expect_false(httr::http_error(convert(rst,to="url")))
})

test_that("saving as path works", {
  expect_true(file.exists(convert(url,to="path")))
  expect_true(file.exists(convert(pth,to="path")))
  expect_true(file.exists(convert(arr,to="path")))
  expect_true(file.exists(convert(lng,to="path")))
  expect_true(file.exists(convert(mat,to="path")))
  expect_true(file.exists(convert(rst,to="path")))
})


test_that("conversion to array works", {
  expect_equal(length(dim(convert(pth,to="array3"))), 3)
  expect_equal(dim(convert(pth,to="array3"))[3], 4)
  expect_is(convert(pth,to="array3")[1,1,1], "numeric")
  expect_is(convert(url,to="array3"), "array")
  expect_is(convert(pth,to="array3"), "array")
  expect_is(convert(arr,to="array3"), "array")
  expect_is(convert(lng,to="array3"), "array")
  expect_is(convert(mat,to="array3"), "array")
  expect_is(convert(rst,to="array3"), "array")
})

test_that("conversion to long works", {
  expect_is(convert(pth,to="long"), "matrix")
  expect_is(convert(pth,to="long")[1],"numeric")
  expect_equal(ncol(convert(url,to="long")), 4)
  expect_equal(ncol(convert(pth,to="long")), 4)
  expect_equal(ncol(convert(arr,to="long")), 4)
  expect_equal(ncol(convert(lng,to="long")), 4)
  expect_equal(ncol(convert(mat,to="long")), 4)
  expect_equal(ncol(convert(rst,to="long")), 4)
})

test_that("conversion to matrix works", {
  expect_is(convert(pth,to="matrix")[1],"character")
  expect_is(convert(url,to="matrix"), "matrix")
  expect_is(convert(pth,to="matrix"), "matrix")
  expect_is(convert(arr,to="matrix"), "matrix")
  expect_is(convert(lng,to="matrix"), "matrix")
  expect_is(convert(mat,to="matrix"), "matrix")
  expect_is(convert(rst,to="matrix"), "matrix")
})

test_that("conversion to raster works", {
  expect_is(convert(pth), "raster")
  expect_equal(length(dim(convert(pth))), 2)
  expect_is(convert(pth,to="raster")[1],"character")
  expect_is(convert(url,to="raster"), "raster")
  expect_is(convert(pth,to="raster"), "raster")
  expect_is(convert(arr,to="raster"), "raster")
  expect_is(convert(lng,to="raster"), "raster")
  expect_is(convert(mat,to="raster"), "raster")
  expect_is(convert(rst,to="raster"), "raster")
})

test_that("all image formats works", {
  # Image Author: Fizyplankton (public domain)
  expect_is(convert("https://www.gstatic.com/webp/gallery3/2_webp_a.webp"), "raster")
  expect_is(convert("https://www.gstatic.com/webp/gallery3/2_webp_a.png"),  "raster")
  expect_is(convert("http://newbusinesstechnology.co.uk/wp-content/uploads/2011/05/linux-penguin.jpg"), "raster")
  # couldn't find a tif...
  expect_is(convert("https://cran.r-project.org/Rlogo.svg"), "raster")
})
moodymudskipper/tweak documentation built on May 20, 2019, 8:49 a.m.