Skip to content

Commit

Permalink
fix error in factor_conc not reporting if molar_mass etc. are needed …
Browse files Browse the repository at this point in the history
…but missing, add tests
  • Loading branch information
randomchars42 committed Oct 14, 2017
1 parent 5f4ecf6 commit 6e27e8b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: bioset
Type: Package
Title: Convert a Matrix of Raw Values into Nice and Tidy Data
Version: 0.2.0.9000
Version: 0.2.0.9001
Authors@R: person("Eike Christian", "Kühn",
email = "[email protected]",
role = c("aut", "cre"))
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* be more permissive with whitespace in input
* add more tests
* permit more metric prefixes
* fix error in `factor_conc`:
* `factor_conc` did stop if `molar_mass` / `density_solute` /
`densitiy_solution` were needed but missing
* add unit tests

# bioset 0.1.0

Expand Down
45 changes: 24 additions & 21 deletions R/calc.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,20 @@ factor_conc <- function(
from_type <- get_conc_type(from)
to_type <- get_conc_type(to)

if (needs_density_solute(from, to) && density_solute == 0) {
message(from_type)
message(to_type)

if (needs_density_solute(from_type, to_type) & density_solute == 0) {
stop("The density of the solute is required to convert ", from, " to ", to)
}

if (needs_density_solution(from, to) && density_solution == 0) {
if (needs_density_solution(from_type, to_type) & density_solution == 0) {
stop(
"The density of the solution (not the solvent!) is required to convert ",
from, " to ", to)
}

if (needs_molar_mass(from, to) && molar_mass == 0) {
if (needs_molar_mass(from_type, to_type) & molar_mass == 0) {
stop("The molar mass is required to convert ", from, " to ", to)
}

Expand Down Expand Up @@ -407,22 +410,22 @@ needs_density_solute <- function(from, to) {

if (from == to) {
needed <- FALSE
} else if ("mass_vol" %in% types && "molar_vol" %in% types) {
} else if ("mass_vol" %in% types & "molar_vol" %in% types) {
# mass_vol <-> molar_vol
needed <- FALSE
} else if ("mass_vol" %in% types && "vol_vol" %in% types) {
} else if ("mass_vol" %in% types & "vol_vol" %in% types) {
# mass_vol <-> vol_vol
needed <- TRUE
} else if ("mass_vol" %in% types && "mass_mass" %in% types) {
} else if ("mass_vol" %in% types & "mass_mass" %in% types) {
# mass_vol <-> mass_mass
needed <- FALSE
} else if ("molar_vol" %in% types && "vol_vol" %in% types) {
} else if ("molar_vol" %in% types & "vol_vol" %in% types) {
# molar_vol <-> vol_vol
needed <- TRUE
} else if ("molar_vol" %in% types && "mass_mass" %in% types) {
} else if ("molar_vol" %in% types & "mass_mass" %in% types) {
# molar_vol <-> mass_mass
needed <- FALSE
} else if ("mass_mass" %in% types && "vol_vol" %in% types) {
} else if ("mass_mass" %in% types & "vol_vol" %in% types) {
# mass_mass <-> vol_vol
needed <- TRUE
} else {
Expand All @@ -437,22 +440,22 @@ needs_density_solution <- function(from, to) {

if (from == to) {
needed <- FALSE
} else if ("mass_vol" %in% types && "molar_vol" %in% types) {
} else if ("mass_vol" %in% types & "molar_vol" %in% types) {
# mass_vol <-> molar_vol
needed <- FALSE
} else if ("mass_vol" %in% types && "vol_vol" %in% types) {
} else if ("mass_vol" %in% types & "vol_vol" %in% types) {
# mass_vol <-> vol_vol
needed <- FALSE
} else if ("mass_vol" %in% types && "mass_mass" %in% types) {
} else if ("mass_vol" %in% types & "mass_mass" %in% types) {
# mass_vol <-> mass_mass
needed <- TRUE
} else if ("molar_vol" %in% types && "vol_vol" %in% types) {
} else if ("molar_vol" %in% types & "vol_vol" %in% types) {
# molar_vol <-> vol_vol
needed <- FALSE
} else if ("molar_vol" %in% types && "mass_mass" %in% types) {
} else if ("molar_vol" %in% types & "mass_mass" %in% types) {
# molar_vol <-> mass_mass
needed <- TRUE
} else if ("mass_mass" %in% types && "vol_vol" %in% types) {
} else if ("mass_mass" %in% types & "vol_vol" %in% types) {
# mass_mass <-> vol_vol
needed <- TRUE
} else {
Expand All @@ -467,22 +470,22 @@ needs_molar_mass <- function(from, to) {

if (from == to) {
needed <- FALSE
} else if ("mass_vol" %in% types && "molar_vol" %in% types) {
} else if ("mass_vol" %in% types & "molar_vol" %in% types) {
# mass_vol <-> molar_vol
needed <- TRUE
} else if ("mass_vol" %in% types && "vol_vol" %in% types) {
} else if ("mass_vol" %in% types & "vol_vol" %in% types) {
# mass_vol <-> vol_vol
needed <- FALSE
} else if ("mass_vol" %in% types && "mass_mass" %in% types) {
} else if ("mass_vol" %in% types & "mass_mass" %in% types) {
# mass_vol <-> mass_mass
needed <- FALSE
} else if ("molar_vol" %in% types && "vol_vol" %in% types) {
} else if ("molar_vol" %in% types & "vol_vol" %in% types) {
# molar_vol <-> vol_vol
needed <- TRUE
} else if ("molar_vol" %in% types && "mass_mass" %in% types) {
} else if ("molar_vol" %in% types & "mass_mass" %in% types) {
# molar_vol <-> mass_mass
needed <- TRUE
} else if ("mass_mass" %in% types && "vol_vol" %in% types) {
} else if ("mass_mass" %in% types & "vol_vol" %in% types) {
# mass_mass <-> vol_vol
needed <- FALSE
} else {
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test_calc.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ test_that("concentration are converted correctly", {
expect_equal(calc_factor_conc("g / l", "\u00B5g / \u00B5l"), 1)
expect_equal(calc_factor_conc("g / dm^3", "\u00B5g / \u00B5l"), 1)
})

test_that("concentration requirements are stated", {
expect_error(calc_factor_conc("g/l", "M"))
expect_error(calc_factor_conc("M", "g/l"))
expect_error(calc_factor_conc("M", "g/g"))
expect_error(calc_factor_conc("M", "l/l"))
expect_error(calc_factor_conc("g/l", "g/g"))
expect_error(calc_factor_conc("l/l", "g/g"))
expect_error(calc_factor_conc("g/l", "l/l"))
})

0 comments on commit 6e27e8b

Please sign in to comment.