Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0a0d8f3
Merge pull request #139 from Tynamix/master
Tynamix May 9, 2022
66a1451
#143 Support filling inherited abstract properties
cguyonnet Jun 22, 2024
ec737d5
Create dotnet.yml
Tynamix Jul 23, 2024
b0f509e
Update dotnet.yml
Tynamix Jul 23, 2024
bb19876
Update dotnet.yml
Tynamix Jul 23, 2024
0ba3447
Update dotnet.yml
Tynamix Jul 23, 2024
99e1c9b
Update dotnet.yml
Tynamix Jul 23, 2024
ae7434b
Update dotnet.yml
Tynamix Jul 23, 2024
4e28969
Update dotnet.yml
Tynamix Jul 23, 2024
0a6f574
Update dotnet.yml
Tynamix Jul 23, 2024
f491302
Update dotnet.yml
Tynamix Jul 23, 2024
daef687
Update dotnet.yml
Tynamix Jul 23, 2024
0d26f7b
Update dotnet.yml
Tynamix Jul 23, 2024
59da21d
Update dotnet.yml
Tynamix Jul 23, 2024
aef6ca6
Merge pull request #145 from Tynamix/Tynamix-patch-1
Tynamix Jul 23, 2024
c1fbb36
Merge pull request #144 from cguyonnet/bug-143
Tynamix Aug 2, 2024
9698b37
Create publish-to-nuget.yml
Tynamix Aug 2, 2024
e1fb44e
Update publish-to-nuget.yml
Tynamix Aug 2, 2024
ad55de7
Merge pull request #146 from Tynamix/Tynamix-patch-2
Tynamix Aug 2, 2024
ef56ea8
remove appveyor. added new version info
Aug 2, 2024
0af5d77
Merge branch 'develop' of https://github.com/Tynamix/ObjectFiller.NET…
Aug 2, 2024
3fbf1e2
Update and rename dotnet.yml to build-and-test.yml
Tynamix Aug 2, 2024
7b264a8
Update publish-to-nuget.yml
Tynamix Aug 2, 2024
113a906
Update publish-to-nuget.yml
Tynamix Aug 2, 2024
2aa1905
changed csproj
Aug 2, 2024
9916470
fix readme.md path
Aug 2, 2024
ced88dc
fix csproj
Aug 2, 2024
dbaebaa
Update build-and-test.yml
Tynamix Aug 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading