Skip to content

Commit

Permalink
Add .NET Helper (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Dec 16, 2022
1 parent ad2f1a4 commit c3d355a
Show file tree
Hide file tree
Showing 34 changed files with 5,111 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
check_package_lock:
name: nanoFramework
uses: nanoframework/nf-tools/.github/workflows/check-package-lock.yml@main
with:
solution: 'nanoFramework.System.Runtime.Serialization.sln'
check_nuget_latest:
name: nanoFramework
uses: nanoframework/nf-tools/.github/workflows/check-packages-updated.yml@main
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/update-dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ name: Update .NET nanoFramework dependencies

on:
schedule:
# At 00:00 UTC every day.
- cron: '00 00 * * *'
# At 00:00 UTC every wednesday.
- cron: '00 00 * * Wed'
repository_dispatch:
types: update-dependencies

Expand Down
15 changes: 15 additions & 0 deletions .runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
<MaxCpuCount>1</MaxCpuCount>
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
<TargetPlatform>x64</TargetPlatform>
</RunConfiguration>
<nanoFrameworkAdapter>
<Logging>None</Logging>
<IsRealHardware>False</IsRealHardware>
</nanoFrameworkAdapter>
</RunSettings>
16 changes: 16 additions & 0 deletions Serialization.Shared/Serialization.Shared.projitems
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>88d19908-57c1-4b09-9993-de00cee88bf4</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>Serialization.Shared</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)FieldNoReflectionAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SerializationHintsAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SerializationOptions.cs" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions Serialization.Shared/Serialization.Shared.shproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>88d19908-57c1-4b09-9993-de00cee88bf4</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="Serialization.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
109 changes: 109 additions & 0 deletions Tests/HelperTests/DeserializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

using System.Collections;

#if NANOFRAMEWORK_1_0
using nanoFramework.TestFramework;
using System;
using BinaryFormatter = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using nanoFramework.Serialization.Helper;
#endif

namespace nanoFramework.System.Runtime.Serialization.Tests
{
[TestClass]
public class DeserializationTests
{
[TestMethod]
public void DeserializePersonClassTest()
{
// serialize person class
var personeOne = UnitTestHelper.CreatePersonOne();

#if NANOFRAMEWORK_1_0
var newPersoneOne = BinaryFormatter.Deserialize(UnitTestHelper.PersonOneSerialized) as Person;
#else
var binaryFormatter = new BinaryFormatter();
var newPersoneOne = binaryFormatter.Deserialize(UnitTestHelper.PersonOneSerialized) as Person;
#endif

Assert.IsNotNull(newPersoneOne);
Assert.AreEqual(newPersoneOne.LastName, personeOne.LastName);
Assert.AreEqual(newPersoneOne.FirstName, personeOne.FirstName);
Assert.AreEqual(newPersoneOne.ID, personeOne.ID);
Assert.AreEqual(newPersoneOne.Address, personeOne.Address);
Assert.AreEqual(newPersoneOne.Birthday, personeOne.Birthday);
Assert.AreEqual(newPersoneOne.Friend.LastName, personeOne.Friend.LastName);
Assert.AreEqual(newPersoneOne.Friend.FirstName, personeOne.Friend.FirstName);
Assert.AreEqual(newPersoneOne.Friend.ID, personeOne.Friend.ID);
Assert.AreEqual(newPersoneOne.Friend.Address, personeOne.Friend.Address);
Assert.AreEqual(newPersoneOne.Friend.Birthday, personeOne.Friend.Birthday);
}

[TestMethod]
public void DeserializeComplexClassTest()
{
// serialize Complex class
var complexeClassOne = UnitTestHelper.CreateComplexClassOne();

#if NANOFRAMEWORK_1_0
var newComplexeClassOne = BinaryFormatter.Deserialize(UnitTestHelper.ComplexClassOneSerialized) as ComplexClass;
#else
var binaryFormatter = new BinaryFormatter();
var newComplexeClassOne = binaryFormatter.Deserialize(UnitTestHelper.ComplexClassOneSerialized) as ComplexClass;
#endif

Assert.IsNotNull(newComplexeClassOne);
Assert.AreEqual(newComplexeClassOne.aInteger, complexeClassOne.aInteger);
Assert.AreEqual(newComplexeClassOne.aShort, complexeClassOne.aShort);
Assert.AreEqual(newComplexeClassOne.aByte, complexeClassOne.aByte);
Assert.AreEqual(newComplexeClassOne.aString, complexeClassOne.aString);
Assert.AreEqual(newComplexeClassOne.aFloat, complexeClassOne.aFloat);
Assert.AreEqual(newComplexeClassOne.aDouble, complexeClassOne.aDouble);
Assert.AreEqual(newComplexeClassOne.aBoolean, complexeClassOne.aBoolean);
Assert.AreEqual(newComplexeClassOne.Timestamp, complexeClassOne.Timestamp);
Assert.AreEqual(newComplexeClassOne.FixedTimestamp, complexeClassOne.FixedTimestamp);
Assert.AreEqual(newComplexeClassOne.nullObject, complexeClassOne.nullObject);
Assert.AreEqual(newComplexeClassOne.nanFloat, complexeClassOne.nanFloat);
Assert.AreEqual(newComplexeClassOne.nanDouble, complexeClassOne.nanDouble);

CollectionAssert.AreEqual(newComplexeClassOne.intArray, complexeClassOne.intArray);
CollectionAssert.AreEqual(newComplexeClassOne.shortArray, complexeClassOne.shortArray);
CollectionAssert.AreEqual(newComplexeClassOne.byteArray, complexeClassOne.byteArray);
CollectionAssert.AreEqual(newComplexeClassOne.stringArray, complexeClassOne.stringArray);
CollectionAssert.AreEqual(newComplexeClassOne.floatArray, complexeClassOne.floatArray);
CollectionAssert.AreEqual(newComplexeClassOne.doubleArray, complexeClassOne.doubleArray);

Assert.AreEqual(newComplexeClassOne.Child.one, complexeClassOne.Child.one);
Assert.AreEqual(newComplexeClassOne.Child.two, complexeClassOne.Child.two);
Assert.AreEqual(newComplexeClassOne.Child.three, complexeClassOne.Child.three);

Assert.AreEqual(newComplexeClassOne.child1.one, complexeClassOne.child1.one);
Assert.AreEqual(newComplexeClassOne.child1.two, complexeClassOne.child1.two);
Assert.AreEqual(newComplexeClassOne.child1.three, complexeClassOne.child1.three);
}

[TestMethod]
public void DeserializeArrayListTest()
{
// serialize array list
var personeOne = UnitTestHelper.CreateArrayListOne();

#if NANOFRAMEWORK_1_0
var newArrayListeOne = BinaryFormatter.Deserialize(UnitTestHelper.ArrayListOneSerialized) as ArrayList;
#else
var binaryFormatter = new BinaryFormatter();
var newArrayListeOne = binaryFormatter.Deserialize(UnitTestHelper.ArrayListOneSerialized) as ArrayList;
#endif

Assert.IsNotNull(newArrayListeOne);
CollectionAssert.AreEqual(newArrayListeOne, personeOne);
}
}
}
27 changes: 27 additions & 0 deletions Tests/HelperTests/HelperTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<RestoreLockedMode Condition="'$(TF_BUILD)' == 'True' or '$(ContinuousIntegrationBuild)' == 'True'">true</RestoreLockedMode>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\nanoFramework.Serialization.Helper\nanoFramework.Serialization.Helper.csproj" />
</ItemGroup>

