File: test-locate.r

package info (click to toggle)
r-cran-stringr 0.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 308 kB
  • sloc: makefile: 3
file content (37 lines) | stat: -rw-r--r-- 1,173 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
context("Locations")

test_that("basic location matching works", {
  expect_that(str_locate("abc", "a")[1, ], equals(c(1, 1),
    check.attributes = F))
  expect_that(str_locate("abc", "b")[1, ], equals(c(2, 2),
    check.attributes = F))
  expect_that(str_locate("abc", "c")[1, ], equals(c(3, 3),
    check.attributes = F))

  expect_that(str_locate("abc", ".+")[1, ], equals(c(1, 3),
    check.attributes = F))
})

test_that("locations are integers", {
  strings <- c("a b c", "d e f")
  expect_that(is.integer(str_locate(strings, "[a-z]")), is_true())

  res <- str_locate_all(strings, "[a-z]")[[1]]
  expect_that(is.integer(res), is_true())
  expect_that(is.integer(invert_match(res)), is_true())
})

test_that("both string and patterns are vectorised", {
  strings <- c("abc", "def")

  locs <- str_locate(strings, "a")
  expect_that(locs[, "start"], equals(c(1, NA)))

  locs <- str_locate(strings, c("a", "d"))
  expect_that(locs[, "start"], equals(c(1, 1)))
  expect_that(locs[, "end"],   equals(c(1, 1)))

  locs <- str_locate_all(c("abab"), c("a", "b"))
  expect_that(locs[[1]][, "start"], equals(c(1, 3)))
  expect_that(locs[[2]][, "start"], equals(c(2, 4)))
})