OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Tests for GeneratedMessage methods. | 5 /// Tests for GeneratedMessage methods. |
6 library message_test; | 6 library message_test; |
7 | 7 |
8 import 'package:test/test.dart' show test, expect, predicate, throwsA; | 8 import 'package:test/test.dart' show test, expect, predicate, throwsA; |
9 | 9 |
10 import 'mock_util.dart' show MockMessage; | 10 import 'mock_util.dart' show MockMessage, mockInfo; |
11 | 11 |
12 class Rec extends MockMessage { | 12 class Rec extends MockMessage { |
13 get className => "Rec"; | 13 get info_ => _info; |
14 Rec create() => new Rec(); | 14 static final _info = mockInfo("Rec", () => new Rec()); |
15 } | 15 } |
16 | 16 |
17 throwsError(Type expectedType, String expectedMessage) => throwsA( | 17 throwsError(Type expectedType, String expectedMessage) => |
18 predicate((x) { | 18 throwsA(predicate((x) { |
19 expect(x.runtimeType, expectedType); | 19 expect(x.runtimeType, expectedType); |
20 expect(x.message, expectedMessage); | 20 expect(x.message, expectedMessage); |
21 return true; | 21 return true; |
22 })); | 22 })); |
23 | 23 |
24 main() { | 24 main() { |
25 test('getField with invalid tag throws exception', () { | 25 test('getField with invalid tag throws exception', () { |
26 var r = new Rec(); | 26 var r = new Rec(); |
27 expect(() { | 27 expect(() { |
28 r.getField(123); | 28 r.getField(123); |
29 }, throwsError(ArgumentError, "tag 123 not defined in Rec")); | 29 }, throwsError(ArgumentError, "tag 123 not defined in Rec")); |
30 }); | 30 }); |
31 | 31 |
32 test('getDefaultForField with invalid tag throws exception', () { | 32 test('getDefaultForField with invalid tag throws exception', () { |
33 var r = new Rec(); | 33 var r = new Rec(); |
34 expect(() { | 34 expect(() { |
35 r.getDefaultForField(123); | 35 r.getDefaultForField(123); |
36 }, throwsError(ArgumentError, "tag 123 not defined in Rec")); | 36 }, throwsError(ArgumentError, "tag 123 not defined in Rec")); |
37 }); | 37 }); |
| 38 |
| 39 test('operator== and hashCode work for a simple record', () { |
| 40 var a = new Rec(); |
| 41 expect(a == a, true); |
| 42 |
| 43 var b = new Rec(); |
| 44 expect(a.info_ == b.info_, true, reason: "BuilderInfo should be the same"); |
| 45 expect(a == b, true); |
| 46 expect(a.hashCode, b.hashCode); |
| 47 |
| 48 a.val = 123; |
| 49 expect(a == b, false); |
| 50 b.val = 123; |
| 51 expect(a == b, true); |
| 52 expect(a.hashCode, b.hashCode); |
| 53 |
| 54 a.child = new Rec(); |
| 55 expect(a == b, false); |
| 56 b.child = new Rec(); |
| 57 expect(a == b, true); |
| 58 expect(a.hashCode, b.hashCode); |
| 59 }); |
38 } | 60 } |
OLD | NEW |