Skip to content

The context_name() function also removes internal [.][rR]; should only be terminal pattern #1041

@stufield

Description

@stufield

Currently when creating the name for testing contexts, context_name() removes all instances of "\\.[Rr]" in the name, not only the terminal one. If following convention to match the test-*.R filename with the function name it tests may sometimes require a .R to exist internally. For example:

test-calculate.RobustMetrics.R

reprex::reprex({
  library(testthat)
  writeLines(
    "test_that('multiplication works', { expect_equal(2 * 2, 4) })",
    "test-calculate.RobustMetrics.R"
  )
  
  # Context below has '.R' removed from file name
  # Resulting in `calculateobustMetrics`
  test_file("test-calculate.RobustMetrics.R")   
})

This of course doesn't usually happen using tidyverse function naming conventions, but some organizations may have differing style guides preventing this adherence.

Suggest a more strict regular expression within context_name(), for example:

context_name <- function (filename) {
    filename <- gsub("^test-", "", filename)      # add regex starts-with '^'; could use `sub()` instead
    filename <- gsub("[.][Rr]$", "", filename)    # add regex ends-with '$'; could use `sub()` instead
    filename
}

As mentioned above, one could also use sub() rather than gsub(), since we're only interested in the starting (^) and terminal ($) pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorreporter 📝

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions