Skip to content

Commit

Permalink
Merge pull request #147 from Tynamix/develop
Browse files Browse the repository at this point in the history
Release 1.5.9
  • Loading branch information
Tynamix authored Aug 2, 2024
2 parents b28d04b + dbaebaa commit fc1bae7
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 189 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: build-and-test

on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop", "master" ]

jobs:
build_and_test:
name: Build and Test .NET Libraries
runs-on: windows-latest

strategy:
matrix:
dotnet-version: [8.0.x, 4.8]

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup .NET 8
if: matrix.dotnet-version == '8.0.x'
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet-version }}

- name: Setup .NET Framework 4.8
if: matrix.dotnet-version == '4.8'
run: echo "Using pre-installed .NET Framework 4.8"

- name: Restore dependencies .NET 8
if: matrix.dotnet-version == '8.0.x'
run: dotnet restore

- name: Restore dependencies .NET 4.8
if: matrix.dotnet-version == '4.8'
run: nuget restore ObjectFillerNET.sln

- name: Build .NET 8
if: matrix.dotnet-version == '8.0.x'
run: dotnet build --configuration Release --no-restore

- name: Build .NET 4.8
if: matrix.dotnet-version == '4.8'
run: |
&"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\bin\MSBuild.exe" ObjectFillerNET.sln /p:Configuration=Release
- name: Test .NET 8
if: matrix.dotnet-version == '8.0.x'
run: dotnet test --configuration Release --no-build --verbosity normal

- name: Test .NET 4.8
if: matrix.dotnet-version == '4.8'
run: |
&"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" Tynamix.ObjectFiller.Test\bin\Release\net48\Tynamix.ObjectFiller.Test.dll
95 changes: 95 additions & 0 deletions .github/workflows/publish-to-nuget.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json

name: publish-to-nuget
on:
workflow_dispatch: # Allow running the workflow manually from the GitHub UI
push:
branches:
- 'master' # Run the workflow when pushing to the main branch

env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_NOLOGO: true
NuGetDirectory: ${{ github.workspace}}/nuget

defaults:
run:
shell: pwsh

jobs:
create_nuget:
runs-on: windows-latest
steps:
- name: checkout
uses: actions/checkout@v3

# Install the .NET SDK indicated in the global.json file
- name: Setup .NET
uses: actions/setup-dotnet@v4

# Create the NuGet package in the folder from the environment variable NuGetDirectory
- run: dotnet pack --configuration Release --output ${{ env.NuGetDirectory }}

