Ejemplo n.º 1
0
        public void NoGetter()
        {
            // Verify that we can generate code for the [NoGetter] data model.
            // This has a single property that only has a getter which should
            // be ignored by the code generator, so this should end up being
            // equivalent to a model with no properties.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <NoGetter>();
                Assert.Equal("{}", data.ToString());

                data = context.CreateDataWrapperFrom <NoGetter>(data.ToString());
                Assert.Equal("{}", data.ToString());

                data = context.CreateDataWrapperFrom <NoGetter>(data.JObject);
                Assert.Equal("{}", data.ToString());
            }
        }
Ejemplo n.º 2
0
        public void AllTargets()
        {
            // Verify that all types are generated when no targets
            // are specified.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_CustomOutput).Namespace,
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));
            var sourceCode     = output.SourceCode;

            Assert.Contains("class Class1", sourceCode);
            Assert.Contains("class Class2", sourceCode);
            Assert.Contains("class Class3", sourceCode);
            Assert.DoesNotContain("class Class4", sourceCode);
            Assert.Contains("class Service1", sourceCode);
            Assert.Contains("class Service2", sourceCode);
        }
Ejemplo n.º 3
0
        public void CustomPropertyNames()
        {
            // Vertify that [JsonProperty(PropertyName = "xxx")] works.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <CustomNamesModel>();
                Assert.Equal("{\"CustomString\":null,\"CustomInt\":0}", data.ToString());

                data["String"] = "Hello World!";
                data["Int"]    = 1001;
                Assert.Equal("{\"CustomString\":\"Hello World!\",\"CustomInt\":1001}", data.ToString());

                data = context.CreateDataWrapperFrom <CustomNamesModel>(data.ToString());
                Assert.Equal("{\"CustomString\":\"Hello World!\",\"CustomInt\":1001}", data.ToString());
            }
        }
Ejemplo n.º 4
0
        public void DefaultValues()
        {
            // Verify that data models with default property values are initialized correctly.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <DefaultValues>();

                Assert.Equal("Joe Bloe", data["Name"]);
                Assert.Equal(67, data["Age"]);
                Assert.Equal(100000.0, data["NetWorth"]);
                Assert.Equal(MyEnum1.Three, (MyEnum1)data["Enum1"]);
            }
        }
Ejemplo n.º 5
0
        public void Filter3AndClient()
        {
            // Verify that only those types tagged with [Target("3")] and [Target("client")]
            // are generated.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_CustomOutput).Namespace,
                Targets         = new List <string>()
                {
                    "3", "client"
                }
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));
            var sourceCode     = output.SourceCode;

            Assert.Contains("class Class1", sourceCode);
            Assert.Contains("class Class2", sourceCode);
            Assert.Contains("class Class3", sourceCode);
            Assert.DoesNotContain("class Class4", sourceCode);
            Assert.Contains("class Service1", sourceCode);
            Assert.DoesNotContain("class Service2", sourceCode);
        }
Ejemplo n.º 6
0
        public void SourceNamespace()
        {
            // Verify that we can filter by source namespace.
            // This should only generate [Class4].

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(FooBar.Class4).Namespace
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));
            var sourceCode     = output.SourceCode;

            Assert.DoesNotContain("class Class1", sourceCode);
            Assert.DoesNotContain("class Class2", sourceCode);
            Assert.DoesNotContain("class Class3", sourceCode);
            Assert.Contains("class Class4", sourceCode);
            Assert.DoesNotContain("class Service1", sourceCode);
            Assert.DoesNotContain("class Service2", sourceCode);
        }
