93 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Syroot.Worms.Core;
namespace Syroot.Worms.Test.Common
{
/// <summary>
/// Represents a collection of methods helping in executing tests.
/// </summary>
internal static class TestHelpers
{
// ---- MEMBERS ------------------------------------------------------------------------------------------------
private static readonly string[] _gamePathsWorms2 = { @"C:\Games\Worms2" };
private static readonly string[] _gamePathsWormsArmageddon = { @"C:\Games\Worms Armageddon 3.6.31.0",
@"C:\Games\Worms Armageddon 3.7.2.1" };
private static readonly string[] _gamePathsWormsWorldParty = { @"C:\Games\Worms World Party" };
// ---- METHODS (INTERNAL) -------------------------------------------------------------------------------------
/// <summary>
/// Loads the files found with the given <paramref name="wildcard"/>. Excludes file names specified in the
/// optional array <paramref name="excludedFiles"/>.
/// </summary>
/// <typeparam name="T">The type of the files to load.</typeparam>
/// <param name="games">The games to test.</param>
/// <param name="wildcard">The wildcard to match.</param>
/// <param name="excludedFiles">Optionally, the files to exclude.</param>
internal static void LoadFiles<T>(Game games, string wildcard, string[] excludedFiles = null)
where T : ILoadableFile, new()
{
foreach (string fileName in FindFiles(games, wildcard))
{
if (excludedFiles?.Contains(Path.GetFileName(fileName), StringComparer.OrdinalIgnoreCase) == true)
{
Debug.WriteLine($"Skipping {fileName}");
continue;
}
Debug.Write($"Loading {fileName}...");
T instance = new T();
instance.Load(fileName);
Debug.WriteLine($" ok");
}
}
// ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------
private static List<string> FindFiles(Game games, string wildcard)
{
List<string> gamePaths = GetGamePaths(games);
List<string> files = new List<string>();
foreach (string testPath in gamePaths)
{
files.AddRange(Directory.GetFiles(testPath, wildcard, SearchOption.AllDirectories));
}
return files;
}
private static List<string> GetGamePaths(Game game)
{
List<string> gamePaths = new List<string>();
if (game.HasFlag(Game.Worms2))
{
gamePaths.AddRange(_gamePathsWorms2);
}
if (game.HasFlag(Game.WormsArmageddon))
{
gamePaths.AddRange(_gamePathsWormsArmageddon);
}
if (game.HasFlag(Game.WormsWorldParty))
{
gamePaths.AddRange(_gamePathsWormsWorldParty);
}
return gamePaths;
}
}
[Flags]
internal enum Game
{
Worms2 = 1 << 0,
WormsArmageddon = 1 << 1,
WormsWorldParty = 1 << 2,
Gen2 = Worms2 | WormsArmageddon | WormsWorldParty,
Armageddon = WormsArmageddon | WormsWorldParty,
All = -1
}
}