Skip to content

Commit 477b5f3

Browse files
authored
Fix cross framework incompatibility for System.Drawing.Color (#208)
* Fix cross framework problem with System.Drawing * Merge PR #206 test case * Generalize type package name transformation for cross framework compatibility * Define compiler directives that are missing in CI/CD * Change class based configuration to lambda function
1 parent bd3a432 commit 477b5f3

15 files changed

+104
-20
lines changed

src/Hyperion.Tests/Bugs.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Collections;
1212
using System.Collections.Generic;
1313
using System.Collections.Immutable;
14+
using System.Drawing;
1415
using System.IO;
1516
using System.Linq;
1617
using System.Reflection;
@@ -24,7 +25,7 @@
2425
namespace Hyperion.Tests
2526
{
2627

27-
public class Bugs
28+
public class Bugs : TestBase
2829
{
2930
private readonly ITestOutputHelper _output;
3031

@@ -218,6 +219,19 @@ public void CanSerializeUri()
218219
Assert.Equal(stream.Length, stream.Position);
219220
}
220221

222+
#region Issue 117
223+
224+
[Fact]
225+
public void CanSerializeColor()
226+
{
227+
var expected = Color.Aquamarine;
228+
Serialize(expected);
229+
Reset();
230+
var actual = Deserialize<Color>();
231+
Assert.Equal(expected, actual);
232+
}
233+
234+
#endregion
221235

222236
public class SnapshotSelectionCriteria
223237
{

src/Hyperion.Tests/CrossFrameworkSerializationTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
6+
using System.Runtime.InteropServices;
57
using FluentAssertions;
68
using Hyperion.Tests.Generator;
79
using Xunit;
@@ -19,9 +21,21 @@ public class CrossFrameworkSerializationTests
1921
public CrossFrameworkSerializationTests(ITestOutputHelper log)
2022
{
2123
_log = log;
22-
_serializer = new Serializer();
2324
_originalObject = CrossFrameworkInitializer.Init();
2425
_originalMixedObject = CrossFrameworkInitializer.InitMixed();
26+
27+
// Demonstrating the use of custom dll package name override
28+
// to convert netcore System.Drawing.Primitives to netfx
29+
// System.Drawing package.
30+
#if NETFX
31+
_serializer = new Serializer(new SerializerOptions(
32+
packageNameOverrides: new List<Func<string, string>>
33+
{
34+
str => str.Contains("System.Drawing.Primitives") ? str.Replace(".Primitives", "") : str
35+
}));
36+
#elif NETCOREAPP
37+
_serializer = new Serializer();
38+
#endif
2539
}
2640

2741
public static IEnumerable<object[]> SerializationFiles()

src/Hyperion.Tests/Generator/CrossFrameworkClass.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
24

35
namespace Hyperion.Tests.Generator
46
{
@@ -144,6 +146,15 @@ public class CrossFrameworkMixedClass : CrossFrameworkBase
144146
public Type FriendType { get; set; }
145147
public CrossFrameworkClass Data { get; set; }
146148

149+
// Test case for (netcore) System.Drawing.Primitives to (net45) System.Drawing
150+
public Color Color { get; set; }
151+
public Point Point { get; set; }
152+
public PointF PointF { get; set; }
153+
public Rectangle Rectangle { get; set; }
154+
public RectangleF RectangleF { get; set; }
155+
public Size Size { get; set; }
156+
public SizeF SizeF { get; set; }
157+
147158
public override bool Equals(object obj)
148159
{
149160
if (!(obj is CrossFrameworkMixedClass other))

src/Hyperion.Tests/Generator/CrossFrameworkInitializer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
24

35
namespace Hyperion.Tests.Generator
46
{
@@ -13,6 +15,13 @@ public static CrossFrameworkMixedClass InitMixed()
1315
Name = "Cookie",
1416
Sound = "Bark",
1517
FriendType = typeof(CrossFrameworkClass),
18+
Color = Color.Blue,
19+
Point = new Point(10, 10),
20+
PointF = new PointF(10, 10),
21+
Rectangle = new Rectangle(10, 10, 10, 10),
22+
RectangleF = new RectangleF(10, 10, 10, 10),
23+
Size = new Size(10, 10),
24+
SizeF = new SizeF(10, 10),
1625
Data = Init()
1726
};
1827
}

src/Hyperion.Tests/Hyperion.Tests.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
<StartupObject>Hyperion.Tests.Generator.Program</StartupObject>
1010
</PropertyGroup>
1111

12+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net5.0' ">
13+
<DefineConstants>$(DefineConstants);NETCOREAPP</DefineConstants>
14+
</PropertyGroup>
15+
16+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net461' ">
17+
<DefineConstants>$(DefineConstants);NETFX</DefineConstants>
18+
</PropertyGroup>
19+
1220
<ItemGroup>
1321
<PackageReference Include="FluentAssertions" Version="5.10.3" />
1422
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
@@ -22,4 +30,9 @@
2230
<ProjectReference Include="..\Hyperion\Hyperion.csproj" />
2331
</ItemGroup>
2432

33+
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
34+
<Reference Include="System.Drawing">
35+
<Private>true</Private>
36+
</Reference>
37+
</ItemGroup>
2538
</Project>
450 Bytes
Binary file not shown.
450 Bytes
Binary file not shown.
450 Bytes
Binary file not shown.
450 Bytes
Binary file not shown.
450 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)