コード例 #1
0
        public void JsonCodeGenerator_ContentResolutionForTheRest()
        {
            // Result will be Int,Int,DateTime
            var root = new WithTheRest()
            {
                Boolean = false,
                Bytes   = new byte[4] {
                    1, 2, 3, 4
                },
                Guid       = Guid.NewGuid(),
                Date       = DateTime.Now,
                DateOffset = DateTimeOffset.Now,
                Time       = TimeSpan.FromMinutes(100),
                Null       = null
            };

            var generator  = new JsonCodeGenerator("csharp");
            var classTypes = generator.GenerateClassesTypesFromObject("Root", JsonExtensions.ToJObject(root))
                             .ToLookup(x => x.Name);

            Assert.Equal(1, classTypes.Count());


            var clazz = classTypes["Root"].Single() as JsonCodeGenerator.ClassType;

            Assert.NotNull(clazz);

            Assert.Equal("Guid", clazz.Properties["Guid"].Name);
            Assert.False(clazz.Properties["Guid"].IsArray);
            Assert.True(clazz.Properties["Guid"].IsPrimitive);

            Assert.Equal("DateTimeOffset", clazz.Properties["Date"].Name);
            Assert.False(clazz.Properties["Date"].IsArray);
            Assert.True(clazz.Properties["Date"].IsPrimitive);

            Assert.Equal("DateTimeOffset", clazz.Properties["DateOffset"].Name);
            Assert.False(clazz.Properties["DateOffset"].IsArray);
            Assert.True(clazz.Properties["DateOffset"].IsPrimitive);

            Assert.Equal("TimeSpan", clazz.Properties["Time"].Name);
            Assert.False(clazz.Properties["Time"].IsArray);
            Assert.True(clazz.Properties["Time"].IsPrimitive);

            Assert.Equal("bool", clazz.Properties["Boolean"].Name);
            Assert.False(clazz.Properties["Boolean"].IsArray);
            Assert.True(clazz.Properties["Boolean"].IsPrimitive);

            Assert.Equal("object", clazz.Properties["Null"].Name);
            Assert.False(clazz.Properties["Null"].IsArray);
            Assert.True(clazz.Properties["Null"].IsPrimitive);

            Assert.Equal("byte", clazz.Properties["Bytes"].Name);
            Assert.True(clazz.Properties["Bytes"].IsArray);
            Assert.True(clazz.Properties["Bytes"].IsPrimitive);
        }
コード例 #2
0
        public void JsonCodeGenerator_ContentResolutionForTheRest()
        {
            // Result will be Int,Int,DateTime
            var root = new WithTheRest()
            {
                Boolean = false,
                Bytes = new byte[4] { 1, 2, 3, 4 },
                Guid = Guid.NewGuid(),
                Date = DateTime.Now,
                DateOffset = DateTimeOffset.Now,
                Time = TimeSpan.FromMinutes(100),
                Null = null
            };

            var generator = new JsonCodeGenerator("csharp");
            var classTypes = generator.GenerateClassesTypesFromObject("Root", JsonExtensions.ToJObject(root))
                                      .ToLookup(x => x.Name);

            Assert.Equal(1, classTypes.Count());

            var clazz = classTypes["Root"].Single() as JsonCodeGenerator.ClassType;
            Assert.NotNull(clazz);

            Assert.Equal("Guid", clazz.Properties["Guid"].Name);
            Assert.False(clazz.Properties["Guid"].IsArray);
            Assert.True(clazz.Properties["Guid"].IsPrimitive);

            Assert.Equal("DateTimeOffset", clazz.Properties["Date"].Name);
            Assert.False(clazz.Properties["Date"].IsArray);
            Assert.True(clazz.Properties["Date"].IsPrimitive);

            Assert.Equal("DateTimeOffset", clazz.Properties["DateOffset"].Name);
            Assert.False(clazz.Properties["DateOffset"].IsArray);
            Assert.True(clazz.Properties["DateOffset"].IsPrimitive);

            Assert.Equal("TimeSpan", clazz.Properties["Time"].Name);
            Assert.False(clazz.Properties["Time"].IsArray);
            Assert.True(clazz.Properties["Time"].IsPrimitive);

            Assert.Equal("bool", clazz.Properties["Boolean"].Name);
            Assert.False(clazz.Properties["Boolean"].IsArray);
            Assert.True(clazz.Properties["Boolean"].IsPrimitive);

            Assert.Equal("object", clazz.Properties["Null"].Name);
            Assert.False(clazz.Properties["Null"].IsArray);
            Assert.True(clazz.Properties["Null"].IsPrimitive);

            Assert.Equal("byte", clazz.Properties["Bytes"].Name);
            Assert.True(clazz.Properties["Bytes"].IsArray);
            Assert.True(clazz.Properties["Bytes"].IsPrimitive);
        }
