| 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[]== works 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 |
| 79 a.val = 123; |
| 80 expect(a == b, false); |
| 81 b.val = 123; |
| 82 expect(a == b, true); |
| 83 |
| 84 a.child = new Rec(); |
| 85 expect(a == b, false); |
| 86 b.child = new Rec(); |
| 87 expect(a == b, true); |
| 88 }); |
| 89 |
| 90 test("protobuf doesn't compare equal to a map with the same values", () { |
| 91 var a = new Rec(); |
| 92 expect(a == new Map.from(a), false); |
| 93 expect(new Map.from(a) == a, false); |
| 94 }); |
| 95 |
| 96 test("reading protobuf values shouldn't change equality", () { |
| 97 var a = new Rec(); |
| 98 var b = new Rec(); |
| 99 expect(a == b, true); |
| 100 new Map.from(a); |
| 101 expect(a == b, true); |
| 102 }); |
| 68 } | 103 } |
| OLD | NEW |