OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 library quiver.core.optional_test; |
| 16 |
| 17 import 'package:quiver/core.dart'; |
| 18 import 'package:test/test.dart'; |
| 19 |
| 20 main() { |
| 21 group('Optional', () { |
| 22 test('absent should be not present and not gettable', () { |
| 23 expect(new Optional<int>.absent().isPresent, isFalse); |
| 24 expect(() => new Optional<int>.absent().value, throws); |
| 25 }); |
| 26 |
| 27 test('of should return value', () { |
| 28 expect(new Optional<int>.of(7).value, 7); |
| 29 }); |
| 30 |
| 31 test('isPresent should execute only if present', () { |
| 32 int value; |
| 33 new Optional<int>.of(7).ifPresent((v) { |
| 34 value = v; |
| 35 }); |
| 36 expect(value, 7); |
| 37 new Optional<int>.absent().ifPresent((v) { |
| 38 value = v; |
| 39 }); |
| 40 expect(value, 7); |
| 41 }); |
| 42 |
| 43 test('isAbsent should execute only if absent', () { |
| 44 int value; |
| 45 new Optional<int>.of(7).ifAbsent(() { |
| 46 value = 7; |
| 47 }); |
| 48 expect(value, null); |
| 49 new Optional<int>.absent().ifAbsent(() { |
| 50 value = 7; |
| 51 }); |
| 52 expect(value, 7); |
| 53 }); |
| 54 |
| 55 test('fromNullable should allow present or absent', () { |
| 56 expect(new Optional<int>.fromNullable(7).value, 7); |
| 57 expect(new Optional<int>.fromNullable(null).isPresent, isFalse); |
| 58 }); |
| 59 |
| 60 test('or should return present and replace absent', () { |
| 61 expect(new Optional<int>.fromNullable(7).or(13), 7); |
| 62 expect(new Optional<int>.fromNullable(null).or(13), 13); |
| 63 }); |
| 64 |
| 65 test('orNull should return value if present or null if absent', () { |
| 66 expect(new Optional<int>.fromNullable(7).orNull, isNotNull); |
| 67 expect(new Optional<int>.fromNullable(null).orNull, isNull); |
| 68 }); |
| 69 |
| 70 test('transform should return transformed value or absent', () { |
| 71 expect(new Optional<int>.fromNullable(7).transform((a) => a + 1), |
| 72 equals(new Optional<int>.of(8))); |
| 73 expect(new Optional<int>.fromNullable(null) |
| 74 .transform((a) => a + 1).isPresent, isFalse); |
| 75 }); |
| 76 |
| 77 test('hashCode should allow optionals to be in hash sets', () { |
| 78 expect(new Set.from([ |
| 79 new Optional<int>.of(7), |
| 80 new Optional<int>.of(8), |
| 81 new Optional<int>.absent() |
| 82 ]), equals(new Set.from([ |
| 83 new Optional<int>.of(7), |
| 84 new Optional<int>.of(8), |
| 85 new Optional<int>.absent() |
| 86 ]))); |
| 87 expect(new Set.from([ |
| 88 new Optional<int>.of(7), |
| 89 new Optional<int>.of(8) |
| 90 ]), isNot(equals( |
| 91 new Set.from([new Optional<int>.of(7), new Optional<int>.of(9)])))); |
| 92 }); |
| 93 |
| 94 test('== should compare by value', () { |
| 95 expect(new Optional<int>.of(7), equals(new Optional<int>.of(7))); |
| 96 expect(new Optional<int>.fromNullable(null), |
| 97 equals(new Optional<int>.fromNullable(null))); |
| 98 expect(new Optional<int>.fromNullable(null), |
| 99 isNot(equals(new Optional<int>.of(7)))); |
| 100 expect(new Optional<int>.of(7), isNot(equals(new Optional<int>.of(8)))); |
| 101 }); |
| 102 |
| 103 test('toString should show the value or absent', () { |
| 104 expect( |
| 105 new Optional<int>.of(7).toString(), equals('Optional { value: 7 }')); |
| 106 expect(new Optional<int>.fromNullable(null).toString(), |
| 107 equals('Optional { absent }')); |
| 108 }); |
| 109 }); |
| 110 } |
OLD | NEW |