summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2017-07-05 21:48:20 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2017-07-13 19:00:53 -0300
commit4502ce658f8fd6717ee5c98d488539a7f3a1b2cf (patch)
tree6d07463f64e275e6b70102cb99565503758f9a57
parent0c1a69cd2ad78bbc51233bc67b7d0b0238d1d9af (diff)
efl_mono: Add a bunch of new methods to our assert
-rw-r--r--src/tests/efl_mono/TestUtils.cs141
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 @@
1using System; 1using System;
2using System.Runtime.CompilerServices; 2using System.Runtime.CompilerServices;
3using System.Runtime.Serialization;
4using System.Diagnostics.CodeAnalysis;
3 5
4public class Test 6
7/// <summary>Exception for assertion failures.</summary>
8[Serializable]
9public class AssertionException : Exception
10{
11 /// <summary> Default constructor.</summary>
12 public AssertionException() : base () { }
13 /// <summary> Most commonly used contructor.</summary>
14 public AssertionException(string msg) : base(msg) { }
15 /// <summary> Wraps an inner exception.</summary>
16 public AssertionException(string msg, Exception inner) : base(msg, inner) { }
17 /// <summary> Serializable constructor.</summary>
18 protected AssertionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
19}
20
21/// <summary> Helper class for Mono EFL bindings tests.</summary>
22public static class Test
5{ 23{
24 /// <summary> Asserts a boolean condition.</summary>
6 public static void Assert(bool res, String msg = "Assertion failed", 25 public static void Assert(bool res, String msg = "Assertion failed",
7 [CallerLineNumber] int line = 0, 26 [CallerLineNumber] int line = 0,
8 [CallerFilePath] string file = null, 27 [CallerFilePath] string file = null,
9 [CallerMemberName] string member = null) 28 [CallerMemberName] string member = null)
10 { 29 {
30 if (msg == null)
31 msg = "Assertion failed.";
32 if (file == null)
33 file = "(unknown file)";
34 if (member == null)
35 member = "(unknown member)";
11 if (!res) 36 if (!res)
12 throw new Exception($"Assertion failed: {file}:{line} ({member}) {msg}"); 37 throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
13 } 38 }
14 39
15 public static void AssertEquals<T>(T expected, T actual, String msg = "", 40 /// <summary> Asserts if expected is equal to actual, using expected.Equals(actual).</summary>
41 public static void AssertEquals<T>(T expected, T actual, String msg = null,
16 [CallerLineNumber] int line = 0, 42 [CallerLineNumber] int line = 0,
17 [CallerFilePath] string file = null, 43 [CallerFilePath] string file = null,
18 [CallerMemberName] string member = null) where T : System.IComparable<T> 44 [CallerMemberName] string member = null)
19 { 45 {
20 if (expected.CompareTo(actual) != 0) { 46 if (file == null)
21 if (msg == "") 47 file = "(unknown file)";
48 if (member == null)
49 member = "(unknown member)";
50 if (expected == null)
51 throw new AssertionException($"{file}:{line} ({member}) Null expected value. Use AssertNull.");
52 if (!expected.Equals(actual)) {
53 if (msg == null || msg.Length == 0)
22 msg = $"Expected \"{expected}\", actual \"{actual}\""; 54 msg = $"Expected \"{expected}\", actual \"{actual}\"";
23 throw new Exception($"{file}:{line} ({member}) {msg}"); 55 throw new AssertionException($"{file}:{line} ({member}) {msg}");
56 }
57 }
58
59 /// <summary> Asserts if expected is not equal to actual, using !expected.Equals(actual).</summary>
60 public static void AssertNotEquals<T>(T expected, T actual, String msg = null,
61 [CallerLineNumber] int line = 0,
62 [CallerFilePath] string file = null,
63 [CallerMemberName] string member = null)
64 {
65 if (file == null)
66 file = "(unknown file)";
67 if (member == null)
68 member = "(unknown member)";
69 if (expected == null)
70 throw new AssertionException($"{file}:{line} ({member}) Null expected value. Use AssertNull.");
71 if (expected.Equals(actual)) {
72 if (msg == null || msg.Length == 0)
73 msg = $"Expected \"{expected}\" shouldn't be equal to actual \"{actual}\"";
74 throw new AssertionException($"{file}:{line} ({member}) {msg}");
75 }
76 }
77
78 /// <summary> Asserts if greater is greater than smaller , using greater.CompareTo(smaller) > 0.</summary>
79 public static void AssertGreaterThan<T>(T greater, T smaller, String msg = null,
80 [CallerLineNumber] int line = 0,
81 [CallerFilePath] string file = null,
82 [CallerMemberName] string member = null) where T : System.IComparable<T>
83 {
84 if (file == null)
85 file = "(unknown file)";
86 if (member == null)
87 member = "(unknown member)";
88 if (greater == null || smaller == null)
89 throw new AssertionException($"{file}:{line} ({member}) Null input value. Use AssertNull.");
90 if (greater.CompareTo(smaller) <= 0) {
91 if (msg == null || msg.Length == 0)
92 msg = $"Greater \"{greater}\" is not greater than smaller \"{smaller}\"";
93 throw new AssertionException($"{file}:{line} ({member}) {msg}");
24 } 94 }
25 } 95 }
26 96
27 public delegate void Operation(); 97 /// <summary> Asserts if smaller is smaller than greater, using greater.CompareTo(smaller) &lt; 0.</summary>
98 public static void AssertLessThan<T>(T smaller, T greater, String msg = null,
99 [CallerLineNumber] int line = 0,
100 [CallerFilePath] string file = null,
101 [CallerMemberName] string member = null) where T : System.IComparable<T>
102 {
103 if (file == null)
104 file = "(unknown file)";
105 if (member == null)
106 member = "(unknown member)";
107 if (greater == null || smaller == null)
108 throw new AssertionException($"{file}:{line} ({member}) Null input value. Use AssertNull.");
109 if (smaller.CompareTo(greater) >= 0) {
110 if (msg == null || msg.Length == 0)
111 msg = $"Smaller \"{smaller}\" is not smaller than greater \"{greater}\"";
112 throw new AssertionException($"{file}:{line} ({member}) {msg}");
113 }
114 }
28 115
29 public static void AssertRaises<T>(Operation op, String msg = "Exception not raised", 116 /// <summary> Asserts if op, when called, raises the exception T.</summary>
117 [SuppressMessage("Gendarme.Rules.Design.Generic", "AvoidMethodWithUnusedGenericTypeRule")]
118 public static void AssertRaises<T>(Action op, String msg = null,
30 [CallerLineNumber] int line = 0, 119 [CallerLineNumber] int line = 0,
31 [CallerFilePath] string file = null, 120 [CallerFilePath] string file = null,
32 [CallerMemberName] string member = null) where T: Exception 121 [CallerMemberName] string member = null) where T: Exception
33 { 122 {
123 if (msg == null)
124 msg = "Exception not raised.";
125 if (file == null)
126 file = "(unknown file)";
127 if (member == null)
128 member = "(unknown member)";
129 if (op == null)
130 throw new AssertionException($"Assertion failed: {file}:{line} ({member}) Null operation.");
34 try { 131 try {
35 op(); 132 op();
36 } catch (T) { 133 } catch (T) {
37 return; 134 return;
38 } 135 }
39 throw new Exception($"Assertion failed: {file}:{line} ({member}) {msg}"); 136 throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
40 } 137 }
41 138
42 public static void AssertNotRaises<T>(Operation op, String msg = "Exception raised.", 139 /// <summary> Asserts if op, when called, does not raise the exception T.</summary>
140 [SuppressMessage("Gendarme.Rules.Design.Generic", "AvoidMethodWithUnusedGenericTypeRule")]
141 public static void AssertNotRaises<T>(Action op, String msg = null,
43 [CallerLineNumber] int line = 0, 142 [CallerLineNumber] int line = 0,
44 [CallerFilePath] string file = null, 143 [CallerFilePath] string file = null,
45 [CallerMemberName] string member = null) where T: Exception 144 [CallerMemberName] string member = null) where T: Exception
46 { 145 {
146 if (msg == null)
147 msg = "Exception raised.";
148 if (file == null)
149 file = "(unknown file)";
150 if (member == null)
151 member = "(unknown member)";
152 if (op == null)
153 throw new AssertionException($"Assertion failed: {file}:{line} ({member}) Null operation.");
47 try { 154 try {
48 op(); 155 op();
49 } catch (T) { 156 } catch (T) {
50 throw new Exception($"Assertion failed: {file}:{line} ({member}) {msg}"); 157 throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
51 } 158 }
52 } 159 }
160
161 /// <summary> Asserts if the given reference is null.</summary>
162 public static void AssertNull(object reference, String msg = "Reference not null",
163 [CallerLineNumber] int line = 0,
164 [CallerFilePath] string file = null,
165 [CallerMemberName] string member = null)
166 {
167 if (reference != null)
168 throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
169 }
53} 170}
54 171
55 172