using System; using System.Runtime.CompilerServices; using System.Runtime.Serialization; using System.Diagnostics.CodeAnalysis; /// Exception for assertion failures. [Serializable] public class AssertionException : Exception { /// Default constructor. public AssertionException() : base () { } /// Most commonly used contructor. public AssertionException(string msg) : base(msg) { } /// Wraps an inner exception. public AssertionException(string msg, Exception inner) : base(msg, inner) { } /// Serializable constructor. protected AssertionException(SerializationInfo info, StreamingContext context) : base(info, context) { } } /// Helper class for Mono EFL bindings tests. public static class Test { /// Asserts a boolean condition. public static void Assert(bool res, String msg = "Assertion failed", [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) { if (msg == null) msg = "Assertion failed."; if (file == null) file = "(unknown file)"; if (member == null) member = "(unknown member)"; if (!res) throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}"); } /// Asserts if expected is equal to actual, using expected.Equals(actual). public static void AssertEquals(T expected, T actual, String msg = null, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) { if (expected == null && actual == null) return; if (expected == null || !expected.Equals(actual)) { if (file == null) file = "(unknown file)"; if (member == null) member = "(unknown member)"; if (msg == null || msg.Length == 0) msg = $"Expected \"{expected}\", actual \"{actual}\""; throw new AssertionException($"{file}:{line} ({member}) {msg}"); } } /// Asserts if expected is not equal to actual, using !expected.Equals(actual). public static void AssertNotEquals(T expected, T actual, String msg = null, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) { if (expected == null ? actual == null : expected.Equals(actual)) { if (file == null) file = "(unknown file)"; if (member == null) member = "(unknown member)"; if (msg == null || msg.Length == 0) msg = $"Expected \"{expected}\" shouldn't be equal to actual \"{actual}\""; throw new AssertionException($"{file}:{line} ({member}) {msg}"); } } /// Asserts if actual is near enough of expected, using the optional tolerance (default 0.00001). public static void AssertAlmostEquals(double expected, double actual, double tolerance=0.00001, String msg = null, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) { if (file == null) file = "(unknown file)"; if (member == null) member = "(unknown member)"; double difference = Math.Abs(expected - actual); if (difference > tolerance) { if (msg == null || msg.Length == 0) msg = $"Expected \"{expected}\". Difference: \"{difference}\""; throw new AssertionException($"{file}:{line} ({member}) {msg}"); } } /// Asserts if greater is greater than smaller , using greater.CompareTo(smaller) > 0. public static void AssertGreaterThan(T greater, T smaller, String msg = null, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) where T : System.IComparable { if (file == null) file = "(unknown file)"; if (member == null) member = "(unknown member)"; if (greater == null || smaller == null) throw new AssertionException($"{file}:{line} ({member}) Null input value. Use AssertNull."); if (greater.CompareTo(smaller) <= 0) { if (msg == null || msg.Length == 0) msg = $"Greater \"{greater}\" is not greater than smaller \"{smaller}\""; throw new AssertionException($"{file}:{line} ({member}) {msg}"); } } /// Asserts if smaller is smaller than greater, using greater.CompareTo(smaller) < 0. public static void AssertLessThan(T smaller, T greater, String msg = null, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) where T : System.IComparable { if (file == null) file = "(unknown file)"; if (member == null) member = "(unknown member)"; if (greater == null || smaller == null) throw new AssertionException($"{file}:{line} ({member}) Null input value. Use AssertNull."); if (smaller.CompareTo(greater) >= 0) { if (msg == null || msg.Length == 0) msg = $"Smaller \"{smaller}\" is not smaller than greater \"{greater}\""; throw new AssertionException($"{file}:{line} ({member}) {msg}"); } } /// Asserts if op, when called, raises the exception T. [SuppressMessage("Gendarme.Rules.Design.Generic", "AvoidMethodWithUnusedGenericTypeRule")] public static void AssertRaises(Action op, String msg = null, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) where T: Exception { if (msg == null) msg = "Exception not raised."; if (file == null) file = "(unknown file)"; if (member == null) member = "(unknown member)"; if (op == null) throw new AssertionException($"Assertion failed: {file}:{line} ({member}) Null operation."); try { op(); } catch (T) { return; } throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}"); } /// Asserts if op, when called, does not raise the exception T. [SuppressMessage("Gendarme.Rules.Design.Generic", "AvoidMethodWithUnusedGenericTypeRule")] public static void AssertNotRaises(Action op, String msg = null, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) where T: Exception { if (msg == null) msg = "Exception raised."; if (file == null) file = "(unknown file)"; if (member == null) member = "(unknown member)"; if (op == null) throw new AssertionException($"Assertion failed: {file}:{line} ({member}) Null operation."); try { op(); } catch (T) { throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}"); } } /// Asserts if the given reference is null. public static void AssertNull(object reference, String msg = "Reference not null", [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) { if (reference != null) throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}"); } /// Asserts if the given reference is not null. public static void AssertNotNull(object reference, String msg = "Reference is null", [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null) { if (reference == null) throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}"); } /// Runs a number of garbage collections and iterate the main loop. /// The iteration is needed to make sure objects collected in the GC thread /// are efl_unref'd in the main thread. public static void CollectAndIterate(int iterations=1000) { for (int i = 0; i < iterations; i++) { System.GC.Collect(); } System.GC.WaitForPendingFinalizers(); Efl.App.AppMain.Iterate(); } }