blob: 2c755972e310b98eeaae5d6bdd599377141507ca [file] [log] [blame]
Jon Skeet0aac0e42009-09-09 18:48:02 +01001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
Jon Skeetad748532009-06-25 16:55:58 +01003// Copyright 2008 Google Inc. All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12// * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14// * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18// * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jon Skeet0aac0e42009-09-09 18:48:02 +010033#endregion
34
Jon Skeetad748532009-06-25 16:55:58 +010035using Google.ProtocolBuffers.DescriptorProtos;
Jon Skeet68036862008-10-22 13:30:34 +010036using Google.ProtocolBuffers.Descriptors;
37using NUnit.Framework;
38
Jon Skeet1d131c92008-11-13 22:29:48 +000039namespace Google.ProtocolBuffers {
Jon Skeet68036862008-10-22 13:30:34 +010040 [TestFixture]
41 public class DescriptorUtilTest {
Jon Skeet68036862008-10-22 13:30:34 +010042 [Test]
43 public void ExplicitNamespace() {
44 FileDescriptorProto proto = new FileDescriptorProto.Builder {
Jon Skeet1d131c92008-11-13 22:29:48 +000045 Name = "x", Package = "pack", Options = new FileOptions.Builder().SetExtension(CSharpOptions.CSharpFileOptions,
46 new CSharpFileOptions.Builder { Namespace = "Foo.Bar" }.Build()).Build()
Jon Skeet68036862008-10-22 13:30:34 +010047 }.Build();
48 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
Jon Skeet1d131c92008-11-13 22:29:48 +000049 Assert.AreEqual("Foo.Bar", descriptor.CSharpOptions.Namespace);
Jon Skeet68036862008-10-22 13:30:34 +010050 }
51
52 [Test]
53 public void NoNamespaceFallsBackToPackage() {
54 FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "x", Package = "pack" }.Build();
55 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
Jon Skeet1d131c92008-11-13 22:29:48 +000056 Assert.AreEqual("pack", descriptor.CSharpOptions.Namespace);
Jon Skeet68036862008-10-22 13:30:34 +010057 }
58
59 [Test]
60 public void NoNamespaceOrPackageFallsBackToEmptyString() {
61 FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "x" }.Build();
62 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
Jon Skeet1d131c92008-11-13 22:29:48 +000063 Assert.AreEqual("", descriptor.CSharpOptions.Namespace);
Jon Skeet68036862008-10-22 13:30:34 +010064 }
65
66 [Test]
67 public void ExplicitlyNamedFileClass() {
68 FileDescriptorProto proto = new FileDescriptorProto.Builder {
Jon Skeet1d131c92008-11-13 22:29:48 +000069 Name = "x", Options = new FileOptions.Builder().SetExtension(CSharpOptions.CSharpFileOptions,
70 new CSharpFileOptions.Builder { UmbrellaClassname = "Foo" }.Build()).Build()
Jon Skeet68036862008-10-22 13:30:34 +010071 }.Build();
72 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
Jon Skeet1d131c92008-11-13 22:29:48 +000073 Assert.AreEqual("Foo", descriptor.CSharpOptions.UmbrellaClassname);
Jon Skeet68036862008-10-22 13:30:34 +010074 }
75
76 [Test]
77 public void ImplicitFileClassWithProtoSuffix() {
78 FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "foo_bar.proto" }.Build();
79 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
Jon Skeet1d131c92008-11-13 22:29:48 +000080 Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
Jon Skeet68036862008-10-22 13:30:34 +010081 }
82
83 [Test]
84 public void ImplicitFileClassWithProtoDevelSuffix() {
85 FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "foo_bar.protodevel" }.Build();
86 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
Jon Skeet1d131c92008-11-13 22:29:48 +000087 Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
Jon Skeet68036862008-10-22 13:30:34 +010088 }
89
90 [Test]
91 public void ImplicitFileClassWithNoSuffix() {
92 FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "foo_bar" }.Build();
93 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
Jon Skeet1d131c92008-11-13 22:29:48 +000094 Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
Jon Skeet68036862008-10-22 13:30:34 +010095 }
96
97 [Test]
98 public void ImplicitFileClassWithDirectoryStructure() {
99 FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "x/y/foo_bar" }.Build();
100 FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
Jon Skeet1d131c92008-11-13 22:29:48 +0000101 Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
102 }
Jon Skeet68036862008-10-22 13:30:34 +0100103 }
104}