r-hyperspec / hySpc.read.txt

Import ASCII formatted data into hyperSpec
https://r-hyperspec.github.io/hySpc.read.txt/
MIT License
0 stars 1 forks source link

Template for `read_*()` unit tests #55

Open GegznaV opened 3 years ago

GegznaV commented 3 years ago

I suggest this basic template for unit tests of read_*() functions. In each situation, function/file-format-specific unit tests should be added. Written as RStudio snippet:

snippet hut
    # Unit tests -----------------------------------------------------------------

    hySpc.testthat::test(${1:function_to_test}) <- function() {
      local_edition(3)

      filename <- system.file(
        "extdata",
        "${2:path_to_file_to_read}",
        package = "hySpc.read.txt"
      )

      expect_silent(spc <- ${1:function_to_test}(filename))

      n_wl <- nwl(spc)
      n_rows <- nrow(spc)
      n_clos <- ncol(spc)

      test_that("${3:file format}: hyperSpec obj. dimensions are correct", {
        expect_equal(n_wl, ___)
        expect_equal(n_rows, ___)
        expect_equal(n_clos, ___)

      })

      test_that("${3:file format}: extra data are correct", {
        # @data colnames
        expect_equal(colnames(spc), c("spc", "filename", ___))

        # @data values
        # (Add tests, if relevant or remove this row)

      })

      test_that("${3:file format}: labels are correct", {
        expect_equal(spc@label\$.wavelength, NULL)
        expect_equal(spc@label\$spc, NULL)
        expect_equal(spc@label\$filename, "filename")
      })

      test_that("${3:file format}: spectra are correct", {
        # Dimensions of spectra matrix (@data$spc)
        expect_equal(dim(spc@data\$spc), c(___, ___))

        # Column names of spectra matrix
        expect_equal(colnames(spc@data\$spc)[1], "___")
        expect_equal(colnames(spc@data\$spc)[10], "___")
        expect_equal(colnames(spc@data\$spc)[n_wl], "___") # last name

        # Values of spectra matrix
        expect_equal(unname(spc@data\$spc[1, 1]), ___)
        expect_equal(unname(spc@data\$spc[2, 10]), ___)
        expect_equal(unname(spc@data\$spc[n_rows, n_wl]), ___) # last spc value

      })

      test_that("${3:file format}: wavelengths are correct", {
        expect_equal(spc@wavelength[1], ___)
        expect_equal(spc@wavelength[10], ___)
        expect_equal(spc@wavelength[n_wl], ___)
      })
    }
  1. I suggest checking just a few values and not a whole vector, matrix, etc. This would check the peace of functionality in a minimal way but make the test more readable.
  2. I think, values must not be rounded if possible.

This is how this template would look like for read_asc_Andor() function:

# Unit tests -----------------------------------------------------------------

hySpc.testthat::test(read_asc_Andor) <- function() {
  local_edition(3)

  filename <- system.file(
    "extdata",
    "asc.Andor/ASCII-Andor-Solis.asc",
    package = "hySpc.read.txt"
  )

  expect_silent(spc <- read_asc_Andor(filename))

  n_wl <- nwl(spc)
  n_rows <- nrow(spc)
  n_clos <- ncol(spc)

  test_that("Andor Solis .asc: hyperSpec obj. dimensions are correct", {
    expect_equal(n_wl, 63)
    expect_equal(n_rows, 5)
    expect_equal(n_clos, 2)

  })

  test_that("Andor Solis .asc: extra data are correct", {
    # @data colnames
    expect_equal(colnames(spc), c("spc", "filename"))

    # @data values
    # (Add tests, if relevant or remove this row)

  })

  test_that("Andor Solis .asc: labels are correct", {
    expect_equal(spc@label$.wavelength, NULL)
    expect_equal(spc@label$spc, NULL)
    expect_equal(spc@label$filename, "filename")
  })

  test_that("Andor Solis .asc: spectra are correct", {
    # Dimensions of spectra matrix (@data$spc)
    expect_equal(dim(spc@data$spc), c(5, 63))

    # Column names of spectra matrix
    expect_equal(colnames(spc@data$spc)[1], "161.408")
    expect_equal(colnames(spc@data$spc)[10], "200.184")
    expect_equal(colnames(spc@data$spc)[n_wl], "423.651") # last name

    # Values of spectra matrix
    expect_equal(unname(spc@data$spc[1, 1]), 3404)
    expect_equal(unname(spc@data$spc[2, 10]), 3405)
    expect_equal(unname(spc@data$spc[n_rows, n_wl]), 3415) # last spc value

  })

  test_that("Andor Solis .asc: wavelengths are correct", {
    expect_equal(spc@wavelength[1], 161.40845)
    expect_equal(spc@wavelength[10], 200.18387)
    expect_equal(spc@wavelength[n_wl], 423.65106)
  })
}

What is your opinion, @sangttruong, @bryanhanson, @cbeleites? Is this template OK for you?

bryanhanson commented 3 years ago

Is there a tool or function that automatically fills this template in with the values set off by, for example, ${3:file format}?

GegznaV commented 3 years ago

Do you mean code snippets in the RStudio IDE? When you insert a code snippet by typing the snippet keyword and pressing Shift+Tab (not sure what's the correct hotkey combination on Mac), the fields that have the same number, e.g. 3, can be filled with the same text at the same time. You can navigate between the fields by pressing Tab (move to the next field) and Shift+Tab (move to the previous field).

bryanhanson commented 3 years ago

Thanks, I'll look at the documentation.