Jon Skeet | 0aac0e4 | 2009-09-09 18:48:02 +0100 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Protocol Buffers - Google's data interchange format |
Jon Skeet | ad74853 | 2009-06-25 16:55:58 +0100 | [diff] [blame] | 3 | // 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 Skeet | 0aac0e4 | 2009-09-09 18:48:02 +0100 | [diff] [blame] | 33 | #endregion |
| 34 | |
Jon Skeet | ad74853 | 2009-06-25 16:55:58 +0100 | [diff] [blame] | 35 | using Google.ProtocolBuffers.DescriptorProtos; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 36 | using Google.ProtocolBuffers.Descriptors; |
| 37 | using NUnit.Framework; |
| 38 | |
Jon Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 39 | namespace Google.ProtocolBuffers { |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 40 | [TestFixture] |
| 41 | public class DescriptorUtilTest { |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 42 | [Test] |
| 43 | public void ExplicitNamespace() { |
| 44 | FileDescriptorProto proto = new FileDescriptorProto.Builder { |
Jon Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 45 | Name = "x", Package = "pack", Options = new FileOptions.Builder().SetExtension(CSharpOptions.CSharpFileOptions, |
| 46 | new CSharpFileOptions.Builder { Namespace = "Foo.Bar" }.Build()).Build() |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 47 | }.Build(); |
| 48 | FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null); |
Jon Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 49 | Assert.AreEqual("Foo.Bar", descriptor.CSharpOptions.Namespace); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 50 | } |
| 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 Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 56 | Assert.AreEqual("pack", descriptor.CSharpOptions.Namespace); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | [Test] |
| 60 | public void NoNamespaceOrPackageFallsBackToEmptyString() { |
| 61 | FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "x" }.Build(); |
| 62 | FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null); |
Jon Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 63 | Assert.AreEqual("", descriptor.CSharpOptions.Namespace); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | [Test] |
| 67 | public void ExplicitlyNamedFileClass() { |
| 68 | FileDescriptorProto proto = new FileDescriptorProto.Builder { |
Jon Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 69 | Name = "x", Options = new FileOptions.Builder().SetExtension(CSharpOptions.CSharpFileOptions, |
| 70 | new CSharpFileOptions.Builder { UmbrellaClassname = "Foo" }.Build()).Build() |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 71 | }.Build(); |
| 72 | FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null); |
Jon Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 73 | Assert.AreEqual("Foo", descriptor.CSharpOptions.UmbrellaClassname); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 74 | } |
| 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 Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 80 | Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 81 | } |
| 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 Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 87 | Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 88 | } |
| 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 Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 94 | Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 95 | } |
| 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 Skeet | 1d131c9 | 2008-11-13 22:29:48 +0000 | [diff] [blame] | 101 | Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname); |
| 102 | } |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 103 | } |
| 104 | } |