diff options
author | Lauro Moura <lauromoura@expertisesolutions.com.br> | 2017-07-05 21:48:20 -0300 |
---|---|---|
committer | Lauro Moura <lauromoura@expertisesolutions.com.br> | 2017-07-05 21:54:40 -0300 |
commit | 377531f24e8b6a91f0d7808a1e3376d256875771 (patch) | |
tree | 15d79e79e917853d2211fe4d98a3661558e4dc65 | |
parent | 865a56dcce1de09383207b45c503f55d2ef18519 (diff) | |
download | efl-377531f24e8b6a91f0d7808a1e3376d256875771.tar.gz |
efl_mono: Add a bunch of new methods to our assert
-rw-r--r-- | src/tests/efl_mono/TestUtils.cs | 141 |
1 files changed, 129 insertions, 12 deletions
diff --git a/src/tests/efl_mono/TestUtils.cs b/src/tests/efl_mono/TestUtils.cs index 459394bf2d..8fcbea191d 100644 --- a/src/tests/efl_mono/TestUtils.cs +++ b/src/tests/efl_mono/TestUtils.cs @@ -1,55 +1,172 @@ using System; using System.Runtime.CompilerServices; +using System.Runtime.Serialization; +using System.Diagnostics.CodeAnalysis; -public class Test + +/// <summary>Exception for assertion failures.</summary> +[Serializable] +public class AssertionException : Exception +{ + /// <summary> Default constructor.</summary> + public AssertionException() : base () { } + /// <summary> Most commonly used contructor.</summary> + public AssertionException(string msg) : base(msg) { } + /// <summary> Wraps an inner exception.</summary> + public AssertionException(string msg, Exception inner) : base(msg, inner) { } + /// <summary> Serializable constructor.</summary> + protected AssertionException(SerializationInfo info, StreamingContext context) : base(info, context) { } +} + +/// <summary> Helper class for Mono EFL bindings tests.</summary> +public static class Test { + /// <summary> Asserts a boolean condition.</summary> 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 Exception($"Assertion failed: {file}:{line} ({member}) {msg}"); + throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}"); } - public static void AssertEquals<T>(T expected, T actual, String msg = "", + /// <summary> Asserts if expected is equal to actual, using expected.Equals(actual).</summary> + public static void AssertEquals<T>(T expected, T actual, String msg = null, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, - [CallerMemberName] string member = null) where T : System.IComparable<T> + [CallerMemberName] string member = null) { - if (expected.CompareTo(actual) != 0) { - if (msg == "") + if (file == null) + file = "(unknown file)"; + if (member == null) + member = "(unknown member)"; + if (expected == null) + throw new AssertionException($"{file}:{line} ({member}) Null expected value. Use AssertNull."); + if (!expected.Equals(actual)) { + if (msg == null || msg.Length == 0) msg = $"Expected \"{expected}\", actual \"{actual}\""; - throw new Exception($"{file}:{line} ({member}) {msg}"); + throw new AssertionException($"{file}:{line} ({member}) {msg}"); + } + } + + /// <summary> Asserts if expected is not equal to actual, using !expected.Equals(actual).</summary> + public static void AssertNotEquals<T>(T expected, T actual, 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)"; + if (expected == null) + throw new AssertionException($"{file}:{line} ({member}) Null expected value. Use AssertNull."); + if (expected.Equals(actual)) { + if (msg == null || msg.Length == 0) + msg = $"Expected \"{expected}\" shouldn't be equal to actual \"{actual}\""; + throw new AssertionException($"{file}:{line} ({member}) {msg}"); + } + } + + /// <summary> Asserts if greater is greater than smaller , using greater.CompareTo(smaller) > 0.</summary> + public static void AssertGreaterThan<T>(T greater, T smaller, String msg = null, + [CallerLineNumber] int line = 0, + [CallerFilePath] string file = null, + [CallerMemberName] string member = null) where T : System.IComparable<T> + { + 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}"); } } - public delegate void Operation(); + /// <summary> Asserts if smaller is smaller than greater, using greater.CompareTo(smaller) < 0.</summary> + public static void AssertLessThan<T>(T smaller, T greater, String msg = null, + [CallerLineNumber] int line = 0, + [CallerFilePath] string file = null, + [CallerMemberName] string member = null) where T : System.IComparable<T> + { + 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}"); + } + } - public static void AssertRaises<T>(Operation op, String msg = "Exception not raised", + /// <summary> Asserts if op, when called, raises the exception T.</summary> + [SuppressMessage("Gendarme.Rules.Design.Generic", "AvoidMethodWithUnusedGenericTypeRule")] + public static void AssertRaises<T>(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 Exception($"Assertion failed: {file}:{line} ({member}) {msg}"); + throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}"); } - public static void AssertNotRaises<T>(Operation op, String msg = "Exception raised.", + /// <summary> Asserts if op, when called, does not raise the exception T.</summary> + [SuppressMessage("Gendarme.Rules.Design.Generic", "AvoidMethodWithUnusedGenericTypeRule")] + public static void AssertNotRaises<T>(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 Exception($"Assertion failed: {file}:{line} ({member}) {msg}"); + throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}"); } } + + /// <summary> Asserts if the given reference is null.</summary> + 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}"); + } } |