Skip to content

Commit

Permalink
clean up mocks in protobuf tests
Browse files Browse the repository at this point in the history
- move mock message to mock_util.dart and clean up
- use it in json_test and remove copied code
- bugfix: info_ should be a getter in a mixin.

BUG=
[email protected]

Review URL: https://chromiumcodereview.appspot.com//1247683002.
  • Loading branch information
Brian Slesinsky committed Jul 22, 2015
1 parent 0a75ca6 commit 66a2a31
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 118 deletions.
2 changes: 1 addition & 1 deletion lib/src/protobuf/mixins/map_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class PbMapMixin implements Map {

// GeneratedMessage properties and methods used by this mixin.

BuilderInfo info_;
BuilderInfo get info_;
void clear();
int getTagNumber(String fieldName);
getField(int tagNumber);
Expand Down
55 changes: 11 additions & 44 deletions test/json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
library json_test;

import 'dart:convert';
import 'package:protobuf/protobuf.dart';
import 'package:test/test.dart';

import 'mock_util.dart' show MockMessage;

class T extends MockMessage {
get className => "T";
T create() => new T();
}

main() {
T example = new T()
..a = 123
..b = "hello";
..val = 123
..str = "hello";

test('testWriteToJson', () {
String json = example.writeToJson();
Expand Down Expand Up @@ -42,45 +48,6 @@ checkJsonMap(Map m) {
}

checkMessage(T t) {
expect(t.a, 123);
expect(t.b, "hello");
}

/// This is generated code based on:
/// dart-protoc-plugin/test/protos/toplevel.proto
/// (Copied to avoid a circular dependency.)
///
/// message T {
/// optional int32 a = 1;
/// optional string b = 2;
/// }
class T extends GeneratedMessage {
static final BuilderInfo _i = new BuilderInfo('T')
..a(1, 'a', GeneratedMessage.O3)
..a(2, 'b', GeneratedMessage.OS)
..hasRequiredFields = false;

T() : super();
T.fromBuffer(List<int> i, [ExtensionRegistry r = ExtensionRegistry.EMPTY])
: super.fromBuffer(i, r);
T.fromJson(String i, [ExtensionRegistry r = ExtensionRegistry.EMPTY])
: super.fromJson(i, r);
T clone() => new T()..mergeFromMessage(this);
BuilderInfo get info_ => _i;
static T create() => new T();
static PbList<T> createRepeated() => new PbList<T>();

int get a => getField(1);
void set a(int v) {
setField(1, v);
}
bool hasA() => hasField(1);
void clearA() => clearField(1);

String get b => getField(2);
void set b(String v) {
setField(2, v);
}
bool hasB() => hasField(2);
void clearB() => clearField(2);
expect(t.val, 123);
expect(t.str, "hello");
}
81 changes: 8 additions & 73 deletions test/map_mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,18 @@ 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:test/test.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);
import 'mock_util.dart' show MockMessage;

@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}");
}
}
// A minimal protobuf implementation compatible with PbMapMixin.
class Rec extends MockMessage with MapMixin, PbMapMixin {
get className => "Rec";
Rec create() => new Rec();

@override
String toString() {
return "Rec(${val}, \"${str}\", ${child})";
}
String toString() => "Rec(${val}, \"${str}\")";
}

main() {
Expand All @@ -99,13 +34,13 @@ main() {
expect(r["val"], 0);
expect(r["str"], "");
expect(r["child"].runtimeType, Rec);
expect(r["child"].toString(), 'Rec(0, "", null)');
expect(r["child"].toString(), 'Rec(0, "")');

var v = r.values;
expect(v.length, 3);
expect(v.first, 0);
expect(v.toList()[1], "");
expect(v.last.toString(), 'Rec(0, "", null)');
expect(v.last.toString(), 'Rec(0, "")');
});

test('operator []= sets record fields', () {
Expand Down
35 changes: 35 additions & 0 deletions test/mock_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

library mock_util;

import 'package:protobuf/protobuf.dart'
show GeneratedMessage, PbMapMixin, BuilderInfo;

/// A minimal protobuf implementation for testing.
abstract class MockMessage extends GeneratedMessage {

BuilderInfo _infoCache;

// subclasses must provide these
String get className;
MockMessage create();

get val => getField(1);
set val(x) => setField(1, x);
get str => getField(2);
set str(x) => setField(2, x);
get child => getField(3);
set child(x) => setField(3, x);

@override
BuilderInfo get info_ {
if (_infoCache != null) return _infoCache;
_infoCache = new BuilderInfo(className)
..a(1, "val", GeneratedMessage.O3)
..a(2, "str", GeneratedMessage.OS)
..a(3, "child", GeneratedMessage.OM, create);
return _infoCache;
}
}

0 comments on commit 66a2a31

Please sign in to comment.