Ejemplo n.º 7
0
        public void NullableProperties()
        {
            // NOTE:
            //
            // This test doesn't work due to apparent limitations of
            // the DataWrapper class an perhaps .NET itself.  The problem
            // is related to assigning a nullable Enum value.  This
            // fails even though I explicitly cast the value to (MyEnum1?).
            //
            // I think the problem is related to the JIT compiler doing some
            // magic here and effectively stripping out the cast and
            // just passing the non-nullable enum value and then
            // [DataWrapper] fails when dynamically assigning the value
            // to the property because the value is no longer a (MyEnum1?).
            //
            // I have verified this manually when using the generated
            // model classes directly.

            // Verify that we handle nullable property types correctly.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <NullableProperties>();
                Assert.Equal("{\"Bool\":null,\"Int\":null,\"Enum\":null}", data.ToString());

                data = context.CreateDataWrapperFrom <NullableProperties>(data.ToString());
                Assert.Equal("{\"Bool\":null,\"Int\":null,\"Enum\":null}", data.ToString());

                data["Bool"] = true;
                data["Int"]  = 100;
                data["Enum"] = (MyEnum1?)MyEnum1.Two;   // This throws an exception

                var s = data.ToString();

                Assert.Equal("", data.ToString());
            }
        }
Ejemplo n.º 8
0
        public void EmptyPersistable()
        {
            // Verify that we can generate code for an empty persistable data model.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <EmptyPersistableData>();
                Assert.Equal("{\"T$$\":\"TestModelGen.UxDataModel.EmptyPersistableData\"}", data.ToString());

                data = context.CreateDataWrapperFrom <EmptyPersistableData>(data.ToString());
                Assert.Equal("{\"T$$\":\"TestModelGen.UxDataModel.EmptyPersistableData\"}", data.ToString());

                data = context.CreateDataWrapperFrom <EmptyPersistableData>(data.ToJObject());
                Assert.Equal("{\"T$$\":\"TestModelGen.UxDataModel.EmptyPersistableData\"}", data.ToString());

                //-------------------------------------------------------------
                // Verify Equals():

                var value1 = context.CreateDataWrapperFrom <EmptyPersistableData>(data.ToJObject());
                var value2 = context.CreateDataWrapperFrom <EmptyPersistableData>(data.ToJObject());

                Assert.True(value1.Equals(value1));
                Assert.True(value1.Equals(value2));
                Assert.True(value2.Equals(value1));

                Assert.False(value1.Equals(null));
                Assert.False(value1.Equals("Hello World!"));
            }
        }
Ejemplo n.º 9
0
        public void TargetNamespace()
        {
            // Verify that we can customize the output namespace.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_CustomOutput).Namespace,
                TargetNamespace = "Foo.Bar"
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));
            var sourceCode     = output.SourceCode;

            Assert.Contains("namespace Foo.Bar", sourceCode);
        }
Ejemplo n.º 10
0
        public void NotEqualsOperator()
        {
            // Verify that the generated binary "==" operator works.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var value1 = context.CreateDataWrapper <SimpleData>();
                var value2 = context.CreateDataWrapper <SimpleData>();

                value1["Name"] = "Jeff";
                value1["Age"]  = 58;
                value1["Enum"] = MyEnum1.One;

                value2["Name"] = "Jeff";
                value2["Age"]  = 58;
                value2["Enum"] = MyEnum1.One;

                Assert.False(DataWrapper.NotEquals <SimpleData>(value1, value1));
                Assert.False(DataWrapper.NotEquals <SimpleData>(value1, value2));

                Assert.True(DataWrapper.NotEquals <SimpleData>(null, value2));
                Assert.True(DataWrapper.NotEquals <SimpleData>(value1, null));

                value2["Name"] = "Bob";
                Assert.True(DataWrapper.NotEquals <SimpleData>(value1, value2));
                Assert.True(DataWrapper.NotEquals <SimpleData>(value2, value1));
            }
        }
