Skip to content

Commit

Permalink
Add assertions and tests for freedom unit converter
Browse files Browse the repository at this point in the history
  • Loading branch information
NeuroShepherd committed Jul 7, 2024
1 parent 6d68f42 commit 4a5fd87
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions R/convert_to_freedom_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
#' convert_to_freedom_units(grades = c(1.3, 2.8))
#'
convert_to_freedom_units <- function(grades, max_passing_grade = 4, min_passing_grade = 1) {

if (max_passing_grade != 4 || min_passing_grade != 1) {
warning("The maximum passing grade and the minimum passing grade should almost always be 4 and 1, respectively, in the US grading system. Are you sure this should be changed?")
}

assertthat::assert_that(
grades >= 1.0,
grades <= 5.0,
msg = "grades must be between 1.0 and 5.0 for the Bavarian formula to work."
)

# Convert grades to freedom units
freedom_units <- 1 + ((max_passing_grade - grades) / (max_passing_grade - min_passing_grade)) * 3

Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-convert_to_freedom_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ test_that("conversion from German to custom US functions", {
expect_vector(convert_to_freedom_units(1:5, max_passing_grade = 4, min_passing_grade = 2))
expect_length(convert_to_freedom_units(1:5, max_passing_grade = 4, min_passing_grade = 2), 5)
})

test_that("check function assertions", {
expect_error(convert_to_freedom_units(0), "grades must be between 1.0 and 5.0 for the Bavarian formula to work.")
expect_error(convert_to_freedom_units(6), "grades must be between 1.0 and 5.0 for the Bavarian formula to work.")
})

test_that("check warning", {
expect_warning(convert_to_freedom_units(1:5, max_passing_grade = 3, min_passing_grade = 2), "The maximum passing grade and the minimum passing grade should almost always be 4 and 1, respectively, in the US grading system. Are you sure this should be changed?")
})

0 comments on commit 4a5fd87

Please sign in to comment.