1library(pystr) 2context("pystr_split") 3 4test_that("it splits on all occurrences by default", { 5 expect_equal(pystr_split("123123", "2")[[1]], c("1", "31", "3")) 6}) 7 8test_that("it splits on only maxsplit occurrences", { 9 expect_equal(pystr_split("123123", "2", 1)[[1]], c("1", "3123")) 10}) 11 12test_that("it splits on single spaces by default", { 13 expect_equal(pystr_split("A B C")[[1]], c("A", "B", "C")) 14}) 15 16test_that("an empty string is a valid separator", { 17 expect_equal(pystr_split("ABC", "")[[1]], c("A", "B", "C")) 18}) 19 20test_that("it can split on an empty string with a maxsplit", { 21 expect_equal(pystr_split("ABCD", "", 2)[[1]], c("A", "B", "CD")) 22}) 23 24test_that("it can split on periods", { 25 expect_equal(pystr_split("www.example.com", ".")[[1]], c("www", "example", "com")) 26}) 27 28test_that("it can split on multi-character seps", { 29 expect_equal(pystr_split("a--b--c", "--")[[1]], c("a", "b", "c")) 30}) 31 32test_that("it can split on multi-character seps with a maxsplit", { 33 expect_equal(pystr_split("a--b--c", "--", 1)[[1]], c("a", "b--c")) 34}) 35 36test_that("if maxsplit is 0 return the original string", { 37 expect_equal(pystr_split("A.B.C", ".", 0)[[1]], "A.B.C") 38}) 39 40test_that("it splits if the sep is at the beginning", { 41 expect_equal(pystr_split("--a--b", "--")[[1]], c("", "a", "b")) 42}) 43 44test_that("it splits if the sep is at the end", { 45 expect_equal(pystr_split("a--b--", "--")[[1]], c("a", "b", "")) 46}) 47 48test_that("it splits correctly if maxsplit = nchar(str) - 1", { 49 expect_equal(pystr_split("A.B.C", ".", 2)[[1]], c("A", "B", "C")) 50}) 51 52test_that("it returns a list of character vectors", { 53 input = c("A.B.C", "B.C", "C") 54 output = list(c("A", "B", "C"), c("B", "C"), "C") 55 expect_equal(pystr_split(input, "."), output) 56}) 57