public void FormatProviderTest(string cultureName) { // Arrange var formatProvider = new CultureInfo(cultureName); var serialization = new DefaultSerialization { FormatProvider = formatProvider }; var dummy = new DummyClass { Number = 1001, SingleClass = new SubClass { Foo = 1.1234f } }; // Act var encoded = EntryConvert.EncodeObject(dummy, serialization); var dummyDecoded = EntryConvert.CreateInstance <DummyClass>(encoded, serialization); // Assert Assert.AreEqual(1001.ToString(formatProvider), encoded.SubEntries[0].Value.Current); Assert.AreEqual(1.1234f.ToString(formatProvider), encoded.SubEntries[3].SubEntries[0].Value.Current); Assert.AreEqual(1001, dummyDecoded.Number); Assert.AreEqual(1.1234f, dummyDecoded.SingleClass.Foo); }
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 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); } } } }
public void CreateInstanceWithConstructor() { // Arrange var dummyType = typeof(ConstructorDummy); // Act var constructors = EntryConvert.EncodeConstructors(dummyType).ToArray(); var constructor = constructors.First(c => c.Parameters.SubEntries.Count == 1); constructor.Parameters.SubEntries[0].Value.Current = "42"; var instance = (ConstructorDummy)EntryConvert.CreateInstance(dummyType, constructor); // Assert Assert.NotNull(instance); Assert.AreEqual(42, instance.Foo); Assert.AreEqual(string.Empty, instance.Text, "EntryConvert did not pick the correct overload"); }
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"]); } }