Ejemplo n.º 11
0
        public async Task NoServices()
        {
            using (var runner = new ProgramRunner())
            {
                // Verify that all types are generated when no targets
                // are specified.

                var result = await runner.ExecuteAsync(Program.Main, $"--source-namespace={typeof(Test_ModelGeneration).Namespace}", "--no-services", thisAssemblyPath);

                Assert.Equal(0, result.ExitCode);

                var sourceCode = result.OutputText;

                CSharpHelper.Compile(sourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

                Assert.Contains("class Class1", sourceCode);
                Assert.Contains("class Class2", sourceCode);
                Assert.Contains("class Class3", sourceCode);
                Assert.DoesNotContain("class Service1", sourceCode);
                Assert.DoesNotContain("class Service2", sourceCode);
            }
        }
Ejemplo n.º 12
0
        public void ReadOnlyCollections()
        {
            // Verify serializing read-only collection properties.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_DataModel).Namespace,
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <ReadOnlyCollectionsModel>();

                data["Collection"] = new ReadOnlyCollection <string>(new List <string> {
                    "one", "two", "three"
                });
                data["Dictionary"] = new ReadOnlyDictionary <string, string>(new Dictionary <string, string>()
                {
                    { "one", "1" }
                });

                data = context.CreateDataWrapperFrom <ReadOnlyCollectionsModel>(data.ToString());

                Assert.Equal(new List <string> {
                    "one", "two", "three"
                }, data["Collection"]);
                Assert.Equal(new Dictionary <string, string>()
                {
                    { "one", "1" }
                }, data["Dictionary"]);
            }
        }
Ejemplo n.º 13
0
        public void ListOfList()
        {
            // Verify serializing a list of lists.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_DataModel).Namespace,
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <ListOfListModel>();

                var list = new List <List <string> >();

                list.Add(new List <string>()
                {
                    "zero", "one", "two"
                });
                list.Add(new List <string>()
                {
                    "three", "four", "five"
                });

                data["List"] = list;

                data = context.CreateDataWrapperFrom <ListOfListModel>(data.ToString());

                Assert.Equal(list, data["List"]);
            }
        }
Ejemplo n.º 14
0
        public void NoServices()
        {
            // Verify that we can disable service client generation.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace  = typeof(Test_CustomOutput).Namespace,
                NoServiceClients = true
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));
            var sourceCode     = output.SourceCode;

            Assert.Contains("class Class1", sourceCode);
            Assert.Contains("class Class2", sourceCode);
            Assert.Contains("class Class3", sourceCode);
            Assert.DoesNotContain("class Service1", sourceCode);
            Assert.DoesNotContain("class Service2", sourceCode);
        }
Ejemplo n.º 15
0
        public void SerializationDefaults()
        {
            // Verify that we honor the [JsonProperty(DefaultValueHandling)] options.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <SerializationDefaultsModel>();

                // Verify that the instance starts out with the correct
                // default property values.

                Assert.Equal("Ignore", data["Ignore"]);
                Assert.Equal("IgnoreAndPopulate", data["IgnoreAndPopulate"]);
                Assert.Equal("Include", data["Include"]);
                Assert.Equal("Populate", data["Populate"]);

                // Verify that we get the same output when serializing
                // the same data multple times (this wasn't working
                // early on).

                var serialized = data.ToString();

                Assert.Equal(serialized, data.ToString());
                Assert.Equal(serialized, data.ToString());

                // Verify that defaults serialize correctly.

                Assert.Equal("{\"Include\":\"Include\",\"Populate\":\"Populate\"}", data.ToString());

                // Verify that defaults deserialize correctly.

                data = context.CreateDataWrapper <SerializationDefaultsModel>();
                data = context.CreateDataWrapperFrom <SerializationDefaultsModel>(data.ToString());

                Assert.Equal("Ignore", data["Ignore"]);
                Assert.Equal("IgnoreAndPopulate", data["IgnoreAndPopulate"]);
                Assert.Equal("Include", data["Include"]);
                Assert.Equal("Populate", data["Populate"]);

                // Verify that non-default values serialize/desearlize correctly.

                data = context.CreateDataWrapper <SerializationDefaultsModel>();

                data["Ignore"]            = "NotIgnore";
                data["IgnoreAndPopulate"] = "NotIgnoreAndPopulate";
                data["Include"]           = "NotInclude";
                data["Populate"]          = "NotPopulate";

                Assert.Equal("{\"Ignore\":\"NotIgnore\",\"IgnoreAndPopulate\":\"NotIgnoreAndPopulate\",\"Include\":\"NotInclude\",\"Populate\":\"NotPopulate\"}", data.ToString());

                data = context.CreateDataWrapperFrom <SerializationDefaultsModel>(data.ToString());
                Assert.Equal("{\"Ignore\":\"NotIgnore\",\"IgnoreAndPopulate\":\"NotIgnoreAndPopulate\",\"Include\":\"NotInclude\",\"Populate\":\"NotPopulate\"}", data.ToString());

                Assert.Equal("NotIgnore", data["Ignore"]);
                Assert.Equal("NotIgnoreAndPopulate", data["IgnoreAndPopulate"]);
                Assert.Equal("NotInclude", data["Include"]);
                Assert.Equal("NotPopulate", data["Populate"]);
            }
        }
