Skip to content

Commit

Permalink
Converted all MS Tests to xUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Nov 4, 2023
1 parent 26e71f3 commit ad525ce
Show file tree
Hide file tree
Showing 34 changed files with 3,035 additions and 3,357 deletions.
4 changes: 0 additions & 4 deletions src/Downloader.Test/ConfigAwait.cs

This file was deleted.

10 changes: 6 additions & 4 deletions src/Downloader.Test/Downloader.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Sign.snk</AssemblyOriginatorKeyFile>
Expand All @@ -25,20 +25,22 @@
<PackageReference Include="AssertMessage.Fody" Version="2.1.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.2" />
<PackageReference Include="Fody" Version="6.8.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Downloader.Test/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<AssertMessage />
<ConfigureAwait ContinueOnCapturedContext="false" />
</Weavers>
5 changes: 0 additions & 5 deletions src/Downloader.Test/FodyWeavers.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" />
</xs:complexType>
</xs:element>
<xs:element name="AssertMessage" minOccurs="0" maxOccurs="1" type="xs:anyType" />
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
Expand Down
35 changes: 30 additions & 5 deletions src/Downloader.Test/Helper/AssertHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;
using System;
using System.Linq;

Expand All @@ -14,7 +14,7 @@ public static void DoesNotThrow<T>(Action action) where T : Exception
}
catch (T)
{
Assert.Fail("Expected no {0} to be thrown", typeof(T).Name);
Assert.Fail($"Expected no {typeof(T).Name} to be thrown");
}
catch
{
Expand All @@ -24,12 +24,37 @@ public static void DoesNotThrow<T>(Action action) where T : Exception

public static void AreEquals(Chunk source, Chunk destination)
{
Assert.IsNotNull(source);
Assert.IsNotNull(destination);
Assert.NotNull(source);
Assert.NotNull(destination);

foreach (var prop in typeof(Chunk).GetProperties().Where(p => p.CanRead && p.CanWrite))
{
Assert.AreEqual(prop.GetValue(source), prop.GetValue(destination), prop.Name);
Assert.Equal(prop.GetValue(source), prop.GetValue(destination));
}
}

public static void AreEquals(DownloadPackage source, DownloadPackage destination)
{
Assert.NotNull(source);
Assert.NotNull(destination);
Assert.NotNull(source.Chunks);
Assert.NotNull(destination.Chunks);
Assert.Equal(source.FileName, destination.FileName);
Assert.Equal(source.ReceivedBytesSize, destination.ReceivedBytesSize);
Assert.Equal(source.TotalFileSize, destination.TotalFileSize);
Assert.Equal(source.IsSaving, destination.IsSaving);
Assert.Equal(source.IsSaveComplete, destination.IsSaveComplete);
Assert.Equal(source.SaveProgress, destination.SaveProgress);
Assert.Equal(source.Chunks?.Length, destination.Chunks?.Length);
Assert.Equal(source.IsSupportDownloadInRange, destination.IsSupportDownloadInRange);
Assert.Equal(source.InMemoryStream, destination.InMemoryStream);
Assert.Equal(source.Storage.Path, destination.Storage.Path);
//Assert.Equal(source.Storage.Length, destination.Storage.Length);
Assert.True(source.Urls.SequenceEqual(destination.Urls));

for (int i = 0; i < source.Chunks.Length; i++)
{
AreEquals(source.Chunks[i], destination.Chunks[i]);
}
}
}
15 changes: 7 additions & 8 deletions src/Downloader.Test/HelperTests/AssertHelperTest.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using Downloader.Test.Helper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Xunit;

namespace Downloader.Test.HelperTests;

