Skip to content

Instantly share code, notes, and snippets.

@NearlyUnique
Last active April 26, 2017 07:19
Show Gist options
  • Save NearlyUnique/644873ec889bf6b69cb39ae62ceba9d9 to your computer and use it in GitHub Desktop.
Save NearlyUnique/644873ec889bf6b69cb39ae62ceba9d9 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Reflection;
namespace play {
public class Program {
public static void Main() {
TestRunner.Run(new Program());
Console.Read();
}
public void type_equality_fails_iof_type_mismatch() {
Assert.AreEqual(1, "1");
}
public void arrays_return_the_index_of_failure() {
Assert.AreEqual(new[] { 1 }, new[] { 2 });
}
public void arrays_will_fail_if_lengths_are_not_equal() {
Assert.AreEqual(new[] { 1 }, new[] { 1, 3 });
}
public void arrays_of_ints_can_be_compared() {
Assert.AreEqual(new[] {3, 2}, new[] {3, 2});
}
public void arrays_of_strings_can_be_compared() {
Assert.AreEqual(new[] { "abc","def","xyz" }, new[] { "abc", "def", "xyz" });
}
}
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 (Exception ex) {
if (ex.InnerException is Failed) {
var f = (Failed)ex.InnerException;
Console.WriteLine("Failed " + f.Message);
failed++;
} else {
Console.WriteLine("Unexpected Exception ["+ex.GetType().FullName+"] " + ex.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(IEnumerable expected, IEnumerable actual) {
var exp = expected.GetEnumerator();
var act = actual.GetEnumerator();
var i = 0;
while (true) {
var more = act.MoveNext();
if (more != exp.MoveNext()) {
throw new Failed("Collections not same length at index " + i);
}
if (!more) {
break;
}
try {
AreEqual(exp.Current, act.Current);
} catch (Failed ex) {
throw new Failed("At index " + i + ", " + ex.Message);
}
i++;
}
}
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