1library(pystr)
2context("pystr_format")
3
4test_that("numeric arguments work with individual args", {
5  original = "Hello {1}, my name is {2}."
6  formatted = pystr_format(original, "World", "Nicole")
7  expected = "Hello World, my name is Nicole."
8  expect_equal(formatted, expected)
9})
10
11test_that("numeric arguments work with a list of args", {
12  original = "Hello {1}, my name is {2}."
13  formatted = pystr_format(original, list("World", "Nicole"))
14  expected = "Hello World, my name is Nicole."
15  expect_equal(formatted, expected)
16})
17
18test_that("named arguments works with individual args", {
19  original = "Hello {thing}, my name is {name}."
20  formatted = pystr_format(original, thing="World", name="Nicole")
21  expected = "Hello World, my name is Nicole."
22  expect_equal(formatted, expected)
23})
24
25test_that("named arguments works with a list of args", {
26  original = "Hello {thing}, my name is {name}."
27  formatted = pystr_format(original, list(thing="World", name="Nicole"))
28  expected = "Hello World, my name is Nicole."
29  expect_equal(formatted, expected)
30})
31
32test_that("numeric arguments work with args showing up in multiple places", {
33  original = "The name is {2}. {1} {2}."
34  formatted = pystr_format(original, "James", "Bond")
35  expected = "The name is Bond. James Bond."
36  expect_equal(formatted, expected)
37})
38
39test_that("numeric list of arguments work with args showing up in multiple places", {
40  original = "The name is {2}. {1} {2}."
41  formatted = pystr_format(original, list("James", "Bond"))
42  expected = "The name is Bond. James Bond."
43  expect_equal(formatted, expected)
44})
45
46test_that("named arguments work with args showing up in multiple places", {
47  original = "The name is {last}. {first} {last}."
48  formatted = pystr_format(original, first="James", last="Bond")
49  expected = "The name is Bond. James Bond."
50  expect_equal(formatted, expected)
51})
52
53test_that("named list of arguments work with args showing up in multiple places", {
54  original = "The name is {last}. {first} {last}."
55  formatted = pystr_format(original, list(first="James", last="Bond"))
56  expected = "The name is Bond. James Bond."
57  expect_equal(formatted, expected)
58})
59
60test_that("character and numeric args work at the same time", {
61  original = "Hello {1}, you have {2} new notifications!"
62  formatted = pystr_format(original, "Nicole", 0)
63  expected = "Hello Nicole, you have 0 new notifications!"
64  expect_equal(formatted, expected)
65})
66
67test_that("the same string is returned if the args are empty", {
68  original = "Hello {1}."
69  formatted = pystr_format(original)
70  expected = "Hello {1}."
71  expect_equal(formatted, expected)
72})
73
74test_that("the same string is returned if the single list arg is empty", {
75  original = "Hello {1}."
76  formatted = pystr_format(original, list())
77  expected = "Hello {1}."
78  expect_equal(formatted, expected)
79})
80
81test_that("it can format a single indexed parameter - individual arg", {
82  original = "Hello {1}."
83  formatted = pystr_format(original, "World")
84  expected = "Hello World."
85  expect_equal(formatted, expected)
86})
87
88test_that("it can format a single indexed parameter - character vector arg", {
89  original = "Hello {1}."
90  formatted = pystr_format(original, c("World"))
91  expected = "Hello World."
92  expect_equal(formatted, expected)
93})
94
95test_that("it can format a single indexed parameter - list arg", {
96  original = "Hello {1}."
97  formatted = pystr_format(original, list("World"))
98  expected = "Hello World."
99  expect_equal(formatted, expected)
100})
101
102test_that("It can format a single named parameter - individual arg", {
103  original = "Hello {thing}."
104  formatted = pystr_format(original, thing="World")
105  expected = "Hello World."
106  expect_equal(formatted, expected)
107})
108
109test_that("It can format a single named parameter - named vector arg", {
110  original = "Hello {thing}."
111  formatted = pystr_format(original, c(thing="World"))
112  expected = "Hello World."
113  expect_equal(formatted, expected)
114})
115
116test_that("It can format a single named parameter - named list arg", {
117  original = "Hello {thing}."
118  formatted = pystr_format(original, list(thing="World"))
119  expected = "Hello World."
120  expect_equal(formatted, expected)
121})
122
123test_that("It works with data.frames", {
124  supers <- data.frame(first=c("Bruce", "Hal", "Clark", "Diana"),
125                       last=c("Wayne", "Jordan", "Kent", "Prince"),
126                       is=c("Batman", "Green Lantern", "Superman", "Wonder Woman"))
127
128  sentences = pystr_format("{first} {last} is really {is} but you shouldn't call them {first} in public.", supers)
129
130  expect_equal(sentences[1], "Bruce Wayne is really Batman but you shouldn't call them Bruce in public.")
131  expect_equal(sentences[2], "Hal Jordan is really Green Lantern but you shouldn't call them Hal in public.")
132  expect_equal(sentences[3], "Clark Kent is really Superman but you shouldn't call them Clark in public." )
133  expect_equal(sentences[4], "Diana Prince is really Wonder Woman but you shouldn't call them Diana in public.")
134})
135
136test_that("it works with a characte vector", {
137  input = c("Hello {world}!", "Hello {world}")
138  output = c("Hello thing!", "Hello thing")
139  expect_equal(pystr_format(input, world="thing"), output)
140})
141