|
13 | 13 | ##
|
14 | 14 | ## Copyright 2017 by Felix Prasse <[email protected]>
|
15 | 15 | ## Copyright 2017-2018 by Claus Hunsen <[email protected]>
|
16 |
| -## Copyright 2017 by Thomas Bock <[email protected]> |
| 16 | +## Copyright 2017-2018 by Thomas Bock <[email protected]> |
17 | 17 | ## All Rights Reserved.
|
18 | 18 |
|
19 | 19 |
|
| 20 | +## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / |
| 21 | +## Parameter verification -------------------------------------------------- |
| 22 | + |
| 23 | +## |
| 24 | +## Match argument or take default. |
| 25 | +## |
| 26 | + |
| 27 | +test_that("Match argument or take default.", { |
| 28 | + |
| 29 | + ## 1) tests for single choice without default |
| 30 | + test.function = function(param = c("choice1", "choice2", "choice3")) { |
| 31 | + param = match.arg.or.default(param) |
| 32 | + return(param) |
| 33 | + } |
| 34 | + |
| 35 | + actual.result = test.function() |
| 36 | + expected.result = "choice1" |
| 37 | + expect_equal(actual.result, expected.result, info = "Single choice without default, no choice") |
| 38 | + |
| 39 | + actual.result = test.function("choice3") |
| 40 | + expected.result = "choice3" |
| 41 | + expect_equal(actual.result, expected.result, info = "Single choice without default, one choice") |
| 42 | + |
| 43 | + expect_error(test.function(c("choice3", "choice1")), info = "Single choice with default, two choices") |
| 44 | + |
| 45 | + ## 2) tests for single choice with default |
| 46 | + test.function = function(param = c("choice1", "choice2", "choice3")) { |
| 47 | + param = match.arg.or.default(param, default = "choice2") |
| 48 | + return(param) |
| 49 | + } |
| 50 | + |
| 51 | + actual.result = test.function() |
| 52 | + expected.result = "choice2" |
| 53 | + expect_equal(actual.result, expected.result, info = "Single choice with default, no choice") |
| 54 | + |
| 55 | + actual.result = test.function("choice3") |
| 56 | + expected.result = "choice3" |
| 57 | + expect_equal(actual.result, expected.result, info = "Single choice with default, one choice") |
| 58 | + |
| 59 | + actual.result = test.function(c("choice3", "choice1")) |
| 60 | + expected.result = "choice2" |
| 61 | + expect_equal(actual.result, expected.result, info = "Single choice without default, two choices") |
| 62 | + |
| 63 | + ## 3) tests for single choice with illegal default |
| 64 | + test.function = function(param = c("choice1", "choice2", "choice3")) { |
| 65 | + param = match.arg.or.default(param, default = "c2") |
| 66 | + return(param) |
| 67 | + } |
| 68 | + |
| 69 | + expect_error(test.function(), info = "Single choice with illegal default, no choice") |
| 70 | + expect_error(test.function(c("choice3", "choice1")), info = "Single choice with illegal default, one choice") |
| 71 | + expect_error(test.function(c("choice3", "choice1")), info = "Single choice with illegal default, two choices") |
| 72 | + |
| 73 | + ## 4) tests for multiple choices |
| 74 | + test.function = function(param = c("choice1", "choice2", "choice3")) { |
| 75 | + param = match.arg.or.default(param, several.ok = TRUE) |
| 76 | + return(param) |
| 77 | + } |
| 78 | + |
| 79 | + actual.result = test.function() |
| 80 | + expected.result = c("choice1", "choice2", "choice3") |
| 81 | + expect_equal(actual.result, expected.result, info = "Multiple choices, no choice") |
| 82 | + |
| 83 | + actual.result = test.function("choice3") |
| 84 | + expected.result = "choice3" |
| 85 | + expect_equal(actual.result, expected.result, info = "Multiple choices, one choice") |
| 86 | + |
| 87 | + actual.result = test.function(c("choice3", "choice1")) |
| 88 | + expected.result = c("choice3", "choice1") |
| 89 | + expect_equal(actual.result, expected.result, info = "Multiple choices, two choices") |
| 90 | + |
| 91 | + ## 5) tests for multiple choices with ignored default |
| 92 | + test.function = function(param = c("choice1", "choice2", "choice3")) { |
| 93 | + param = match.arg.or.default(param, default = "choice2", several.ok = TRUE) |
| 94 | + return(param) |
| 95 | + } |
| 96 | + |
| 97 | + actual.result = test.function() |
| 98 | + expected.result = c("choice1", "choice2", "choice3") |
| 99 | + expect_equal(actual.result, expected.result, info = "Multiple choices with ignored default, no choice") |
| 100 | + |
| 101 | + actual.result = test.function("choice3") |
| 102 | + expected.result = "choice3" |
| 103 | + expect_equal(actual.result, expected.result, info = "Multiple choices with ignored default, one choice") |
| 104 | + |
| 105 | + actual.result = test.function(c("choice3", "choice1")) |
| 106 | + expected.result = c("choice3", "choice1") |
| 107 | + expect_equal(actual.result, expected.result, info = "Multiple choices with ignored default, two choices") |
| 108 | +}) |
| 109 | + |
20 | 110 | ## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
|
21 | 111 | ## Date handling -----------------------------------------------------------
|
22 | 112 |
|
@@ -238,6 +328,53 @@ test_that("Construct cumulative ranges.", {
|
238 | 328 | ## TODO use expect_identical here? why failing?
|
239 | 329 | })
|
240 | 330 |
|
| 331 | +## |
| 332 | +## Construct ranges when difference between start and end is smaller than time period. |
| 333 | +## |
| 334 | + |
| 335 | +test_that("Construct ranges when difference between start and end is smaller than time period.", { |
| 336 | + |
| 337 | + start = ("2015-05-01 01:01:01") |
| 338 | + start.date = get.date.from.string(start) |
| 339 | + end = ("2015-05-02 02:02:02") |
| 340 | + end.date = get.date.from.string(end) |
| 341 | + end.including = end.date + 1 |
| 342 | + |
| 343 | + ## expected results (equal for consecutive, cumulative, and overlapping range construction) |
| 344 | + expected.formatted = c("2015-05-01 01:01:01-2015-05-02 02:02:03") |
| 345 | + expected.raw = lapply(expected.formatted, get.range.bounds) |
| 346 | + names(expected.raw) = expected.formatted |
| 347 | + |
| 348 | + ## consecutive |
| 349 | + ## 1) formatted |
| 350 | + result.formatted = construct.consecutive.ranges(start, end, time.period = "1 week", raw = FALSE) |
| 351 | + expect_identical(result.formatted, expected.formatted, info = "Consecutive range (formatted).") |
| 352 | + ## 2) raw |
| 353 | + result.raw = construct.consecutive.ranges(start, end, time.period = "1 week", raw = TRUE) |
| 354 | + expect_equal(result.raw, expected.raw, info = "Consecutive range (raw).") |
| 355 | + ## TODO use expect_identical here? why failing? |
| 356 | + |
| 357 | + ## cumulative |
| 358 | + ## 1) formatted |
| 359 | + result.formatted = construct.cumulative.ranges(start, end, time.period = "1 week", raw = FALSE) |
| 360 | + expect_identical(result.formatted, expected.formatted, info = "Cumulative range (formatted).") |
| 361 | + ## 2) raw |
| 362 | + result.raw = construct.cumulative.ranges(start, end, time.period = "1 week", raw = TRUE) |
| 363 | + expect_equal(result.raw, expected.raw, info = "Cumulative range (raw).") |
| 364 | + ## TODO use expect_identical here? why failing? |
| 365 | + |
| 366 | + ## overlapping |
| 367 | + ## 1) formatted |
| 368 | + result.formatted = construct.overlapping.ranges(start, end, time.period = "1 week", |
| 369 | + overlap = "1 day", raw = FALSE) |
| 370 | + expect_identical(result.formatted, expected.formatted, info = "Overlapping range (formatted).") |
| 371 | + ## 2) raw |
| 372 | + result.raw = construct.overlapping.ranges(start, end, time.period = "1 week", |
| 373 | + overlap = "5 days", raw = TRUE) |
| 374 | + expect_equal(result.raw, expected.raw, info = "Overlapping range (raw).") |
| 375 | + ## TODO use expect_identical here? why failing? |
| 376 | +}) |
| 377 | + |
241 | 378 | ##
|
242 | 379 | ## Aggregate ranges.
|
243 | 380 | ##
|
|
0 commit comments