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

Unified 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 side-by-side diff with in-line comments
Download patch
« lib/mixins_meta.dart ('K') | « test/all_tests.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/map_mixin_test.dart
diff --git a/test/map_mixin_test.dart b/test/map_mixin_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..b0f9248c963e64c0ff56b7d233eb7b8f44941edc
--- /dev/null
+++ b/test/map_mixin_test.dart
@@ -0,0 +1,127 @@
+#!/usr/bin/env dart
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// Unit tests for PbMapMixin.
+// There are more tests in the dart-protoc-plugin package.
+library map_mixin_test;
+
+import 'dart:collection' show MapMixin;
+
+import 'package:protobuf/protobuf.dart'
+ show GeneratedMessage, PbMapMixin, BuilderInfo;
+import 'package:protobuf/src/protobuf/mixins/map_mixin.dart';
+import 'package:unittest/unittest.dart' show test, expect, predicate, same;
+
+// A minimal protobuf implementation compatible with PbMapMixin.
+class Rec extends GeneratedMessage with MapMixin, PbMapMixin {
+ int val = 0;
+ String str = "";
+ Rec child;
+
+ @override
+ BuilderInfo info_ = new BuilderInfo("rec")
+ ..a(1, "val", null)
+ ..a(2, "str", null)
+ ..a(3, "child", null);
+
+ @override
+ void clear() {
+ val = 0;
+ str = "";
+ child = null;
+ }
+
+ @override
+ int getTagNumber(String fieldName) {
+ switch (fieldName) {
+ case 'val':
+ return 1;
+ case 'str':
+ return 2;
+ case 'child':
+ return 3;
+ default:
+ return null;
+ }
+ }
+
+ @override
+ getField(int tagNumber) {
+ switch (tagNumber) {
+ case 1:
+ return val;
+ case 2:
+ return str;
+ case 3:
+ // lazy initializaton
+ if (child == null) {
+ child = new Rec();
+ }
+ return child;
+ default:
+ return null;
+ }
+ }
+
+ @override
+ void setField(int tagNumber, var value, [int fieldType = null]) {
+ switch (tagNumber) {
+ case 1:
+ val = value;
+ return;
+ case 2:
+ str = value;
+ return;
+ case 3:
+ child = value;
+ return;
+ default:
+ throw new ArgumentError("Rec doesn't support tag: ${tagNumber}");
+ }
+ }
+
+ @override
+ String toString() {
+ return "Rec(${val}, \"${str}\", ${child})";
+ }
+}
+
+main() {
+ test('PbMapMixin methods return default field values', () {
+ var r = new Rec();
+
+ expect(r.isEmpty, false);
+ expect(r.isNotEmpty, true);
+ expect(r.keys, ["val", "str", "child"]);
+
+ expect(r["val"], 0);
+ expect(r["str"], "");
+ expect(r["child"].runtimeType, Rec);
+ expect(r["child"].toString(), 'Rec(0, "", null)');
+
+ var v = r.values;
+ expect(v.length, 3);
+ expect(v.first, 0);
+ expect(v.toList()[1], "");
+ expect(v.last.toString(), 'Rec(0, "", null)');
+ });
+
+ test('operator []= sets record fields', () {
+ var r = new Rec();
+
+ r["val"] = 123;
+ expect(r.val, 123);
+ expect(r["val"], 123);
+
+ r["str"] = "hello";
+ expect(r.str, "hello");
+ expect(r["str"], "hello");
+
+ var child = new Rec();
+ r["child"] = child;
+ expect(r.child, same(child));
+ expect(r["child"], same(child));
+ });
+}
« 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