Skip to content

Commit 43ae01b

Browse files
committed
[GR-45046] Add ResultVerifier for division by 0 in Ruby TCK
PullRequest: truffleruby/3723
2 parents 1d3b6c8 + 53b6cb9 commit 43ae01b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/tck/java/org/truffleruby/tck/RubyTCKLanguageProvider.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public Collection<? extends Snippet> createValueConstructors(Context context) {
7575
// Interop Primitives
7676
vals.add(createValueConstructor(context, "nil", NULL)); // should also be OBJECT?
7777
vals.add(createValueConstructor(context, "false", BOOLEAN));
78-
// NOTE: NUMBER is only for primitives and types which are instanceof java.lang.Number.
78+
// NOTE: NUMBER means InteropLibrary#isNumber
79+
vals.add(createValueConstructor(context, "0", NUMBER));
7980
vals.add(createValueConstructor(context, "7", NUMBER)); // int
8081
vals.add(createValueConstructor(context, "1 << 42", NUMBER)); // long
8182
vals.add(createValueConstructor(context, "1 << 84", NUMBER)); // Bignum
@@ -118,7 +119,16 @@ public Collection<? extends Snippet> createExpressions(Context context) {
118119
// ops.add(createBinaryOperator(context, "a * b", NUMBER, NUMBER, NUMBER));
119120
// ops.add(createBinaryOperator(context, "a * b", STRING, NUMBER, STRING));
120121
// ops.add(createBinaryOperator(context, "a * b", ARRAY, NUMBER, ARRAY));
121-
ops.add(createBinaryOperator(context, "a / b", NUMBER, NUMBER, NUMBER));
122+
ops.add(createBinaryOperator(context, "a / b", NUMBER, NUMBER, NUMBER, run -> {
123+
Value rhs = run.getParameters().get(1);
124+
// Ideally we should test if both LHS and RHS are integral but interop gives us no way currently
125+
if (rhs.fitsInInt() && rhs.asInt() == 0 && run.getException() != null &&
126+
run.getException().getMessage().equals("divided by 0")) {
127+
// OK, division by 0 raises
128+
} else {
129+
ResultVerifier.getDefaultResultVerifier().accept(run);
130+
}
131+
}));
122132
// ops.add(createBinaryOperator(context, "a % b", NUMBER, NUMBER, NUMBER));
123133
// ops.add(createBinaryOperator(context, "a ** b", NUMBER, NUMBER, NUMBER));
124134

@@ -248,11 +258,18 @@ private Snippet createValueConstructor(Context context, String value, TypeDescri
248258

249259
private Snippet createBinaryOperator(Context context, String operator, TypeDescriptor lhsType,
250260
TypeDescriptor rhsType, TypeDescriptor returnType) {
261+
return createBinaryOperator(context, operator, lhsType, rhsType, returnType,
262+
ResultVerifier.getDefaultResultVerifier());
263+
}
264+
265+
private Snippet createBinaryOperator(Context context, String operator, TypeDescriptor lhsType,
266+
TypeDescriptor rhsType, TypeDescriptor returnType, ResultVerifier resultVerifier) {
251267
final Value function = context.eval(getId(), String.format("-> a, b { %s }", operator));
252268

253269
return Snippet
254270
.newBuilder(operator, function, returnType)
255271
.parameterTypes(lhsType, rhsType)
272+
.resultVerifier(resultVerifier)
256273
.build();
257274
}
258275

0 commit comments

Comments
 (0)