OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env dart | |
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 | |
4 // BSD-style license that can be found in the LICENSE file. | |
5 | |
6 // Unit tests for PbMapMixin. | |
7 // There are more tests in the dart-protoc-plugin package. | |
8 library map_mixin_test; | |
9 | |
10 import 'package:protobuf/protobuf.dart' show GeneratedMessage, PbMapMixin, Build erInfo; | |
11 import 'package:unittest/unittest.dart' show test, expect, predicate, same; | |
12 | |
13 // A minimal protobuf implementation compatible with PbMapMixin. | |
14 class Rec extends GeneratedMessage with PbMapMixin { | |
15 int val = 0; | |
16 String str = ""; | |
17 Rec child; | |
18 | |
19 @override | |
20 BuilderInfo info_ = new BuilderInfo("rec") | |
21 ..a(1, "val", null) | |
22 ..a(2, "str", null) | |
23 ..a(3, "child", null); | |
24 | |
25 @override | |
26 void clear() { | |
27 val = 0; | |
28 str = ""; | |
29 child = null; | |
30 } | |
31 | |
32 @override | |
33 int getTagNumber(String fieldName) { | |
34 switch (fieldName) { | |
35 case 'val': return 1; | |
36 case 'str': return 2; | |
37 case 'child': return 3; | |
38 default: return null; | |
39 } | |
40 } | |
41 | |
42 @override | |
43 getField(int tagNumber) { | |
44 switch (tagNumber) { | |
45 case 1: return val; | |
46 case 2: return str; | |
47 case 3: | |
48 // lazy initializaton | |
49 if (child == null) { | |
50 child = new Rec(); | |
51 } | |
52 return child; | |
53 default: return null; | |
54 } | |
55 } | |
56 | |
57 @override | |
58 void setField(int tagNumber, var value, [int fieldType = null]) { | |
59 switch (tagNumber) { | |
60 case 1: val = value; return; | |
61 case 2: str = value; return; | |
62 case 3: child = value; return; | |
63 default: throw ArgumentError("Rec doesn't support tag: ${tagNumber}"); | |
64 } | |
65 } | |
66 | |
67 @override | |
68 String toString() { | |
69 return "Rec(${val}, \"${str}\", ${child})"; | |
70 } | |
71 } | |
72 | |
73 main() { | |
74 test('PbMapMixin methods return default field values', () { | |
75 var r = new Rec(); | |
76 | |
77 expect(r.isEmpty, false); | |
78 expect(r.isNotEmpty, true); | |
79 expect(r.keys, ["val", "str", "child"]); | |
80 | |
81 expect(r["val"], 0); | |
82 expect(r["str"], ""); | |
83 expect(r["child"].runtimeType, Rec); | |
84 expect(r["child"].toString(), 'Rec(0, "", null)'); | |
85 | |
86 var v = r.values; | |
87 expect(v.length, 3); | |
88 expect(v.first, 0); | |
89 expect(v.toList()[1], ""); | |
90 expect(v.last.toString(), 'Rec(0, "", null)'); | |
91 }); | |
92 | |
93 test('operator []= sets record fields', () { | |
94 var r = new Rec(); | |
95 | |
96 r["val"] = 123; | |
97 expect(r.val, 123); | |
98 expect(r["val"], 123); | |
99 | |
100 r["str"] = "hello"; | |
101 expect(r.str, "hello"); | |
102 expect(r["str"], "hello"); | |
103 | |
104 var child = new Rec(); | |
105 r["child"] = child; | |
106 expect(r.child, same(child)); | |
107 expect(r["child"], same(child)); | |
108 }); | |
Søren Gjesse
2015/06/22 07:25:01
Test iterators as well, and test that concurrent m
skybrian
2015/06/23 02:35:34
Obsolete because this mixin is "inheriting" MapMix
| |
109 } | |
OLD | NEW |