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
{
///
/// Contains several unit testing helper methods.
///
public static class Helper
{
///
/// Gets the absolute path to the specified file name in the solution 'Resources' folder.
///
/// Name of the file.
///
public static String GetResourcePath(String fileName)
{
return Path.GetFullPath(@"..\..\..\Resources\" + fileName);
}
///
/// Gets the absolute path to the 'Resources' folder.
///
///
public static String GetResourcePath()
{
return Path.GetFullPath(@"..\..\..\Resources\");
}
///
/// Gets the PMR (Protobuf Messages Repository) path.
///
///
public static String GetPMRPath()
{
return Path.GetFullPath(@"..\..\..\PMR\Messages\");
}
///
/// Gets the machine studio exe path.
///
///
public static String GetMachineStudioPath()
{
return Path.GetFullPath(@"..\..\Build\Debug\Tango.MachineStudio.UI.exe");
}
///
/// Gets the tango build path.
///
///
public static String GetBuildPath()
{
return Path.GetFullPath(@"..\..\Build\Debug");
}
///
/// Gets the SQLite database file path in DB folder.
///
///
public static String GetSQLiteFilePath()
{
return Path.GetFullPath(@"..\..\..\DB\Tango.db");
}
///
/// Initializes the logging manager.
///
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;
}
///
/// Creates a temporary folder and returns it's path.
///
///
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;
}
///
/// Creates a temporary folder and returns it's path.
///
///
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;
}
///
/// Tries to delete folder.
///
/// The path.
///
public static bool TryDeleteFolder(String path)
{
try
{
Directory.Delete(path, true);
return true;
}
catch
{
return false;
}
}
///
/// Shows the file in explorer.
///
/// Name of the file/folder.
public static void ShowInExplorer(String path)
{
Process.Start("explorer.exe", string.Format("/select,\"{0}\"", path));
}
///
/// Returns true if the two objects are equal by using deep equal.
///
/// First object.
/// Second object.
///
public static bool IsDeepEqual(Object obj1, Object obj2)
{
return obj1.IsDeepEqual(obj2);
}
}
}