Skip to content

Commit 8b1a709

Browse files
committed
Add unit tests for String.replace(Map<String, String>) extension function.
1 parent b9665dd commit 8b1a709

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ dependencies {
290290
implementation libs.mozilla.ui.tabcounter
291291
implementation libs.mozilla.ui.widgets
292292

293+
testImplementation libs.junit
294+
293295
androidTestImplementation libs.androidx.test.espresso.core
294296
androidTestImplementation libs.androidx.test.espresso.idling.resources
295297
constraints {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
package org.mozilla.reference.browser.ext
6+
7+
import org.junit.Assert.assertEquals
8+
import org.junit.Test
9+
10+
class StringKtTest {
11+
@Test
12+
fun `replace should replace all keys with their corresponding values`() {
13+
val input = "The quick brown fox jumps over the lazy dog"
14+
val pairs = mapOf(
15+
"quick" to "slow",
16+
"brown" to "red",
17+
"fox" to "cat",
18+
"dog" to "mouse",
19+
)
20+
21+
val result = input.replace(pairs)
22+
23+
assertEquals("The slow red cat jumps over the lazy mouse", result)
24+
}
25+
26+
@Test
27+
fun `replace should return the same string if the map is empty`() {
28+
val input = "Hello world"
29+
val pairs = emptyMap<String, String>()
30+
31+
val result = input.replace(pairs)
32+
33+
assertEquals(input, result)
34+
}
35+
36+
@Test
37+
fun `replace should handle keys that are not present in the string`() {
38+
val input = "Hello world"
39+
val pairs = mapOf("foo" to "bar")
40+
41+
val result = input.replace(pairs)
42+
43+
assertEquals("Hello world", result)
44+
}
45+
}

0 commit comments

Comments
 (0)