Skip to content

Commit 38fed6e

Browse files
committed
Copied IGLib.TestBase project to this repository (part of moving from IGLibSandbox)
1 parent df1a318 commit 38fed6e

17 files changed

Lines changed: 3803 additions & 0 deletions

tests/IGLib.TestBase/ExampleTestClass.cs

Lines changed: 306 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+

2+
// This file provides alternative implementation of the HashCode.Combine{T1}(T1) method for
3+
// legacy .NET Framework and earlier versions of .NET Coree (below .NET 8).
4+
5+
#if NET8_0_OR_GREATER
6+
using System;
7+
8+
namespace IGLib.Base
9+
{
10+
11+
/// <summary>Contains methods that combines hashes of multipe objects into a single hash.
12+
/// <para>In .NET 8 or greater, this just calls <see cref="HashCode.Combine{T1}(T1)"/>.
13+
/// </para></summary>
14+
public static class HashCodeHelper
15+
{
16+
public static int Combine<T1>(T1 value1)
17+
{
18+
return HashCode.Combine(value1);
19+
}
20+
21+
public static int Combine<T1, T2>(T1 value1, T2 value2)
22+
{
23+
return HashCode.Combine(value1, value2);
24+
}
25+
26+
public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3)
27+
{
28+
return HashCode.Combine(value1, value2, value3);
29+
}
30+
31+
// Add more overloads as needed
32+
}
33+
}
34+
#else
35+
using System;
36+
37+
namespace IGLib.Base
38+
{
39+
40+
/// <summary>Contains methods that combines hashes of multipe objects into a single hash.
41+
/// <para>In .NET 8 or greater, this just calls `HashCode.Combine{T1}(T1)`, but for lower
42+
/// versions (including .NET Framework), the class provides its own implementation.
43+
/// </para></summary>
44+
public static class HashCodeHelper
45+
{
46+
public static int Combine<T1>(T1 value1)
47+
{
48+
return value1?.GetHashCode() ?? 0;
49+
}
50+
51+
public static int Combine<T1, T2>(T1 value1, T2 value2)
52+
{
53+
int hash1 = value1?.GetHashCode() ?? 0;
54+
int hash2 = value2?.GetHashCode() ?? 0;
55+
56+
return CombineHashes(hash1, hash2);
57+
}
58+
59+
public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3)
60+
{
61+
int hash1 = value1?.GetHashCode() ?? 0;
62+
int hash2 = value2?.GetHashCode() ?? 0;
63+
int hash3 = value3?.GetHashCode() ?? 0;
64+
65+
return CombineHashes(hash1, hash2, hash3);
66+
}
67+
68+
// Add more overloads as needed
69+
70+
private static int CombineHashes(params int[] hashes)
71+
{
72+
int hash = 17;
73+
74+
foreach (int h in hashes)
75+
{
76+
hash = hash * 31 + h;
77+
}
78+
79+
return hash;
80+
}
81+
}
82+
}
83+
#endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
// This file is necessary to enable properties' init accessors when .NET 4.8 is targeted
3+
// This is a fix for the following compiler error in .NET 4.8 targets:
4+
// "Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported"
5+
6+
#if NET48
7+
namespace System.Runtime.CompilerServices
8+
{
9+
public class IsExternalInit {}
10+
}
11+
#endif
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<!--
5+
<TargetFramework>net8.0</TargetFramework>
6+
<TargetFrameworks>net8.0;net48;netstandard2.0</TargetFrameworks>
7+
WARNING: always use just one of both elements, either TargetFrameworks or TargetFramework;
8+
Usually, the .NET 8 and higher should be used because some tested projects are not compatile
9+
with the old frameworks.
10+
-->
11+
<TargetFrameworks>net8.0;net48;netstandard2.0</TargetFrameworks>
12+
<LangVersion>12.0</LangVersion>
13+
</PropertyGroup>
14+
15+
<ItemGroup Condition="'$(TargetFramework)' == 'net48' OR '$(TargetFramework)' == 'netstandard2.0'" >
16+
<PackageReference Include="System.Text.Json" Version="9.0.3" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<!--
21+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.12.0" />
22+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"></PackageReference>
23+
<PackageReference Include="System.Runtime.Loader" Version="4.3.0"></PackageReference>
24+
-->
25+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.*" />
26+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.*"></PackageReference>
27+
<PackageReference Include="System.Runtime.Loader" Version="4.*"></PackageReference>
28+
29+
30+
<PackageReference Include="xunit" Version="2.4.1" />
31+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
32+
<PackageReference Include="FluentAssertions" Version="5.10.3" />
33+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
34+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
35+
</ItemGroup>
36+
<!-- References needed for code compilationL
37+
<ItemGroup>
38+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"></PackageReference>
39+
<PackageReference Include="System.Runtime.Loader" Version="4.3.0"></PackageReference>
40+
</ItemGroup>
41+
-->
42+
</Project>

tests/IGLib.TestBase/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
# TestBase Repository
3+
4+
This repository contains base classes for C# XUnit tests. This provides some additional functionality to test classes such as Output and Console classes, which can be used to output partial results on tests output.
5+
6+
Support for ILogger messages is yet to be added.
7+

0 commit comments

Comments
 (0)