Skip to content

Commit f5300a4

Browse files
committed
Join tests
1 parent aad408e commit f5300a4

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/gleam/string.gleam

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,19 +435,19 @@ fn repeat_loop(string: String, times: Int, acc: String) -> String {
435435
pub fn join(strings: List(String), with separator: String) -> String {
436436
case strings {
437437
[] -> ""
438-
[first, ..rest] -> do_join(rest, separator, first)
438+
[first, ..rest] -> join_loop(rest, separator, first)
439439
}
440440
}
441441

442-
fn do_join(
442+
fn join_loop(
443443
strings: List(String),
444444
separator: String,
445445
accumulator: String,
446446
) -> String {
447447
case strings {
448448
[] -> accumulator
449449
[string, ..strings] ->
450-
do_join(strings, separator, accumulator <> separator <> string)
450+
join_loop(strings, separator, accumulator <> separator <> string)
451451
}
452452
}
453453

test/gleam/string_test.gleam

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,27 @@ pub fn repeat_test() {
161161
|> should.equal("")
162162
}
163163

164-
pub fn join_test() {
164+
pub fn join_0_test() {
165+
[]
166+
|> string.join(with: ", ")
167+
|> should.equal("")
168+
169+
[]
170+
|> string.join(with: "-")
171+
|> should.equal("")
172+
}
173+
174+
pub fn join_1_test() {
175+
["Hello"]
176+
|> string.join(with: ", ")
177+
|> should.equal("Hello")
178+
179+
["Hello"]
180+
|> string.join(with: "-")
181+
|> should.equal("Hello")
182+
}
183+
184+
pub fn join_2_test() {
165185
["Hello", "world!"]
166186
|> string.join(with: ", ")
167187
|> should.equal("Hello, world!")
@@ -171,6 +191,16 @@ pub fn join_test() {
171191
|> should.equal("Hello-world!")
172192
}
173193

194+
pub fn join_3_test() {
195+
["Hello", "there", "world!"]
196+
|> string.join(with: ", ")
197+
|> should.equal("Hello, there, world!")
198+
199+
["Hello", "there", "world!"]
200+
|> string.join(with: "-")
201+
|> should.equal("Hello-there-world!")
202+
}
203+
174204
pub fn trim_test() {
175205
" hats \n"
176206
|> string.trim

0 commit comments

Comments
 (0)