public void CreateInstanceWithArray()
        {
            // Arrange
            var type    = typeof(ArrayDummy);
            var encoded = EntryConvert.EncodeClass(type);

            var entry1 = encoded.SubEntries[0];
            var entry2 = encoded.SubEntries[1];

            for (var i = 1; i <= 5; i++)
            {
                var newInstance = entry1.Prototypes[0].Instantiate();
                newInstance.Value.Current = (1 + i).ToString();
                entry1.SubEntries.Add(newInstance);
            }

            for (var i = 1; i <= 5; i++)
            {
                var newInstance = entry2.Prototypes[0].Instantiate();
                newInstance.Value.Current = "Number: " + i;
                entry2.SubEntries.Add(newInstance);
            }

            // Act
            var dummy = EntryConvert.CreateInstance <ArrayDummy>(encoded);

            // Assert
            for (var i = 1; i <= 5; i++)
            {
                Assert.AreEqual(i + 1, dummy.Array[i - 1]);
                Assert.AreEqual("Number: " + i, dummy.Keys[i - 1]);
            }
        }
        public void PropertyLocalization()
        {
            var resourceManager  = new ResourceManager(typeof(strings));
            var germanCulture    = new CultureInfo("de");
            var invariantCulture = new CultureInfo("en");

            // Switch to german
            Thread.CurrentThread.CurrentUICulture = germanCulture;
            var expectedDisplayPropName        = resourceManager.GetString(nameof(strings.PropDisplayAttribute_Name), germanCulture);
            var expectedDisplayPropDescription = resourceManager.GetString(nameof(strings.PropDisplayAttribute_Description), germanCulture);
            var encoded = EntryConvert.EncodeClass(typeof(LocalizedClass));

            Assert.AreEqual(expectedDisplayPropName, encoded.SubEntries[0].DisplayName);
            Assert.AreEqual(expectedDisplayPropDescription, encoded.SubEntries[0].Description);
            Assert.AreEqual(LocalizedClass.PropDisplayNameAttributeDisplayName, encoded.SubEntries[1].DisplayName);

            // Switch to invariant
            Thread.CurrentThread.CurrentUICulture = invariantCulture;
            expectedDisplayPropName        = resourceManager.GetString(nameof(strings.PropDisplayAttribute_Name), invariantCulture);
            expectedDisplayPropDescription = resourceManager.GetString(nameof(strings.PropDisplayAttribute_Description), invariantCulture);
            encoded = EntryConvert.EncodeClass(typeof(LocalizedClass));

            Assert.AreEqual(expectedDisplayPropName, encoded.SubEntries[0].DisplayName);
            Assert.AreEqual(expectedDisplayPropDescription, encoded.SubEntries[0].Description);
            Assert.AreEqual(LocalizedClass.PropDisplayNameAttributeDisplayName, encoded.SubEntries[1].DisplayName);
        }
        public void PrimitiveCollectionPrototypes(bool possibleValues)
        {
            // Arrange
            var serialization = possibleValues
                ? new PossibleValuesSerialization(null, new ValueProviderExecutor(new ValueProviderExecutorSettings().AddDefaultValueProvider()))
                : (ICustomSerialization) new DefaultSerialization();

            // Act
            var encoded = EntryConvert.EncodeClass(typeof(ListDummy), serialization);

            // Assert
            var doubles = encoded.SubEntries[1];

            Assert.AreEqual(nameof(EntryValueType.Double), doubles.Value.Possible[0]);
            Assert.AreEqual(1, doubles.Prototypes.Count);
            if (possibleValues)
            {
                Assert.AreEqual(3, doubles.Prototypes[0].Value.Possible.Length);
            }
            else
            {
                Assert.IsNull(doubles.Prototypes[0].Value.Possible);
            }


            var enums = encoded.SubEntries[2];

            Assert.AreEqual(3, enums.Value.Possible.Length);
            Assert.AreEqual(3, enums.Prototypes.Count);
            for (int i = 0; i < 3; i++)
            {
                Assert.AreEqual(enums.Value.Possible[i], enums.Prototypes[i].Value.Current);
            }
        }
