1library(pystr) 2context("pystr_count") 3 4test_that("it doesn't count overlapping occurrences", { 5 count = pystr_count("ababab", "aba") 6 expect_equal(count, 1) 7}) 8 9test_that("it counts non-overlapping occurrences", { 10 count = pystr_count("babbab", "bab") 11 expect_equal(count, 2) 12}) 13 14test_that("it only counts things within the specified range", { 15 count = pystr_count("babbab", "bab", 1, 3) 16 expect_equal(count, 1) 17}) 18 19test_that("it returns 0 when asking for a range outside the string's range", { 20 count = pystr_count("ababab", "ab", 10, 15) 21 expect_equal(count, 0) 22}) 23 24test_that("it handles NAs", { 25 count = pystr_count(NA, "bab", 1, 3) 26 expect_true(is.na(count)) 27}) 28test_that("it counts substrings not adjacent", { 29 expect_equal(pystr_count("a--b--c", "--"), 2) 30}) 31 32test_that("it counts substrings at the beginning", { 33 expect_equal(pystr_count("--a--b", "--"), 2) 34}) 35 36test_that("it counts substrings at the end", { 37 expect_equal(pystr_count("a--b--", "--"), 2) 38}) 39 40test_that("it counts empty strings", { 41 expect_equal(pystr_count("hi", ""), 3) 42}) 43 44test_that("it works with a vector of strings", { 45 expect_equal(pystr_count(c("one", "two", "three"), "e"), c(1, 0, 2)) 46}) 47