Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: test/map_mixin_test.dart

Issue 1196773004: changes for 0.3.9 (Closed) Base URL: https://github.com/dart-lang/dart-protobuf.git@master
Patch Set: add mixins_meta, move PbMapMixin to separate library Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« lib/mixins_meta.dart ('K') | « test/all_tests.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 'dart:collection' show MapMixin;
11
12 import 'package:protobuf/protobuf.dart'
13 show GeneratedMessage, PbMapMixin, BuilderInfo;
14 import 'package:protobuf/src/protobuf/mixins/map_mixin.dart';
15 import 'package:unittest/unittest.dart' show test, expect, predicate, same;
16
17 // A minimal protobuf implementation compatible with PbMapMixin.
18 class Rec extends GeneratedMessage with MapMixin, PbMapMixin {
19 int val = 0;
20 String str = "";
21 Rec child;
22
23 @override
24 BuilderInfo info_ = new BuilderInfo("rec")
25 ..a(1, "val", null)
26 ..a(2, "str", null)
27 ..a(3, "child", null);
28
29 @override
30 void clear() {
31 val = 0;
32 str = "";
33 child = null;
34 }
35
36 @override
37 int getTagNumber(String fieldName) {
38 switch (fieldName) {
39 case 'val':
40 return 1;
41 case 'str':
42 return 2;
43 case 'child':
44 return 3;
45 default:
46 return null;
47 }
48 }
49
50 @override
51 getField(int tagNumber) {
52 switch (tagNumber) {
53 case 1:
54 return val;
55 case 2:
56 return str;
57 case 3:
58 // lazy initializaton
59 if (child == null) {
60 child = new Rec();
61 }
62 return child;
63 default:
64 return null;
65 }
66 }
67
68 @override
69 void setField(int tagNumber, var value, [int fieldType = null]) {
70 switch (tagNumber) {
71 case 1:
72 val = value;
73 return;
74 case 2:
75 str = value;
76 return;
77 case 3:
78 child = value;
79 return;
80 default:
81 throw new ArgumentError("Rec doesn't support tag: ${tagNumber}");
82 }
83 }
84
85 @override
86 String toString() {
87 return "Rec(${val}, \"${str}\", ${child})";
88 }
89 }
90
91 main() {
92 test('PbMapMixin methods return default field values', () {
93 var r = new Rec();
94
95 expect(r.isEmpty, false);
96 expect(r.isNotEmpty, true);
97 expect(r.keys, ["val", "str", "child"]);
98
99 expect(r["val"], 0);
100 expect(r["str"], "");
101 expect(r["child"].runtimeType, Rec);
102 expect(r["child"].toString(), 'Rec(0, "", null)');
103
104 var v = r.values;
105 expect(v.length, 3);
106 expect(v.first, 0);
107 expect(v.toList()[1], "");
108 expect(v.last.toString(), 'Rec(0, "", null)');
109 });
110
111 test('operator []= sets record fields', () {
112 var r = new Rec();
113
114 r["val"] = 123;
115 expect(r.val, 123);
116 expect(r["val"], 123);
117
118 r["str"] = "hello";
119 expect(r.str, "hello");
120 expect(r["str"], "hello");
121
122 var child = new Rec();
123 r["child"] = child;
124 expect(r.child, same(child));
125 expect(r["child"], same(child));
126 });
127 }
OLDNEW
« lib/mixins_meta.dart ('K') | « test/all_tests.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698