Skip to content

Commit

Permalink
100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mailund committed Jan 30, 2017
1 parent 6250307 commit e7e8e01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions R/simplify.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@ simplify_exponentiation <- function(f, g) {
call("^", left, right)
}

# FIXME: I don't handle named arguments here...
.simplify_built_in_functions <- c("sin", "cos", "exp")
simplify_built_in_function <- function(expr, x) {
simplify_built_in_function <- function(expr) {
call_name <- as.character(expr[[1]])
arguments <- Map(simplify_expr, expr[[-1]])
if (all(is.numeric(arguments))) do.call(call_name, arguments)
else do.call("call", call_name, arguments)
arguments <- vector("list", length(expr) - 1)
for (i in seq_along(arguments)) {
arguments[i] <- list(simplify_expr(expr[[i + 1]]))
}
if (all(unlist(Map(is.numeric, arguments)))) do.call(call_name, arguments)
else do.call("call", c(list(call_name), arguments), quote = TRUE)
}

simplify_call <- function(expr) {
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-simpification.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,14 @@ test_that("we can do some simplifications when there are variables involved", {

expect_equal(simplify_expr(quote(x ^ y)), quote(x ^ y))
})

test_that("we can handle some simple functions", {
expect_equal(simplify_expr(quote(sin(x))), quote(sin(x)))
expect_equal(simplify_expr(quote(sin(1*x))), quote(sin(x)))

expect_equal(simplify_expr(quote(sin(0*x))), 0)
})

test_that("we handle errors correctly", {
expect_error(simplify_expr(quote(x %in% y)))
})

0 comments on commit e7e8e01

Please sign in to comment.