एक तरीका है, लेकिन यह उतना आसान नहीं है जितना आप चाहें। आपको अपने @examples
ब्लॉक के अंदर testthat
फ़ंक्शन कॉल करना होगा। यहाँ एक उदाहरण समारोह है:
#' @examples
#' testStrings <- c("1234567890",
#' "123 456 7890")
#'
#' testthat::expect_equal(extractPhoneNumbers(testStrings), "0123")
extractPhoneNumbers <- function(inputStr) {
# check input:
if (!is.character(inputStr)) {
stop("'inputStr' must be a (vector of) string(s)!")
}
# imports
`%>%` <- stringr::`%>%`
replace_all <- stringr::str_replace_all
extract_all <- stringr::str_extract_all
# intermediary regex's
visualDelimitersRegex <- "[()+\\-_. ]"
phoneNumberRegex <- "[:digit:]{10}"
inputStr %>%
replace_all(pattern = visualDelimitersRegex, replacement = "") %>%
extract_all(pattern = phoneNumberRegex)
}
जब आप चलाने devtools::run_examples()
या devtools::check
, दोनों त्रुटियों के बाद से testthat::expect_equal()
करने के लिए कॉल एक त्रुटि फेंकता फेंक देते हैं। devtools::check
से
उदाहरण आउटपुट लग रहा है
*** SNIP ***
* checking for unstated dependencies in examples ... OK
* checking examples ... ERROR
Running examples in ‘demoPkg-Ex.R’ failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: extractPhoneNumbers
> ### Title: Extract Phone Numbers
> ### Aliases: extractPhoneNumbers
>
> ### ** Examples
>
> testStrings <- c("1234567890",
+ "123 456 7890")
>
> testthat::expect_equal(extractPhoneNumbers(testStrings), "0123")
Error: extractPhoneNumbers(testStrings) not equal to "0123"
Modes: list, character
Length mismatch: comparison on first 1 components
Component 1: 1 string mismatch
Execution halted
* checking for unstated dependencies in ‘tests’ ... OK
* checking tests ...
Running ‘testthat.R’
OK
* checking PDF version of manual ... OK
* DONE
Status: 1 ERROR
तरह प्रलेखन पढ़ना, यह लगता है कि यह पुष्टि करेगा कि उदाहरण त्रुटियों के बिना भाग गया है, लेकिन यह नहीं लगता है जैसे कि यह सटीकता परीक्षण प्रदर्शन करेंगे। मुझे लगता है कि मैं अपने उदाहरण चला सकता हूं जहां उन्हें सटीकता की आवश्यकता होती है, लेकिन यह सीधे परीक्षण ढांचे में नहीं डालती है। यदि अन्य उत्तर आने वाले अन्य उत्तर नहीं हैं, तो मैं इसे उत्तर के रूप में सेट करूंगा। लेकिन, मैं भी उदाहरणों की सटीकता की पुष्टि करने के लिए एक तरीका की उम्मीद कर रहा हूं। –
अधिक सीधे, 'testthat :: test_examples' परीक्षण के दौरान पैकेज में उदाहरण चलाएगा। उदाहरण चलते समय कोई त्रुटि नहीं होने पर परीक्षण पास होते हैं। सुझाव के लिए –