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
+20-4Lines changed: 20 additions & 4 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
@@ -142,9 +150,13 @@ class StringFormatTest {
142
150
val args =listOf("Alice")
143
151
144
152
// An exception should be thrown because the second argument (%2$d) is missing
145
-
assertFailsWith<IndexOutOfBoundsException> {
153
+
val exception =assertFailsWith<IllegalArgumentException> {
146
154
template.replaceWithArgs(args)
147
155
}
156
+
assertEquals(
157
+
"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!\"",
158
+
exception.message
159
+
)
148
160
}
149
161
150
162
@Test
@@ -154,9 +166,13 @@ class StringFormatTest {
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