basic testing

Run Settings
LanguageC#
Language Version
Run Command
using System; public class Program { public static void Main() { TestRunner.Run(new Program()); } 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" }); } }
using System; using System.Collections; 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"); } } }
using System; using System.Reflection; 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) { string msg = "", mark= "✔"; var argCount = m.GetParameters().Length; var isVoid = m.ReturnType == typeof(void); if (argCount == 0 && isVoid) { Action<string> write; try { m.Invoke(target, null); write = Console.WriteLine; passed++; } catch (Exception ex) { write = Console.Error.WriteLine; mark = "❌"; msg = ex.Message; if (!(ex.InnerException is Failed)) { msg = "Unexpected Exception ["+ex.GetType().FullName+"] " + msg; } failed++; } write(mark + " " + m.Name + "\n\t" + msg); } } Console.WriteLine("Total: " + (passed + failed) + ", Passed: " + passed + ", Failed: " + failed); } } class Failed : Exception { public Failed(string message) : base(message) { } }
Editor Settings
Theme
Key bindings
Full width
Lines