---
title: "PKNCA Validation"
author: "Bill Denney"
output:
  rmarkdown::html_vignette:
    toc: yes
    toc_depth: 6
vignette: >
  %\VignetteIndexEntry{PKNCA Validation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE,
                      fig.width=6,
                      fig.height=4)
library(PKNCA)
library(knitr)
library(testthat)
library(dplyr)

run_tests <- dir.exists(system.file(package="PKNCA", "tests/"))
testing_results <- "Tests were not run because tests are not installed."
```

# Introduction

To run the tests, the package must be installed with its tests:

* To install from CRAN:
    * `install.packages(pkgs="PKNCA", INSTALL_opts="--install-tests", type="source")`
* To install from GitHub:
    * `library(devtools)`
    * `install_github("billdenney/pknca", INSTALL_opts="--install-tests")`

Testing and validation that results match in a local environment compared to the original environment is an important part of confirmation that a package works as expected.

Re-running this vignette in your local environment will confirm that local results match those in the original package development.  Test success is confirmed by the existence of no failed tests; warnings are expected during testing (and not shown in this vignette for that reason); and some tests may be skipped, but those are expected as well.

# Summary of Testing

```{r validation, include=FALSE, eval=run_tests}
testresult <- test_package(package="PKNCA")
test_log <- as.data.frame(testresult)
failed_tests <- sum(test_log$failed)
testing_results <-
  if (failed_tests == 0) {
    "All tests passed."
  } else {
    "Some tests failed (see below)."
  }
```

The following sentence is dynamically generated to summarize the testing results:  `r testing_results`

```{r testing_log, results="asis", echo=FALSE, eval=run_tests}
cat("# Testing Log\n\n")

all_contexts <- unique(test_log$context)
test_log_printing <-
  test_log %>%
  select(context, file, test, nb, failed) %>%
  rename(`Testing Filename`=file,
         `Test Description`=test,
         `Number of Tests`=nb,
         `Number of Failed Tests`=failed)

for (current_context in all_contexts) {
  current_log <-
    test_log_printing[
      test_log_printing$context %in% current_context,
      setdiff(names(test_log_printing), "context"), # context is in the header
      drop=FALSE]
  cat("## ", current_context, "\n\n")
  print(kable(x=current_log,
              row.names=FALSE))
  cat("\n\n")
}
```

# Session Information

```{r session_info}
Sys.Date()
sessionInfo()
```
