diff --git a/DESCRIPTION b/DESCRIPTION index f31d3e1a..c90753c5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,7 @@ Description: Automates fixed item parameter linking for test linking under the item response theory paradigm using mirt package estimates. License: GPL-3 | file LICENSE Imports: mirt, methods -Suggests: testthat (>= 3.0.0) +Suggests: testthat (>= 3.0.0), mockery Encoding: UTF-8 Config/testthat/edition: 3 Config/roxygen2/version: 8.0.0 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-sentinel-integer-coercion.R b/tests/testthat/test-sentinel-integer-coercion.R new file mode 100644 index 00000000..adb87e2e --- /dev/null +++ b/tests/testthat/test-sentinel-integer-coercion.R @@ -0,0 +1,73 @@ +test_that("interactive readline valid inputs", { + # For valid inputs, it should correctly parse the '1' and eventually hit the estimation + # We test the prompt reading by providing minimal data that causes mirt to fail early + # but after the prompt logic. + + mock_readline_confirm <- mockery::mock('1') + mockery::stub(aFIPC::autoFIPC, 'interactive', TRUE) + mockery::stub(aFIPC::autoFIPC, 'readline', mock_readline_confirm) + + # A 100x4 matrix works better to avoid degrees of freedom errors in some cases, + # but our goal is just to pass the `checkCorrect()` prompt logic. + expect_error( + aFIPC::autoFIPC( + newformXData = matrix(sample(c(0,1), 1000, replace=T), ncol=10, dimnames=list(NULL, paste0('I',1:10))), + oldformYData = matrix(sample(c(0,1), 1000, replace=T), ncol=10, dimnames=list(NULL, paste0('I',1:10))), + newformCommonItemNames = 'I1', + oldformCommonItemNames = 'I1', + itemtype = '3PL', + newformBILOGprior = TRUE, + oldformBILOGprior = TRUE, + confirmCommonItems = NULL + ) + ) +}) + +test_that("interactive readline invalid inputs", { + # Mock invalid inputs + mock_readline_invalid <- mockery::mock('3', 'a', '9999999999999999999999999999999', cycle = TRUE) + mockery::stub(aFIPC::autoFIPC, 'interactive', TRUE) + mockery::stub(aFIPC::autoFIPC, 'readline', mock_readline_invalid) + + expect_error( + aFIPC::autoFIPC( + newformXData = matrix(sample(c(0,1), 1000, replace=T), ncol=10, dimnames=list(NULL, paste0('I',1:10))), + oldformYData = matrix(sample(c(0,1), 1000, replace=T), ncol=10, dimnames=list(NULL, paste0('I',1:10))), + newformCommonItemNames = 'I1', + oldformCommonItemNames = 'I1', + itemtype = '3PL', + newformBILOGprior = NULL, + oldformBILOGprior = TRUE, + confirmCommonItems = TRUE + ), + "Too many invalid newform BILOG prior attempts" + ) + + expect_error( + aFIPC::autoFIPC( + newformXData = matrix(sample(c(0,1), 1000, replace=T), ncol=10, dimnames=list(NULL, paste0('I',1:10))), + oldformYData = matrix(sample(c(0,1), 1000, replace=T), ncol=10, dimnames=list(NULL, paste0('I',1:10))), + newformCommonItemNames = 'I1', + oldformCommonItemNames = 'I1', + itemtype = '3PL', + newformBILOGprior = TRUE, + oldformBILOGprior = NULL, + confirmCommonItems = TRUE + ), + "Too many invalid oldform BILOG prior attempts" + ) + + expect_error( + aFIPC::autoFIPC( + newformXData = matrix(sample(c(0,1), 1000, replace=T), ncol=10, dimnames=list(NULL, paste0('I',1:10))), + oldformYData = matrix(sample(c(0,1), 1000, replace=T), ncol=10, dimnames=list(NULL, paste0('I',1:10))), + newformCommonItemNames = 'I1', + oldformCommonItemNames = 'I1', + itemtype = '3PL', + newformBILOGprior = TRUE, + oldformBILOGprior = TRUE, + confirmCommonItems = NULL + ), + "Too many invalid common item confirmation attempts" + ) +})