Ejemplo n.º 16
0
        public void Complex()
        {
            // Verify that we can generate code for complex data types.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <ComplexData>();

                // This is going to throw a serialization exception because 0 is not a valid
                // [Enum2] orginal value, but that's what it will be initially set to because
                // we didn't use [DefaultValue].

                Assert.Throws <SerializationException>(() => data.ToString());

                // Set a valid [Enum2] Value and test again.

                data["Enum2"] = MyEnum2.Three;
                Assert.Equal("{\"List\":null,\"Dictionary\":null,\"Enum1\":\"One\",\"Enum2\":\"three\",\"Simple\":null,\"SingleArray\":null,\"DoubleArray\":null}", data.ToString());

                // Initialize the list and verify.

                data["List"] = new ObservableCollection <string>()
                {
                    "item0", "item1"
                };

                Assert.Equal("{\"List\":[\"item0\",\"item1\"],\"Dictionary\":null,\"Enum1\":\"One\",\"Enum2\":\"three\",\"Simple\":null,\"SingleArray\":null,\"DoubleArray\":null}", data.ToString());

                // Initialize the dictionary and verify.

                data["Dictionary"] = new Dictionary <string, int>()
                {
                    { "zero", 0 },
                    { "one", 1 }
                };

                Assert.Equal("{\"List\":[\"item0\",\"item1\"],\"Dictionary\":{\"zero\":0,\"one\":1},\"Enum1\":\"One\",\"Enum2\":\"three\",\"Simple\":null,\"SingleArray\":null,\"DoubleArray\":null}", data.ToString());

                // Initialize the one dimensional array and verify.

                data["SingleArray"] = new int[] { 100, 200 };
                Assert.Equal("{\"List\":[\"item0\",\"item1\"],\"Dictionary\":{\"zero\":0,\"one\":1},\"Enum1\":\"One\",\"Enum2\":\"three\",\"Simple\":null,\"SingleArray\":[100,200],\"DoubleArray\":null}", data.ToString());

                // Initialize the two dimensional array and verify.

                data["DoubleArray"] = new int[][]
                {
                    new int[] { 100, 200 },
                    new int[] { 300, 400 }
                };

                Assert.Equal("{\"List\":[\"item0\",\"item1\"],\"Dictionary\":{\"zero\":0,\"one\":1},\"Enum1\":\"One\",\"Enum2\":\"three\",\"Simple\":null,\"SingleArray\":[100,200],\"DoubleArray\":[[100,200],[300,400]]}", data.ToString());

                // Verify that a property with [JsonIgnore] is not persisted.

                data["IgnoreThis"] = 1000;
                Assert.DoesNotContain("IgnoreThis", data.ToString());

                //-------------------------------------------------------------
                // Verify Equals():

                var value1 = context.CreateDataWrapperFrom <ComplexData>("{\"List\":[\"zero\"],\"Dictionary\":null,\"Enum1\":\"One\",\"Enum2\":\"three\",\"Simple\":null,\"SingleArray\":null,\"DoubleArray\":null}");
                var value2 = context.CreateDataWrapperFrom <ComplexData>("{\"List\":[\"zero\"],\"Dictionary\":null,\"Enum1\":\"One\",\"Enum2\":\"three\",\"Simple\":null,\"SingleArray\":null,\"DoubleArray\":null}");

                Assert.True(value1.Equals(value1));
                Assert.True(value1.Equals(value2));
                Assert.True(value2.Equals(value1));

                Assert.False(value1.Equals(null));
                Assert.False(value1.Equals("Hello World!"));

                value2 = context.CreateDataWrapperFrom <ComplexData>("{\"List\":[\"NOT-ZERO\"],\"Dictionary\":null,\"Enum1\":\"One\",\"Enum2\":\"three\",\"Simple\":null,\"SingleArray\":null,\"DoubleArray\":null}");

                Assert.True(value1.Equals(value1));

                Assert.False(value1.Equals(value2));
                Assert.False(value2.Equals(value1));
                Assert.False(value1.Equals(null));
                Assert.False(value1.Equals("Hello World!"));
            }
        }
