ea091517c6b8abdf2f777e46046d8786623d1edc
[arvados.git] / sdk / R / tests / testthat / test-util.R
1 context("Utility function")
2
3 test_that("listAll always returns all resource items from server", {
4
5     serverResponseLimit <- 3
6     itemsAvailable <- 8
7     items <- list("collection1", "collection2", "collection3", "collection4",
8                   "collection5", "collection6", "collection7", "collection8")
9
10     testFunction <- function(offset, ...)
11     {
12         response <- list()
13         response$items_available <- itemsAvailable
14
15         maxIndex <- offset + serverResponseLimit
16         lastElementIndex <- if(maxIndex < itemsAvailable) maxIndex else itemsAvailable
17
18         response$items <- items[(offset + 1):lastElementIndex]
19         response
20     }
21
22     result <- listAll(testFunction)
23
24     expect_that(length(result), equals(8))
25 }) 
26
27 test_that("trimFromStart trims string correctly if string starts with trimCharacters", {
28
29     sample <- "./something/random"
30     trimCharacters <- "./something/"
31
32     result <- trimFromStart(sample, trimCharacters)
33
34     expect_that(result, equals("random"))
35 }) 
36
37 test_that("trimFromStart returns original string if string doesn't starts with trimCharacters", {
38
39     sample <- "./something/random"
40     trimCharacters <- "./nothing/"
41
42     result <- trimFromStart(sample, trimCharacters)
43
44     expect_that(result, equals("./something/random"))
45 }) 
46
47 test_that("trimFromEnd trims string correctly if string ends with trimCharacters", {
48
49     sample <- "./something/random"
50     trimCharacters <- "/random"
51
52     result <- trimFromEnd(sample, trimCharacters)
53
54     expect_that(result, equals("./something"))
55 }) 
56
57 test_that("trimFromEnd returns original string if string doesn't end with trimCharacters", {
58
59     sample <- "./something/random"
60     trimCharacters <- "specific"
61
62     result <- trimFromStart(sample, trimCharacters)
63
64     expect_that(result, equals("./something/random"))
65 }) 
66
67 test_that("RListToPythonList converts nested R list to char representation of Python list", {
68
69     sample <- list("insert", list("random", list("text")), list("here")) 
70
71     result              <- RListToPythonList(sample)
72     resultWithSeparator <- RListToPythonList(sample, separator = ",+")
73
74     expect_that(result, equals("[\"insert\", [\"random\", \"text\"], \"here\"]"))
75     expect_that(resultWithSeparator,
76                 equals("[\"insert\",+[\"random\",+\"text\"],+\"here\"]"))
77 }) 
78
79 test_that("appendToStartIfNotExist appends characters to beginning of a string", {
80
81     sample <- "New Year"
82     charactersToAppend <- "Happy "
83
84     result <- appendToStartIfNotExist(sample, charactersToAppend)
85
86     expect_that(result, equals("Happy New Year"))
87 }) 
88
89 test_that(paste("appendToStartIfNotExist returns original string if string",
90                 "doesn't start with specified characters"), {
91
92     sample <- "Happy New Year"
93     charactersToAppend <- "Happy"
94
95     result <- appendToStartIfNotExist(sample, charactersToAppend)
96
97     expect_that(result, equals("Happy New Year"))
98 }) 
99
100 test_that(paste("splitToPathAndName splits relative path to file/folder",
101                 "name and rest of the path"), {
102
103     relativePath <- "path/to/my/file.exe"
104
105     result <- splitToPathAndName( relativePath)
106
107     expect_that(result$name, equals("file.exe"))
108     expect_that(result$path, equals("path/to/my"))
109 })