Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Unified Diff: test/names_test.dart

Issue 2043913005: Change how to set the Dart name of a field (Closed) Base URL: git@github.com:dart-lang/dart-protoc-plugin.git@master
Patch Set: better error checking, clean up pubspec Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/names_test.dart
diff --git a/test/names_test.dart b/test/names_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..e0ced0342472374646ac9dff96cbed4938488667
--- /dev/null
+++ b/test/names_test.dart
@@ -0,0 +1,92 @@
+// Copyright (c) 2016, 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 dart_name_test;
+
+import 'package:test/test.dart';
+import 'package:protoc_plugin/names.dart' as names;
+import 'package:protoc_plugin/src/descriptor.pb.dart';
+import 'package:protoc_plugin/src/dart_options.pb.dart';
+
+import '../out/protos/dart_name.pb.dart';
+
+Matcher throwsMessage(String msg) => throwsA(new _ToStringMatcher(equals(msg)));
+
+class _ToStringMatcher extends CustomMatcher {
+ _ToStringMatcher(Matcher matcher)
+ : super("object where toString() returns", "toString()", matcher);
+ featureValueOf(actual) => actual.toString();
+}
+
+void main() {
+ test('Can access a field that was renamed using dart_name option', () {
+ var msg = new DartName();
+ expect(msg.hasRenamedField(), false);
+ msg.renamedField = 'test';
+ expect(msg.hasRenamedField(), true);
+ expect(msg.renamedField, 'test');
+ msg.clearRenamedField();
+ expect(msg.hasRenamedField(), false);
+ });
+
+ test('Can swap field names using dart_name option', () {
+ var msg = new SwapNames();
+ msg.first = "one";
+ msg.second = "two";
+ expect(msg.getField(1), "two");
+ expect(msg.getField(2), "one");
+ });
+
+ test("Can take another field's name using dart_name option", () {
+ var msg = new TakeExistingName();
+ msg.first = "one";
+ 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.
+ });
+
+ test('Throws exception for dart_name option containing a space', () {
+ var descriptor = new DescriptorProto()
+ ..name = 'Example'
+ ..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.
+ expect(() {
+ names.messageFieldNames(descriptor);
+ },
+ throwsMessage("Example.first: dart_name option is invalid: "
+ "'hello world' is not a valid Dart field name"));
+ });
+
+ test('Throws exception for dart_name option set to reserved word', () {
+ var descriptor = new DescriptorProto()
+ ..name = 'Example'
+ ..field.addAll([stringField("first", 1, "class"),]);
+ expect(() {
+ names.messageFieldNames(descriptor);
+ },
+ throwsMessage("Example.first: "
+ "dart_name option is invalid: 'class' is already used"));
+ });
+
+ test('Throws exception for duplicate dart_name options', () {
+ var descriptor = new DescriptorProto()
+ ..name = 'Example'
+ ..field.addAll([
+ stringField("first", 1, "renamed"),
+ stringField("second", 2, "renamed"),
+ ]);
+ expect(() {
+ names.messageFieldNames(descriptor);
+ },
+ throwsMessage("Example.second: "
+ "dart_name option is invalid: 'renamed' is already used"));
+ });
+}
+
+FieldDescriptorProto stringField(String name, int number, String dartName) {
+ return new FieldDescriptorProto()
+ ..name = name
+ ..number = number
+ ..label = FieldDescriptorProto_Label.LABEL_OPTIONAL
+ ..type = FieldDescriptorProto_Type.TYPE_STRING
+ ..options =
+ (new FieldOptions()..setExtension(Dart_options.dartName, dartName));
+}

Powered by Google App Engine
This is Rietveld 408576698