Ejemplo n.º 17
0
        public void BasicTypes()
        {
            // Verify that we can generate code for basic data types.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <BasicTypes>();
                Assert.Equal("{\"Bool\":false,\"Byte\":0,\"SByte\":0,\"Short\":0,\"UShort\":0,\"Int\":0,\"UInt\":0,\"Long\":0,\"ULong\":0,\"Float\":0.0,\"Double\":0.0,\"Decimal\":0.0,\"String\":null}", data.ToString());

                data["Bool"]    = true;
                data["Byte"]    = (byte)1;
                data["SByte"]   = (sbyte)2;
                data["Short"]   = (short)3;
                data["UShort"]  = (ushort)4;
                data["Int"]     = (int)5;
                data["UInt"]    = (uint)6;
                data["Long"]    = (long)7;
                data["ULong"]   = (ulong)8;
                data["Float"]   = (float)9;
                data["Double"]  = (double)10;
                data["Decimal"] = (decimal)11;
                data["String"]  = "12";

                Assert.Equal("{\"Bool\":true,\"Byte\":1,\"SByte\":2,\"Short\":3,\"UShort\":4,\"Int\":5,\"UInt\":6,\"Long\":7,\"ULong\":8,\"Float\":9.0,\"Double\":10.0,\"Decimal\":11.0,\"String\":\"12\"}", data.ToString());

                var jsonText = data.ToString(indented: false);
                data = context.CreateDataWrapperFrom <BasicTypes>(jsonText);
                Assert.Equal("{\"Bool\":true,\"Byte\":1,\"SByte\":2,\"Short\":3,\"UShort\":4,\"Int\":5,\"UInt\":6,\"Long\":7,\"ULong\":8,\"Float\":9.0,\"Double\":10.0,\"Decimal\":11.0,\"String\":\"12\"}", data.ToString());

                jsonText = data.ToString(indented: true);
                data     = context.CreateDataWrapperFrom <BasicTypes>(jsonText);
                Assert.Equal("{\"Bool\":true,\"Byte\":1,\"SByte\":2,\"Short\":3,\"UShort\":4,\"Int\":5,\"UInt\":6,\"Long\":7,\"ULong\":8,\"Float\":9.0,\"Double\":10.0,\"Decimal\":11.0,\"String\":\"12\"}", data.ToString());

                //-------------------------------------------------------------
                // Verify Equals():

                var value1 = context.CreateDataWrapperFrom <BasicTypes>("{\"Bool\":true,\"Byte\":1,\"SByte\":2,\"Short\":3,\"UShort\":4,\"Int\":5,\"UInt\":6,\"Long\":7,\"ULong\":8,\"Float\":9.0,\"Double\":10.0,\"Decimal\":11.0,\"String\":\"12\"}");
                var value2 = context.CreateDataWrapperFrom <BasicTypes>("{\"Bool\":true,\"Byte\":1,\"SByte\":2,\"Short\":3,\"UShort\":4,\"Int\":5,\"UInt\":6,\"Long\":7,\"ULong\":8,\"Float\":9.0,\"Double\":10.0,\"Decimal\":11.0,\"String\":\"12\"}");

                Assert.True(value1.Equals(value1));
                Assert.True(value1.Equals(value2));
                Assert.True(value2.Equals(value1));

                Assert.False(value1.Equals(null));
                Assert.False(value1.Equals("Hello World!"));

                value2["String"] = "Bob";

                Assert.True(value1.Equals(value1));

                Assert.False(value1.Equals(value2));
                Assert.False(value2.Equals(value1));
                Assert.False(value1.Equals(null));
                Assert.False(value1.Equals("Hello World!"));
            }
        }
