OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library dart_name_test; | |
6 | |
7 import 'package:test/test.dart'; | |
8 import 'package:protoc_plugin/names.dart' as names; | |
9 import 'package:protoc_plugin/src/descriptor.pb.dart'; | |
10 import 'package:protoc_plugin/src/dart_options.pb.dart'; | |
11 | |
12 import '../out/protos/dart_name.pb.dart'; | |
13 | |
14 Matcher throwsMessage(String msg) => throwsA(new _ToStringMatcher(equals(msg))); | |
15 | |
16 class _ToStringMatcher extends CustomMatcher { | |
17 _ToStringMatcher(Matcher matcher) | |
18 : super("object where toString() returns", "toString()", matcher); | |
19 featureValueOf(actual) => actual.toString(); | |
20 } | |
21 | |
22 void main() { | |
23 test('Can access a field that was renamed using dart_name option', () { | |
24 var msg = new DartName(); | |
25 expect(msg.hasRenamedField(), false); | |
26 msg.renamedField = 'test'; | |
27 expect(msg.hasRenamedField(), true); | |
28 expect(msg.renamedField, 'test'); | |
29 msg.clearRenamedField(); | |
30 expect(msg.hasRenamedField(), false); | |
31 }); | |
32 | |
33 test('Can swap field names using dart_name option', () { | |
34 var msg = new SwapNames(); | |
35 msg.first = "one"; | |
36 msg.second = "two"; | |
37 expect(msg.getField(1), "two"); | |
38 expect(msg.getField(2), "one"); | |
39 }); | |
40 | |
41 test("Can take another field's name using dart_name option", () { | |
42 var msg = new TakeExistingName(); | |
43 msg.first = "one"; | |
44 expect(msg.getField(2), "one"); | |
Søren Gjesse
2016/06/09 09:07:13
Also test the original first field (called first_1
skybrian
2016/06/09 19:10:47
Done.
| |
45 }); | |
46 | |
47 test('Throws exception for dart_name option containing a space', () { | |
48 var descriptor = new DescriptorProto() | |
49 ..name = 'Example' | |
50 ..field.addAll([stringField("first", 1, "hello world"),]); | |
frederikmutzel
2016/06/09 07:50:28
just add? (also below)
skybrian
2016/06/09 19:10:47
Done.
| |
51 expect(() { | |
52 names.messageFieldNames(descriptor); | |
53 }, | |
54 throwsMessage("Example.first: dart_name option is invalid: " | |
55 "'hello world' is not a valid Dart field name")); | |
56 }); | |
57 | |
58 test('Throws exception for dart_name option set to reserved word', () { | |
59 var descriptor = new DescriptorProto() | |
60 ..name = 'Example' | |
61 ..field.addAll([stringField("first", 1, "class"),]); | |
62 expect(() { | |
63 names.messageFieldNames(descriptor); | |
64 }, | |
65 throwsMessage("Example.first: " | |
66 "dart_name option is invalid: 'class' is already used")); | |
67 }); | |
68 | |
69 test('Throws exception for duplicate dart_name options', () { | |
70 var descriptor = new DescriptorProto() | |
71 ..name = 'Example' | |
72 ..field.addAll([ | |
73 stringField("first", 1, "renamed"), | |
74 stringField("second", 2, "renamed"), | |
75 ]); | |
76 expect(() { | |
77 names.messageFieldNames(descriptor); | |
78 }, | |
79 throwsMessage("Example.second: " | |
80 "dart_name option is invalid: 'renamed' is already used")); | |
81 }); | |
82 } | |
83 | |
84 FieldDescriptorProto stringField(String name, int number, String dartName) { | |
85 return new FieldDescriptorProto() | |
86 ..name = name | |
87 ..number = number | |
88 ..label = FieldDescriptorProto_Label.LABEL_OPTIONAL | |
89 ..type = FieldDescriptorProto_Type.TYPE_STRING | |
90 ..options = | |
91 (new FieldOptions()..setExtension(Dart_options.dartName, dartName)); | |
92 } | |
OLD | NEW |