aboutsummaryrefslogtreecommitdiff
path: root/csharp/ProtoGen.Test
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2008-10-22 07:11:17 +0100
committerJon Skeet <skeet@pobox.com>2008-10-22 07:11:17 +0100
commite60ce8bfafca616ed4fd430ae4f82360de165e80 (patch)
treecb1a82655772f4995c02debb31cfeb208960acf1 /csharp/ProtoGen.Test
parent7f90d8ee57d4af4215bc31dbb07726c023c5e047 (diff)
downloadprotobuf-e60ce8bfafca616ed4fd430ae4f82360de165e80.tar.gz
protobuf-e60ce8bfafca616ed4fd430ae4f82360de165e80.tar.bz2
protobuf-e60ce8bfafca616ed4fd430ae4f82360de165e80.zip
Final commit before changing layout
Diffstat (limited to 'csharp/ProtoGen.Test')
-rw-r--r--csharp/ProtoGen.Test/DependencyResolutionTest.cs82
-rw-r--r--csharp/ProtoGen.Test/DescriptorUtilTest.cs69
-rw-r--r--csharp/ProtoGen.Test/GeneratorTest.cs9
-rw-r--r--csharp/ProtoGen.Test/HelpersTest.cs33
-rw-r--r--csharp/ProtoGen.Test/ProtoGen.Test.csproj6
5 files changed, 193 insertions, 6 deletions
diff --git a/csharp/ProtoGen.Test/DependencyResolutionTest.cs b/csharp/ProtoGen.Test/DependencyResolutionTest.cs
index 1906c44d..ef911263 100644
--- a/csharp/ProtoGen.Test/DependencyResolutionTest.cs
+++ b/csharp/ProtoGen.Test/DependencyResolutionTest.cs
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
+using Google.ProtocolBuffers.Descriptors;
using NUnit.Framework;
+using Google.ProtocolBuffers.DescriptorProtos;
+using Google.ProtocolBuffers.ProtoGen;
-namespace ProtoGen {
+namespace Google.ProtocolBuffers.ProtoGen {
/// <summary>
/// Tests for the dependency resolution in Generator.
/// </summary>
@@ -12,6 +15,81 @@ namespace ProtoGen {
[Test]
public void TwoDistinctFiles() {
+ FileDescriptorProto first = new FileDescriptorProto.Builder { Name="First" }.Build();
+ FileDescriptorProto second = new FileDescriptorProto.Builder { Name="Second" }.Build();
+ FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } };
+
+ IList<FileDescriptor> converted = Generator.ConvertDescriptors(set);
+ Assert.AreEqual(2, converted.Count);
+ Assert.AreEqual("First", converted[0].Name);
+ Assert.AreEqual(0, converted[0].Dependencies.Count);
+ Assert.AreEqual("Second", converted[1].Name);
+ Assert.AreEqual(0, converted[1].Dependencies.Count);
+ }
+
+ [Test]
+ public void FirstDependsOnSecond() {
+ FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = {"Second"} }.Build();
+ FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second" }.Build();
+ FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } };
+ IList<FileDescriptor> converted = Generator.ConvertDescriptors(set);
+ Assert.AreEqual(2, converted.Count);
+ Assert.AreEqual("First", converted[0].Name);
+ Assert.AreEqual(1, converted[0].Dependencies.Count);
+ Assert.AreEqual(converted[1], converted[0].Dependencies[0]);
+ Assert.AreEqual("Second", converted[1].Name);
+ Assert.AreEqual(0, converted[1].Dependencies.Count);
+ }
+
+ [Test]
+ public void SecondDependsOnFirst() {
+ FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First" }.Build();
+ FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second", DependencyList = {"First"} }.Build();
+ FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } };
+ IList<FileDescriptor> converted = Generator.ConvertDescriptors(set);
+ Assert.AreEqual(2, converted.Count);
+ Assert.AreEqual("First", converted[0].Name);
+ Assert.AreEqual(0, converted[0].Dependencies.Count);
+ Assert.AreEqual("Second", converted[1].Name);
+ Assert.AreEqual(1, converted[1].Dependencies.Count);
+ Assert.AreEqual(converted[0], converted[1].Dependencies[0]);
+ }
+
+ [Test]
+ public void CircularDependency() {
+ FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "Second" } }.Build();
+ FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second", DependencyList = { "First" } }.Build();
+ FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } };
+ try {
+ Generator.ConvertDescriptors(set);
+ Assert.Fail("Expected exception");
+ } catch (DependencyResolutionException) {
+ // Expected
+ }
+ }
+
+ [Test]
+ public void MissingDependency() {
+ FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "Second" } }.Build();
+ FileDescriptorSet set = new FileDescriptorSet { FileList = { first } };
+ try {
+ Generator.ConvertDescriptors(set);
+ Assert.Fail("Expected exception");
+ } catch (DependencyResolutionException) {
+ // Expected
+ }
+ }
+
+ [Test]
+ public void SelfDependency() {
+ FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "First" } }.Build();
+ FileDescriptorSet set = new FileDescriptorSet { FileList = { first } };
+ try {
+ Generator.ConvertDescriptors(set);
+ Assert.Fail("Expected exception");
+ } catch (DependencyResolutionException) {
+ // Expected
+ }
}
}
-}
+} \ No newline at end of file
diff --git a/csharp/ProtoGen.Test/DescriptorUtilTest.cs b/csharp/ProtoGen.Test/DescriptorUtilTest.cs
new file mode 100644
index 00000000..5674892e
--- /dev/null
+++ b/csharp/ProtoGen.Test/DescriptorUtilTest.cs
@@ -0,0 +1,69 @@
+using Google.ProtocolBuffers.DescriptorProtos;
+using Google.ProtocolBuffers.Descriptors;
+using NUnit.Framework;
+
+namespace Google.ProtocolBuffers.ProtoGen {
+ [TestFixture]
+ public class DescriptorUtilTest {
+
+ [Test]
+ public void ExplicitNamespace() {
+ FileDescriptorProto proto = new FileDescriptorProto.Builder {
+ Name = "x", Package = "pack", Options = new FileOptions.Builder().SetExtension(CSharpOptions.CSharpNamespace, "Foo.Bar").Build()
+ }.Build();
+ FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
+ Assert.AreEqual("Foo.Bar", DescriptorUtil.GetNamespace(descriptor));
+ }
+
+ [Test]
+ public void NoNamespaceFallsBackToPackage() {
+ FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "x", Package = "pack" }.Build();
+ FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
+ Assert.AreEqual("pack", DescriptorUtil.GetNamespace(descriptor));
+ }
+
+ [Test]
+ public void NoNamespaceOrPackageFallsBackToEmptyString() {
+ FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "x" }.Build();
+ FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
+ Assert.AreEqual("", DescriptorUtil.GetNamespace(descriptor));
+ }
+
+ [Test]
+ public void ExplicitlyNamedFileClass() {
+ FileDescriptorProto proto = new FileDescriptorProto.Builder {
+ Name = "x", Options = new FileOptions.Builder().SetExtension(CSharpOptions.CSharpUmbrellaClassname, "Foo").Build()
+ }.Build();
+ FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
+ Assert.AreEqual("Foo", DescriptorUtil.GetUmbrellaClassName(descriptor));
+ }
+
+ [Test]
+ public void ImplicitFileClassWithProtoSuffix() {
+ FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "foo_bar.proto" }.Build();
+ FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
+ Assert.AreEqual("FooBar", DescriptorUtil.GetUmbrellaClassName(descriptor));
+ }
+
+ [Test]
+ public void ImplicitFileClassWithProtoDevelSuffix() {
+ FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "foo_bar.protodevel" }.Build();
+ FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
+ Assert.AreEqual("FooBar", DescriptorUtil.GetUmbrellaClassName(descriptor));
+ }
+
+ [Test]
+ public void ImplicitFileClassWithNoSuffix() {
+ FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "foo_bar" }.Build();
+ FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
+ Assert.AreEqual("FooBar", DescriptorUtil.GetUmbrellaClassName(descriptor));
+ }
+
+ [Test]
+ public void ImplicitFileClassWithDirectoryStructure() {
+ FileDescriptorProto proto = new FileDescriptorProto.Builder { Name = "x/y/foo_bar" }.Build();
+ FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);
+ Assert.AreEqual("FooBar", DescriptorUtil.GetUmbrellaClassName(descriptor));
+ }
+ }
+}
diff --git a/csharp/ProtoGen.Test/GeneratorTest.cs b/csharp/ProtoGen.Test/GeneratorTest.cs
index 137d9348..dbd18787 100644
--- a/csharp/ProtoGen.Test/GeneratorTest.cs
+++ b/csharp/ProtoGen.Test/GeneratorTest.cs
@@ -1,7 +1,10 @@
-using NUnit.Framework;
+using Google.ProtocolBuffers.DescriptorProtos;
+using NUnit.Framework;
+using Google.ProtocolBuffers.Descriptors;
-namespace ProtoGen {
+namespace Google.ProtocolBuffers.ProtoGen {
[TestFixture]
public class GeneratorTest {
+
}
-}
+} \ No newline at end of file
diff --git a/csharp/ProtoGen.Test/HelpersTest.cs b/csharp/ProtoGen.Test/HelpersTest.cs
new file mode 100644
index 00000000..af084973
--- /dev/null
+++ b/csharp/ProtoGen.Test/HelpersTest.cs
@@ -0,0 +1,33 @@
+using Google.ProtocolBuffers.ProtoGen;
+using NUnit.Framework;
+
+namespace Google.ProtocolBuffers.ProtoGen {
+ [TestFixture]
+ public class HelpersTest {
+
+ [Test]
+ public void UnderscoresToPascalCase() {
+ Assert.AreEqual("FooBar", Helpers.UnderscoresToPascalCase("Foo_bar"));
+ Assert.AreEqual("FooBar", Helpers.UnderscoresToPascalCase("foo_bar"));
+ Assert.AreEqual("Foo0Bar", Helpers.UnderscoresToPascalCase("Foo0bar"));
+ Assert.AreEqual("FooBar", Helpers.UnderscoresToPascalCase("Foo_+_Bar"));
+ }
+
+ [Test]
+ public void UnderscoresToCamelCase() {
+ Assert.AreEqual("fooBar", Helpers.UnderscoresToCamelCase("Foo_bar"));
+ Assert.AreEqual("fooBar", Helpers.UnderscoresToCamelCase("foo_bar"));
+ Assert.AreEqual("foo0Bar", Helpers.UnderscoresToCamelCase("Foo0bar"));
+ Assert.AreEqual("fooBar", Helpers.UnderscoresToCamelCase("Foo_+_Bar"));
+ }
+
+ [Test]
+ public void StripSuffix() {
+ string text = "FooBar";
+ Assert.IsFalse(Helpers.StripSuffix(ref text, "Foo"));
+ Assert.AreEqual("FooBar", text);
+ Assert.IsTrue(Helpers.StripSuffix(ref text, "Bar"));
+ Assert.AreEqual("Foo", text);
+ }
+ }
+} \ No newline at end of file
diff --git a/csharp/ProtoGen.Test/ProtoGen.Test.csproj b/csharp/ProtoGen.Test/ProtoGen.Test.csproj
index c1c05243..83e6f961 100644
--- a/csharp/ProtoGen.Test/ProtoGen.Test.csproj
+++ b/csharp/ProtoGen.Test/ProtoGen.Test.csproj
@@ -8,7 +8,7 @@
<ProjectGuid>{C268DA4C-4004-47DA-AF23-44C983281A68}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
- <RootNamespace>ProtoGen</RootNamespace>
+ <RootNamespace>Google.ProtocolBuffers.ProtoGen</RootNamespace>
<AssemblyName>Google.ProtocolBuffers.ProtoGen.Test</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
@@ -42,10 +42,14 @@
<HintPath>..\lib\Rhino.Mocks.dll</HintPath>
</Reference>
<Reference Include="System" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DependencyResolutionTest.cs" />
+ <Compile Include="DescriptorUtilTest.cs" />
<Compile Include="GeneratorTest.cs" />
+ <Compile Include="HelpersTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>