Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 7575bee

Browse files
authored
Fix XmlSerializer timespan tests for any full framework version (#19564)
1 parent 573ac31 commit 7575bee

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

src/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ static XmlSerializerTests()
2727
{
2828
MethodInfo method = typeof(XmlSerializer).GetMethod(SerializationModeSetterName, BindingFlags.NonPublic | BindingFlags.Static);
2929
Assert.True(method != null, $"No method named {SerializationModeSetterName}");
30-
method.Invoke(null, new object[] { 1 });
30+
method.Invoke(null, new object[] { 1 });
3131
}
3232
}
33-
3433
#endif
34+
35+
private static bool IsTimeSpanSerializationAvailable => !PlatformDetection.IsFullFramework || (AppContext.TryGetSwitch("Switch.System.Xml.EnableTimeSpanSerialization", out bool result) && result);
36+
3537
[Fact]
3638
public static void Xml_BoolAsRoot()
3739
{
@@ -139,7 +141,7 @@ public static void Xml_TypeWithDateTimePropertyAsXmlTime()
139141
Value = new DateTime(549269870000L, DateTimeKind.Utc)
140142
};
141143

142-
if (!PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer())
144+
if (IsTimeSpanSerializationAvailable)
143145
{
144146
TypeWithDateTimePropertyAsXmlTime utcTimeRoundTrip = SerializeAndDeserialize(utcTimeOjbect,
145147
@"<?xml version=""1.0"" encoding=""utf-8""?>
@@ -1632,54 +1634,45 @@ public static void Xml_DifferentSerializeDeserializeOverloads()
16321634
}
16331635
}
16341636

1635-
[Fact]
1637+
[ConditionalFact(nameof(IsTimeSpanSerializationAvailable))]
16361638
public static void Xml_TimeSpanAsRoot()
16371639
{
1638-
if (!PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx470OrNewer())
1639-
{
1640-
Assert.StrictEqual(new TimeSpan(1, 2, 3), SerializeAndDeserialize<TimeSpan>(new TimeSpan(1, 2, 3),
1641-
@"<?xml version=""1.0"" encoding=""utf-8""?>
1640+
Assert.StrictEqual(new TimeSpan(1, 2, 3), SerializeAndDeserialize<TimeSpan>(new TimeSpan(1, 2, 3),
1641+
@"<?xml version=""1.0"" encoding=""utf-8""?>
16421642
<TimeSpan>PT1H2M3S</TimeSpan>"));
1643-
Assert.StrictEqual(TimeSpan.Zero, SerializeAndDeserialize<TimeSpan>(TimeSpan.Zero,
1644-
@"<?xml version=""1.0"" encoding=""utf-8""?>
1643+
Assert.StrictEqual(TimeSpan.Zero, SerializeAndDeserialize<TimeSpan>(TimeSpan.Zero,
1644+
@"<?xml version=""1.0"" encoding=""utf-8""?>
16451645
<TimeSpan>PT0S</TimeSpan>"));
1646-
Assert.StrictEqual(TimeSpan.MinValue, SerializeAndDeserialize<TimeSpan>(TimeSpan.MinValue,
1647-
@"<?xml version=""1.0"" encoding=""utf-8""?>
1646+
Assert.StrictEqual(TimeSpan.MinValue, SerializeAndDeserialize<TimeSpan>(TimeSpan.MinValue,
1647+
@"<?xml version=""1.0"" encoding=""utf-8""?>
16481648
<TimeSpan>-P10675199DT2H48M5.4775808S</TimeSpan>"));
1649-
Assert.StrictEqual(TimeSpan.MaxValue, SerializeAndDeserialize<TimeSpan>(TimeSpan.MaxValue,
1650-
@"<?xml version=""1.0"" encoding=""utf-8""?>
1649+
Assert.StrictEqual(TimeSpan.MaxValue, SerializeAndDeserialize<TimeSpan>(TimeSpan.MaxValue,
1650+
@"<?xml version=""1.0"" encoding=""utf-8""?>
16511651
<TimeSpan>P10675199DT2H48M5.4775807S</TimeSpan>"));
1652-
}
16531652
}
16541653

1655-
[Fact]
1654+
[ConditionalFact(nameof(IsTimeSpanSerializationAvailable))]
16561655
public static void Xml_TypeWithTimeSpanProperty()
16571656
{
1658-
if (!PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx470OrNewer())
1659-
{
1660-
var obj = new TypeWithTimeSpanProperty { TimeSpanProperty = TimeSpan.FromMilliseconds(1) };
1661-
var deserializedObj = SerializeAndDeserialize(obj,
1657+
var obj = new TypeWithTimeSpanProperty { TimeSpanProperty = TimeSpan.FromMilliseconds(1) };
1658+
var deserializedObj = SerializeAndDeserialize(obj,
16621659
@"<?xml version=""1.0"" encoding=""utf-16""?>
16631660
<TypeWithTimeSpanProperty xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
1664-
<TimeSpanProperty>PT0.001S</TimeSpanProperty>
1661+
<TimeSpanProperty>PT0.001S</TimeSpanProperty>
16651662
</TypeWithTimeSpanProperty>");
1666-
Assert.StrictEqual(obj.TimeSpanProperty, deserializedObj.TimeSpanProperty);
1667-
}
1663+
Assert.StrictEqual(obj.TimeSpanProperty, deserializedObj.TimeSpanProperty);
16681664
}
16691665

1670-
[Fact]
1666+
[ConditionalFact(nameof(IsTimeSpanSerializationAvailable))]
16711667
public static void Xml_TypeWithDefaultTimeSpanProperty()
16721668
{
1673-
if (!PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx470OrNewer())
1674-
{
1675-
var obj = new TypeWithDefaultTimeSpanProperty { TimeSpanProperty2 = new TimeSpan(0, 1, 0) };
1676-
var deserializedObj = SerializeAndDeserialize(obj,
1677-
@"<?xml version=""1.0""?>
1669+
var obj = new TypeWithDefaultTimeSpanProperty { TimeSpanProperty2 = new TimeSpan(0, 1, 0) };
1670+
var deserializedObj = SerializeAndDeserialize(obj,
1671+
@"<?xml version=""1.0""?>
16781672
<TypeWithDefaultTimeSpanProperty xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><TimeSpanProperty2>PT1M</TimeSpanProperty2></TypeWithDefaultTimeSpanProperty>");
1679-
Assert.NotNull(deserializedObj);
1680-
Assert.Equal(obj.TimeSpanProperty, deserializedObj.TimeSpanProperty);
1681-
Assert.Equal(obj.TimeSpanProperty2, deserializedObj.TimeSpanProperty2);
1682-
}
1673+
Assert.NotNull(deserializedObj);
1674+
Assert.Equal(obj.TimeSpanProperty, deserializedObj.TimeSpanProperty);
1675+
Assert.Equal(obj.TimeSpanProperty2, deserializedObj.TimeSpanProperty2);
16831676
}
16841677

16851678
[Fact]

0 commit comments

Comments
 (0)