Skip to content

Commit

Permalink
:sparkles :enable validation directly in question see #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Cazelles committed Nov 10, 2020
1 parent 71ef2b4 commit caa1f34
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
8 changes: 7 additions & 1 deletion R/generate.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ generate_form <- function(prompt = "", question = NULL, choices = NULL,
out <- function(x = NULL) {

if (!is.null(x)) {
return(validate(x))
if (is.function(pre)) x <- pre(x)
if (is.function(validate)) {
if (validate(x)) {
if (is.function(post)) x <- post(x)
return(x)
} else return(NA)
} else return(x)
}

if (!is.null(question)) {
Expand Down
4 changes: 3 additions & 1 deletion R/join.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ join_pair <- function(x, y) {
stopifnot(class(x) %in% c("form_partial", "form"))
stopifnot(class(y) %in% c("form_partial", "form"))

structure(function() {
structure(function(...) {
# so far arguments are not used
arg <- list(...)
r1 <- x()
r2 <- y()
if (class(x) == "form" | class(y) == "form") {
Expand Down
18 changes: 17 additions & 1 deletion tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,20 @@ q2 <- fob_yorn("Be or not to be", "shake")
q3 <- fob_date("When was that?", "date")
q4 <- fob_pattern("2 letters + 1 digit", "^[A-Za-z]{2}[0-9]$")
myform <- q1 %+% q2 %+% q3 %+% q4
# res <- myform()
# res <- myform()

test_that("unit questions work", {
expect_equal(q1(1), "Apple")
expect_true(is.na(q1(3)))

expect_true(q2("yes"))
expect_true(!q2("N"))
expect_true(is.na(q2("k")))

expect_equal(q3("2012-01-11"), "2012-01-11")
expect_true(is.na(q3("2012-01")))

expect_equal(q4("ll1"), "ll1")
expect_true(is.na(q4("lll")))
})

0 comments on commit caa1f34

Please sign in to comment.