[TestClass]
public class AssertHelperTest
{
[TestMethod]
[Fact]
public void TestDoesNotThrowWhenThrowExp()
{
void ThrowException() => throw new DivideByZeroException("TEST");

AssertHelper.DoesNotThrow<ArgumentNullException>(ThrowException);
}

[TestMethod]
[Fact]
public void TestChunksAreEquals()
{
// arrange
Expand All @@ -41,10 +40,10 @@ public void TestChunksAreEquals()
AssertHelper.AreEquals(chunk1, chunk2);

// assert
Assert.AreNotEqual(chunk1, chunk2);
Assert.NotEqual(chunk1, chunk2);
}

[TestMethod]
[Fact]
public void TestChunksAreNotEquals()
{
// arrange
Expand All @@ -70,7 +69,7 @@ public void TestChunksAreNotEquals()
void testAssertHelper() => AssertHelper.AreEquals(chunk1, chunk2);

// assert
Assert.ThrowsException<AssertFailedException>(testAssertHelper);
Assert.AreNotEqual(chunk1, chunk2);
Assert.ThrowsAny<Exception>(testAssertHelper);
Assert.NotEqual(chunk1, chunk2);
}
}
144 changes: 71 additions & 73 deletions src/Downloader.Test/HelperTests/DummyDataTest.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,79 @@
using Downloader.DummyHttpServer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using Xunit;

namespace Downloader.Test.HelperTests
namespace Downloader.Test.HelperTests;

public class DummyDataTest
{
[TestClass]
public class DummyDataTest
[Fact]
public void GenerateOrderedBytesTest()
{
// arrange
int size = 1024;
byte[] bytes = Enumerable.Range(0, size).Select(i => (byte)i).ToArray();

// act
var dummyData = DummyData.GenerateOrderedBytes(size);

// assert
Assert.Equal(size, dummyData.Length);
Assert.True(dummyData.SequenceEqual(bytes));
}

[Fact]
public void GenerateOrderedBytesLessThan1Test()
{
[TestMethod]
public void GenerateOrderedBytesTest()
{
// arrange
int size = 1024;
byte[] bytes = Enumerable.Range(0, size).Select(i => (byte)i).ToArray();

// act
var dummyData = DummyData.GenerateOrderedBytes(size);

// assert
Assert.AreEqual(size, dummyData.Length);
Assert.IsTrue(dummyData.SequenceEqual(bytes));
}

[TestMethod]
public void GenerateOrderedBytesLessThan1Test()
{
// arrange
int size = 0;

// act
void act() => DummyData.GenerateOrderedBytes(size);

// assert
Assert.ThrowsException<ArgumentException>(act);
}

[TestMethod]
public void GenerateRandomBytesTest()
{
// arrange
int size = 1024;

// act
var dummyData = DummyData.GenerateRandomBytes(size);

// assert
Assert.AreEqual(size, dummyData.Length);
Assert.IsTrue(dummyData.Any(i => i > 0));
}

[TestMethod]
public void GenerateRandomBytesLessThan1Test()
{
// arrange
int size = 0;

// act
void act() => DummyData.GenerateRandomBytes(size);

// assert
Assert.ThrowsException<ArgumentException>(act);
}

[TestMethod]
public void GenerateSingleBytesTest()
{
// arrange
int size = 1024;
byte fillByte = 13;

// act
var dummyData = DummyData.GenerateSingleBytes(size, fillByte);

// assert
Assert.AreEqual(size, dummyData.Length);
Assert.IsTrue(dummyData.All(i => i == fillByte));
}
// arrange
int size = 0;

// act
void act() => DummyData.GenerateOrderedBytes(size);

// assert
Assert.ThrowsAny<ArgumentException>(act);
}

[Fact]
public void GenerateRandomBytesTest()
{
// arrange
int size = 1024;

// act
var dummyData = DummyData.GenerateRandomBytes(size);

// assert
Assert.Equal(size, dummyData.Length);
Assert.Contains(dummyData, i => i > 0);
}

[Fact]
public void GenerateRandomBytesLessThan1Test()
{
// arrange
int size = 0;

// act
void act() => DummyData.GenerateRandomBytes(size);

// assert
Assert.ThrowsAny<ArgumentException>(act);
}

[Fact]
public void GenerateSingleBytesTest()
{
// arrange
int size = 1024;
byte fillByte = 13;

// act
var dummyData = DummyData.GenerateSingleBytes(size, fillByte);

// assert
Assert.Equal(size, dummyData.Length);
Assert.True(dummyData.All(i => i == fillByte));
}
}
Loading

0 comments on commit ad525ce

Please sign in to comment.