<Import Project="..\UnitTests.Shared\UnitTests.Shared.projitems" Label="Shared" />

<Import Project="..\..\Serialization.Shared\Serialization.Shared.projitems" Label="Shared" />

</Project>
77 changes: 77 additions & 0 deletions Tests/HelperTests/SerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

#if NANOFRAMEWORK_1_0
using nanoFramework.TestFramework;
using BinaryFormatter = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using nanoFramework.Serialization.Helper;
#endif

namespace nanoFramework.System.Runtime.Serialization.Tests
{
[TestClass]
public class SerializationTests
{
[TestMethod]
public void SerializePersonClassTest()
{
// serialize person class
var personeOne = UnitTestHelper.CreatePersonOne();

#if NANOFRAMEWORK_1_0
var serializedPerson = BinaryFormatter.Serialize(personeOne);
#else
var binaryFormatter = new BinaryFormatter();
var serializedPerson = binaryFormatter.Serialize(personeOne);
#endif

Assert.IsNotNull(serializedPerson);
Assert.IsTrue(serializedPerson.Length == UnitTestHelper.PersonOneSerialized.Length, "PersonOne serialized data has different length.");

CollectionAssert.AreEqual(UnitTestHelper.PersonOneSerialized, serializedPerson);
}

[TestMethod]
public void SerializeComplexClassTest()
{
// serialize complex class
var complexClassOne = UnitTestHelper.CreateComplexClassOne();

#if NANOFRAMEWORK_1_0
var serializedComplexClass = BinaryFormatter.Serialize(complexClassOne);
#else
var binaryFormatter = new BinaryFormatter();
var serializedComplexClass = binaryFormatter.Serialize(complexClassOne);
#endif

Assert.IsNotNull(serializedComplexClass);
Assert.IsTrue(serializedComplexClass.Length == UnitTestHelper.ComplexClassOneSerialized.Length, "ComplexClass serialized data has different length.");

CollectionAssert.AreEqual(UnitTestHelper.ComplexClassOneSerialized, serializedComplexClass);
}

[TestMethod]
public void SerializeArrayListTest()
{
// serialize array list
var arrayListOne = UnitTestHelper.CreateArrayListOne();

#if NANOFRAMEWORK_1_0
var serializedArrayList = BinaryFormatter.Serialize(arrayListOne);
#else
var binaryFormatter = new BinaryFormatter();
var serializedArrayList = binaryFormatter.Serialize(arrayListOne);
#endif

Assert.IsNotNull(serializedArrayList);
Assert.IsTrue(serializedArrayList.Length == UnitTestHelper.ArrayListOneSerialized.Length, "ArrayListOne serialized data has different length.");

CollectionAssert.AreEqual(UnitTestHelper.ArrayListOneSerialized, serializedArrayList);
}
}
}
Loading

0 comments on commit c3d355a

Please sign in to comment.