summaryrefslogtreecommitdiff
path: root/src/tests/efl_mono/TestUtils.cs
blob: 657bdb0300ae37199b416751fa3579fcb9042183 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;


/// <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 AssertionException($"Assertion failed: {file}:{line} ({member}) {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)
    {
        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 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 actual is near enough of expected, using the optional tolerance (default 0.00001).</summary>
    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}");
        }
    }

    /// <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}");
        }
    }

    /// <summary> Asserts if smaller is smaller than greater, using greater.CompareTo(smaller) &lt; 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}");
        }
    }

    /// <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 AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
    }

    /// <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 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}");
    }

    /// <summary> Asserts if the given reference is not null.</summary>
    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}");
    }

    /// <summary>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.</summary>
    public static void CollectAndIterate(int iterations=1000)
    {
        for (int i = 0; i < iterations; i++)
        {
            System.GC.Collect();
        }
        System.GC.WaitForPendingFinalizers();
        Efl.App.AppMain.Iterate();
    }

}