2020-06-18 01:04:40 +00:00
using System ;
using System.Collections.Concurrent ;
using System.IO ;
using System.Runtime.CompilerServices ;
namespace GamecraftModdingAPI.Tests
{
2020-06-23 17:49:42 +00:00
/// <summary>
/// API test system assertion utilities.
/// </summary>
2020-06-18 01:04:40 +00:00
public static class Assert
{
private static StreamWriter logFile = null ;
private static ConcurrentDictionary < string , string > callbacks = new ConcurrentDictionary < string , string > ( ) ;
private const string PASS = "SUCCESS: " ;
private const string FAIL = "FAILURE: " ;
private const string WARN = "WARNING: " ;
private const string INFO = "DEBUG: " ;
2020-06-23 17:49:42 +00:00
/// <summary>
/// Log a message to the test log.
/// </summary>
/// <param name="msg">Message.</param>
/// <param name="end">Message ending.</param>
2020-06-18 01:04:40 +00:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Log ( string msg , string end = "\n" )
{
if ( logFile = = null ) openTestLog ( ) ;
logFile . Write ( msg + end ) ;
logFile . Flush ( ) ;
}
2020-06-23 17:49:42 +00:00
/// <summary>
/// Asserts that the event receives a callback... eventually.
/// Add the eventhandler returned by this method to the relevant event.
/// This does not assert that the callback happens under that event's intended circumstances.
/// Add another event handler to assert specific circumstance requirements.
/// </summary>
/// <returns>The callback event handler.</returns>
/// <param name="eventName">Event name.</param>
/// <param name="eventMsg">Event error message.</param>
/// <typeparam name="T">The event handler callback argument object.</typeparam>
2020-06-18 01:04:40 +00:00
public static EventHandler < T > CallsBack < T > ( string eventName , string eventMsg = null )
{
if ( eventMsg = = null ) eventMsg = $"expected callback to {eventName} but it never occurred..." ;
callbacks [ eventName ] = eventMsg ;
return ( sender , args ) = >
{
string value = null ;
if ( ! callbacks . TryRemove ( eventName , out value ) ) { Log ( WARN + $"callback to {eventName} occurred again or a related error occurred... (Received '{args.ToString()}' from '{(sender == null ? (string)sender : sender.ToString())}')" ) ; }
Log ( PASS + $"callback to {eventName} occurred... (Received '{args.ToString()}' from '{(sender == null ? (string)sender : sender.ToString())}')" ) ;
TestRoot . TestsPassed = true ;
} ;
}
internal static void CallsComplete ( )
{
foreach ( string key in callbacks . Keys )
{
Log ( FAIL + callbacks [ key ] ) ;
TestRoot . TestsPassed = false ;
}
}
internal static void CloseLog ( )
{
if ( logFile ! = null ) logFile . Close ( ) ;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void openTestLog ( )
{
logFile = File . CreateText ( TestRoot . ReportFile ) ;
}
}
}