diff --git a/R/aFIPC.R b/R/aFIPC.R index 62546519..918e19b1 100644 --- a/R/aFIPC.R +++ b/R/aFIPC.R @@ -141,7 +141,7 @@ autoFIPC <- } for (attempt in seq_len(3)) { n <- readline(prompt = "Is it correct? (1: Yes 2: No) : ") - if (grepl("^[0-9]+$", n)) { + if (grepl("^[12]$", n)) { return(as.integer(n)) } } @@ -171,7 +171,7 @@ autoFIPC <- readline( prompt = "Do you want to use default BILOG-MG priors for oldform Data? (1: Yes 2: No) : " ) - if (grepl("^[0-9]+$", n)) { + if (grepl("^[12]$", n)) { return(as.integer(n)) } } @@ -390,7 +390,7 @@ autoFIPC <- readline( prompt = "Do you want to use default BILOG-MG priors for newform Data? (1: Yes 2: No) : " ) - if (grepl("^[0-9]+$", n)) { + if (grepl("^[12]$", n)) { return(as.integer(n)) } } diff --git a/tests/testthat/test-readline-choice-validation.R b/tests/testthat/test-readline-choice-validation.R new file mode 100644 index 00000000..9463316e --- /dev/null +++ b/tests/testthat/test-readline-choice-validation.R @@ -0,0 +1,12 @@ +test_that("interactive yes/no prompts accept only their declared choices", { + source_text <- paste(deparse(body(aFIPC::autoFIPC)), collapse = "\n") + + bounded_choice_pattern <- 'grepl("^[12]$", n)' + legacy_unbounded_pattern <- 'grepl("^[0-9]+$", n)' + + expect_equal( + lengths(regmatches(source_text, gregexpr(bounded_choice_pattern, source_text, fixed = TRUE))), + 3L + ) + expect_false(grepl(legacy_unbounded_pattern, source_text, fixed = TRUE)) +})