You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResourcesUtils.kt
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,11 @@ internal fun String.replaceWithArgs(args: List<String>): String {
19
19
)
20
20
}
21
21
}
22
-
args[index]
22
+
args.getOrElse(index) {
23
+
throwIllegalArgumentException(
24
+
"Formatting failed: Placeholder '${match.value}' at position ${index +1} is out of bounds. Only ${args.size} argument(s) provided for format string \"$this\""
Copy file name to clipboardExpand all lines: components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/StringFormatTest.kt
+25-5Lines changed: 25 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,10 @@ class StringFormatTest {
16
16
17
17
assertEquals("Hello Alice, you have 5 new messages!", result)
18
18
}
19
+
//@TestOnly
20
+
internalfundropStringItemsCache() {
21
+
stringItemsCache.clear()
22
+
}
19
23
20
24
@Test
21
25
fun`replaceWithArgs works with multiple placeholders referring to the same argument`() {
@@ -52,9 +56,13 @@ class StringFormatTest {
52
56
val template ="Hello %1\$s, %2\$s!"
53
57
val args =listOf("Alice")
54
58
55
-
assertFailsWith<IndexOutOfBoundsException> {
59
+
val exception =assertFailsWith<IllegalArgumentException> {
56
60
template.replaceWithArgs(args)
57
61
}
62
+
assertEquals(
63
+
"Formatting failed: Placeholder '%2\$s' at position 2 is out of bounds. Only 1 argument(s) provided for format string \"Hello %1\$s, %2\$s!\"",
64
+
exception.message
65
+
)
58
66
}
59
67
60
68
@Test
@@ -72,9 +80,13 @@ class StringFormatTest {
72
80
val template ="Hello %1\$s, you have %3\$s messages"
73
81
val args =listOf("Alice")
74
82
75
-
assertFailsWith<IndexOutOfBoundsException> {
83
+
val exception =assertFailsWith<IllegalArgumentException> {
76
84
template.replaceWithArgs(args)
77
85
}
86
+
assertEquals(
87
+
"Formatting failed: Placeholder '%3\$s' at position 3 is out of bounds. Only 1 argument(s) provided for format string \"Hello %1\$s, you have %3\$s messages\"",
88
+
exception.message
89
+
)
78
90
}
79
91
80
92
@Test
@@ -137,26 +149,34 @@ class StringFormatTest {
137
149
}
138
150
139
151
@Test
152
+
@IgnoreWasmTest // https://youtrack.jetbrains.com/issue/KT-69014, wasm throws RuntimeError instead of IndexOutOfBounds
140
153
fun`replaceWithArgs throws exception for missing arguments`() {
141
154
val template ="Hello %1\$s, you have %2\$d messages!"
142
155
val args =listOf("Alice")
143
156
144
157
// An exception should be thrown because the second argument (%2$d) is missing
145
-
assertFailsWith<IndexOutOfBoundsException> {
158
+
val exception =assertFailsWith<IllegalArgumentException> {
146
159
template.replaceWithArgs(args)
147
160
}
161
+
assertEquals(
162
+
"Formatting failed: Placeholder '%2\$d' at position 2 is out of bounds. Only 1 argument(s) provided for format string \"Hello %1\$s, you have %2\$d messages!\"",
163
+
exception.message
164
+
)
148
165
}
149
166
150
167
@Test
151
-
@IgnoreWasmTest // https://youtrack.jetbrains.com/issue/KT-69014, wasm throws RuntimeError instead of IndexOutOfBounds
152
168
fun`replaceWithArgs throws exception for unmatched placeholders`() {
153
169
val template ="Hello %1\$s, your rank is %3\$s"
154
170
val args =listOf("Alice", "1")
155
171
156
172
// The template has a %3$s placeholder, but there is no third argument
157
-
assertFailsWith<IndexOutOfBoundsException> {
173
+
val exception =assertFailsWith<IllegalArgumentException> {
158
174
template.replaceWithArgs(args)
159
175
}
176
+
assertEquals(
177
+
"Formatting failed: Placeholder '%3\$s' at position 3 is out of bounds. Only 2 argument(s) provided for format string \"Hello %1\$s, your rank is %3\$s\"",
0 commit comments