Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Unified Diff: packages/quiver/test/core/optional_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « packages/quiver/test/core/hash_test.dart ('k') | packages/quiver/test/io_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/quiver/test/core/optional_test.dart
diff --git a/packages/quiver/test/core/optional_test.dart b/packages/quiver/test/core/optional_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..0072ba15da757dd50fa5df277791d799c1d0c89c
--- /dev/null
+++ b/packages/quiver/test/core/optional_test.dart
@@ -0,0 +1,110 @@
+// Copyright 2013 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+library quiver.core.optional_test;
+
+import 'package:quiver/core.dart';
+import 'package:test/test.dart';
+
+main() {
+ group('Optional', () {
+ test('absent should be not present and not gettable', () {
+ expect(new Optional<int>.absent().isPresent, isFalse);
+ expect(() => new Optional<int>.absent().value, throws);
+ });
+
+ test('of should return value', () {
+ expect(new Optional<int>.of(7).value, 7);
+ });
+
+ test('isPresent should execute only if present', () {
+ int value;
+ new Optional<int>.of(7).ifPresent((v) {
+ value = v;
+ });
+ expect(value, 7);
+ new Optional<int>.absent().ifPresent((v) {
+ value = v;
+ });
+ expect(value, 7);
+ });
+
+ test('isAbsent should execute only if absent', () {
+ int value;
+ new Optional<int>.of(7).ifAbsent(() {
+ value = 7;
+ });
+ expect(value, null);
+ new Optional<int>.absent().ifAbsent(() {
+ value = 7;
+ });
+ expect(value, 7);
+ });
+
+ test('fromNullable should allow present or absent', () {
+ expect(new Optional<int>.fromNullable(7).value, 7);
+ expect(new Optional<int>.fromNullable(null).isPresent, isFalse);
+ });
+
+ test('or should return present and replace absent', () {
+ expect(new Optional<int>.fromNullable(7).or(13), 7);
+ expect(new Optional<int>.fromNullable(null).or(13), 13);
+ });
+
+ test('orNull should return value if present or null if absent', () {
+ expect(new Optional<int>.fromNullable(7).orNull, isNotNull);
+ expect(new Optional<int>.fromNullable(null).orNull, isNull);
+ });
+
+ test('transform should return transformed value or absent', () {
+ expect(new Optional<int>.fromNullable(7).transform((a) => a + 1),
+ equals(new Optional<int>.of(8)));
+ expect(new Optional<int>.fromNullable(null)
+ .transform((a) => a + 1).isPresent, isFalse);
+ });
+
+ test('hashCode should allow optionals to be in hash sets', () {
+ expect(new Set.from([
+ new Optional<int>.of(7),
+ new Optional<int>.of(8),
+ new Optional<int>.absent()
+ ]), equals(new Set.from([
+ new Optional<int>.of(7),
+ new Optional<int>.of(8),
+ new Optional<int>.absent()
+ ])));
+ expect(new Set.from([
+ new Optional<int>.of(7),
+ new Optional<int>.of(8)
+ ]), isNot(equals(
+ new Set.from([new Optional<int>.of(7), new Optional<int>.of(9)]))));
+ });
+
+ test('== should compare by value', () {
+ expect(new Optional<int>.of(7), equals(new Optional<int>.of(7)));
+ expect(new Optional<int>.fromNullable(null),
+ equals(new Optional<int>.fromNullable(null)));
+ expect(new Optional<int>.fromNullable(null),
+ isNot(equals(new Optional<int>.of(7))));
+ expect(new Optional<int>.of(7), isNot(equals(new Optional<int>.of(8))));
+ });
+
+ test('toString should show the value or absent', () {
+ expect(
+ new Optional<int>.of(7).toString(), equals('Optional { value: 7 }'));
+ expect(new Optional<int>.fromNullable(null).toString(),
+ equals('Optional { absent }'));
+ });
+ });
+}
« no previous file with comments | « packages/quiver/test/core/hash_test.dart ('k') | packages/quiver/test/io_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698