Skip to content

Commit

Permalink
Rename .makeOptions as .preprocessArgs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisGuillem committed Jul 4, 2017
1 parent 02c8f7b commit af6ea69
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Suggests: knitr,
rmarkdown,
dplyr,
shiny,
manipulateWidget
manipulateWidget,
testthat
VignetteBuilder: knitr
4 changes: 2 additions & 2 deletions R/add_flows.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ addFlows <- function(map, lng0, lat0, lng1, lat1, color = "blue", flow = 1,
if (is.null(time)) time <- 1
if (is.null(layerId)) layerId <- sprintf("_flow (%s,%s) -> (%s,%s)", lng0, lat0, lng1, lat1)

options <- .makeOptions(
options <- .preprocessArgs(
required = list(lng0 = lng0, lat0 = lat0, lng1 = lng1, lat1 = lat1, layerId = layerId, time = time),
optional = list(dir = dir, color = color, value = flow, maxValue = maxFlow,
minThickness = minThickness, maxThickness = maxThickness,
Expand Down Expand Up @@ -84,7 +84,7 @@ updateFlows <- function(map, layerId, color = NULL, flow = NULL, opacity = NULL,
minThickness = 1, maxThickness = 20) {
if (is.null(time)) time <- 1

options <- .makeOptions(
options <- .preprocessArgs(
required = list(layerId = layerId, time = time),
optional = list(dir = dir, color = color, value = flow, maxValue = maxFlow,
minThickness = minThickness, maxThickness = maxThickness,
Expand Down
4 changes: 2 additions & 2 deletions R/add_minicharts.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ addMinicharts <- function(map, lng, lat, chartdata = 1, time = NULL, maxValues =
labels <- "none"
}

options <- .makeOptions(
options <- .preprocessArgs(
required = list(lng = lng, lat = lat, layerId = layerId, time = time),
optional = list(type = type, width = width, height = height,
opacity = opacity, labels = labels,
Expand Down Expand Up @@ -208,7 +208,7 @@ updateMinicharts <- function(map, layerId, chartdata = NULL, time = NULL, maxVal
}
}

options <- .makeOptions(
options <- .preprocessArgs(
required = list(layerId = layerId, time = time),
optional = list(type = type, width = width, height = height,
opacity = opacity, labels = labels,
Expand Down
11 changes: 8 additions & 3 deletions R/make_options.R → R/preprocess_args.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# Copyright © 2016 RTE Réseau de transport d’électricité

#' Private function that prepare R arguments to be sent to javascript functions.
#' Private function that prepare arguments for other functions. It transforms
#' them in a table with one column per argument. In order to improve memory
#' management, optional arguments with a single value are not added to the table
#' but there value is stored in a specific list.
#'
#' @param required
#' Named list of required parameters
#' @param optional
#' Named list of optional parameters
#'
#' @return
#' A data.frame where each column represent one parameter
#' A list with two elements:
#' - options: data.frame with required args and variing optional args
#' - staticOptions: a list with single value args.
#'
#' @noRd
#'
.makeOptions <- function(required, optional) {
.preprocessArgs <- function(required, optional) {
options <- do.call(data.frame, required)
staticOptions <- list()
for (o in names(optional)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(leaflet.minicharts)

test_check("leaflet.minicharts")
40 changes: 40 additions & 0 deletions tests/testthat/test-preprocess_args.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
context(".preprocessArgs")

describe(".preprocessArgs", {
it ("creates a data.frame with one column per argument", {
opts <- .preprocessArgs(list(a = 1:3, b = 4:6), list(c = 7:9, d = 10:12))
expect_is(opts$options, "data.frame")
expect_equal(dim(opts$options), c(3, 4))
expect_equal(names(opts$options), c("a", "b", "c", "d"))
})

it ("separates static optional arguments", {
opts <- .preprocessArgs(list(a = 1:3, b = 4:6), list(c = 7:9, d = 10))
expect_is(opts$options, "data.frame")
expect_equal(dim(opts$options), c(3, 3))
expect_equal(names(opts$options), c("a", "b", "c"))
expect_is(opts$staticOptions, "list")
expect_equal(names(opts$staticOptions), "d")
})

it ("does not separate static required arguments", {
opts <- .preprocessArgs(list(a = 1:3, b = 4), list(c = 7:9, d = 10:12))
expect_is(opts$options, "data.frame")
expect_equal(dim(opts$options), c(3, 4))
expect_equal(names(opts$options), c("a", "b", "c", "d"))
expect_equal(length(opts$staticOptions), 0)
})

it ("does not accept NULL required arguments", {
expect_error(.preprocessArgs(list(a = 1:3, b = NULL), list(c = 7:9, d = 10:12)))
})

it ("accepts NULL optional required", {
opts <- .preprocessArgs(list(a = 1:3, b = 4:6), list(c = 7:9, d = NULL))
expect_is(opts$options, "data.frame")
expect_equal(dim(opts$options), c(3, 3))
expect_equal(names(opts$options), c("a", "b", "c"))
expect_equal(length(opts$staticOptions), 0)
})

})

0 comments on commit af6ea69

Please sign in to comment.