Skip to content

Commit 8771df9

Browse files
committed
Port & update appropriate v2 samples to v3
1 parent 3b7635c commit 8771df9

File tree

112 files changed

+4763
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+4763
-27
lines changed

global.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"sdk": {
3+
"version": "8.0.100",
4+
"rollForward": "latestMinor"
5+
}
6+
}

v2/CollectionFixtureExample/packages.config

-10
This file was deleted.

v3/.editorconfig

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
insert_final_newline = true
8+
indent_style = space
9+
indent_size = 4
10+
11+
[*.sln]
12+
end_of_line = crlf
13+
14+
[*.{csproj,json,props,targets,xslt,yaml,yml}]
15+
indent_size = 2
16+
17+
[*.cs]
18+
dotnet_diagnostic.CA1050.severity = none # Declare types in namespaces
19+
dotnet_diagnostic.IDE0022.severity = none # Use block body for method
20+
dotnet_diagnostic.IDE0040.severity = none # Add accessibility modifiers
21+
dotnet_diagnostic.IDE0130.severity = none # Namespace does not match folder structure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net472;net8.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(Version_Microsoft_NET_Test_Sdk)" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="$(Version_xunit_runner_visualstudio)" PrivateAssets="all" />
11+
<PackageReference Include="xunit.v3" Version="$(Version_xunit_v3)" />
12+
</ItemGroup>
13+
14+
</Project>

