diff --git a/.travis.yml b/.travis.yml index e4c7d23..b1e776b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,5 @@ env: - REPO_TO_TEST=tmcRtestrunner - REPO_TO_TEST=tmcrstudioaddin - after_success: - Rscript -e 'library(covr);coveralls()' diff --git a/example_projects/example_project1/.Rbuildignore b/example_projects/example_project1/.Rbuildignore index fa7b248..4434527 100644 --- a/example_projects/example_project1/.Rbuildignore +++ b/example_projects/example_project1/.Rbuildignore @@ -1,4 +1,3 @@ ^\.travis\.yml$ ^.*\.Rproj$ ^\.Rproj\.user$ - diff --git a/example_projects/example_project1/.gitignore b/example_projects/example_project1/.gitignore index 0b09ec8..bec965e 100644 --- a/example_projects/example_project1/.gitignore +++ b/example_projects/example_project1/.gitignore @@ -1,2 +1,3 @@ .Rdata .Rhistory +.results.json diff --git a/example_projects/example_project1/.results.json b/example_projects/example_project1/.results.json deleted file mode 100644 index 6022408..0000000 --- a/example_projects/example_project1/.results.json +++ /dev/null @@ -1,84 +0,0 @@ -[ - { - "backtrace": [ - - ], - "status": "passed", - "name": "Addition works", - "message": "", - "points": [ - "point1" - ] - }, - { - "backtrace": [ - - ], - "status": "passed", - "name": "Multiplication works", - "message": "", - "points": [ - "point1", - "point2" - ] - }, - { - "backtrace": [ - - ], - "status": "passed", - "name": "Subtraction works", - "message": "", - "points": [ - "point1", - "point2" - ] - }, - { - "backtrace": [ - - ], - "status": "passed", - "name": "Division works", - "message": "", - "points": [ - "point1", - "point2" - ] - }, - { - "backtrace": [ - - ], - "status": "passed", - "name": "Matrix transpose with [[1,2]] works", - "message": "", - "points": [ - "point1" - ] - }, - { - "backtrace": [ - - ], - "status": "passed", - "name": "Matrix transpose with [[1,2],[3,4]] works", - "message": "", - "points": [ - "point1" - ] - }, - { - "backtrace": [ - - ], - "status": "passed", - "name": "Constant string works", - "message": "", - "points": [ - "point1", - "point2" - ] - } -] - diff --git a/example_projects/example_project1/DESCRIPTION b/example_projects/example_project1/DESCRIPTION index 2ed586d..329bf0b 100644 --- a/example_projects/example_project1/DESCRIPTION +++ b/example_projects/example_project1/DESCRIPTION @@ -3,7 +3,7 @@ Title: What the Package Does (one line, title case) Version: 0.0.0.9000 Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre")) Description: What the package does (one paragraph). -Depends: R (>= 3.3.2) +Depends: R (>= 3.2.3) License: What license is it under? Encoding: UTF-8 LazyData: true diff --git a/example_projects/example_project1/NAMESPACE b/example_projects/example_project1/NAMESPACE new file mode 100644 index 0000000..ce08526 --- /dev/null +++ b/example_projects/example_project1/NAMESPACE @@ -0,0 +1,2 @@ +# Generated by roxygen2: fake comment so roxygen2 overwrites silently. +exportPattern("^[^\\.]") \ No newline at end of file diff --git a/example_projects/example_project1/R/week1.R b/example_projects/example_project1/R/week1.R new file mode 100644 index 0000000..07deb63 --- /dev/null +++ b/example_projects/example_project1/R/week1.R @@ -0,0 +1,73 @@ +##These exercises are taken from the course "Data-analyysi R-ohjelmistolla" +#(https://wiki.helsinki.fi/pages/viewpage.action?pageId=135074576). +## + +##Exercise 1 + +a1 <- 4 + 10 +b1 <- 5*a1 +c1 <- b1^3 +d1 <- exp(b1) +e1 <- d1^(1/10) +f1 <- sin(c1) +res1 <- (b1 + c1 + e1 + f1) + +##Exercise 2 + +a2 <- 51%%7 + +##Excercise 3 +a3 <- 51%%7 + 51%/%7 + +##Excercise 4 +v4_1 <- c(20, 5, -2, 3, 47) +v4_2 <- seq(0, 100, 5) +v4_3 <- c(v4_1, v4_2) +v4_4 <- v4_3[(v4_3 > 3) & (v4_3 < 50)] +v4_5 <- v4_4[(v4_4 %% 10) == 0] + +##Exercise 5 +v5_1 <- rep(0, 1, 50) +v5_1[c(F, T)] <- 2 + +sum5_1 <- sum(v5_1) +v5_1 +v5_2 <- v5_1 +v5_2[c(T, F)] <- 1.2 +sum5_2 <- sum(v5_2) + +##Excerice 6 + +A <- matrix(c(3, 5, 6, 1/2, sqrt(5), 16, 0, 2, 0),nrow = 3, ncol = 3, byrow = TRUE) + + +##Excercise 7 +B <- c(1, 1, 0)%*% solve(A) + +##Excercise 8 +I_3 <- diag(c(1, 1, 1)) +A_8 <- A %*%I_3 + +##Excercise 9 +is_eq_matrix <- t(A) == A +is_eq_matrix <- F + +##Excercise 10 +C_10 <- matrix(c(runif(20, min = 1, max=20)), ncol=4) +v10_1 <- C_10[C_10 < 5] +number10 <- length(v10_1) + +D_10 <- C_10 +D_10[D_10 < 5] <- 10 + +E_10 <- D_10 +E_10 <- D_10[c(T, T, T, T, F),c(F,T,T, T)] + + +##Excercise 11 +C_11 <- matrix(1:100, ncol=2) +C_11[c(F, T)] <- NA + +##Excercise 12 +C_12 <- C_11 +C_12[is.na(C_12)] <- 0 diff --git a/example_projects/example_project1/example_project1.Rproj b/example_projects/example_project1/example_project1.Rproj index 8e3c2eb..21a4da0 100644 --- a/example_projects/example_project1/example_project1.Rproj +++ b/example_projects/example_project1/example_project1.Rproj @@ -11,3 +11,7 @@ Encoding: UTF-8 RnwWeave: Sweave LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/example_projects/example_project1/tests/testthat/helperTMC.R b/example_projects/example_project1/tests/testthat/helperTMC.R new file mode 100644 index 0000000..51a55d8 --- /dev/null +++ b/example_projects/example_project1/tests/testthat/helperTMC.R @@ -0,0 +1,13 @@ + +#Sets the points for all tests to global environment, wherefrom they can +#be retrieved. +pointsForAllTests <- function(points) { + .GlobalEnv$points_for_all_tests <- points +} + +#The test that wraps around test_that()-method and stores the points +#to global environment. +test <- function(desc, points, code) { + .GlobalEnv$points[[desc]] <- points + test_that(desc, code) +} diff --git a/example_projects/example_project1/tests/testthat/testArithmetics.R b/example_projects/example_project1/tests/testthat/testArithmetics.R index ec9b5e9..f889db7 100644 --- a/example_projects/example_project1/tests/testthat/testArithmetics.R +++ b/example_projects/example_project1/tests/testthat/testArithmetics.R @@ -1,25 +1,36 @@ library('testthat') -source('../../R/arithmetics.R') -#Testing whether addition works. -test_that("Addition works#[point1]", { +source("../../R/arithmetics.R") + +pointsForAllTests(c("r1")) + +test("Addition works", c("r1.1", "r1.2"), { expect_equal(add(1, 2), 3) expect_equal(add(1, 2), 3.0) expect_equal(add(1, 4), 5) }) -test_that("Multiplication works#[point1, point2]", { +test("Multiplication works", c("r1.3", "r1.4"), { expect_equal(multiply(1, 2), 2) expect_equal(multiply(2, 10), 20) }) -test_that("Subtraction works#[point1, point2]", { - expect_equal(subtract(10, 2), 8) - expect_equal(subtract(0, 0), 0) - expect_equal(subtract(0, 4), -4) +test("Subtraction works", c("r1.5"), { + expect_equal(subtract(10, 2), 8) + expect_equal(subtract(0, 0), 0) + expect_equal(subtract(0, 4), -4) +}) + +test("Division works", c("r1.6"), { + expect_equal(divide(10, 2), 5) + expect_equal(divide(1, 2), 0.5) +}) + +test("Test with no points", c(), { + expect_equal(1,1) }) -test_that("Division works#[point1, point2]", { - expect_equal(divide(10, 2), 5) - expect_equal(divide(1, 2), 0.5) +test("Dummy test set to fail", c(), { + expect_true(FALSE) + expect_equal(1,2) }) diff --git a/example_projects/example_project1/tests/testthat/testMatrices.R b/example_projects/example_project1/tests/testthat/testMatrices.R index 788b661..8c8eb82 100644 --- a/example_projects/example_project1/tests/testthat/testMatrices.R +++ b/example_projects/example_project1/tests/testthat/testMatrices.R @@ -1,11 +1,14 @@ library('testthat') source('../../R/matrices.R') -test_that("Matrix transpose with [[1,2]] works#[point1]", { +pointsForAllTests(c("r2")) + +test("Matrix transpose with [[1,2]] works", c("r2.1"), { A<- matrix(c(1,2),nrow=1) expect_equal(transposeMatrix(A),matrix(c(1,2),nrow=2)) }) -test_that("Matrix transpose with [[1,2],[3,4]] works#[point1]", { + +test("Matrix transpose with [[1,2],[3,4]] works", c("r2.2"), { A<- matrix(c(1,2,3,4),nrow=2) expect_equal(transposeMatrix(A),matrix(c(1,3,2,4),nrow=2)) }) diff --git a/example_projects/example_project1/tests/testthat/testStrings.R b/example_projects/example_project1/tests/testthat/testStrings.R index 6a06c44..05b30a9 100644 --- a/example_projects/example_project1/tests/testthat/testStrings.R +++ b/example_projects/example_project1/tests/testthat/testStrings.R @@ -1,6 +1,8 @@ library('testthat') source('../../R/strings.R') -test_that("Constant string works#[point1, point2]", { +pointsForAllTests(c("r3")) + +test("Constant string works", c("r3.1"), { expect_equal(constant_string(), "jono") }) diff --git a/example_projects/example_project1/tests/testthat/testWeek1.R b/example_projects/example_project1/tests/testthat/testWeek1.R new file mode 100644 index 0000000..6896f4f --- /dev/null +++ b/example_projects/example_project1/tests/testthat/testWeek1.R @@ -0,0 +1,79 @@ +library('testthat') +source('../../R/week1.R') + +pointsForAllTests(c("r4")) + +help_A <- matrix(c(3, 5, 6, 1/2, sqrt(5), 16, 0, 2, 0),nrow = 3, ncol = 3, byrow = TRUE) + +test("Exercise 1 is correct", c("r4.1"), { + expect_equal(a1, 14) + expect_equal(b1, 70) + expect_equal(c1, 343000) + expect_true(abs(e1-1096.63) < 0.1) + expect_true(abs(f1 - 0.7920019) < 0.1) + expect_true(abs(res1 - 344167.4) < 0.1) +}) + +test("Exercise 2 is correct", c("r4.2"), { + expect_equal(a2, 2) +}) + +test("Exercise 3 is correct", c("r4.3", "r4.4"), { + expect_equal(a3, 9) +}) + +test("Exercise 4 is correct", c("r4.5"), { + expect_equal(v4_1, c(20, 5, -2, 3, 47)) + expect_equal(v4_2, c(0:20)*5) + expect_equal(v4_3, c(c(20, 5, -2, 3, 47), seq(0, 100, 5))) + expect_equal(v4_4, c(20, 5, 47, 5, 10, 15, 20, 25, 30, 35, 40, 45)) + expect_equal(v4_5, c(20, 10, 20, 30, 40)) +}) + +test("Exercise 5 is correct", c("r4.6"), { + t1 <- rep(0, 1, 50) + t1[c(F, T)] <- 2 + expect_equal(v5_1, t1) + expect_equal(sum5_1, 50) + expect_equal(sum5_2, 80) +}) + +test("Exercise 6 is correct", c("r4.7"), { + expect_equal(A, matrix(c(3, 5, 6, 1/2, sqrt(5), 16, 0, 2, 0),nrow = 3, ncol = 3, byrow = TRUE)) +}) + +test("Exercise 7 is correct", c("r4.8"), { + expect_equal(A, help_A) +}) + +test("Exercise 8 is correct", c("r4.9"), { + expect_equal(B, c(1, 1, 0)%*%solve(help_A)) +}) + +test("Exercise 9 is correct", c("r4.10"), { + expect_equal(I_3, diag(c(1, 1, 1))) + expect_equal(A_8, help_A) +}) + +test("Exercise 10 is correct", c("r4.11"), { + expect_false(is_eq_matrix) +}) + +test("Exercise 11 is correct", c("r4.12"), { + expect_true(number10 < 10) + + expect_true(dim(E_10)[1] == 4) + expect_true(dim(E_10)[2] == 3) +}) + +test("Exercise 12 is correct", c("r4.13"), { + M <- matrix(1:100, ncol=2) + M[c(F, T)] <- NA + expect_equal(C_11, M) +}) + +test("Exercise 13 is correct", c("r4.14"), { + M <- matrix(1:100, ncol=2) + M[c(F, T)] <- 0 + expect_equal(C_12, M) +}) diff --git a/example_projects/example_project1/tmc/result.R b/example_projects/example_project1/tmc/result.R index e124dd9..73f5a91 100644 --- a/example_projects/example_project1/tmc/result.R +++ b/example_projects/example_project1/tmc/result.R @@ -1,54 +1,4 @@ -library('testthat') -library('jsonlite') +library(tmcRtestrunner) -testthat_output <- test_dir('tests/testthat/', reporter="silent") - -results = list() - -for (test in testthat_output) { - test_failed <- FALSE - test_failures <- c() - for (result in test$results) { - if (format(result) != "As expected") { - test_failed <- TRUE - test_failures <- c(test_failures, format(result)) - } - } - - test_description <- strsplit(format(test$test), "#") - - #Contains an array of the points associated with this test. - points_assoc <- test_description[[1]][2] - points_assoc <- gsub("\\[|\\]", "", points_assoc) - points_assoc <- gsub(",", "", points_assoc) - points_assoc <- as.list(lapply(strsplit(points_assoc, '\\s+')[[1]], unbox)) - - #Contains the name of the test - test_name <- test_description[[1]][1] - - if (test_failed) { - print(paste(test_name, ": FAIL", sep = "")) - print(paste(" ", test_failures, sep = "")) - - test_result <- list(backtrace=list(), - status=unbox("failed"), - name=unbox(format(test_name)), - message=unbox(""), - points=points_assoc) - } else { - print(paste(test_name, ": PASS", sep = "")) - test_result <- list(backtrace=list(), - status=unbox("passed"), - name=unbox(format(test_name)), - message=unbox(""), - points=points_assoc) - } - results[[length(results)+1]] <- test_result -} - -#json utf-8 coded: -json <- enc2utf8(toJSON(results, pretty = FALSE)) -json <- prettify(json) - -#encode json to utf-8 and write file -write(json, ".results.json") +# Note: working directory must be the projects root! +runTests(getwd()) diff --git a/example_projects/example_project1/tmc_run_test_example.sh b/example_projects/example_project1/tmc_run_test_example.sh index 4b7a7a3..fc3febd 100755 --- a/example_projects/example_project1/tmc_run_test_example.sh +++ b/example_projects/example_project1/tmc_run_test_example.sh @@ -1,2 +1,4 @@ #!/bin/sh -Rscript tmc/result.R +#Currently this script needs to be run at project root! + +Rscript -e "library(tmcRtestrunner);runTests(\"$PWD\", print=TRUE)" diff --git a/tmcRtestrunner/DESCRIPTION b/tmcRtestrunner/DESCRIPTION index 972841a..9f2b751 100644 --- a/tmcRtestrunner/DESCRIPTION +++ b/tmcRtestrunner/DESCRIPTION @@ -16,3 +16,4 @@ Collate: 'ResultsJsonParser.R' 'TestthatResultReader.R' 'RunTests.R' + 'getAvailablePoints.R' diff --git a/tmcRtestrunner/R/getAvailablePoints.R b/tmcRtestrunner/R/getAvailablePoints.R new file mode 100644 index 0000000..fd519ee --- /dev/null +++ b/tmcRtestrunner/R/getAvailablePoints.R @@ -0,0 +1,21 @@ +library("testthat") +###tempOverride +test <- function(a,point,c){ + if(!is.null(point)){ + all_available_points<<-c(all_available_points,point) + } +} +###tempOverride +pointsForAllTests<-function(point){ + all_available_points<<-c(all_available_points,point) +} + +getAvailablePoints <-function(project_path){ + setwd(project_path) + all_available_points <<- c() + testFiles <- list.files(path="tests/testthat", pattern = "test.*\\.R", full.names = T, recursive = FALSE) + for (testFile in testFiles) { + test_file(testFile, reporter = "silent",load_helpers=FALSE) + } + return(all_available_points) +} diff --git a/tmcRtestrunner_0.1.0.tar.gz b/tmcRtestrunner_0.1.0.tar.gz new file mode 100644 index 0000000..d5fd4bb Binary files /dev/null and b/tmcRtestrunner_0.1.0.tar.gz differ diff --git a/tmcrstudioaddin/tests/testthat/testExample.R b/tmcrstudioaddin/tests/testthat/testExample.R new file mode 100644 index 0000000..9ad3c2a --- /dev/null +++ b/tmcrstudioaddin/tests/testthat/testExample.R @@ -0,0 +1,3 @@ +test_that("Example file with dummy function gets called", { + expect_true(returnsTrue()) +})