Skip to content

Commit f5f2a31

Browse files
committed
Testthat repo added
1 parent 664b15e commit f5f2a31

5 files changed

Lines changed: 57 additions & 7 deletions

File tree

temperature/temp_conversion.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' Fahrenheit conversion
2+
#'
3+
#' Convert degrees Fahrenheit temperatures to degrees Celsius
4+
#' @param F_temp The temperature in degrees Fahrenheit
5+
#' @return The temperature in degrees Celsius
6+
#' @examples
7+
#' temp1 <- F_to_C(50);
8+
#' temp2 <- F_to_C( c(50, 63, 23) );
9+
#' @export
10+
F_to_C <- function(F_temp){
11+
C_temp <- (F_temp - 32) * 5/9;
12+
return(C_temp);
13+
}
14+
15+
#' Celsius conversion
16+
#'
17+
#' Convert degrees Celsius temperatures to degrees Fahrenheit
18+
#' @param C_temp The temperature in degrees Celsius
19+
#' @return The temperature in degrees Fahrenheit
20+
#' @examples
21+
#' temp1 <- C_to_F(22);
22+
#' temp2 <- C_to_F( c(-2, 12, 23) );
23+
#' @export
24+
C_to_F <- function(C_temp){
25+
F_temp <- (C_temp * 9/5) + 32;
26+
return(F_temp);
27+
}

temperature/test-temp_conversion.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library(testthat);
2+
context("Temperature function testing");
3+
source("temp_conversion.R");
4+
5+
test_that("Fahrenheit to Celsius", {
6+
7+
temp_C <- F_to_C(50);
8+
9+
# Test that the result is numeric
10+
expect_that( object = is.numeric(temp_C), condition = equals(TRUE) );
11+
12+
# Test that the result is the correct value
13+
expect_that( object = temp_C, condition = equals(10) );
14+
})
15+
16+
test_that("Celsius to Fahrenheit", {
17+
18+
temp_F <- C_to_F(10);
19+
20+
# Test that the result is numeric
21+
expect_that( object = is.numeric(temp_F), condition = equals(TRUE) );
22+
23+
# Test that the result is the correct value
24+
expect_that( object = temp_F, condition = equals(50) );
25+
})
26+
27+
28+
# The two tests can be run on the command line as below
29+
# test_dir(".")

test-temp_conversion.R

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ test_that("Celsius to Fahrenheit", {
2424
expect_that( object = temp_F, condition = equals(50) );
2525
})
2626

27-
# This test will fail
28-
test_that(desc = "Fahrenheit to Celsius wrong", code = {
29-
temp_F <- F_to_C(50);
30-
31-
expect_that( object = temp_F, condition = equals(2) );
32-
})
3327

3428
# The two tests can be run on the command line as below
3529
# test_dir(".")

testing_slides.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Introduction to testing R code"
33
subtitle: "https://stirlingcodingclub.github.io/code_testing"
44
author: "Brad Duthie"
5-
date: "24 April 2024"
5+
date: "15 April 2025"
66
output:
77
beamer_presentation:
88
theme: "default"

testing_slides.pdf

139 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)