Git Product home page Git Product logo

testwhat.ext's Introduction

⚠️ This repo has outdated tokens in its travisci config To make new releases for this project it needs to be moved to circleci

testwhat.ext

Build Status codecov FOSSA Status

Extensions for testwhat for specific use-cases or more high level checks. Documentation can be found here.

To use the extensions in the submission correctness tests for an exercise, explicitly load the package before you use the function:

library(testwhat.ext)
ex() %>% check_cpp_function_exported("int", "answer")

Adding SCTs to testwhat.ext

Follow these steps

  1. Open a PR, merge into master when appropriate.
  2. Once merged, edit DESCRIPTION, incrementing VERSION: 0.0.1 to reflect changes (see semver for guidance).
  3. Create a github release labeled vVERSION. E.g. v0.0.1. (see here).

License

FOSSA Status

testwhat.ext's People

Contributors

andy-techen avatar ddmkr avatar filipsch avatar fossabot avatar machow avatar richierocks avatar sumedh10 avatar timsangster avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

fossabot

testwhat.ext's Issues

roxygen2:::parse_blocks no longer exists in roxygen2 version 6.1.0

@richierocks a new version of roxygen2 came out, version 6.1.0, and parse_blocks is no longer a hidden function in the package. You use this function in the R/parse.R file to find the roxygen blocks.

As these functions are nowhere used for the moment, I temporarily commented out the tests (in this branch). Feel free to do a PR that makes things work again. I can also do it at some point.

I personally don't think it makes sense to sink time in this until the functions are actually needed.

Allow pre-tidying of character strings

In the Bayesian Modeling with RJAGS course, JAGS models have to be specified as a string.

This means that many students are complaining about overly strict SCTs that don't allow for variation in spacing and simple formatting differences.

I had a play around in the internals of rjags, but there's no obvious way of extracting a normalized or compiled model.

However, since JAGS model code is very close to R code, formatR::tidy_source() can be used to standardize the spacing.

tidy_code <- function(x) {
  tidied <- formatR::tidy_source(
    text    = x, 
    comment = FALSE, 
    blank   = FALSE, 
    arrow   = TRUE,
    output  = FALSE
  )$text.tidy
  unlist(strsplit(tidied, "\n"))
}

Example usage is

model_text <- "model
{
    # Likelihood model for Y[i]
    for    (i in 1: length(  Y)   ) 
{
Y[i] ~     dnorm(  m, s    ^(-2))
    }

    # Prior models for m and s
 m ~ dnorm(50, 25    ^( -2)   )
         s      ~ dunif     (0,        200)
}"

tidy_code(model_text)
## [1] "model"                           "{"                              
## [3] "    for (i in 1:length(Y)) {"    "        Y[i] ~ dnorm(m, s^(-2))"
## [5] "    }"                           "    m ~ dnorm(50, 25^(-2))"     
## [7] "    s ~ dunif(0, 200)"           "}" 

My current plan is to combine this with

tidy_ex_code <- function(state) {
  childState <- ChildState$new(state)
  childState$set(
    student_code = tidy_code(childState$get("student_code")),
    solution_code = tidy_code(childState$get("solution_code"))
  )
  return(invisible(childState))
}

in order to be able to write SCTs like

ex() %>% tidy_ex_code() %>% {
  # check something
}

Does this sound like a sensible approach?

Run pre-exercise code when testing examples

From Devel R Pkgs Ch2 Ex9:

#' @examples
#' data_summary(iris)
#' data_summary(airquality, na.rm = FALSE)
data_summary <- function(x, na.rm = TRUE){
  
  num_data <- select_if(x, .predicate = is.numeric) 
  
  map_df(num_data, .f = numeric_summary, na.rm = na.rm, .id = "ID")
  
}

The examples don't run because dplyr and purrr aren't loaded.

I think the easiest fix is to run the pre-exercise code in the student environment/solution environment before running the example code.

So in the PEC, you would write

library(dplyr)
library(purrr)

This affects check_roxy_examples_run() and check_roxy_examples_result_equals().

Reorganize @import, @importFrom fields

roxygen2:::parse_blocks() imports these in a weird format that makes tests hard to write.

@import ought to become a character vector of package names.

@importFrom ought to become a list of character vectors. The names of the elements ought to be packages, and the character vectors ought to be names of objects imported from those packages.

Check @import tag in roxygen

Looks like testwhat.ext no longer exports check_roxy_imports_package() and check_roxy_imports_object_from_package(). @richierocks wrote the following SCT a while ago, but it no longer works.

Solution

#' @import dplyr
#' @import purrr
#' @importFrom tidyr gather

SCT

library(testwhat.ext)
ex() %>%
   parse_roxy() %>% {
     check_roxy_imports_package(., "dplyr")
     check_roxy_imports_package(., "purrr")
     check_roxy_imports_object_from_package(., "tidyr", "gather")
  }

GH
Teach

Was this accidental or is this replaced by some other functions? The plan is to launch the course by EOD, Friday (Aug 24). So any help is much appreciated!

cc @filipsch

Easier testing of base graphics

You can write code to draw base plots in several ways. For example, the following are all equivalent.

plot(cars$speed, cars$dist)

plot(dist ~ speed, cars)

with(cars, plot(speed, dist))

library(magrittr)
cars %$% plot(speed, dist)

Writing SCTs to check for all syntaxes is tricky. You have to do something like this:

test_or({
  ex() %>% check_function('plot') %>% {
    check_arg(., 'x') %>% check_equal()
    check_arg(., 'y') %>% check_equal()
  }
}, {
  ex() %>% override_solution('plot(dist ~ speed, cars))') %>% check_function('plot') %>% {
    check_arg(., 'formula') %>% check_equal()
    check_arg(., 'data') %>% check_equal()
  }
}, {
  ex() %>% override_solution('with(cars, plot(speed, dist))') %>% check_function('with') %>% {
    check_arg(., 'data') %>% check_equal()
    check_arg(., 'expr') %>% check_function('plot') %>% {
      check_arg(., 'x') %>% check_equal(eval = FALSE)
      check_arg(., 'y') %>% check_equal(eval = FALSE)
    }
  }
}, {
  ex() %>% override_solution('cars %$% plot(speed, dist)') %>%  {
    check_code(., 'cars %$%', fixed = TRUE)
    check_function(., 'plot') %>% {
      check_arg(., 'x') %>% check_equal(eval = FALSE)
      check_arg(., 'y') %>% check_equal(eval = FALSE)
    }
  }
})

Ideally, there would be a test_base_plot() function that handled all the specific cases.

One complication is that formula syntax is not supported for all base plot types. For example, hist() doesn't allow this. So you'd need a completely separate test_base_hist().

It might be easier from a technical point of view if the plot was first converted to a grid plot, in order to have an object to test. You can do this using grid.echo() and grid.grab() from the gridGraphics package.

See https://journal.r-project.org/archive/2015-1/murrell.pdf

FYI For base plotting, the Data Visualization in R course has longer, messy SCTs for the different cases in Ch1 and Ch2 (I didn’t get round to doing them for the later chapters yet) https://github.com/datacamp/courses-data-vis-in-r

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.