OLD | NEW |
(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 library service_generator_test; |
| 7 |
| 8 import 'package:protoc_plugin/src/descriptor.pb.dart'; |
| 9 import 'package:protoc_plugin/src/plugin.pb.dart'; |
| 10 import 'package:protoc_plugin/protoc.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 ServiceDescriptorProto buildServiceDescriptor() { |
| 14 ServiceDescriptorProto sd = new ServiceDescriptorProto() |
| 15 ..name = 'Test' |
| 16 ..method.addAll([ |
| 17 new MethodDescriptorProto() |
| 18 ..name = 'AMethod' |
| 19 ..inputType = 'SomeRequest' |
| 20 ..outputType = 'SomeReply', |
| 21 new MethodDescriptorProto() |
| 22 ..name = 'AnotherMethod' |
| 23 ..inputType = '.foo.bar.EmptyMessage' |
| 24 ..outputType = '.foo.bar.AnotherReply', |
| 25 ]); |
| 26 return sd; |
| 27 } |
| 28 |
| 29 void main() { |
| 30 test('testServiceGenerator', () { |
| 31 // NOTE: Below > 80 cols because it is matching generated code > 80 cols. |
| 32 String expected = r''' |
| 33 abstract class TestServiceBase extends GeneratedService { |
| 34 Future<SomeReply> aMethod(ServerContext ctx, SomeRequest request); |
| 35 Future<AnotherReply> anotherMethod(ServerContext ctx, EmptyMessage request); |
| 36 |
| 37 GeneratedMessage createRequest(String method) { |
| 38 switch (method) { |
| 39 case 'AMethod': return new SomeRequest(); |
| 40 case 'AnotherMethod': return new EmptyMessage(); |
| 41 default: throw new ArgumentError('Unknown method: $method'); |
| 42 } |
| 43 } |
| 44 |
| 45 Future<GeneratedMessage> handleCall(ServerContext ctx, String method, Generate
dMessage request) async { |
| 46 switch (method) { |
| 47 case 'AMethod': return await aMethod(ctx, request); |
| 48 case 'AnotherMethod': return await anotherMethod(ctx, request); |
| 49 default: throw new ArgumentError('Unknown method: $method'); |
| 50 } |
| 51 } |
| 52 } |
| 53 |
| 54 '''; |
| 55 var options = parseGenerationOptions( |
| 56 new CodeGeneratorRequest(), new CodeGeneratorResponse()); |
| 57 var context = |
| 58 new GenerationContext(options, new DefaultOutputConfiguration()); |
| 59 var fd = new FileDescriptorProto(); |
| 60 var fg = new FileGenerator(fd, null, context); |
| 61 ServiceDescriptorProto sd = buildServiceDescriptor(); |
| 62 MemoryWriter buffer = new MemoryWriter(); |
| 63 IndentingWriter writer = new IndentingWriter(' ', buffer); |
| 64 var sg = new ServiceGenerator(sd, fg, context); |
| 65 sg.generate(writer); |
| 66 expect(buffer.toString(), expected); |
| 67 }); |
| 68 } |
OLD | NEW |