Skip to content

Commit

Permalink
Merge pull request #12 from Smit-tay/RS_foo_finder
Browse files Browse the repository at this point in the history
refined regex for finding functions
  • Loading branch information
RobertASmith authored Jan 26, 2024
2 parents 0594f00 + 5a6c4b3 commit bb11003
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/cheers_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ extract_function_name <- function(string) {
#'
extract_function_name2 <- function(string){

foo_def_start <- stringr::str_locate_all(pattern = "function\\s*\\(",
foo_def_start <- stringr::str_locate_all(pattern = "(\\s|=|-)function\\s*\\(",
string = string)[[1]][1,1]

assign_operand_locations <- stringr::str_locate_all(pattern = c("=|<-"),
Expand Down Expand Up @@ -148,7 +148,7 @@ extract_function_name2 <- function(string){
#'
get_file_cheers_classifications <- function(filename,
cheers_pattern,
function_pattern = "function\\("){
function_pattern = "(\\s|=|-)function\\("){
lines <- NULL
# check files exists
#if(file.exists(filename))
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-cheers_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,34 @@ test_that("Extracting function names works as intended",
example4 <- "extract_function_name = function(string) {
# does the string 'function' exist in the string
"

example5 <- "function_name <-function(a){}"
example6 <- "function_name =function(a){}"
example7 <- "function_name<-function(a){}"
example8 <- "function_name=function(a){}"

# run through all four examples.
expect_equal(extract_function_name(example1), ".function.name.")
expect_equal(extract_function_name(example2), "function_name")
expect_equal(extract_function_name(example3), "__function.name__")
expect_equal(extract_function_name(example4), "extract_function_name")

expect_equal(extract_function_name(example5), "function_name")
expect_equal(extract_function_name(example6), "function_name")
expect_equal(extract_function_name(example7), "function_name")
expect_equal(extract_function_name(example8), "function_name")

# run through all four examples.
expect_equal(extract_function_name2(example1), ".function.name.")
expect_equal(extract_function_name2(example2), "function_name")
expect_equal(extract_function_name2(example3), "__function.name__")
expect_equal(extract_function_name2(example4), "extract_function_name")

expect_equal(extract_function_name2(example5), "function_name")
expect_equal(extract_function_name2(example6), "function_name")
expect_equal(extract_function_name2(example7), "function_name")
expect_equal(extract_function_name2(example8), "function_name")


})

Expand Down

0 comments on commit bb11003

Please sign in to comment.