OLD | NEW |
1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 }); | 63 }); |
64 | 64 |
65 test('orNull should return value if present or null if absent', () { | 65 test('orNull should return value if present or null if absent', () { |
66 expect(new Optional<int>.fromNullable(7).orNull, isNotNull); | 66 expect(new Optional<int>.fromNullable(7).orNull, isNotNull); |
67 expect(new Optional<int>.fromNullable(null).orNull, isNull); | 67 expect(new Optional<int>.fromNullable(null).orNull, isNull); |
68 }); | 68 }); |
69 | 69 |
70 test('transform should return transformed value or absent', () { | 70 test('transform should return transformed value or absent', () { |
71 expect(new Optional<int>.fromNullable(7).transform((a) => a + 1), | 71 expect(new Optional<int>.fromNullable(7).transform((a) => a + 1), |
72 equals(new Optional<int>.of(8))); | 72 equals(new Optional<int>.of(8))); |
73 expect(new Optional<int>.fromNullable(null) | 73 expect( |
74 .transform((a) => a + 1).isPresent, isFalse); | 74 new Optional<int>.fromNullable(null) |
| 75 .transform((a) => a + 1) |
| 76 .isPresent, |
| 77 isFalse); |
75 }); | 78 }); |
76 | 79 |
77 test('hashCode should allow optionals to be in hash sets', () { | 80 test('hashCode should allow optionals to be in hash sets', () { |
78 expect(new Set.from([ | 81 expect( |
79 new Optional<int>.of(7), | 82 new Set.from([ |
80 new Optional<int>.of(8), | 83 new Optional<int>.of(7), |
81 new Optional<int>.absent() | 84 new Optional<int>.of(8), |
82 ]), equals(new Set.from([ | 85 new Optional<int>.absent() |
83 new Optional<int>.of(7), | 86 ]), |
84 new Optional<int>.of(8), | 87 equals(new Set.from([ |
85 new Optional<int>.absent() | 88 new Optional<int>.of(7), |
86 ]))); | 89 new Optional<int>.of(8), |
87 expect(new Set.from([ | 90 new Optional<int>.absent() |
88 new Optional<int>.of(7), | 91 ]))); |
89 new Optional<int>.of(8) | 92 expect( |
90 ]), isNot(equals( | 93 new Set.from([new Optional<int>.of(7), new Optional<int>.of(8)]), |
91 new Set.from([new Optional<int>.of(7), new Optional<int>.of(9)])))); | 94 isNot(equals(new Set.from( |
| 95 [new Optional<int>.of(7), new Optional<int>.of(9)])))); |
92 }); | 96 }); |
93 | 97 |
94 test('== should compare by value', () { | 98 test('== should compare by value', () { |
95 expect(new Optional<int>.of(7), equals(new Optional<int>.of(7))); | 99 expect(new Optional<int>.of(7), equals(new Optional<int>.of(7))); |
96 expect(new Optional<int>.fromNullable(null), | 100 expect(new Optional<int>.fromNullable(null), |
97 equals(new Optional<int>.fromNullable(null))); | 101 equals(new Optional<int>.fromNullable(null))); |
98 expect(new Optional<int>.fromNullable(null), | 102 expect(new Optional<int>.fromNullable(null), |
99 isNot(equals(new Optional<int>.of(7)))); | 103 isNot(equals(new Optional<int>.of(7)))); |
100 expect(new Optional<int>.of(7), isNot(equals(new Optional<int>.of(8)))); | 104 expect(new Optional<int>.of(7), isNot(equals(new Optional<int>.of(8)))); |
101 }); | 105 }); |
102 | 106 |
103 test('toString should show the value or absent', () { | 107 test('toString should show the value or absent', () { |
104 expect( | 108 expect( |
105 new Optional<int>.of(7).toString(), equals('Optional { value: 7 }')); | 109 new Optional<int>.of(7).toString(), equals('Optional { value: 7 }')); |
106 expect(new Optional<int>.fromNullable(null).toString(), | 110 expect(new Optional<int>.fromNullable(null).toString(), |
107 equals('Optional { absent }')); | 111 equals('Optional { absent }')); |
108 }); | 112 }); |
| 113 |
| 114 test('length when absent should return 0', () { |
| 115 expect(const Optional.absent().length, equals(0)); |
| 116 }); |
| 117 |
| 118 test('length when present should return 1', () { |
| 119 expect(new Optional<int>.of(1).length, equals(1)); |
| 120 }); |
| 121 |
| 122 test('expand should behave as equivalent iterable', () { |
| 123 final optionals = <Optional<int>>[ |
| 124 new Optional<int>.of(1), |
| 125 const Optional.absent(), |
| 126 new Optional<int>.of(2) |
| 127 ].expand((i) => i); |
| 128 expect(optionals, orderedEquals([1, 2])); |
| 129 }); |
109 }); | 130 }); |
110 } | 131 } |
OLD | NEW |