blob: 752d9a0c6ef6de4618e40fad2a7d87101ba203b0 [file] [log] [blame]
csharptest71f662c2011-05-20 15:15:34 -05001#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc. All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15// * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19// * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37using Google.ProtocolBuffers.DescriptorProtos;
38using Google.ProtocolBuffers.Descriptors;
csharptesteac64a52011-10-04 13:43:26 -050039using Microsoft.VisualStudio.TestTools.UnitTesting;
csharptest71f662c2011-05-20 15:15:34 -050040
41namespace Google.ProtocolBuffers
42{
csharptesteac64a52011-10-04 13:43:26 -050043 [TestClass]
csharptest71f662c2011-05-20 15:15:34 -050044 public class DescriptorUtilTest
45 {
csharptesteac64a52011-10-04 13:43:26 -050046 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050047 public void ExplicitNamespace()
48 {
49 FileDescriptorProto proto = new FileDescriptorProto.Builder
50 {
51 Name = "x",
52 Package = "pack",
53 Options =
54 new FileOptions.Builder().SetExtension(
55 CSharpOptions.CSharpFileOptions,
56 new CSharpFileOptions.Builder {Namespace = "Foo.Bar"}.Build()).
57 Build()
58 }.Build();
59 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
60 Assert.AreEqual("Foo.Bar", descriptor.CSharpOptions.Namespace);
61 }
62
csharptesteac64a52011-10-04 13:43:26 -050063 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050064 public void NoNamespaceFallsBackToPackage()
65 {
66 FileDescriptorProto proto = new FileDescriptorProto.Builder {Name = "x", Package = "pack"}.Build();
67 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
68 Assert.AreEqual("pack", descriptor.CSharpOptions.Namespace);
69 }
70
csharptesteac64a52011-10-04 13:43:26 -050071 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050072 public void NoNamespaceOrPackageFallsBackToEmptyString()
73 {
74 FileDescriptorProto proto = new FileDescriptorProto.Builder {Name = "x"}.Build();
75 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
76 Assert.AreEqual("", descriptor.CSharpOptions.Namespace);
77 }
78
csharptesteac64a52011-10-04 13:43:26 -050079 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050080 public void ExplicitlyNamedFileClass()
81 {
82 FileDescriptorProto proto = new FileDescriptorProto.Builder
83 {
84 Name = "x",
85 Options =
86 new FileOptions.Builder().SetExtension(
87 CSharpOptions.CSharpFileOptions,
88 new CSharpFileOptions.Builder {UmbrellaClassname = "Foo"}.Build())
89 .Build()
90 }.Build();
91 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
92 Assert.AreEqual("Foo", descriptor.CSharpOptions.UmbrellaClassname);
93 }
94
csharptesteac64a52011-10-04 13:43:26 -050095 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050096 public void ImplicitFileClassWithProtoSuffix()
97 {
98 FileDescriptorProto proto = new FileDescriptorProto.Builder {Name = "foo_bar.proto"}.Build();
99 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
100 Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
101 }
102
csharptesteac64a52011-10-04 13:43:26 -0500103 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500104 public void ImplicitFileClassWithProtoDevelSuffix()
105 {
106 FileDescriptorProto proto = new FileDescriptorProto.Builder {Name = "foo_bar.protodevel"}.Build();
107 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
108 Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
109 }
110
csharptesteac64a52011-10-04 13:43:26 -0500111 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500112 public void ImplicitFileClassWithNoSuffix()
113 {
114 FileDescriptorProto proto = new FileDescriptorProto.Builder {Name = "foo_bar"}.Build();
115 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
116 Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
117 }
118
csharptesteac64a52011-10-04 13:43:26 -0500119 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500120 public void ImplicitFileClassWithDirectoryStructure()
121 {
122 FileDescriptorProto proto = new FileDescriptorProto.Builder {Name = "x/y/foo_bar"}.Build();
123 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
124 Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
125 }
126 }
127}