tests/testthat/test-r-macro.R

context("test-r-macro.R")

test_that("macro calculator work", {
  # should work
  expect_equal(calculate_macros(2565, c(
    c = 0.5, p = 0.25, f = 0.25
  )),
  c(c = 320.62, p = 160.31, f = 71.25))
  # balance is more than 1
  expect_error(calculate_macros(2565, c(
    c = 0.5, p = 0.5, f = 0.5
  )))
  # Balance is not of length 3
  expect_error(calculate_macros(2565, c(c = 0.5, p = 0.25)))
  # tdee is not a number
  expect_error(calculate_macros("2565", c(
    c = 0.5, p = 0.5, f = 0.5
  )))
  # balance is not numerical
  expect_error(calculate_macros(2565, c(
    c = 0.5, p = 0.5, f = "0.5"
  )))
  # output is numerical
  expect_equal(class(calculate_macros(2565, c(
    c = 0.5, p = 0.25, f = 0.25
  ))), "numeric")
  # output is of length 3
  expect_equal(length(calculate_macros(2565, c(
    c = 0.5, p = 0.25, f = 0.25
  ))), 3)
})

test_that("protein intake calculates correctly" , {
  df <- structure(
    list(
      `Grams of protein per kg bodyweight` = c(0.8,
                                               0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6),
      `Grams of protein intake` = c(64,
                                    72, 80, 88, 96, 104, 112, 120, 128)
    ),
    class = "data.frame",
    row.names = c(NA,
                  -9L)
  )
  expect_equal(calculate_protein_intake(80), df)
})

test_that("protein intake calculator checks it's input", {
  expect_error(calculate_protein_intake(-20))
  expect_error(calculate_protein_intake("a"))
})
MarijnJABoer/befitteR documentation built on April 24, 2020, 5:43 a.m.