mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 08:24:39 +00:00
[3.0 alpha] Remove deprecated onError API on int.parse, double.parse, and num.parse
Change-Id: I9ed24c380bf716cb893b99ba228e34f4671cc8fd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/259042 Reviewed-by: Lasse Nielsen <lrn@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com>
This commit is contained in:
parent
4028b9b0fe
commit
b3304af17d
|
@ -5695,7 +5695,6 @@
|
|||
"../../../tests/language_2/regress/regress40066_test.dart",
|
||||
"../../../tests/language_2/regress/regress40765_test.dart",
|
||||
"../../../tests/language_2/regress/regress4157508_test.dart",
|
||||
"../../../tests/language_2/regress/regress41613_test.dart",
|
||||
"../../../tests/language_2/regress/regress41983_test.dart",
|
||||
"../../../tests/language_2/regress/regress42946_test.dart",
|
||||
"../../../tests/language_2/regress/regress4295001_test.dart",
|
||||
|
|
|
@ -221,11 +221,11 @@ abstract class double extends num {
|
|||
///
|
||||
/// Leading and trailing whitespace is ignored.
|
||||
///
|
||||
/// If the [source] string is not a valid double literal, the [onError]
|
||||
/// is called with the [source] as argument, and its return value is
|
||||
/// used instead.
|
||||
/// Throws a [FormatException] if the [source] string is not valid
|
||||
/// and no `onError` is provided.
|
||||
/// Throws a [FormatException] if the [source] string is not
|
||||
/// a valid double literal.
|
||||
///
|
||||
/// Rather than throwing and immediately catching the [FormatException],
|
||||
/// instead use [tryParse] to handle a potential parsing error.
|
||||
///
|
||||
/// Examples of accepted strings:
|
||||
/// ```
|
||||
|
@ -238,11 +238,7 @@ abstract class double extends num {
|
|||
/// "+.12e-9"
|
||||
/// "-NaN"
|
||||
/// ```
|
||||
/// The [onError] parameter is deprecated and will be removed.
|
||||
/// Instead of `double.parse(string, (string) { ... })`,
|
||||
/// you should use `double.tryParse(string) ?? (...)`.
|
||||
external static double parse(String source,
|
||||
[@deprecated double onError(String source)?]);
|
||||
external static double parse(String source);
|
||||
|
||||
/// Parse [source] as a double literal and return its value.
|
||||
///
|
||||
|
|
|
@ -379,11 +379,10 @@ abstract class int extends num {
|
|||
/// `n == int.parse(n.toRadixString(r), radix: r)`.
|
||||
///
|
||||
/// If the [source] string does not contain a valid integer literal,
|
||||
/// optionally prefixed by a sign, a [FormatException] is thrown
|
||||
/// (unless the deprecated [onError] parameter is used, see below).
|
||||
/// optionally prefixed by a sign, a [FormatException] is thrown.
|
||||
///
|
||||
/// Instead of throwing and immediately catching the [FormatException],
|
||||
/// instead use [tryParse] to handle a parsing error.
|
||||
/// Rather than throwing and immediately catching the [FormatException],
|
||||
/// instead use [tryParse] to handle a potential parsing error.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
|
@ -393,17 +392,7 @@ abstract class int extends num {
|
|||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// The [onError] parameter is deprecated and will be removed.
|
||||
/// Instead of `int.parse(string, onError: (string) => ...)`,
|
||||
/// you should use `int.tryParse(string) ?? (...)`.
|
||||
///
|
||||
/// When the source string is not valid and [onError] is provided,
|
||||
/// whenever a [FormatException] would be thrown,
|
||||
/// [onError] is instead called with [source] as argument,
|
||||
/// and the result of that call is returned by [parse].
|
||||
external static int parse(String source,
|
||||
{int? radix, @deprecated int onError(String source)?});
|
||||
external static int parse(String source, {int? radix});
|
||||
|
||||
/// Parse [source] as a, possibly signed, integer literal.
|
||||
///
|
||||
|
|
|
@ -541,11 +541,10 @@ abstract class num implements Comparable<num> {
|
|||
/// [int.parse] without a radix).
|
||||
/// If that fails, it tries to parse the [input] as a double (similar to
|
||||
/// [double.parse]).
|
||||
/// If that fails, too, it invokes [onError] with [input], and the result
|
||||
/// of that invocation becomes the result of calling `parse`.
|
||||
/// If that fails, too, it throws a [FormatException].
|
||||
///
|
||||
/// If no [onError] is supplied, it defaults to a function that throws a
|
||||
/// [FormatException].
|
||||
/// Rather than throwing and immediately catching the [FormatException],
|
||||
/// instead use [tryParse] to handle a potential parsing error.
|
||||
///
|
||||
/// For any number `n`, this function satisfies
|
||||
/// `identical(n, num.parse(n.toString()))` (except when `n` is a NaN `double`
|
||||
|
@ -573,8 +572,7 @@ abstract class num implements Comparable<num> {
|
|||
static num parse(String input, [@deprecated num onError(String input)?]) {
|
||||
num? result = tryParse(input);
|
||||
if (result != null) return result;
|
||||
if (onError == null) throw FormatException(input);
|
||||
return onError(input);
|
||||
throw FormatException(input);
|
||||
}
|
||||
|
||||
/// Parses a string containing a number literal into a number.
|
||||
|
|
|
@ -103,13 +103,10 @@ void testDouble(double value) {
|
|||
|
||||
void testFail(String source) {
|
||||
var object = new Object();
|
||||
Expect.throws(() {
|
||||
double.parse(source, (s) {
|
||||
Expect.equals(source, s);
|
||||
throw object;
|
||||
});
|
||||
}, (e) => identical(object, e), "Fail: '$source'");
|
||||
Expect.equals(1.5, double.parse(source, (s) => 1.5));
|
||||
Expect.throwsFormatException(() {
|
||||
double.parse(source);
|
||||
});
|
||||
Expect.equals(1.5, double.tryParse(source) ?? 1.5);
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
// Copyright (c) 2012, 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";
|
||||
|
||||
void main() {
|
||||
// These are compile time errors for Dart 2.0.
|
||||
Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: "not a function"));
|
||||
Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: () => 42));
|
||||
Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: (v1, v2) => 42));
|
||||
}
|
|
@ -92,12 +92,9 @@ void main() {
|
|||
Expect.equals(1, int.parse("+1", radix: 2));
|
||||
|
||||
void testFails(String source, int radix) {
|
||||
Expect.throws(() {
|
||||
throw int.parse(source, radix: radix, onError: (s) {
|
||||
throw "FAIL";
|
||||
});
|
||||
}, isFail, "$source/$radix");
|
||||
Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999));
|
||||
Expect.throwsFormatException(
|
||||
() => int.parse(source, radix: radix), "$source/$radix");
|
||||
Expect.equals(-999, int.tryParse(source, radix: radix) ?? -999);
|
||||
}
|
||||
|
||||
for (int i = 2; i < 36; i++) {
|
||||
|
@ -114,19 +111,10 @@ void main() {
|
|||
testFails("0x10", i);
|
||||
}
|
||||
|
||||
testBadTypes(var source, var radix) {
|
||||
Expect.throwsTypeError(() => int.parse(source, radix: radix, onError: (s) => 0)); //# badTypes: ok
|
||||
}
|
||||
|
||||
testBadTypes(9, 10);
|
||||
testBadTypes(true, 10);
|
||||
testBadTypes("0", true);
|
||||
testBadTypes("0", "10");
|
||||
|
||||
testBadArguments(String source, int radix) {
|
||||
// If the types match, it should be an ArgumentError of some sort.
|
||||
Expect.throwsArgumentError(
|
||||
() => int.parse(source, radix: radix, onError: (s) => 0));
|
||||
() => int.parse(source, radix: radix));
|
||||
}
|
||||
|
||||
testBadArguments("0", -1);
|
||||
|
@ -136,5 +124,3 @@ void main() {
|
|||
|
||||
// See also int_parse_radix_bad_handler_test.dart
|
||||
}
|
||||
|
||||
bool isFail(e) => e == "FAIL";
|
||||
|
|
|
@ -7,9 +7,6 @@
|
|||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
const int ERROR = 42;
|
||||
int returnError(String s) => ERROR;
|
||||
|
||||
Expect.equals(0, int.parse("0"));
|
||||
Expect.equals(1, int.parse("1"));
|
||||
Expect.equals(-1, int.parse("-1"));
|
||||
|
@ -26,11 +23,11 @@ main() {
|
|||
Expect.equals(-9223372036854775807, int.parse("-9223372036854775807"));
|
||||
Expect.equals(-9223372036854775807 - 1, int.parse("-9223372036854775808"));
|
||||
|
||||
Expect.equals(ERROR, int.parse("-0x8000000000000001", onError: returnError));
|
||||
Expect.equals(ERROR, int.parse("9223372036854775808", onError: returnError));
|
||||
Expect.equals(ERROR, int.parse("9223372036854775809", onError: returnError));
|
||||
Expect.equals(ERROR, int.parse("-9223372036854775809", onError: returnError));
|
||||
Expect.equals(ERROR, int.parse("10000000000000000000", onError: returnError));
|
||||
Expect.throws(() => int.parse("-0x8000000000000001"));
|
||||
Expect.throws(() => int.parse("9223372036854775808"));
|
||||
Expect.throws(() => int.parse("9223372036854775809"));
|
||||
Expect.throws(() => int.parse("-9223372036854775809"));
|
||||
Expect.throws(() => int.parse("10000000000000000000"));
|
||||
|
||||
Expect.equals(
|
||||
0x7fffffffffffffff,
|
||||
|
@ -48,28 +45,16 @@ main() {
|
|||
"-1000000000000000000000000000000000000000000000000000000000000000",
|
||||
radix: 2));
|
||||
|
||||
Expect.equals(
|
||||
ERROR,
|
||||
int.parse(
|
||||
"1000000000000000000000000000000000000000000000000000000000000000",
|
||||
radix: 2,
|
||||
onError: returnError));
|
||||
Expect.equals(
|
||||
ERROR,
|
||||
int.parse(
|
||||
"1111111111111111111111111111111111111111111111111111111111111110",
|
||||
radix: 2,
|
||||
onError: returnError));
|
||||
Expect.equals(
|
||||
ERROR,
|
||||
int.parse(
|
||||
"1111111111111111111111111111111111111111111111111111111111111111",
|
||||
radix: 2,
|
||||
onError: returnError));
|
||||
Expect.equals(
|
||||
ERROR,
|
||||
int.parse(
|
||||
"-1000000000000000000000000000000000000000000000000000000000000001",
|
||||
radix: 2,
|
||||
onError: returnError));
|
||||
Expect.throws(() => int.parse(
|
||||
"1000000000000000000000000000000000000000000000000000000000000000",
|
||||
radix: 2));
|
||||
Expect.throws(() => int.parse(
|
||||
"1111111111111111111111111111111111111111111111111111111111111110",
|
||||
radix: 2));
|
||||
Expect.throws(() => int.parse(
|
||||
"1111111111111111111111111111111111111111111111111111111111111111",
|
||||
radix: 2));
|
||||
Expect.throws(() => int.parse(
|
||||
"-1000000000000000000000000000000000000000000000000000000000000001",
|
||||
radix: 2));
|
||||
}
|
||||
|
|
|
@ -114,12 +114,9 @@ void testDouble(double value) {
|
|||
|
||||
void testFail(String source) {
|
||||
var object = new Object();
|
||||
Expect.throws(() {
|
||||
num.parse(source, (s) {
|
||||
Expect.equals(source, s);
|
||||
throw object;
|
||||
});
|
||||
}, (e) => identical(object, e), "Fail: '$source'");
|
||||
Expect.throwsFormatException(() {
|
||||
num.parse(source);
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
|
|
@ -105,13 +105,10 @@ void testDouble(double value) {
|
|||
|
||||
void testFail(String source) {
|
||||
var object = new Object();
|
||||
Expect.throws(() {
|
||||
double.parse(source, (s) {
|
||||
Expect.equals(source, s);
|
||||
throw object;
|
||||
});
|
||||
}, (e) => identical(object, e), "Fail: '$source'");
|
||||
Expect.equals(1.5, double.parse(source, (s) => 1.5));
|
||||
Expect.throwsFormatException(() {
|
||||
double.parse(source);
|
||||
});
|
||||
Expect.equals(1.5, double.tryParse(source) ?? 1.5);
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
// Copyright (c) 2012, 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.
|
||||
|
||||
// @dart = 2.9
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
void main() {
|
||||
// These are compile time errors for Dart 2.0.
|
||||
Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: "not a function"));
|
||||
Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: () => 42));
|
||||
Expect.throws(() => int.parse("9", radix: 8, /*@compile-error=unspecified*/ onError: (v1, v2) => 42));
|
||||
}
|
|
@ -94,12 +94,9 @@ void main() {
|
|||
Expect.equals(1, int.parse("+1", radix: 2));
|
||||
|
||||
void testFails(String source, int radix) {
|
||||
Expect.throws(() {
|
||||
throw int.parse(source, radix: radix, onError: (s) {
|
||||
throw "FAIL";
|
||||
});
|
||||
}, isFail, "$source/$radix");
|
||||
Expect.equals(-999, int.parse(source, radix: radix, onError: (s) => -999));
|
||||
Expect.throwsFormatException(
|
||||
() => int.parse(source, radix: radix), "$source/$radix");
|
||||
Expect.equals(-999, int.tryParse(source, radix: radix) ?? -999);
|
||||
}
|
||||
|
||||
for (int i = 2; i < 36; i++) {
|
||||
|
@ -117,19 +114,10 @@ void main() {
|
|||
testFails("0x10", i);
|
||||
}
|
||||
|
||||
testBadTypes(var source, var radix) {
|
||||
Expect.throwsTypeError(() => int.parse(source, radix: radix, onError: (s) => 0)); //# badTypes: ok
|
||||
}
|
||||
|
||||
testBadTypes(9, 10);
|
||||
testBadTypes(true, 10);
|
||||
testBadTypes("0", true);
|
||||
testBadTypes("0", "10");
|
||||
|
||||
testBadArguments(String source, int radix) {
|
||||
// If the types match, it should be an ArgumentError of some sort.
|
||||
Expect.throwsArgumentError(
|
||||
() => int.parse(source, radix: radix, onError: (s) => 0));
|
||||
() => int.parse(source, radix: radix));
|
||||
}
|
||||
|
||||
testBadArguments("0", -1);
|
||||
|
@ -139,5 +127,3 @@ void main() {
|
|||
|
||||
// See also int_parse_radix_bad_handler_test.dart
|
||||
}
|
||||
|
||||
bool isFail(e) => e == "FAIL";
|
||||
|
|
|
@ -9,9 +9,6 @@
|
|||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
const int ERROR = 42;
|
||||
var returnError = (_) => ERROR;
|
||||
|
||||
Expect.equals(0, int.parse("0"));
|
||||
Expect.equals(1, int.parse("1"));
|
||||
Expect.equals(-1, int.parse("-1"));
|
||||
|
@ -28,11 +25,11 @@ main() {
|
|||
Expect.equals(-9223372036854775807, int.parse("-9223372036854775807"));
|
||||
Expect.equals(-9223372036854775807 - 1, int.parse("-9223372036854775808"));
|
||||
|
||||
Expect.equals(ERROR, int.parse("-0x8000000000000001", onError: returnError));
|
||||
Expect.equals(ERROR, int.parse("9223372036854775808", onError: returnError));
|
||||
Expect.equals(ERROR, int.parse("9223372036854775809", onError: returnError));
|
||||
Expect.equals(ERROR, int.parse("-9223372036854775809", onError: returnError));
|
||||
Expect.equals(ERROR, int.parse("10000000000000000000", onError: returnError));
|
||||
Expect.throws(() => int.parse("-0x8000000000000001"));
|
||||
Expect.throws(() => int.parse("9223372036854775808"));
|
||||
Expect.throws(() => int.parse("9223372036854775809"));
|
||||
Expect.throws(() => int.parse("-9223372036854775809"));
|
||||
Expect.throws(() => int.parse("10000000000000000000"));
|
||||
|
||||
Expect.equals(
|
||||
0x7fffffffffffffff,
|
||||
|
@ -50,28 +47,16 @@ main() {
|
|||
"-1000000000000000000000000000000000000000000000000000000000000000",
|
||||
radix: 2));
|
||||
|
||||
Expect.equals(
|
||||
ERROR,
|
||||
int.parse(
|
||||
"1000000000000000000000000000000000000000000000000000000000000000",
|
||||
radix: 2,
|
||||
onError: returnError));
|
||||
Expect.equals(
|
||||
ERROR,
|
||||
int.parse(
|
||||
"1111111111111111111111111111111111111111111111111111111111111110",
|
||||
radix: 2,
|
||||
onError: returnError));
|
||||
Expect.equals(
|
||||
ERROR,
|
||||
int.parse(
|
||||
"1111111111111111111111111111111111111111111111111111111111111111",
|
||||
radix: 2,
|
||||
onError: returnError));
|
||||
Expect.equals(
|
||||
ERROR,
|
||||
int.parse(
|
||||
"-1000000000000000000000000000000000000000000000000000000000000001",
|
||||
radix: 2,
|
||||
onError: returnError));
|
||||
Expect.throws(() => int.parse(
|
||||
"1000000000000000000000000000000000000000000000000000000000000000",
|
||||
radix: 2));
|
||||
Expect.throws(() => int.parse(
|
||||
"1111111111111111111111111111111111111111111111111111111111111110",
|
||||
radix: 2));
|
||||
Expect.throws(() => int.parse(
|
||||
"1111111111111111111111111111111111111111111111111111111111111111",
|
||||
radix: 2));
|
||||
Expect.throws(() => int.parse(
|
||||
"-1000000000000000000000000000000000000000000000000000000000000001",
|
||||
radix: 2));
|
||||
}
|
||||
|
|
|
@ -116,12 +116,9 @@ void testDouble(double value) {
|
|||
|
||||
void testFail(String source) {
|
||||
var object = new Object();
|
||||
Expect.throws(() {
|
||||
num.parse(source, (s) {
|
||||
Expect.equals(source, s);
|
||||
throw object;
|
||||
});
|
||||
}, (e) => identical(object, e), "Fail: '$source'");
|
||||
Expect.throwsFormatException(() {
|
||||
num.parse(source);
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
// Copyright (c) 2020, 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.
|
||||
|
||||
// @dart=2.7
|
||||
|
||||
// Requirements=nnbd-weak
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
// In legacy Dart 2 and null safety weak mode the error handlers can
|
||||
// return null.
|
||||
Expect.equals(null, int.parse("foo", onError: (_) => null));
|
||||
Expect.equals(null, double.parse("foo", (_) => null));
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
// Copyright (c) 2020, 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.
|
||||
|
||||
// @dart = 2.9
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
// In legacy Dart 2 and null safety weak mode the error handlers can
|
||||
// return null.
|
||||
Expect.equals(null, int.parse("foo", onError: (_) => null));
|
||||
Expect.equals(null, double.parse("foo", (_) => null));
|
||||
}
|
Loading…
Reference in a new issue