File: test-case.R

package info (click to toggle)
r-cran-stringr 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,112 kB
  • sloc: javascript: 11; sh: 9; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 2,355 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
test_that("to_upper and to_lower have equivalent base versions", {
  x <- "This is a sentence."
  expect_identical(str_to_upper(x), toupper(x))
  expect_identical(str_to_lower(x), tolower(x))
})

test_that("to_title creates one capital letter per word", {
  x <- "This is a sentence."
  expect_equal(str_count(x, "\\W+"), str_count(str_to_title(x), "[[:upper:]]"))
})

test_that("to_sentence capitalizes just the first letter", {
  expect_identical(str_to_sentence("a Test"), "A test")
})

test_that("case conversions preserve names", {
  x <- c(C = "3", B = "2", A = "1")
  expect_equal(names(str_to_lower(x)), names(x))
  expect_equal(names(str_to_upper(x)), names(x))
  expect_equal(names(str_to_title(x)), names(x))
})

# programming cases -----------------------------------------------------------

test_that("to_camel can control case of first argument", {
  expect_equal(str_to_camel("my_variable"), "myVariable")
  expect_equal(str_to_camel("my$variable"), "myVariable")
  expect_equal(str_to_camel(" my    variable  "), "myVariable")
  expect_equal(str_to_camel("my_variable", first_upper = TRUE), "MyVariable")
})

test_that("to_kebab converts to kebab case", {
  expect_equal(str_to_kebab("myVariable"), "my-variable")
  expect_equal(str_to_kebab("MyVariable"), "my-variable")
  expect_equal(str_to_kebab("1MyVariable1"), "1-my-variable-1")
  expect_equal(str_to_kebab("My$Variable"), "my-variable")
  expect_equal(str_to_kebab(" My   Variable  "), "my-variable")
  expect_equal(str_to_kebab("testABCTest"), "test-abc-test")
  expect_equal(str_to_kebab("IlÉtaitUneFois"), "il-était-une-fois")
})

test_that("to_snake converts to snake case", {
  expect_equal(str_to_snake("myVariable"), "my_variable")
  expect_equal(str_to_snake("MyVariable"), "my_variable")
  expect_equal(str_to_snake("1MyVariable1"), "1_my_variable_1")
  expect_equal(str_to_snake("My$Variable"), "my_variable")
  expect_equal(str_to_snake(" My   Variable  "), "my_variable")
  expect_equal(str_to_snake("testABCTest"), "test_abc_test")
  expect_equal(str_to_snake("IlÉtaitUneFois"), "il_était_une_fois")
})

test_that("to_words handles common compound cases", {
  expect_equal(to_words("a_b"), "a b")
  expect_equal(to_words("a-b"), "a b")
  expect_equal(to_words("aB"), "a b")
  expect_equal(to_words("a123b"), "a 123 b")
  expect_equal(to_words("HTML"), "html")
})