Skip to content

Commit 407e4be

Browse files
Add DataContractSerializer tests for collection types
Co-authored-by: StephenMolloy <[email protected]>
1 parent 8b22062 commit 407e4be

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

src/libraries/System.Runtime.Serialization.Xml/tests/DataContractSerializer.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4573,4 +4573,104 @@ private static void TestObjectWithDifferentPayload<T>(T value, string netcorePay
45734573
Assert.NotNull(deserializedDesktopObject);
45744574
SerializationTestTypes.ComparisonHelper.CompareRecursively(value, deserializedDesktopObject);
45754575
}
4576+
4577+
[Fact]
4578+
public static void DCS_SortedList_IntSimpleType()
4579+
{
4580+
var value = new System.Collections.Generic.SortedList<int, SerializationTypes.SimpleType>();
4581+
value.Add(1, new SerializationTypes.SimpleType { P1 = "Test1", P2 = 10 });
4582+
value.Add(2, new SerializationTypes.SimpleType { P1 = "Test2", P2 = 20 });
4583+
var deserializedValue = DataContractSerializerHelper.SerializeAndDeserialize(value,
4584+
@"<ArrayOfKeyValueOfintSimpleType xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><KeyValueOfintSimpleType><Key>1</Key><Value xmlns:a=""http://schemas.datacontract.org/2004/07/SerializationTypes""><a:P1>Test1</a:P1><a:P2>10</a:P2></Value></KeyValueOfintSimpleType><KeyValueOfintSimpleType><Key>2</Key><Value xmlns:a=""http://schemas.datacontract.org/2004/07/SerializationTypes""><a:P1>Test2</a:P1><a:P2>20</a:P2></Value></KeyValueOfintSimpleType></ArrayOfKeyValueOfintSimpleType>",
4585+
skipStringCompare: true);
4586+
Assert.NotNull(deserializedValue);
4587+
Assert.Equal(value.Count, deserializedValue.Count);
4588+
Assert.Equal(value[1].P1, deserializedValue[1].P1);
4589+
Assert.Equal(value[1].P2, deserializedValue[1].P2);
4590+
Assert.Equal(value[2].P1, deserializedValue[2].P1);
4591+
Assert.Equal(value[2].P2, deserializedValue[2].P2);
4592+
}
4593+
4594+
[Fact]
4595+
public static void DCS_ObservableCollection_Int()
4596+
{
4597+
var value = new System.Collections.ObjectModel.ObservableCollection<int>();
4598+
value.Add(1);
4599+
value.Add(2);
4600+
value.Add(3);
4601+
var deserializedValue = DataContractSerializerHelper.SerializeAndDeserialize(value,
4602+
@"<ArrayOfint xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><int>1</int><int>2</int><int>3</int></ArrayOfint>",
4603+
skipStringCompare: true);
4604+
Assert.NotNull(deserializedValue);
4605+
Assert.Equal(value.Count, deserializedValue.Count);
4606+
Assert.Equal(value[0], deserializedValue[0]);
4607+
Assert.Equal(value[1], deserializedValue[1]);
4608+
Assert.Equal(value[2], deserializedValue[2]);
4609+
}
4610+
4611+
[Fact]
4612+
public static void DCS_ReadOnlyDictionary_IntString()
4613+
{
4614+
var dict = new Dictionary<int, string>();
4615+
dict[1] = "One";
4616+
dict[2] = "Two";
4617+
var value = new System.Collections.ObjectModel.ReadOnlyDictionary<int, string>(dict);
4618+
var deserializedValue = DataContractSerializerHelper.SerializeAndDeserialize(value,
4619+
@"<ReadOnlyDictionaryOfintstring xmlns=""http://schemas.datacontract.org/2004/07/System.Collections.ObjectModel"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><m_dictionary xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfintstring><a:Key>1</a:Key><a:Value>One</a:Value></a:KeyValueOfintstring><a:KeyValueOfintstring><a:Key>2</a:Key><a:Value>Two</a:Value></a:KeyValueOfintstring></m_dictionary></ReadOnlyDictionary>",
4620+
skipStringCompare: true);
4621+
Assert.NotNull(deserializedValue);
4622+
Assert.Equal(value.Count, deserializedValue.Count);
4623+
Assert.Equal(value[1], deserializedValue[1]);
4624+
Assert.Equal(value[2], deserializedValue[2]);
4625+
}
4626+
4627+
[Fact]
4628+
public static void DCS_ReadOnlyObservableCollection_Int()
4629+
{
4630+
var sourceCollection = new System.Collections.ObjectModel.ObservableCollection<int>();
4631+
sourceCollection.Add(1);
4632+
sourceCollection.Add(2);
4633+
sourceCollection.Add(3);
4634+
var value = new System.Collections.ObjectModel.ReadOnlyObservableCollection<int>(sourceCollection);
4635+
var deserializedValue = DataContractSerializerHelper.SerializeAndDeserialize(value,
4636+
@"<ReadOnlyObservableCollectionOfint xmlns=""http://schemas.datacontract.org/2004/07/System.Collections.ObjectModel"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><list xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:int>1</a:int><a:int>2</a:int><a:int>3</a:int></list></ReadOnlyObservableCollectionOfint>",
4637+
skipStringCompare: true);
4638+
Assert.NotNull(deserializedValue);
4639+
Assert.Equal(value.Count, deserializedValue.Count);
4640+
Assert.Equal(value[0], deserializedValue[0]);
4641+
Assert.Equal(value[1], deserializedValue[1]);
4642+
Assert.Equal(value[2], deserializedValue[2]);
4643+
}
4644+
4645+
[Fact]
4646+
public static void DCS_ListDictionary()
4647+
{
4648+
var value = new System.Collections.Specialized.ListDictionary();
4649+
value.Add("Key1", "Value1");
4650+
value.Add("Key2", "Value2");
4651+
var deserializedValue = DataContractSerializerHelper.SerializeAndDeserialize(value,
4652+
@"<ArrayOfKeyValueOfanyTypeanyType xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><KeyValueOfanyTypeanyType><Key i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">Key1</Key><Value i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">Value1</Value></KeyValueOfanyTypeanyType><KeyValueOfanyTypeanyType><Key i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">Key2</Key><Value i:type=""a:string"" xmlns:a=""http://www.w3.org/2001/XMLSchema"">Value2</Value></KeyValueOfanyTypeanyType></ArrayOfKeyValueOfanyTypeanyType>",
4653+
skipStringCompare: true);
4654+
Assert.NotNull(deserializedValue);
4655+
Assert.Equal(value.Count, deserializedValue.Count);
4656+
Assert.Equal(value["Key1"], deserializedValue["Key1"]);
4657+
Assert.Equal(value["Key2"], deserializedValue["Key2"]);
4658+
}
4659+
4660+
[Fact]
4661+
public static void DCS_StringCollection()
4662+
{
4663+
var value = new System.Collections.Specialized.StringCollection();
4664+
value.Add("First");
4665+
value.Add("Second");
4666+
value.Add("Third");
4667+
var deserializedValue = DataContractSerializerHelper.SerializeAndDeserialize(value,
4668+
@"<ArrayOfstring xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><string>First</string><string>Second</string><string>Third</string></ArrayOfstring>",
4669+
skipStringCompare: true);
4670+
Assert.NotNull(deserializedValue);
4671+
Assert.Equal(value.Count, deserializedValue.Count);
4672+
Assert.Equal(value[0], deserializedValue[0]);
4673+
Assert.Equal(value[1], deserializedValue[1]);
4674+
Assert.Equal(value[2], deserializedValue[2]);
4675+
}
45764676
}

0 commit comments

Comments
 (0)