コード例 #3
0
        public void JsonCodeGenerator_ContentResolutionForTheRestGenerator()
        {
            var root = new WithTheRest()
            {
                Boolean = false,
                Bytes   = new byte[4] {
                    1, 2, 3, 4
                },
                Guid       = Guid.NewGuid(),
                Date       = DateTime.Now,
                DateOffset = DateTimeOffset.Now,
                Time       = TimeSpan.FromMinutes(100),
                Null       = null
            };

            var document = new JsonDocument()
            {
                DataAsJson = JsonExtensions.ToJObject(root)
            };

            // "Raven-Clr-Type": "Namespace.ClassName, AssemblyName"
            document.Metadata["Raven-Clr-Type"] = new RavenJValue("Code.Generated.WithTheRest, AssemblyName");

            var generator = new JsonCodeGenerator("csharp");
            var code      = generator.Execute(document);

            Assert.Contains("namespace Code.Generated", code);
            Assert.Contains("public class WithTheRest", code);
            Assert.Contains("public bool Boolean { get; set; }", code);
            Assert.Contains("public Guid Guid { get; set; }", code);
            Assert.Contains("public DateTimeOffset Date { get; set; }", code);
            Assert.Contains("public DateTimeOffset DateOffset { get; set; }", code);
            Assert.Contains("public TimeSpan Time { get; set; }", code);
            Assert.Contains("public object Null { get; set; }", code);
            Assert.Contains("public byte[] Bytes { get; set; }", code);
        }
コード例 #4
0
        public void JsonCodeGenerator_ContentResolutionForTheRestGenerator()
        {
            var root = new WithTheRest()
            {
                Boolean = false,
                Bytes = new byte[4] { 1, 2, 3, 4 },
                Guid = Guid.NewGuid(),
                Date = DateTime.Now,
                DateOffset = DateTimeOffset.Now,
                Time = TimeSpan.FromMinutes(100),
                Null = null
            };

            var document = new JsonDocument()
            {
                DataAsJson = JsonExtensions.ToJObject(root)
            };

            // "Raven-Clr-Type": "Namespace.ClassName, AssemblyName"
            document.Metadata["Raven-Clr-Type"] = new RavenJValue("Code.Generated.WithTheRest, AssemblyName");

            var generator = new JsonCodeGenerator("csharp");
            var code = generator.Execute(document);

            Assert.Contains("namespace Code.Generated", code);
            Assert.Contains("public class WithTheRest", code);
            Assert.Contains("public bool Boolean { get; set; }", code);
            Assert.Contains("public Guid Guid { get; set; }", code);
            Assert.Contains("public DateTimeOffset Date { get; set; }", code);
            Assert.Contains("public DateTimeOffset DateOffset { get; set; }", code);
            Assert.Contains("public TimeSpan Time { get; set; }", code);
            Assert.Contains("public object Null { get; set; }", code);
            Assert.Contains("public byte[] Bytes { get; set; }", code);
        }