Exemple #4
0
 public ProductDefinitionModel ConvertProductType(Type productType)
 {
     return(new()
     {
         Name = productType.Name,
         DisplayName = productType.GetDisplayName() ?? productType.Name,
         BaseDefinition = productType.BaseType?.Name,
         Properties = EntryConvert.EncodeClass(productType, ProductSerialization)
     });
 }
        public void EncodeType()
        {
            // Arrange
            var type    = typeof(DummyClass);
            var subType = typeof(SubClass);

            // Act
            var encoded    = EntryConvert.EncodeClass(type).SubEntries;
            var encodedSub = EntryConvert.EncodeClass(subType).SubEntries;

            // Assert
            DummyAssert(encoded, encodedSub);
        }
        public void CreateInstance()
        {
            // Arrange
            var defaultSerialization = new DefaultSerialization();
            var encoded = EntryConvert.EncodeClass(typeof(DummyClass), defaultSerialization);

            // Act
            encoded.SubEntries[0].Value.Current = "10";
            encoded.SubEntries[1].Value.Current = "Thomas";
            encoded.SubEntries[3].SubEntries[1].Value.Current = encoded.SubEntries[3].SubEntries[1].Value.Possible[2];
            for (var i = 4; i < 7; i++)
            {
                var colEntry = encoded.SubEntries[i];
                for (var j = 0; j < i; j++)
                {
                    var newInstance = colEntry.Prototypes[0].Instantiate();
                    newInstance.SubEntries[0].Value.Current = j.ToString("F2", defaultSerialization.FormatProvider);
                    newInstance.SubEntries[1].Value.Current = newInstance.SubEntries[1].Value.Possible[1];
                    colEntry.SubEntries.Add(newInstance);
                }
            }
            var obj = EntryConvert.CreateInstance <DummyClass>(encoded);

            // Assert
            Assert.AreEqual(10, obj.Number);
            Assert.AreEqual("Thomas", obj.Name);
            Assert.AreEqual(DummyEnum.ValueB, obj.SingleClass.Enum);
            var colAssert = new[] { CollectionType.Array, CollectionType.List, CollectionType.Enumerable };

            for (var i = 0; i < colAssert.Length; i++)
            {
                var length     = 4 + i;
                var collection = ExtractCollection(colAssert[i], obj);
                Assert.AreEqual(length, collection.Count);

                if (colAssert[i] == CollectionType.Dictionary)
                {
                }
                else
                {
                    var array = (collection as IEnumerable <SubClass>).ToArray();
                    for (var j = 0; j < length; j++)
                    {
                        Assert.AreEqual((float)j, array[j].Foo);
                        Assert.AreEqual(DummyEnum.ValueA, array[j].Enum);
                    }
                }
            }
        }
Exemple #7
0
        private void ConvertParts(IProductType productType, IEnumerable <PropertyInfo> properties, ProductModel converted)
        {
            var connectors = new List <PartConnector>();

            foreach (var property in properties)
            {
                var displayName = property.GetDisplayName();

                if (typeof(IProductPartLink).IsAssignableFrom(property.PropertyType))
                {
                    var link      = (IProductPartLink)property.GetValue(productType);
                    var partModel = ConvertPart(link);
                    var connector = new PartConnector
                    {
                        Name              = property.Name,
                        DisplayName       = displayName,
                        Type              = FetchProductType(property.PropertyType),
                        Parts             = partModel is null ? new PartModel[0] : new[] { partModel },
                        PropertyTemplates = EntryConvert.EncodeClass(property.PropertyType, ProductSerialization)
                    };
                    connectors.Add(connector);
                }
                else if (typeof(IEnumerable <IProductPartLink>).IsAssignableFrom(property.PropertyType))
                {
                    var links     = (IEnumerable <IProductPartLink>)property.GetValue(productType);
                    var linkType  = property.PropertyType.GetGenericArguments()[0];
                    var connector = new PartConnector
                    {
                        IsCollection      = true,
                        Name              = property.Name,
                        DisplayName       = displayName,
                        Type              = FetchProductType(linkType),
                        Parts             = links?.Select(ConvertPart).ToArray(),
                        PropertyTemplates = EntryConvert.EncodeClass(linkType, ProductSerialization)
                    };
                    connectors.Add(connector);
                }
            }
            converted.Parts = connectors.ToArray();
        }
        public void CreateInstanceWithPrimitiveList()
        {
            // Arrange
            var type    = typeof(ListDummy);
            var encoded = EntryConvert.EncodeClass(type);
            var ent     = encoded.SubEntries[1];

            for (var i = 1; i <= 5; i++)
            {
                var newInstance = ent.Prototypes[0].Instantiate();
                newInstance.Value.Current = (1 + i).ToString();
                ent.SubEntries.Add(newInstance);
            }

            // Act
            var listDummy = EntryConvert.CreateInstance <ListDummy>(encoded);

            // Assert
            for (var i = 1; i <= 5; i++)
            {
                Assert.AreEqual(i + 1, listDummy.DoubleList[i - 1]);
            }
        }
        public void CreateInstanceWithDictionary()
        {
            // Arrange
            var type    = typeof(DictionaryClass);
            var encoded = EntryConvert.EncodeClass(type);

            var entry1 = encoded.SubEntries[1];
            var entry2 = encoded.SubEntries[2];

            for (var i = 1; i <= 5; i++)
            {
                var newInstance = entry1.Prototypes[0].Instantiate();
                newInstance.Value.Current = (1 + i).ToString();
                newInstance.DisplayName   = "Key" + i;
                entry1.SubEntries.Add(newInstance);
            }

            for (var i = 1; i <= 3; i++)
            {
                var newInstance = entry2.Prototypes[0].Instantiate();
                newInstance.Value.Current = ((DummyEnum)(i % 3)).ToString();
                newInstance.DisplayName   = "Key_0121" + i;
                entry2.SubEntries.Add(newInstance);
            }

            // Act
            var dummy = EntryConvert.CreateInstance <DictionaryClass>(encoded);

            // Assert
            for (var i = 1; i <= 5; i++)
            {
                Assert.AreEqual(3, dummy.EnumDictionary.Count);
                Assert.AreEqual(5, dummy.SubDictionary.Count);
                Assert.AreEqual(6, dummy.SubDictionary["Key5"]);
                Assert.AreEqual(DummyEnum.Unset, dummy.EnumDictionary["Key_01213"]);
            }
        }