| 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 library mock_util; | 5 library mock_util; |
| 6 | 6 |
| 7 import 'package:protobuf/protobuf.dart' | 7 import 'package:protobuf/protobuf.dart' |
| 8 show GeneratedMessage, BuilderInfo, PbFieldType; | 8 show GeneratedMessage, BuilderInfo, CreateBuilderFunc, PbFieldType; |
| 9 |
| 10 BuilderInfo mockInfo(String className, CreateBuilderFunc create) { |
| 11 return new BuilderInfo(className) |
| 12 ..a(1, "val", PbFieldType.O3, 42) |
| 13 ..a(2, "str", PbFieldType.OS) |
| 14 ..a(3, "child", PbFieldType.OM, create, create) |
| 15 ..p(4, "int32s", PbFieldType.P3); |
| 16 } |
| 9 | 17 |
| 10 /// A minimal protobuf implementation for testing. | 18 /// A minimal protobuf implementation for testing. |
| 11 abstract class MockMessage extends GeneratedMessage { | 19 abstract class MockMessage extends GeneratedMessage { |
| 12 BuilderInfo _infoCache; | |
| 13 | |
| 14 // subclasses must provide these | 20 // subclasses must provide these |
| 15 String get className; | 21 BuilderInfo get info_; |
| 16 MockMessage create(); | |
| 17 | 22 |
| 18 int get val => $_get(0, 1, 42); | 23 int get val => $_get(0, 1, 42); |
| 19 set val(x) => setField(1, x); | 24 set val(x) => setField(1, x); |
| 20 | 25 |
| 21 String get str => $_get(1, 2, ""); | 26 String get str => $_get(1, 2, ""); |
| 22 set str(x) => $_setString(1, 2, x); | 27 set str(x) => $_setString(1, 2, x); |
| 23 | 28 |
| 24 MockMessage get child => $_get(2, 3, null); | 29 MockMessage get child => $_get(2, 3, null); |
| 25 set child(x) => setField(3, x); | 30 set child(x) => setField(3, x); |
| 26 | 31 |
| 27 List<int> get int32s => $_get(3, 4, null); | 32 List<int> get int32s => $_get(3, 4, null); |
| 28 | 33 |
| 29 @override | 34 clone() { |
| 30 BuilderInfo get info_ { | 35 CreateBuilderFunc create = info_.byName["child"].subBuilder; |
| 31 if (_infoCache != null) return _infoCache; | 36 return create()..mergeFromMessage(this); |
| 32 _infoCache = new BuilderInfo(className) | |
| 33 ..a(1, "val", PbFieldType.O3, 42) | |
| 34 ..a(2, "str", PbFieldType.OS) | |
| 35 ..a(3, "child", PbFieldType.OM, create, create) | |
| 36 ..p(4, "int32s", PbFieldType.P3); | |
| 37 return _infoCache; | |
| 38 } | 37 } |
| 39 | |
| 40 clone() => create()..mergeFromMessage(this); | |
| 41 } | 38 } |
| OLD | NEW |