Use ChoiceFormat for better plurals in error messages (#1848)

This commit is contained in:
Jen Basch
2026-09-10 22:03:05 +00:00
committed by GitHub
parent 90245589b6
commit 574ad844f3
6 changed files with 30 additions and 11 deletions
@@ -181,7 +181,7 @@ public final class FunctionNode extends RegularMemberNode {
assert argCount != paramCount; assert argCount != paramCount;
return exceptionBuilder() return exceptionBuilder()
.evalError("wrongFunctionArgumentCount", paramCount, argCount, paramCount == 1 ? "" : "s") .evalError("wrongFunctionArgumentCount", paramCount, argCount)
.withSourceSection(member.getHeaderSection()) .withSourceSection(member.getHeaderSection())
.build(); .build();
} }
@@ -57,7 +57,7 @@ public final class IdentityMixinNode extends PklRootNode {
if (arguments.length != 3) { if (arguments.length != 3) {
CompilerDirectives.transferToInterpreter(); CompilerDirectives.transferToInterpreter();
throw exceptionBuilder() throw exceptionBuilder()
.evalError("wrongFunctionArgumentCount", 1, arguments.length - 2, "") .evalError("wrongFunctionArgumentCount", 1, arguments.length - 2)
.withSourceSection(sourceSection) .withSourceSection(sourceSection)
.build(); .build();
} }
@@ -207,7 +207,7 @@ expectedModuleAsArgument=\
Expected a module as argument, but got an object that amends a module. Expected a module as argument, but got an object that amends a module.
wrongTypeArgumentCount=\ wrongTypeArgumentCount=\
Expected {0} type argument(s) but got {1}. Expected {0} type argument{0,choice,0#s|1#|1<s} but got {1}.
duplicateDefinition=\ duplicateDefinition=\
Duplicate definition of member `{0}`. Duplicate definition of member `{0}`.
@@ -310,7 +310,7 @@ cannotDefineExternalMember=\
External members can only be defined by standard library modules. External members can only be defined by standard library modules.
wrongFunctionArgumentCount=\ wrongFunctionArgumentCount=\
Expected {0} function argument{2} but got {1}. Expected {0} function argument{0,choice,0#s|1#|1<s} but got {1}.
noOuterScope=\ noOuterScope=\
Top-level scope does not have an outer scope. Top-level scope does not have an outer scope.
@@ -810,7 +810,7 @@ Error parsing YAML document: The number of aliases for collection nodes exceeds
To increase the allowed maximum, set `YamlRenderer.maxCollectionAliases`. To increase the allowed maximum, set `YamlRenderer.maxCollectionAliases`.
evaluationTimedOut=\ evaluationTimedOut=\
Evaluation timed out after {0,number,#.##} second(s). Evaluation timed out after {0,number,#.##} second{0,choice,0<s|1#|1.0<s}.
cannotFindStdLibModule=\ cannotFindStdLibModule=\
Cannot find standard library module `pkl:{0}`.\n\ Cannot find standard library module `pkl:{0}`.\n\
@@ -1,2 +1,2 @@
f = (a1, a2, a3) -> a1 f = (a1, a2) -> a1
res = f.applyToList(List(1, 2, 3, 4, 5, 6)) res = f.applyToList(List(1, 2, 3, 4, 5, 6))
@@ -1,8 +1,8 @@
–– Pkl Error –– –– Pkl Error ––
Expected 3 function arguments but got 6. Expected 2 function arguments but got 6.
x | f = (a1, a2, a3) -> a1 x | f = (a1, a2) -> a1
^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
at wrongArgumentListLength#f.<function#1> (file:///$snippetsDir/input/lambdas/wrongArgumentListLength.pkl) at wrongArgumentListLength#f.<function#1> (file:///$snippetsDir/input/lambdas/wrongArgumentListLength.pkl)
x | res = f.applyToList(List(1, 2, 3, 4, 5, 6)) x | res = f.applyToList(List(1, 2, 3, 4, 5, 6))
@@ -530,8 +530,27 @@ class EmbeddedExecutorTest {
timeout(Duration.ofSeconds(1)) timeout(Duration.ofSeconds(1))
} }
} }
assertThat(e.message)
.containsAnyOf(
"Evaluation timed out after 1 second.",
"Evaluation timed out after 1 second(s).",
)
assertThat(e.message).contains("Evaluation timed out after 1 second(s).") val e2 =
assertThrows<ExecutorException> {
executor.evaluatePath(pklFile) {
allowedModules("file:")
allowedResources("prop:")
rootDir(tempDir)
timeout(Duration.ofMillis(1500))
}
}
assertThat(e2.message)
.containsAnyOf(
"Evaluation timed out after 1.5 seconds.",
"Evaluation timed out after 1.5 second(s).",
)
} }
@Test @Test