v3/AssemblyFixtureExample/Samples.cs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using Xunit;
3+
4+
// Attach this type as a fixture. This will be created one for the test assembly and shared for
5+
// any test which wants to get access to it (via constructor).
6+
//
7+
// All fixtures can optionally implement IDisposable, IAsyncDisposable, or IAsyncLifetime in order
8+
// get full lifetime control.
9+
[assembly: AssemblyFixture(typeof(MyAssemblyFixture))]
10+
11+
public class Sample1(MyAssemblyFixture fixture)
12+
{
13+
[Fact]
14+
public void EnsureSingleton() =>
15+
Assert.Equal(1, fixture.InstantiationCount);
16+
}
17+
18+
public class Sample2(MyAssemblyFixture fixture)
19+
{
20+
[Fact]
21+
public void EnsureSingleton() =>
22+
Assert.Equal(1, fixture.InstantiationCount);
23+
}
24+
25+
public class MyAssemblyFixture : IDisposable
26+
{
27+
static int instantiationCount;
28+
29+
public MyAssemblyFixture() =>
30+
instantiationCount++;
31+
32+
public int InstantiationCount =>
33+
instantiationCount;
34+
35+
public void Dispose()
36+
{
37+
// Uncomment this and it will surface as an assembly cleanup failure
38+
//throw new DivideByZeroException();
39+
}
40+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net472;net8.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(Version_Microsoft_NET_Test_Sdk)" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="$(Version_xunit_runner_visualstudio)" PrivateAssets="all" />
11+
<PackageReference Include="xunit.v3" Version="$(Version_xunit_v3)" />
12+
</ItemGroup>
13+
14+
</Project>

v3/AssertExamples/AsyncExamples.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
public class AsyncExamples
6+
{
7+
// Your async tests can return Task...
8+
[Fact]
9+
public async Task CodeThrowsAsync()
10+
{
11+
var ex = await Assert.ThrowsAsync<NotImplementedException>(ThrowingMethod);
12+
13+
Assert.IsType<NotImplementedException>(ex);
14+
}
15+
16+
// ...or ValueTask
17+
[Fact]
18+
public async ValueTask RecordAsync()
19+
{
20+
var ex = await Record.ExceptionAsync(ThrowingMethod);
21+
22+
Assert.IsType<NotImplementedException>(ex);
23+
}
24+
25+
async Task ThrowingMethod()
26+
{
27+
await Task.Yield();
28+
29+
throw new NotImplementedException();
30+
}
31+
}
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Xunit;
4+
5+
// Collection equivalence means the collections have the exact same values
6+
// in any order.
7+
8+
public class CollectionExamples
9+
{
10+
[Fact]
11+
public void CollectionEquality()
12+
{
13+
List<int> left = [4, 12, 16, 27];
14+
List<int> right = [4, 12, 16, 27];
15+
16+
Assert.Equal(left, right, new CollectionEquivalenceComparer<int>());
17+
}
18+
19+
[Fact]
20+
public void LeftCollectionSmallerThanRight()
21+
{
22+
List<int> left = [4, 12, 16];
23+
List<int> right = [4, 12, 16, 27];
24+
25+
Assert.NotEqual(left, right, new CollectionEquivalenceComparer<int>());
26+
}
27+
28+
[Fact]
29+
public void LeftCollectionLargerThanRight()
30+
{
31+
List<int> left = [4, 12, 16, 27, 42];
32+
List<int> right = [4, 12, 16, 27];
33+
34+
Assert.NotEqual(left, right, new CollectionEquivalenceComparer<int>());
35+
}
36+
37+
[Fact]
38+
public void SameValuesOutOfOrder()
39+
{
40+
List<int> left = [4, 16, 12, 27];
41+
List<int> right = [4, 12, 16, 27];
42+
43+
Assert.Equal(left, right, new CollectionEquivalenceComparer<int>());
44+
}
45+
46+
[Fact]
47+
public void DuplicatedItemInOneListOnly()
48+
{
49+
List<int> left = [4, 12, 16, 27, 4];
50+
List<int> right = [4, 12, 16, 27];
51+
52+
Assert.NotEqual(left, right, new CollectionEquivalenceComparer<int>());
53+
}
54+
55+
[Fact]
56+
public void DuplicatedItemInBothLists()
57+
{
58+
List<int> left = [4, 16, 12, 27, 4];
59+
List<int> right = [4, 12, 16, 4, 27];
60+
61+
Assert.Equal(left, right, new CollectionEquivalenceComparer<int>());
62+
}
63+
}
64+
65+
class CollectionEquivalenceComparer<T> : IEqualityComparer<IEnumerable<T>>
66+
where T : IEquatable<T>
67+
{
68+
public bool Equals(IEnumerable<T>? x, IEnumerable<T>? y)
69+
{
70+
if (x is null)
71+
return y is null;
72+
if (y is null)
73+
return false;
74+
75+
var leftList = new List<T>(x);
76+
var rightList = new List<T>(y);
77+
leftList.Sort();
78+
rightList.Sort();
79+
80+
using var enumeratorX = leftList.GetEnumerator();
81+
using var enumeratorY = rightList.GetEnumerator();
82+
83+
while (true)
84+
{
85+
bool hasNextX = enumeratorX.MoveNext();
86+
bool hasNextY = enumeratorY.MoveNext();
87+
88+
if (!hasNextX || !hasNextY)
89+
return (hasNextX == hasNextY);
90+
91+
if (!enumeratorX.Current.Equals(enumeratorY.Current))
92+
return false;
93+
}
94+
}
95+
96+
public int GetHashCode(IEnumerable<T> obj) =>
97+
throw new NotImplementedException();
98+
}

v3/AssertExamples/EqualExamples.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Xunit;
4+
5+
public class EqualExamples
6+
{
7+
[Fact]
8+
public void EqualStringIgnoreCase()
9+
{
10+
string expected = "TestString";
11+
string actual = "teststring";
12+
13+
Assert.False(actual == expected);
14+
Assert.NotEqual(expected, actual);
15+
Assert.Equal(expected, actual, StringComparer.CurrentCultureIgnoreCase);
16+
}
17+
18+
class DateComparer : IEqualityComparer<DateTime>
19+
{
20+
public bool Equals(DateTime x, DateTime y) =>
21+
x.Date == y.Date;
22+
23+
public int GetHashCode(DateTime obj) =>
24+
obj.GetHashCode();
25+
}
26+
27+
[Fact]
28+
public void DateShouldBeEqualEvenThoughTimesAreDifferent()
29+
{
30+
var firstTime = DateTime.Now.Date;
31+
var later = firstTime.AddMinutes(90);
32+
33+
Assert.NotEqual(firstTime, later);
34+
Assert.Equal(firstTime, later, new DateComparer());
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<!--
5+
Since we've imported the assertions via source, and have nullable enabled, we need to let the
6+
assertion library know that by defining XUNIT_NULLABLE.
7+
For more information, see https://github.com/xunit/assert.xunit/#annotations
8+
-->
9+
<DefineConstants>$(DefineConstants);XUNIT_NULLABLE</DefineConstants>
10+
<OutputType>Exe</OutputType>
11+
<TargetFrameworks>net472;net8.0</TargetFrameworks>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(Version_Microsoft_NET_Test_Sdk)" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="$(Version_xunit_runner_visualstudio)" PrivateAssets="all" />
17+
<!--
18+
We want to reference xunit.v3.assert.source (so that we can extend the Assert class itself),
19+
which means we need to bring in references to the three individual packages instead of referencing
20+
xunit.v3 directly.
21+
-->
22+
<PackageReference Include="xunit.analyzers" Version="$(Version_xunit_analyzers)" />
23+
<PackageReference Include="xunit.v3.assert.source" Version="$(Version_xunit_v3)" />
24+
<PackageReference Include="xunit.v3.core" Version="$(Version_xunit_v3)" />
25+
</ItemGroup>
26+
27+
<!-- Need this to allow nullable support in the source-based assertion library to work with .NET Framework -->
28+
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
29+
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
30+
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[8.0.11]" />
31+
</ItemGroup>
32+
33+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Person(string firstName, string lastName)
2+
{
3+
public string FirstName => firstName;
4+
public string LastName => lastName;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Xunit;
2+
3+
// When importing xUnit.net's assertion library as source, the Assert class is partial and you can additional
4+
public partial class Assert
5+
{
6+
public static void IsBrad(Person person) =>
7+
Equal("Brad", person.FirstName);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Xunit;
2+
3+
// This is an example of writing your own assertion that lives under Assert, by importing the assertion
4+
// library as source rather than as binary, so that you can write more assertions on the Assert
5+
// partial class.
6+
public class PersonAssertionsExamples
7+
{
8+
[Fact]
9+
public void EnsureBradness()
10+
{
11+
var person1 = new Person("Brad", "Wilson");
12+
var person2 = new Person("Brad", "Pitt");
13+
14+
Assert.IsBrad(person1);
15+
Assert.IsBrad(person2);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Xunit.Extensions.AssertExtensions;
2+
3+
/// <summary>
4+
/// Extensions which provide assertions to classes derived from <see cref="bool"/>.
5+
/// </summary>
6+
public static class BooleanExtensions
7+
{
8+
/// <summary>
9+
/// Verifies that the condition is false.
10+
/// </summary>
11+
/// <param name="condition">The condition to be tested</param>
12+
/// <exception cref="FalseException">Thrown if the condition is not false</exception>
13+
public static void ShouldBeFalse(this bool condition) =>
14+
Assert.False(condition);
15+
16+
/// <summary>
17+
/// Verifies that the condition is false.
18+
/// </summary>
19+
/// <param name="condition">The condition to be tested</param>
20+
/// <param name="userMessage">The message to show when the condition is not false</param>
21+
/// <exception cref="FalseException">Thrown if the condition is not false</exception>
22+
public static void ShouldBeFalse(this bool condition, string userMessage) =>
23+
Assert.False(condition, userMessage);
24+
25+
/// <summary>
26+
/// Verifies that an expression is true.
27+
/// </summary>
28+
/// <param name="condition">The condition to be inspected</param>
29+
/// <exception cref="TrueException">Thrown when the condition is false</exception>
30+
public static void ShouldBeTrue(this bool condition) =>
31+
Assert.True(condition);
32+
33+
/// <summary>
34+
/// Verifies that an expression is true.
35+
/// </summary>
36+
/// <param name="condition">The condition to be inspected</param>
37+
/// <param name="userMessage">The message to be shown when the condition is false</param>
38+
/// <exception cref="TrueException">Thrown when the condition is false</exception>
39+
public static void ShouldBeTrue(this bool condition, string userMessage) =>
40+
Assert.True(condition, userMessage);
41+
}

0 commit comments

Comments
 (0)