OLD | NEW |
1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 // Unit tests for PbMapMixin. | 6 // Unit tests for PbMapMixin. |
7 // There are more tests in the dart-protoc-plugin package. | 7 // There are more tests in the dart-protoc-plugin package. |
8 library map_mixin_test; | 8 library map_mixin_test; |
9 | 9 |
10 import 'dart:collection' show MapMixin; | 10 import 'dart:collection' show MapMixin; |
11 | 11 |
12 import 'package:protobuf/src/protobuf/mixins/map_mixin.dart'; | 12 import 'package:protobuf/src/protobuf/mixins/map_mixin.dart'; |
13 import 'package:test/test.dart' show test, expect, predicate, same, throws; | 13 import 'package:test/test.dart' show test, expect, predicate, same, throws; |
14 | 14 |
15 import 'mock_util.dart' show MockMessage; | 15 import 'mock_util.dart' show MockMessage, mockInfo; |
16 | 16 |
17 // A minimal protobuf implementation compatible with PbMapMixin. | 17 // A minimal protobuf implementation compatible with PbMapMixin. |
18 class Rec extends MockMessage with MapMixin, PbMapMixin { | 18 class Rec extends MockMessage with MapMixin, PbMapMixin { |
19 get className => "Rec"; | 19 get info_ => _info; |
20 Rec create() => new Rec(); | 20 static final _info = mockInfo("Rec", () => new Rec()); |
21 | 21 |
22 @override | 22 @override |
23 String toString() => "Rec(${val}, \"${str}\")"; | 23 String toString() => "Rec(${val}, \"${str}\")"; |
24 } | 24 } |
25 | 25 |
26 main() { | 26 main() { |
27 test('PbMapMixin methods return default field values', () { | 27 test('PbMapMixin methods return default field values', () { |
28 var r = new Rec(); | 28 var r = new Rec(); |
29 | 29 |
30 expect(r.isEmpty, false); | 30 expect(r.isEmpty, false); |
(...skipping 27 matching lines...) Expand all Loading... |
58 var child = new Rec(); | 58 var child = new Rec(); |
59 r["child"] = child; | 59 r["child"] = child; |
60 expect(r.child, same(child)); | 60 expect(r.child, same(child)); |
61 expect(r["child"], same(child)); | 61 expect(r["child"], same(child)); |
62 | 62 |
63 expect(() => r["int32s"] = 123, throws); | 63 expect(() => r["int32s"] = 123, throws); |
64 r["int32s"].add(123); | 64 r["int32s"].add(123); |
65 expect(r["int32s"], [123]); | 65 expect(r["int32s"], [123]); |
66 expect(r["int32s"], same(r["int32s"])); | 66 expect(r["int32s"], same(r["int32s"])); |
67 }); | 67 }); |
| 68 |
| 69 test('operator== and hashCode work for Map mixin', () { |
| 70 var a = new Rec(); |
| 71 expect(a == a, true); |
| 72 expect(a == {}, false); |
| 73 expect({} == a, false); |
| 74 |
| 75 var b = new Rec(); |
| 76 expect(a.info_ == b.info_, true, reason: "BuilderInfo should be the same"); |
| 77 expect(a == b, true); |
| 78 expect(a.hashCode, b.hashCode); |
| 79 |
| 80 a.val = 123; |
| 81 expect(a == b, false); |
| 82 b.val = 123; |
| 83 expect(a == b, true); |
| 84 expect(a.hashCode, b.hashCode); |
| 85 |
| 86 a.child = new Rec(); |
| 87 expect(a == b, false); |
| 88 b.child = new Rec(); |
| 89 expect(a == b, true); |
| 90 expect(a.hashCode, b.hashCode); |
| 91 }); |
| 92 |
| 93 test("protobuf doesn't compare equal to a map with the same values", () { |
| 94 var a = new Rec(); |
| 95 expect(a == new Map.from(a), false); |
| 96 expect(new Map.from(a) == a, false); |
| 97 }); |
| 98 |
| 99 test("reading protobuf values shouldn't change equality", () { |
| 100 var a = new Rec(); |
| 101 var b = new Rec(); |
| 102 expect(a == b, true); |
| 103 new Map.from(a); |
| 104 expect(a == b, true); |
| 105 }); |
68 } | 106 } |
OLD | NEW |