Skip to content

Instantly share code, notes, and snippets.

@NearlyUnique
Created April 4, 2017 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NearlyUnique/86555c08d253269f85f200fb81fd9903 to your computer and use it in GitHub Desktop.
Save NearlyUnique/86555c08d253269f85f200fb81fd9903 to your computer and use it in GitHub Desktop.
For use in glot.io if required
using System;
using System.Reflection;
namespace testing {
public static class TestRunner{
public static void Run(object target){
var mi = target.GetType().GetMethods(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
int passed=0,failed=0;
foreach(var m in mi){
var argCount = m.GetParameters().Length;
var isVoid=m.ReturnType == typeof(void);
if (argCount==0 && isVoid){
Console.WriteLine($"> {m.Name}");
try{
m.Invoke(target, null);
Console.WriteLine("\tPassed");
passed++;
}catch(TargetInvocationException tex) when (tex.InnerException is Failed){
var f = tex.InnerException as Failed;
Console.WriteLine($"Failed {f.Message}");
failed++;
}
}
}
Console.WriteLine($"Total: {passed+failed}, Passed: {passed}, Failed: {failed}");
}
}
class Failed:Exception{
public Failed(string message):base(message){}
}
public static class Assert{
public static void AreEqual(object expected, object actual){
if (expected.GetType() != actual.GetType()){
throw new Failed("Types are not equal");
}
if (!expected.Equals(actual)){
var q=(actual is string)?"\"":"";
throw new Failed($"Values are not equal,\n\texpected {q}{expected}{q}\n\tactual {q}{actual}{q}");
}
}
public static void IsNull(object expected){
if (expected!=null){
throw new Failed($"Expected null");
}
}
public static void NotNull(object expected){
if (expected==null){
throw new Failed($"Expected not null");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment