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-2Lines changed: 6 additions & 2 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\""
25
+
)
26
+
}
23
27
}
24
28
}
25
29
@@ -37,7 +41,7 @@ internal fun dropStringItemsCache() {
Copy file name to clipboardExpand all lines: components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/StringFormatTest.kt
+21-5Lines changed: 21 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -52,9 +52,13 @@ class StringFormatTest {
52
52
val template ="Hello %1\$s, %2\$s!"
53
53
val args =listOf("Alice")
54
54
55
-
assertFailsWith<IndexOutOfBoundsException> {
55
+
val exception =assertFailsWith<IllegalArgumentException> {
56
56
template.replaceWithArgs(args)
57
57
}
58
+
assertEquals(
59
+
"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!\"",
60
+
exception.message
61
+
)
58
62
}
59
63
60
64
@Test
@@ -72,9 +76,13 @@ class StringFormatTest {
72
76
val template ="Hello %1\$s, you have %3\$s messages"
73
77
val args =listOf("Alice")
74
78
75
-
assertFailsWith<IndexOutOfBoundsException> {
79
+
val exception =assertFailsWith<IllegalArgumentException> {
76
80
template.replaceWithArgs(args)
77
81
}
82
+
assertEquals(
83
+
"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\"",
84
+
exception.message
85
+
)
78
86
}
79
87
80
88
@Test
@@ -137,26 +145,34 @@ class StringFormatTest {
137
145
}
138
146
139
147
@Test
148
+
@IgnoreWasmTest // https://youtrack.jetbrains.com/issue/KT-69014, wasm throws RuntimeError instead of IndexOutOfBounds
140
149
fun`replaceWithArgs throws exception for missing arguments`() {
141
150
val template ="Hello %1\$s, you have %2\$d messages!"
142
151
val args =listOf("Alice")
143
152
144
153
// An exception should be thrown because the second argument (%2$d) is missing
145
-
assertFailsWith<IndexOutOfBoundsException> {
154
+
val exception =assertFailsWith<IllegalArgumentException> {
146
155
template.replaceWithArgs(args)
147
156
}
157
+
assertEquals(
158
+
"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!\"",
159
+
exception.message
160
+
)
148
161
}
149
162
150
163
@Test
151
-
@IgnoreWasmTest // https://youtrack.jetbrains.com/issue/KT-69014, wasm throws RuntimeError instead of IndexOutOfBounds
152
164
fun`replaceWithArgs throws exception for unmatched placeholders`() {
153
165
val template ="Hello %1\$s, your rank is %3\$s"
154
166
val args =listOf("Alice", "1")
155
167
156
168
// The template has a %3$s placeholder, but there is no third argument
157
-
assertFailsWith<IndexOutOfBoundsException> {
169
+
val exception =assertFailsWith<IllegalArgumentException> {
158
170
template.replaceWithArgs(args)
159
171
}
172
+
assertEquals(
173
+
"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