using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Tango.Logging;
using DeepEqual;
using DeepEqual.Syntax;
namespace Tango.UnitTesting
{
/// <summary>
/// Contains several unit testing helper methods.
/// </summary>
public static class Helper
{
/// <summary>
/// Gets the absolute path to the specified file name in the solution 'Resources' folder.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns></returns>
public static String GetResourcePath(String fileName)
{
return Path.GetFullPath(@"..\..\..\Resources\" + fileName);
}
/// <summary>
/// Gets the absolute path to the 'Resources' folder.
/// </summary>
/// <returns></returns>
public static String GetResourcePath()
{
return Path.GetFullPath(@"..\..\..\Resources\");
}
/// <summary>
/// Gets the PMR (Protobuf Messages Repository) path.
/// </summary>
/// <returns></returns>
public static String GetPMRPath()
{
return Path.GetFullPath(@"..\..\..\PMR\Messages\");
}
/// <summary>
/// Gets the machine studio exe path.
/// </summary>
/// <returns></returns>
public static String GetMachineStudioPath()
{
return Path.GetFullPath(@"..\..\Build\Debug\Tango.MachineStudio.UI.exe");
}
/// <summary>
/// Gets the tango build path.
/// </summary>
/// <returns></returns>
public static String GetBuildPath()
{
return Path.GetFullPath(@"..\..\Build\Debug");
}
/// <summary>
/// Gets the SQLite database file path in DB folder.
/// </summary>
/// <returns></returns>
public static String GetSQLiteFilePath()
{
return Path.GetFullPath(@"..\..\..\DB\Tango.db");
}
/// <summary>
/// Initializes the logging manager.
/// </summary>
public static ConsoleLogger InitializeLogging(bool useConsole = false, [CallerMemberName] string testName = null)
{
LogManager.Default.RegisterLogger(new FileLogger(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "logs", "Unit Testing"), "Unit_Testing"));
if (useConsole)
{
var consoleLogger = new ConsoleLogger(testName);
LogManager.Default.RegisterLogger(consoleLogger);
return consoleLogger;
}
return null;
}
/// <summary>
/// Creates a temporary folder and returns it's path.
/// </summary>
/// <returns></returns>
public static String GetTempFolderPathAppend(String customFolder, [CallerMemberName] string testName = null)
{
String tempDirectory = Path.Combine(Path.GetTempPath(), "Twine", "Unit Testing", testName, customFolder);
Directory.CreateDirectory(tempDirectory);
return tempDirectory;
}
/// <summary>
/// Creates a temporary folder and returns it's path.
/// </summary>
/// <returns></returns>
public static String GetTempFolderPath([CallerMemberName] string testName = null)
{
String tempDirectory = Path.Combine(Path.GetTempPath(), "Twine", "Unit Testing", testName, Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);
return tempDirectory;
}
/// <summary>
/// Tries to delete folder.
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
public static bool TryDeleteFolder(String path)
{
try
{
Directory.Delete(path, true);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Shows the file in explorer.
/// </summary>
/// <param name="path">Name of the file/folder.</param>
public static void ShowInExplorer(String path)
{
Process.Start("explorer.exe", string.Format("/select,\"{0}\"", path));
}
/// <summary>
/// Returns true if the two objects are equal by using deep equal.
/// </summary>
/// <param name="obj1">First object.</param>
/// <param name="obj2">Second object.</param>
/// <returns></returns>
public static bool IsDeepEqual(Object obj1, Object obj2)
{
return obj1.IsDeepEqual(obj2);
}
}
}