# Publish the NuGet package as an artifact, so they can be used in the following jobs
- uses: actions/upload-artifact@v3
with:
name: nuget
if-no-files-found: error
retention-days: 7
path: ${{ env.NuGetDirectory }}/*.nupkg

validate_nuget:
runs-on: windows-latest
needs: [ create_nuget ]
steps:
# Install the .NET SDK indicated in the global.json file
- name: Setup .NET
uses: actions/setup-dotnet@v4

# Download the NuGet package created in the previous job
- uses: actions/download-artifact@v3
with:
name: nuget
path: ${{ env.NuGetDirectory }}

- name: Install nuget validator
run: dotnet tool update Meziantou.Framework.NuGetPackageValidation.Tool --global

# Validate metadata and content of the NuGet package
# https://www.nuget.org/packages/Meziantou.Framework.NuGetPackageValidation.Tool#readme-body-tab
# If some rules are not applicable, you can disable them
# using the --excluded-rules or --excluded-rule-ids option
- name: Validate package
run: meziantou.validate-nuget-package (Get-ChildItem "${{ env.NuGetDirectory }}/*.nupkg")

run_test:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v4
- name: Run tests
run: dotnet test --configuration Release

deploy:
runs-on: windows-latest
needs: [ validate_nuget, run_test ]
steps:
# Download the NuGet package created in the previous job
- uses: actions/download-artifact@v3
with:
name: nuget
path: ${{ env.NuGetDirectory }}

# Install the .NET SDK indicated in the global.json file
- name: Setup .NET Core
uses: actions/setup-dotnet@v4

# Publish all NuGet packages to NuGet.org
# Use --skip-duplicate to prevent errors if a package with the same version already exists.
# If you retry a failed workflow, already published packages will be skipped without error.
- name: Publish NuGet package
run: |
foreach($file in (Get-ChildItem "${{ env.NuGetDirectory }}" -Recurse -Include *.nupkg)) {
dotnet nuget push $file --api-key "${{ secrets.NUGET_API_KEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,4 @@ ObjectFiller.Test/ObjectFiller.Test.v2.ncrunchproject
.vs/config/applicationhost.config
Output-Build.txt
.vs/
.idea/
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tynamix.ObjectFiller.Test.BugfixTests
{
internal abstract class EntityBase
{
public string Name { get; set; }
public abstract string AbstractName { get; set; }
}

internal class CustomEntity : EntityBase
{
public override string AbstractName { get; set; }
}

[TestClass]
public class Bug143InheritedAbstractPropertiesAreNotFilled
{
[TestMethod]
public void InheritedAbstractPropertiesShouldBeFilled()
{
Filler<CustomEntity> filler = new Filler<CustomEntity>();
filler
.Setup(true)
.OnProperty(x => x.Name).Use("Alice")
.OnProperty(x => x.AbstractName).Use("John");
var entity = filler.Create();

Assert.IsNotNull(entity);
Assert.AreEqual("Alice", entity.Name);
Assert.AreEqual("John", entity.AbstractName);
}
}
}
69 changes: 45 additions & 24 deletions Tynamix.ObjectFiller/NetTypeApiExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ internal static bool IsAbstract(this Type source)
return source.IsAbstract;
#endif
}

internal static bool IsAbstract(this PropertyInfo source)
{
#if NETSTANDARD
return source.GetMethod.IsAbstract;
#else
var methodInfo = source.GetGetMethod() ?? source.GetSetMethod();
return methodInfo != null && methodInfo.IsAbstract;
#endif
}

internal static Type GetBaseType(this Type source)
{
#if NETSTANDARD
return source.GetTypeInfo().BaseType;
#else
return source.BaseType;
#endif
}

internal static IEnumerable<Type> GetImplementedInterfaces(this Type source)
{
Expand All @@ -100,50 +119,52 @@ internal static IEnumerable<Type> GetImplementedInterfaces(this Type source)

internal static IEnumerable<PropertyInfo> GetProperties(this Type source, bool ignoreInheritance)
{
#if NETSTANDARD

if (ignoreInheritance)
{
return source.GetTypeInfo().DeclaredProperties.ToList();
return GetOwnProperties(source);
}

return GetDeclaredPropertyInfosRecursive(new List<PropertyInfo>(), source.GetTypeInfo());
#else

if (ignoreInheritance)
{
return source.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
}

return source.GetProperties();
#endif

return GetPropertiesRecursively(source, new List<PropertyInfo>());
}

private static PropertyInfo[] GetOwnProperties(Type source)
{
#if NETSTANDARD

internal static List<PropertyInfo> GetDeclaredPropertyInfosRecursive(List<PropertyInfo> propertyInfos, TypeInfo typeInfo)
return source.GetTypeInfo().DeclaredProperties.ToArray();
#else
return source.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
#endif
}

private static IEnumerable<PropertyInfo> GetPropertiesRecursively(Type source, List<PropertyInfo> propertyInfos)
{
foreach (var property in typeInfo.DeclaredProperties)
foreach (var property in GetOwnProperties(source))
{
if (!propertyInfos.Any(x => x.Name == property.Name))
var existingProperty = propertyInfos.FirstOrDefault(p => p.Name == property.Name);
if (existingProperty != null)
{
if (IsAbstract(property))
{
// abstract properties take precedence over their concrete declaration counterpart
propertyInfos.Remove(existingProperty);
propertyInfos.Add(property);
}
}
else
{
propertyInfos.Add(property);
}
}

if(typeInfo.BaseType != null)
Type baseType = GetBaseType(source);
if(baseType != null)
{
return GetDeclaredPropertyInfosRecursive(propertyInfos, typeInfo.BaseType.GetTypeInfo());
return GetPropertiesRecursively(baseType, propertyInfos);
}

return propertyInfos;
}

#endif



internal static Type[] GetGenericTypeArguments(this Type source)
{
#if NETSTANDARD
Expand Down
Loading

0 comments on commit fc1bae7

Please sign in to comment.