dart-sdk/tests/corelib/symbol_test.dart
Robert Nystrom b998b10f3e Migrate remaining corelib_2/ tests to NNBD.
Change-Id: I3a31632ce28fb87a410b759d092c7ebc9393574d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128306
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
2019-12-17 23:06:24 +00:00

80 lines
1.9 KiB
Dart

// Copyright (c) 2013, 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.
// Basic test of Symbol class.
main() {
var x;
print(x = const Symbol('fisk'));
try {
print(const Symbol(0)); //# 01: compile-time error
} on NoSuchMethodError {
print('Caught NoSuchMethodError');
} on TypeError {
print('Caught TypeError');
}
try {
print(const Symbol('0')); //# 02: compile-time error
} on ArgumentError catch (e) {
print('Caught $e');
}
try {
print(const Symbol('_')); //# 03: compile-time error
} on ArgumentError catch (e) {
print('Caught $e');
}
try {
print(new Symbol('0'));
throw 'Expected an ArgumentError';
} on ArgumentError catch (e) {
print('Caught $e');
}
try {
print(new Symbol('_'));
throw 'Expected an ArgumentError';
} on ArgumentError catch (e) {
print('Caught $e');
}
if (!identical(const Symbol('fisk'), x)) {
throw 'Symbol constant is not canonicalized';
}
if (const Symbol('fisk') != x) {
throw 'Symbol constant is not equal to itself';
}
if (const Symbol('fisk') != new Symbol('fisk')) {
throw 'Symbol constant is not equal to its non-const equivalent';
}
if (new Symbol('fisk') != new Symbol('fisk')) {
throw 'new Symbol is not equal to its equivalent';
}
if (new Symbol('fisk') == new Symbol('hest')) {
throw 'unrelated Symbols are equal';
}
if (new Symbol('fisk') == new Object()) {
throw 'unrelated objects are equal';
}
x.hashCode as int;
new Symbol('fisk').hashCode as int;
if (new Symbol('fisk').hashCode != x.hashCode) {
throw "non-const Symbol's hashCode not equal to its const equivalent";
}
if (new Symbol('') != Symbol.empty) {
throw 'empty Symbol not equals to itself';
}
}