dart-sdk/tests/corelib_2/errors_test.dart
James Lin 10e8d5d1a6 Try to clarify the RangeError.range message
The old RangeError.range message was confusing:

  RangeError (index): Invalid value: Not in range 0..2, inclusive: 9

The comma makes the message hard to parse.  I've seen a number of
people misinterpret "inclusive" as describing the invalid value (9)
instead of acting as a modifier on the range.

https://github.com/dart-lang/sdk/issues/29586 has a lot of
bikeshedding about this, but in the interest of mitigating confusion
sooner, I propose changing it to:

  RangeError (index): Invalid value: Not in inclusive range 0..2: 9

I'm intentionally trying to improve the message with minimal
disruption to the structure of the error message.  Although I'd much
prefer that "Invalid value" and the actual value be adjacent instead
of being interrupted by the explanation, such restructuring is rather
non-trivial.  RangeError allows the "Invalid value" message to be
customized, and rearranging terms could produce even worse
constructions.

I also considered:

  RangeError (index): Invalid value: Not in range [0, 2]: 9

And while I like that that is brief and clear, I chose not to use it
for people who are unfamiliar with interval notation and who might
mistake it as a Dart List.

Bug: https://github.com/dart-lang/sdk/issues/29586
Change-Id: I0f23b195437e4053ae5f76b5d303123979a8c9fe
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/146024
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
2020-05-27 14:59:39 +00:00

100 lines
4.7 KiB
Dart

// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Test that error constructors do what they are documented as doing.
main() {
Expect.equals("Invalid argument(s)", new ArgumentError().toString());
Expect.equals(
"Invalid argument(s): message", new ArgumentError("message").toString());
Expect.equals(
"Invalid argument: null", new ArgumentError.value(null).toString());
Expect.equals("Invalid argument: 42", new ArgumentError.value(42).toString());
Expect.equals(
"Invalid argument: \"bad\"", new ArgumentError.value("bad").toString());
Expect.equals("Invalid argument (foo): null",
new ArgumentError.value(null, "foo").toString());
Expect.equals("Invalid argument (foo): 42",
new ArgumentError.value(42, "foo").toString());
Expect.equals("Invalid argument (foo): message: 42",
new ArgumentError.value(42, "foo", "message").toString());
Expect.equals("Invalid argument: message: 42",
new ArgumentError.value(42, null, "message").toString());
Expect.equals("Invalid argument(s): Must not be null",
new ArgumentError.notNull().toString());
Expect.equals("Invalid argument(s) (foo): Must not be null",
new ArgumentError.notNull("foo").toString());
Expect.equals("RangeError", new RangeError(null).toString());
Expect.equals("RangeError: message", new RangeError("message").toString());
Expect.equals("RangeError: Value not in range: 42",
new RangeError.value(42).toString());
Expect.equals("RangeError (foo): Value not in range: 42",
new RangeError.value(42, "foo").toString());
Expect.equals("RangeError (foo): message: 42",
new RangeError.value(42, "foo", "message").toString());
Expect.equals("RangeError: message: 42",
new RangeError.value(42, null, "message").toString());
Expect.equals("RangeError: Invalid value: Not in inclusive range 2..9: 42",
new RangeError.range(42, 2, 9).toString());
Expect.equals(
"RangeError (foo): Invalid value: Not in inclusive range 2..9: 42",
new RangeError.range(42, 2, 9, "foo").toString());
Expect.equals("RangeError (foo): message: Not in inclusive range 2..9: 42",
new RangeError.range(42, 2, 9, "foo", "message").toString());
Expect.equals("RangeError: message: Not in inclusive range 2..9: 42",
new RangeError.range(42, 2, 9, null, "message").toString());
Expect.equals(
"RangeError: Index out of range: "
"index should be less than 3: 42",
new RangeError.index(42, [1, 2, 3]).toString());
Expect.equals(
"RangeError (foo): Index out of range: "
"index should be less than 3: 42",
new RangeError.index(42, [1, 2, 3], "foo").toString());
Expect.equals(
"RangeError (foo): message: "
"index should be less than 3: 42",
new RangeError.index(42, [1, 2, 3], "foo", "message").toString());
Expect.equals(
"RangeError: message: "
"index should be less than 3: 42",
new RangeError.index(42, [1, 2, 3], null, "message").toString());
Expect.equals(
"RangeError (foo): message: "
"index should be less than 2: 42",
new RangeError.index(42, [1, 2, 3], "foo", "message", 2).toString());
Expect.equals(
"RangeError: Index out of range: "
"index must not be negative: -5",
new RangeError.index(-5, [1, 2, 3]).toString());
Expect.equals(42, ArgumentError.checkNotNull(42));
Expect.equals(42, ArgumentError.checkNotNull(42, "name"));
Expect.throwsArgumentError(() => ArgumentError.checkNotNull(null));
Expect.equals(1, RangeError.checkNotNegative(1));
Expect.equals(0, RangeError.checkNotNegative(0));
Expect.throwsRangeError(() => RangeError.checkNotNegative(-1));
Expect.equals(1, RangeError.checkValueInInterval(1, 0, 2));
Expect.equals(1, RangeError.checkValueInInterval(1, 1, 2));
Expect.equals(1, RangeError.checkValueInInterval(1, 0, 1));
Expect.equals(1, RangeError.checkValueInInterval(1, 1, 1));
Expect.throwsRangeError(() => RangeError.checkValueInInterval(1, 2, 3));
Expect.throwsRangeError(() => RangeError.checkValueInInterval(1, 1, 0));
Expect.throwsRangeError(() => RangeError.checkValueInInterval(0, 1, 0));
Expect.equals(1, RangeError.checkValidIndex(1, [1, 2]));
Expect.equals(1, RangeError.checkValidIndex(1, null, null, 2));
Expect.throwsRangeError(() => RangeError.checkValidIndex(1, []));
Expect.throwsRangeError(() => RangeError.checkValidIndex(1, null, null, 1));
Expect.throwsRangeError(() => RangeError.checkValidIndex(-1, [1, 2, 3]));
Expect.throwsRangeError(() => RangeError.checkValidIndex(-1, null, null, 3));
}