Skip to content

Commit

Permalink
adding more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Jun 5, 2024
1 parent 18283e4 commit 7a9f42d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion R/standalone-stringr.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ str_replace_all <- function (string, pattern, replacement, fixed = FALSE){
gsub(x = string, pattern = pattern, replacement = replacement, fixed = fixed)
}

word <- function(string, start, end = start, sep = " ", fixed = FALSE) {
word <- function(string, start, end = start, sep = " ", fixed = TRUE) {
# Handle vectorized string input
if (length(string) > 1) {
return(sapply(string, word, start, end, sep, fixed, USE.NAMES = FALSE))
Expand Down
35 changes: 31 additions & 4 deletions tests/testthat/test-standalone-stringr.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
test_that("str_trim() works", {
s <- str_trim(" trailing and leading whitespace\t")
expect_identical(s, "trailing and leading whitespace")
expect_identical(s, stringr::str_trim(" trailing and leading whitespace\t"))

s <- str_trim("\n\ntrailing and leading whitespace\n\n", "left")
expect_identical(s, "trailing and leading whitespace\n\n")
expect_identical(s, stringr::str_trim("\n\ntrailing and leading whitespace\n\n", "left"))

s <- str_trim("\n\ntrailing and leading whitespace\n\n", "right")
expect_identical(s, "\n\ntrailing and leading whitespace")

s <- str_trim("\n\ntrailing and leading whitespace\n\n", "right")
expect_identical(s, stringr::str_trim("\n\ntrailing and leading whitespace\n\n", "right"))

s <- str_trim(" ")
expect_identical(s, "")
expect_identical(s, stringr::str_trim(" "))
})

test_that("str_squish() works", {
s <- str_squish(" String with trailing, middle, and leading white space\t")
expect_identical(s, "String with trailing, middle, and leading white space")
expect_identical(s, stringr::str_squish(" String with trailing, middle, and leading white space\t"))

s <- str_squish(c("one 1 ", "two\n\n2", "three\r3\t"))
expect_identical(s, c("one 1", "two 2", "three 3"))
expect_identical(s, stringr::str_squish(c("one 1 ", "two\n\n2", "three\r3\t")))

s_notfixed <- str_squish("\n\nString with excess trailing and leading white space\n\n")
expect_identical(s_notfixed, "String with excess trailing and leading white space")
expect_identical(s_notfixed, stringr::str_squish("\n\nString with excess trailing and leading white space\n\n"))

s_fixed <- str_squish("\n\nString with excess trailing and leading white space\n\n", fixed = TRUE)
expect_identical(s_fixed, "String with excess trailing and leading white space")
expect_identical(s_fixed, stringr::str_squish(stringr::fixed("\n\nString with excess trailing and leading white space\n\n")))

s <- str_squish("\n\nString with excess trailing and leading white space\n\n")
expect_identical(s, stringr::str_squish("\n\nString with excess trailing and leading white space\n\n"))
Expand All @@ -35,15 +40,19 @@ test_that("str_squish() works", {
test_that("str_remove_all() works", {
s <- str_remove_all(c("one 1", "two 2", "three 3"), "[aeiou]")
expect_identical(s, c("n 1", "tw 2", "thr 3"))
expect_identical(s, stringr::str_remove_all(c("one 1", "two 2", "three 3"), "[aeiou]"))

s <- str_remove_all(c("one 1 ", "two\n\n2", "three\r3\t"), "[ \t\r\n]")
expect_identical(s, c("one1", "two2", "three3"))
expect_identical(s, stringr::str_remove_all(c("one 1 ", "two\n\n2", "three\r3\t"), "[ \t\r\n]"))

s_notfixed <- str_remove_all(c("one.1", "two..2", "three...3"), ".")
expect_identical(s_notfixed, c("", "", ""))
expect_identical(s_notfixed, stringr::str_remove_all(c("one.1", "two..2", "three...3"), "."))

s_fixed <- str_remove_all(c("one.1", "two..2", "three...3"), ".", fixed = TRUE)
expect_identical(s_fixed, c("one1", "two2", "three3"))
expect_identical(s_fixed, stringr::str_remove_all(c("one.1", "two..2", "three...3"), stringr::fixed(".")))

s <- str_remove_all(c("one.1", "two..2", "three...3"), ".")
expect_identical(s, stringr::str_remove_all(c("one.1", "two..2", "three...3"), "."))
Expand All @@ -54,15 +63,19 @@ test_that("str_extract() works", {

s <- str_extract(shopping_list, "[a-z]+")
expect_identical(s, c("apples", "bag", "bag", "milk"))
expect_identical(s, stringr::str_extract(shopping_list, "[a-z]+"))

s <- str_extract(shopping_list, "([a-z]+) of ([a-z]+)")
expect_identical(s, c(NA, "bag of sugar", "bag of flour", NA))
expect_identical(s, stringr::str_extract(shopping_list, "([a-z]+) of ([a-z]+)"))

s_notfixed <- str_extract(shopping_list, "\\d")
expect_identical(s_notfixed, c("4", NA, NA, "2"))
expect_identical(s_notfixed, stringr::str_extract(shopping_list, "\\d"))

s_fixed <- str_extract(shopping_list, "\\d", fixed = TRUE)
expect_identical(s_fixed, rep(NA_character_, 4))
expect_identical(s_fixed, stringr::str_extract(shopping_list, stringr::fixed("\\d")))

s <- str_extract(shopping_list, "[a-z]+")
expect_identical(s, stringr::str_extract(shopping_list, "[a-z]+"))
Expand All @@ -73,15 +86,19 @@ test_that("str_detect() works", {

s <- str_detect(fruits, "apple")
expect_identical(s, c(TRUE, FALSE, FALSE, TRUE))
expect_identical(s, stringr::str_detect(fruits, "apple"))

s <- str_detect(fruits, "p")
expect_identical(s, c(TRUE, FALSE, TRUE, TRUE))
expect_identical(s, stringr::str_detect(fruits, "p"))

s_notfixed <- str_detect(fruits, "^a")
expect_identical(s_notfixed, c(TRUE, rep(FALSE, 3)))
expect_identical(s_notfixed, stringr::str_detect(fruits, "^a"))

s_fixed <- str_detect(fruits, "^a", fixed = TRUE)
expect_identical(s_fixed, rep(FALSE, 4))
expect_identical(s_fixed, stringr::str_detect(fruits, stringr::fixed("^a")))

s <- str_detect(fruits, "p")
expect_identical(s, stringr::str_detect(fruits, "p"))
Expand All @@ -90,15 +107,19 @@ test_that("str_detect() works", {
test_that("str_remove() works", {
s <- str_remove(c("one 1", "two 2", "three 3"), "[aeiou]")
expect_identical(s, c("ne 1", "tw 2", "thre 3"))
expect_identical(s, stringr::str_remove(c("one 1", "two 2", "three 3"), "[aeiou]"))

s <- str_remove(c("one 1 ", "two\n\n2", "three\r3\t"), "[ \t\r\n]")
expect_identical(s, c("one1 ", "two\n2", "three3\t"))
expect_identical(s, stringr::str_remove(c("one 1 ", "two\n\n2", "three\r3\t"), "[ \t\r\n]"))

s_notfixed <- str_remove(c("one.1", "two..2", "three...3"), ".")
expect_identical(s_notfixed, c("ne.1", "wo..2", "hree...3"))
expect_identical(s_notfixed, stringr::str_remove(c("one.1", "two..2", "three...3"), "."))

s_fixed <- str_remove(c("one.1", "two..2", "three...3"), ".", fixed = TRUE)
expect_identical(s_fixed, c("one1", "two.2", "three..3"))
expect_identical(s_fixed, stringr::str_remove(c("one.1", "two..2", "three...3"), stringr::fixed(".")))

s <- str_remove(c("one.1", "two..2", "three...3"), ".")
expect_identical(s, stringr::str_remove(c("one.1", "two..2", "three...3"), "."))
Expand All @@ -109,9 +130,11 @@ test_that("str_replace() works", {

s <- str_replace(fruits, "[aeiou]", "-")
expect_identical(s, c("-ne apple", "tw- pears", "thr-e bananas"))
expect_identical(s, stringr::str_replace(fruits, "[aeiou]", "-"))

s <- str_replace(fruits, "([aeiou])", "\\1\\1")
expect_identical(s, c("oone apple", "twoo pears", "threee bananas"))
expect_identical(s, stringr::str_replace(fruits, "([aeiou])", "\\1\\1"))

s <- str_replace(fruits, "([aeiou])", "\\1\\1")
expect_identical(s, stringr::str_replace(fruits, "([aeiou])", "\\1\\1"))
Expand All @@ -122,37 +145,41 @@ test_that("str_replace_all() works", {

s <- str_replace_all(fruits, "[aeiou]", "-")
expect_identical(s, c("-n- -ppl-", "tw- p--rs", "thr-- b-n-n-s"))
expect_identical(s, stringr::str_replace_all(fruits, "[aeiou]", "-"))

s <- str_replace_all(fruits, "([aeiou])", "\\1\\1")
expect_identical(s, c("oonee aapplee", "twoo peeaars", "threeee baanaanaas"))
expect_identical(s, stringr::str_replace_all(fruits, "([aeiou])", "\\1\\1"))

s <- str_replace_all(fruits, "[aeiou]", "-")
expect_identical(s, stringr::str_replace_all(fruits, "[aeiou]", "-"))

})

test_that("str_sub() works", {
hw <- "Hadley Wickham"

s <- str_sub(hw, 1, 6)
expect_identical(s, "Hadley")
expect_identical(s, stringr::str_sub(hw, 1, 6))

s <- str_sub(hw, -1)
expect_identical(s, "m")
expect_identical(s, stringr::str_sub(hw, -1))

s <- str_sub(hw, -1)
expect_identical(s, stringr::str_sub(hw, -1))

})

test_that("str_sub_all() works", {
fruits <- c("one apple", "two pears", "three bananas")

s <- str_replace_all(fruits, "[aeiou]", "-")
expect_identical(s, c("-n- -ppl-", "tw- p--rs", "thr-- b-n-n-s"))
expect_identical(s, stringr::str_replace_all(fruits, "[aeiou]", "-"))

s <- str_replace_all(fruits, "([aeiou])", "\\1\\1")
expect_identical(s, c("oonee aapplee", "twoo peeaars", "threeee baanaanaas"))
expect_identical(s, stringr::str_replace_all(fruits, "([aeiou])", "\\1\\1"))

s <- str_replace_all(fruits, "([aeiou])", "\\1\\1")
expect_identical(s, stringr::str_replace_all(fruits, "([aeiou])", "\\1\\1"))
Expand Down

0 comments on commit 7a9f42d

Please sign in to comment.