Ejemplo n.º 18
0
        public void HashCode()
        {
            // Verify that GetHashCode() works.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                // Verify that we see a [InvalidOperationException] when computing a hash
                // on a data model with no properties tagged by [HashSource].

                var emptyData = context.CreateDataWrapper <EmptyData>();

                Assert.Throws <InvalidOperationException>(() => emptyData.GetHashCode());

                // Verify that we compute HASH=0 for a data model with a
                // single tagged parameter set to NULL.

                var baseData = context.CreateDataWrapper <BaseModel>();

                Assert.Equal(0, baseData.GetHashCode());

                // Verify that we compute a non-zero HASH for a data model with a
                // single tagged parameter set to to a value.

                baseData = context.CreateDataWrapper <BaseModel>();
                baseData["ParentProperty"] = "Hello World!";
                Assert.NotEqual(0, baseData.GetHashCode());

                // Verify that we can compute a HASH code for a derived class
                // and that the hash includes properties from both the derived
                // and parent classes.

                var derivedData = context.CreateDataWrapper <DerivedModel>();
                derivedData["ChildProperty"] = "Goodbye World!";
                var derivedCode1 = derivedData.GetHashCode();
                Assert.NotEqual(0, derivedCode1);

                derivedData["ParentProperty"] = "Hello World!";
                var derivedCode2 = derivedData.GetHashCode();
                Assert.NotEqual(0, derivedCode2);
                Assert.NotEqual(derivedCode1, derivedCode2);

                // Verify that we can compute hash codes for models
                // with multiple hash source properties.

                var defaultValues = context.CreateDataWrapper <DefaultValues>();

                defaultValues["Name"] = null;
                defaultValues["Age"]  = 0;
                Assert.Equal(0, defaultValues.GetHashCode());

                defaultValues["Name"] = "JoeBob";
                defaultValues["Age"]  = 0;
                Assert.Equal("JoeBob".GetHashCode(), defaultValues.GetHashCode());

                defaultValues["Name"] = null;
                defaultValues["Age"]  = 67;
                Assert.Equal(67.GetHashCode(), defaultValues.GetHashCode());

                defaultValues["Name"] = "JoeBob";
                defaultValues["Age"]  = 67;
                Assert.Equal(67.GetHashCode() ^ "JoeBob".GetHashCode(), defaultValues.GetHashCode());
            }
        }
