Skip to content

Commit

Permalink
use nunit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Mar 31, 2024
1 parent 210f7ad commit f440461
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
14 changes: 10 additions & 4 deletions Osu.Stubs.Tests/Osu.Stubs.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Build">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>Osu.Stubs.Tests</RootNamespace>
<TargetFramework>net462</TargetFramework>
<PlatformTarget>x86</PlatformTarget> <!-- Running in 32bit is necessary for loading in osu! -->
Expand All @@ -28,10 +27,17 @@
<ProjectReference Include="../Osu.Stubs/Osu.Stubs.csproj"/>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="all"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17">

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0"/>
<PackageReference Include="NUnit" Version="4.1.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="4.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
40 changes: 19 additions & 21 deletions Osu.Stubs.Tests/Program.cs → Osu.Stubs.Tests/TestStubs.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using NUnit.Framework;
using Osu.Utils.Extensions;
using Osu.Utils.Lazy;

#pragma warning disable CS0618 // Type or member is obsolete

namespace Osu.Stubs.Tests;

internal static class Program
[TestFixture]
[Parallelizable]
public class TestStubs
{
private static async Task Main()
[TestCaseSource(nameof(LoadStubs))]
public void TestStub(ILazy<MemberInfo> lazy) =>
Assert.DoesNotThrow(lazy.Fill);

private static IEnumerable<ILazy<MemberInfo>> LoadStubs()
{
var osuDir = Path.Combine(Environment.CurrentDirectory, "osu!");
var osuExe = Path.Combine(osuDir, "osu!.exe");

if (!Directory.Exists(osuDir))
if (!Directory.Exists(osuDir)) // TODO: check file hashes to force re-download
{
Directory.CreateDirectory(osuDir);
await OsuApi.DownloadOsu(osuDir);
OsuApi.DownloadOsu(osuDir).Wait();
}

#pragma warning disable CS0618 // Type or member is obsolete
// Add osu! directory to assembly search path
AppDomain.CurrentDomain.AppendPrivatePath(osuDir);
#pragma warning restore CS0618 // Type or member is obsolete

// Load the osu! executable and it's dependencies as assemblies without executing
Assembly.LoadFile(osuExe);

List<ILazy<MemberInfo>> stubs = new(200);
var stubs = new List<ILazy<MemberInfo>>(300);

foreach (var type in Assembly.GetAssembly(typeof(Stub)).GetTypes())
{
Expand All @@ -45,17 +54,6 @@ private static async Task Main()
stubs.Sort((a, b) =>
string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase));

foreach (var lazy in stubs)
{
try
{
Console.WriteLine($"{lazy.Name} -> {lazy.Reference}");
}
catch (Exception e)
{
Console.WriteLine($"{lazy.Name} -> [Failure]");
Console.WriteLine(e);
}
}
return stubs;
}
}
2 changes: 2 additions & 0 deletions Osu.Utils/Lazy/LazyConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public LazyConstructor(string name, Func<ConstructorInfo> action)
public string Name { get; }

public ConstructorInfo Reference => this.GetReference(Name, _lazy);

public override string ToString() => $"{nameof(LazyConstructor)}({Name})";

/// <summary>
/// Find if not already cached and reflectively invoke this constructor to create a new instance of a class.
Expand Down
2 changes: 2 additions & 0 deletions Osu.Utils/Lazy/LazyField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public LazyField(string name, Func<FieldInfo> action)
public string Name { get; }

public FieldInfo Reference => this.GetReference(Name, _lazy);

public override string ToString() => $"{nameof(LazyField<object>)}({Name})";

/// <summary>
/// Gets the current value of this field.
Expand Down
2 changes: 2 additions & 0 deletions Osu.Utils/Lazy/LazyMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public LazyMethod(string name, Func<MethodInfo> action)
public string Name { get; }

public MethodInfo Reference => this.GetReference(Name, _lazy);

public override string ToString() => $"{nameof(LazyMethod)}({Name})";

/// <summary>
/// Find if not already cached and reflectively invoke this method. Does not return any value.
Expand Down
2 changes: 2 additions & 0 deletions Osu.Utils/Lazy/LazyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public LazyType(string name, Func<Type> action)

public Type Reference => this.GetReference(Name, _lazy);

public override string ToString() => $"{nameof(LazyType)}({Name})";

/// <summary>
/// Finds a class based on it's full name including namespace.
/// </summary>
Expand Down

0 comments on commit f440461

Please sign in to comment.