private static string GetGeneratedCodeFromXmlDefinition(string xml)
        {
            ////var serializer = new XmlSerializer(typeof(ApisRoot));
            ////var reader = new StringReader(xml);
            ////var root = (ApisRoot)serializer.Deserialize(reader);

            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new System.IO.StreamWriter(stream);
            var generator = new CSharpGenerator(writer);
            var xmlStream = new MemoryStream();
            var xmlWriter = new StreamWriter(xmlStream);
            xmlWriter.Write(xml);
            xmlWriter.Flush();
            xmlStream.Seek(0L, SeekOrigin.Begin);

            var builder = new ServiceDefinitionBuilder();
            ////builder.AppendServiceDefinition(new ServiceDefinition(root));
            builder.AppendServiceDefinition(xmlStream);
            generator.Run(builder.Definition);
            stream.Seek(0L, SeekOrigin.Begin);
            var result = new StreamReader(stream).ReadToEnd();
            return result;
        }
        public void MyTestMethod()
        {
            var stream = new System.IO.MemoryStream();
            var writer = new System.IO.StreamWriter(stream);
            var generator = new CSharpGenerator(writer);
            var root = new ApisRoot
            {
                ApiGroups = new List<ApiGroup>()
                {
                    new ApiGroup
                    {
                        Name = "g",
                        Methods = new List<ApiMethod>()
                        {
                            new ApiMethod
                            {
                                Path = "/v1/test1",
                                MethodName="mtd1",
                            },
                            new ApiMethod
                            {
                                Path = "/v1/test1/{UserId}",
                                MethodName="mtd2",
                            },
                        },
                        ReturnTypes = new List<ReturnType>(),
                    },
                },
            };
            var builder = new ServiceDefinitionBuilder();
            builder.AppendServiceDefinition(new ServiceDefinition(root));
            generator.Run(builder.Definition);

            writer.Flush();
            stream.Seek(0L, System.IO.SeekOrigin.Begin);
            var result = new StreamReader(stream).ReadToEnd();

            Assert.IsFalse(string.IsNullOrEmpty(result));
            Assert.IsTrue(result.Contains("public void mtd1("));
            Assert.IsTrue(result.Contains("public void mtd2("));
            Assert.IsTrue(result.Contains("string userId"));
        }
        public void ImplicitSubReturnTypes()
        {
            var stream = new System.IO.MemoryStream();
            var writer = new System.IO.StreamWriter(stream);
            var generator = new CSharpGenerator(writer);
            var root = new ApisRoot
            {
                ApiGroups = new List<ApiGroup>()
                {
                    new ApiGroup
                    {
                        Name = "g",
                        Methods = new List<ApiMethod>(),
                        ReturnTypes = new List<ReturnType>()
                        {
                            new ReturnType
                            {
                                Name = "r1",
                                Fields = new List<Field>()
                                {
                                    new Field
                                    {
                                        Name = "f1",
                                    },
                                    new Field
                                    {
                                        Name = "f2:(r2:(name))",
                                    },
                                },
                            },
                        },
                    },
                },
            };
            var builder = new ServiceDefinitionBuilder();
            builder.AppendServiceDefinition(new ServiceDefinition(root));
            generator.Run(builder.Definition);

            writer.Flush();
            stream.Seek(0L, System.IO.SeekOrigin.Begin);
            var result = new StreamReader(stream).ReadToEnd();

            Assert.IsFalse(string.IsNullOrEmpty(result));
            Assert.IsTrue(result.Contains("public class R1"));
            Assert.IsTrue(result.Contains("public class F2"));
            Assert.IsTrue(result.Contains("public string F1"));
            Assert.IsTrue(result.Contains("public F2 F2"));
            Assert.IsTrue(result.Contains("public string Name"));
        }
        public void ImplicitSubReturnTypes2()
        {
            var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
            <Root>
              <ApiGroup Name=""group"">
            <ReturnType Name=""person"" ClassName=""Person"">
              <Field Name=""location:(name)"" />
              <Field Name=""location:(country:(code))"" />
            </ReturnType>
              </ApiGroup>
            </Root>";
            var serializer = new XmlSerializer(typeof(ApisRoot));
            var reader = new StringReader(xml);
            var root = (ApisRoot)serializer.Deserialize(reader);
            var stream = new System.IO.MemoryStream();
            var writer = new System.IO.StreamWriter(stream);
            var generator = new CSharpGenerator(writer);
            var builder = new ServiceDefinitionBuilder();
            builder.AppendServiceDefinition(new ServiceDefinition(root));
            generator.Run(builder.Definition);

            writer.Flush();
            stream.Seek(0L, System.IO.SeekOrigin.Begin);
            var result = new StreamReader(stream).ReadToEnd();

            Assert.IsFalse(string.IsNullOrEmpty(result));
        }
        public void ImplicitAndExplicitReturnTypes()
        {
            var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
            <Root>
              <ApiGroup Name=""group"">
            <ApiMethod MethodName=""ftw"" ReturnType=""Person"" Path=""/ftw"" />
            <ReturnType Name=""person"" ClassName=""Person"">
              <Field Name=""location"" Type=""PersonLocation"" />
            </ReturnType>
            <ReturnType Name=""location"" ClassName=""PersonLocation"">
              <Field Name=""name"" />
            </ReturnType>
              </ApiGroup>
            </Root>";
            var serializer = new XmlSerializer(typeof(ApisRoot));
            var reader = new StringReader(xml);
            var root = (ApisRoot)serializer.Deserialize(reader);
            var stream = new System.IO.MemoryStream();
            var writer = new System.IO.StreamWriter(stream);
            var generator = new CSharpGenerator(writer);
            var builder = new ServiceDefinitionBuilder();
            builder.AppendServiceDefinition(new ServiceDefinition(root));
            generator.Run(builder.Definition);

            writer.Flush();
            stream.Seek(0L, System.IO.SeekOrigin.Begin);
            var result = new StreamReader(stream).ReadToEnd();

            Assert.IsFalse(string.IsNullOrEmpty(result));
            Assert.IsFalse(string.IsNullOrEmpty(result));
            Assert.IsTrue(result.Contains("public class Person"));
            Assert.IsTrue(result.Contains("public class PersonLocation"));
            Assert.IsTrue(result.Contains("public PersonLocation Location"));
            Assert.IsFalse(result.Contains("public class Location"));
            Assert.IsTrue(result.Contains("public string Name"));
        }