Ejemplo n.º 19
0
        public void Simple()
        {
            // Verify that we can generate code for a simple data model.

            var settings = new ModelGeneratorSettings()
            {
                SourceNamespace = typeof(Test_UxDataModel).Namespace,
                UxFramework     = UxFrameworks.Xaml
            };

            var generator = new ModelGenerator(settings);
            var output    = generator.Generate(Assembly.GetExecutingAssembly());

            Assert.False(output.HasErrors);

            var assemblyStream = CSharpHelper.Compile(output.SourceCode, "test-assembly", references => ModelGenTestHelper.ReferenceHandler(references));

            using (var context = new AssemblyContext("Neon.ModelGen.Output", assemblyStream))
            {
                var data = context.CreateDataWrapper <SimpleData>();
                Assert.Equal("{\"Name\":null,\"Age\":0,\"Enum\":\"One\"}", data.ToString());
                Assert.Equal("{\r\n  \"Name\": null,\r\n  \"Age\": 0,\r\n  \"Enum\": \"One\"\r\n}", data.ToString(indented: true));

                data         = context.CreateDataWrapper <SimpleData>();
                data["Name"] = "Jeff";
                data["Age"]  = 58;
                data["Enum"] = MyEnum1.Two;
                Assert.Equal("{\"Name\":\"Jeff\",\"Age\":58,\"Enum\":\"Two\"}", data.ToString());

                data         = context.CreateDataWrapperFrom <SimpleData>("{\"Name\":\"Jeff\",\"Age\":58,\"Enum\":\"Two\"}");
                data["Name"] = "Jeff";
                data["Age"]  = 58;
                data["Enum"] = MyEnum1.Two;
                Assert.Equal("{\"Name\":\"Jeff\",\"Age\":58,\"Enum\":\"Two\"}", data.ToString());

                var jObject = data.ToJObject();
                data         = context.CreateDataWrapperFrom <SimpleData>(jObject);
                data["Name"] = "Jeff";
                data["Age"]  = 58;
                data["Enum"] = MyEnum1.Two;
                Assert.Equal("{\"Name\":\"Jeff\",\"Age\":58,\"Enum\":\"Two\"}", data.ToString());

                var jsonText = data.ToString(indented: false);
                data         = context.CreateDataWrapperFrom <SimpleData>(jsonText);
                data["Name"] = "Jeff";
                data["Age"]  = 58;
                data["Enum"] = MyEnum1.Two;
                Assert.Equal("{\"Name\":\"Jeff\",\"Age\":58,\"Enum\":\"Two\"}", data.ToString());

                jsonText     = data.ToString(indented: true);
                data         = context.CreateDataWrapperFrom <SimpleData>(jsonText);
                data["Name"] = "Jeff";
                data["Age"]  = 58;
                data["Enum"] = MyEnum1.Two;
                Assert.Equal("{\"Name\":\"Jeff\",\"Age\":58,\"Enum\":\"Two\"}", data.ToString());

                //-------------------------------------------------------------
                // Verify Equals():

                var value1 = context.CreateDataWrapperFrom <SimpleData>("{\"Name\":\"Jeff\",\"Age\":58,\"Enum\":\"Two\"}");
                var value2 = context.CreateDataWrapperFrom <SimpleData>("{\"Name\":\"Jeff\",\"Age\":58,\"Enum\":\"Two\"}");

                Assert.True(value1.Equals(value1));
                Assert.True(value1.Equals(value2));
                Assert.True(value2.Equals(value1));

                Assert.False(value1.Equals(null));
                Assert.False(value1.Equals("Hello World!"));

                value2["Name"] = "Bob";

                Assert.True(value1.Equals(value1));

                Assert.False(value1.Equals(value2));
                Assert.False(value2.Equals(value1));
                Assert.False(value1.Equals(null));
                Assert.False(value1.Equals("Hello World!"));

                //-------------------------------------------------------------
                // Verify FromBytes() and ToBytes()

                var newValue1 = context.CreateDataWrapperFrom <SimpleData>(value1.ToBytes());
                var newValue2 = context.CreateDataWrapperFrom <SimpleData>(value2.ToBytes());

                Assert.True(value1.Equals(newValue1));
                Assert.True(value2.Equals(newValue2));
            }
        }