Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 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. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 library file_generator_test; | 6 library file_generator_test; |
| 7 | 7 |
| 8 import 'package:protoc_plugin/indenting_writer.dart'; | 8 import 'package:protoc_plugin/indenting_writer.dart'; |
| 9 import 'package:protoc_plugin/src/descriptor.pb.dart'; | 9 import 'package:protoc_plugin/src/descriptor.pb.dart'; |
| 10 import 'package:protoc_plugin/src/plugin.pb.dart'; | 10 import 'package:protoc_plugin/src/plugin.pb.dart'; |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 new CodeGeneratorRequest(), new CodeGeneratorResponse()); | 290 new CodeGeneratorRequest(), new CodeGeneratorResponse()); |
| 291 | 291 |
| 292 FileGenerator fg = new FileGenerator(fd); | 292 FileGenerator fg = new FileGenerator(fd); |
| 293 link(options, [fg]); | 293 link(options, [fg]); |
| 294 | 294 |
| 295 var writer = new IndentingWriter(); | 295 var writer = new IndentingWriter(); |
| 296 fg.writeMainHeader(writer); | 296 fg.writeMainHeader(writer); |
| 297 expect(writer.toString(), expected); | 297 expect(writer.toString(), expected); |
| 298 }); | 298 }); |
| 299 | 299 |
| 300 test('FileGenerator outputs extra imports for a service', () { | 300 test('FileGenerator outputs files for a service', () { |
| 301 String expected = r''' | 301 String expectedClient = r''' |
| 302 /// | 302 /// |
| 303 // Generated code. Do not modify. | 303 // Generated code. Do not modify. |
| 304 /// | 304 /// |
| 305 library test; | 305 library test; |
| 306 | 306 |
| 307 import 'dart:async'; | 307 import 'dart:async'; |
| 308 | 308 |
| 309 import 'package:protobuf/protobuf.dart'; | 309 import 'package:protobuf/protobuf.dart'; |
| 310 | 310 |
| 311 class Empty extends GeneratedMessage { | |
| 312 static final BuilderInfo _i = new BuilderInfo('Empty') | |
| 313 ..hasRequiredFields = false | |
| 314 ; | |
| 315 | |
| 316 Empty() : super(); | |
| 317 Empty.fromBuffer(List<int> i, [ExtensionRegistry r = ExtensionRegistry.EMPTY]) : super.fromBuffer(i, r); | |
| 318 Empty.fromJson(String i, [ExtensionRegistry r = ExtensionRegistry.EMPTY]) : su per.fromJson(i, r); | |
| 319 Empty clone() => new Empty()..mergeFromMessage(this); | |
| 320 BuilderInfo get info_ => _i; | |
| 321 static Empty create() => new Empty(); | |
| 322 static PbList<Empty> createRepeated() => new PbList<Empty>(); | |
| 323 static Empty getDefault() { | |
| 324 if (_defaultInstance == null) _defaultInstance = new _ReadonlyEmpty(); | |
| 325 return _defaultInstance; | |
| 326 } | |
| 327 static Empty _defaultInstance; | |
| 328 static void $checkItem(Empty v) { | |
| 329 if (v is !Empty) checkItemFailed(v, 'Empty'); | |
| 330 } | |
| 331 } | |
| 332 | |
| 333 class _ReadonlyEmpty extends Empty with ReadonlyMessageMixin {} | |
| 334 | |
| 335 class TestApi { | |
|
Søren Gjesse
2016/05/27 07:34:35
Have you considered to also split out the client s
skybrian
2016/05/27 18:27:50
I considered it but I'm not sure it would accompli
| |
| 336 RpcClient _client; | |
| 337 TestApi(this._client); | |
| 338 | |
| 339 Future<Empty> ping(ClientContext ctx, Empty request) { | |
| 340 var emptyResponse = new Empty(); | |
| 341 return _client.invoke(ctx, 'Test', 'Ping', request, emptyResponse); | |
| 342 } | |
| 343 } | |
| 344 | |
| 345 '''; | |
| 346 | |
| 347 String expectedServer = r''' | |
| 348 /// | |
| 349 // Generated code. Do not modify. | |
| 350 /// | |
| 351 library test_pbserver; | |
| 352 | |
| 353 import 'dart:async'; | |
| 354 | |
| 355 import 'package:protobuf/protobuf.dart'; | |
| 356 | |
| 357 import 'test.pb.dart'; | |
| 358 | |
| 311 import 'test.pbjson.dart'; | 359 import 'test.pbjson.dart'; |
| 312 | 360 |
| 361 abstract class TestServiceBase extends GeneratedService { | |
| 362 Future<Empty> ping(ServerContext ctx, Empty request); | |
| 363 | |
| 364 GeneratedMessage createRequest(String method) { | |
| 365 switch (method) { | |
| 366 case 'Ping': return new Empty(); | |
| 367 default: throw new ArgumentError('Unknown method: $method'); | |
| 368 } | |
| 369 } | |
| 370 | |
| 371 Future<GeneratedMessage> handleCall(ServerContext ctx, String method, Generate dMessage request) { | |
| 372 switch (method) { | |
| 373 case 'Ping': return ping(ctx, request); | |
| 374 default: throw new ArgumentError('Unknown method: $method'); | |
| 375 } | |
| 376 } | |
| 377 | |
| 378 Map<String, dynamic> get $json => Test$json; | |
| 379 Map<String, dynamic> get $messageJson => Test$messageJson; | |
| 380 } | |
| 381 | |
| 313 '''; | 382 '''; |
| 383 | |
| 384 DescriptorProto empty = new DescriptorProto()..name = "Empty"; | |
| 385 | |
| 386 ServiceDescriptorProto sd = new ServiceDescriptorProto() | |
| 387 ..name = 'Test' | |
| 388 ..method.add(new MethodDescriptorProto() | |
| 389 ..name = 'Ping' | |
| 390 ..inputType = '.Empty' | |
| 391 ..outputType = '.Empty'); | |
| 392 | |
| 314 FileDescriptorProto fd = new FileDescriptorProto() | 393 FileDescriptorProto fd = new FileDescriptorProto() |
| 315 ..name = 'test' | 394 ..name = 'test' |
| 316 ..service.add(new ServiceDescriptorProto()); | 395 ..messageType.add(empty) |
| 396 ..service.add(sd); | |
| 317 | 397 |
| 318 var options = parseGenerationOptions( | 398 var options = parseGenerationOptions( |
| 319 new CodeGeneratorRequest(), new CodeGeneratorResponse()); | 399 new CodeGeneratorRequest(), new CodeGeneratorResponse()); |
| 320 | 400 |
| 321 FileGenerator fg = new FileGenerator(fd); | 401 FileGenerator fg = new FileGenerator(fd); |
| 322 link(options, [fg]); | 402 link(options, [fg]); |
| 323 | 403 |
| 324 var writer = new IndentingWriter(); | 404 var writer = new IndentingWriter(); |
| 325 fg.writeMainHeader(writer); | 405 fg.writeMainHeader(writer); |
| 326 expect(writer.toString(), expected); | 406 expect(fg.generateMainFile(), expectedClient); |
| 407 expect(fg.generateServerFile(), expectedServer); | |
| 327 }); | 408 }); |
| 328 | 409 |
| 329 test('FileGenerator handles field_name options', () { | 410 test('FileGenerator handles field_name options', () { |
| 330 // NOTE: Below > 80 cols because it is matching generated code > 80 cols. | 411 // NOTE: Below > 80 cols because it is matching generated code > 80 cols. |
| 331 String expected = r''' | 412 String expected = r''' |
| 332 /// | 413 /// |
| 333 // Generated code. Do not modify. | 414 // Generated code. Do not modify. |
| 334 /// | 415 /// |
| 335 library test; | 416 library test; |
| 336 | 417 |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 559 var request = new CodeGeneratorRequest(); | 640 var request = new CodeGeneratorRequest(); |
| 560 var response = new CodeGeneratorResponse(); | 641 var response = new CodeGeneratorResponse(); |
| 561 var options = parseGenerationOptions(request, response); | 642 var options = parseGenerationOptions(request, response); |
| 562 | 643 |
| 563 FileGenerator fg = new FileGenerator(fd); | 644 FileGenerator fg = new FileGenerator(fd); |
| 564 link(options, [fg, new FileGenerator(fd1), new FileGenerator(fd2)]); | 645 link(options, [fg, new FileGenerator(fd1), new FileGenerator(fd2)]); |
| 565 expect(fg.generateMainFile(), expected); | 646 expect(fg.generateMainFile(), expected); |
| 566 expect(fg.generateJsonFile(), expectedJson); | 647 expect(fg.generateJsonFile(), expectedJson); |
| 567 }); | 648 }); |
| 568 } | 649 } |
| OLD | NEW |