~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to src/NUnitFramework/framework/Assertion.cs

  • Committer: jnewkirk
  • Date: 2002-07-10 20:05:24 UTC
  • Revision ID: vcs-imports@canonical.com-20020710200524-z33q2om2qvsgs6kg
initialĀ load

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace Nunit.Framework 
 
2
{
 
3
        using System;
 
4
 
 
5
        /// <summary>A set of Assert methods.</summary>
 
6
        public class Assertion
 
7
        {
 
8
                /// <summary>
 
9
                /// Asserts that a condition is true. If it isn't it throws
 
10
                /// an <see cref="AssertionFailedError"/>.
 
11
                /// </summary>
 
12
                /// <param name="message">The message to display is the condition
 
13
                /// is false</param>
 
14
                /// <param name="condition">The evaluated condition</param>
 
15
                static public void Assert(string message, bool condition) 
 
16
                {
 
17
                        if (!condition)
 
18
                                Assertion.Fail(message);
 
19
                }
 
20
    
 
21
                /// <summary>
 
22
                /// Asserts that a condition is true. If it isn't it throws
 
23
                /// an <see cref="AssertionFailedError"/>.
 
24
                /// </summary>
 
25
                /// <param name="condition">The evaluated condition</param>
 
26
                static public void Assert(bool condition) 
 
27
                {
 
28
                        Assertion.Assert(string.Empty, condition);
 
29
                }
 
30
 
 
31
                /// <summary>
 
32
                /// /// Asserts that two doubles are equal concerning a delta. If the
 
33
                /// expected value is infinity then the delta value is ignored.
 
34
                /// </summary>
 
35
                /// <param name="expected">The expected value</param>
 
36
                /// <param name="actual">The actual value</param>
 
37
                /// <param name="delta">The maximum acceptable difference between the
 
38
                /// the expected and the actual</param>
 
39
                static public void AssertEquals(double expected, double actual, double delta) 
 
40
                {
 
41
                        Assertion.AssertEquals(string.Empty, expected, actual, delta);
 
42
                }
 
43
                /// <summary>
 
44
                /// /// Asserts that two singles are equal concerning a delta. If the
 
45
                /// expected value is infinity then the delta value is ignored.
 
46
                /// </summary>
 
47
                /// <param name="expected">The expected value</param>
 
48
                /// <param name="actual">The actual value</param>
 
49
                /// <param name="delta">The maximum acceptable difference between the
 
50
                /// the expected and the actual</param>
 
51
                static public void AssertEquals(float expected, float actual, float delta) 
 
52
                {
 
53
                        Assertion.AssertEquals(string.Empty, expected, actual, delta);
 
54
                }
 
55
 
 
56
                /// <summary>Asserts that two objects are equal. If they are not
 
57
                /// an <see cref="AssertionFailedError"/> is thrown.</summary>
 
58
                static public void AssertEquals(Object expected, Object actual) 
 
59
                {
 
60
                        Assertion.AssertEquals(string.Empty, expected, actual);
 
61
                }
 
62
 
 
63
                static public void AssertEquals(int expected, int actual) 
 
64
                {
 
65
                        Assertion.AssertEquals(string.Empty, expected, actual);
 
66
                }
 
67
 
 
68
                static public void AssertEquals(string message, int expected, int actual) 
 
69
                {
 
70
                        if (expected != actual)
 
71
                                Assertion.FailNotEquals(message, expected, actual);
 
72
                }
 
73
                
 
74
                /// <summary>Asserts that two doubles are equal concerning a delta.
 
75
                /// If the expected value is infinity then the delta value is ignored.
 
76
                /// </summary>
 
77
                static public void AssertEquals(string message, double expected, 
 
78
                        double actual, double delta) 
 
79
                {
 
80
                        // handle infinity specially since subtracting two infinite values gives 
 
81
                        // NaN and the following test fails
 
82
                        if (double.IsInfinity(expected)) 
 
83
                        {
 
84
                                if (!(expected == actual))
 
85
                                        Assertion.FailNotEquals(message, expected, actual);
 
86
                        } 
 
87
                        else if (!(Math.Abs(expected-actual) <= delta))
 
88
                                Assertion.FailNotEquals(message, expected, actual);
 
89
                }
 
90
                
 
91
                /// <summary>Asserts that two floats are equal concerning a delta.
 
92
                /// If the expected value is infinity then the delta value is ignored.
 
93
                /// </summary>
 
94
                static public void AssertEquals(string message, float expected, 
 
95
                        float actual, float delta) 
 
96
                {
 
97
                        // handle infinity specially since subtracting two infinite values gives 
 
98
                        // NaN and the following test fails
 
99
                        if (float.IsInfinity(expected)) 
 
100
                        {
 
101
                                if (!(expected == actual))
 
102
                                        Assertion.FailNotEquals(message, expected, actual);
 
103
                        } 
 
104
                        else if (!(Math.Abs(expected-actual) <= delta))
 
105
                                Assertion.FailNotEquals(message, expected, actual);
 
106
                }
 
107
 
 
108
        /// <summary>
 
109
        /// Used to compare int and longs.  Comparisons between
 
110
        /// same types are fine (Int32 to Int32, or Int64 to Int64),
 
111
        /// but the Equals method fails across different types.
 
112
        /// This method was added to allow ints and longs to
 
113
        /// be handled correctly via casting.
 
114
        /// </summary>
 
115
        /// <param name="expected"></param>
 
116
        /// <param name="actual"></param>
 
117
        /// <returns></returns>
 
118
        static private bool ObjectsEqual( Object expected, Object actual )
 
119
        {
 
120
            if( expected is int && actual is long )
 
121
            {
 
122
                return (int)expected == (long)actual;
 
123
            }
 
124
            if( expected is long && actual is int )
 
125
            {
 
126
                return (long)expected == (int)actual;
 
127
            }
 
128
            return expected.Equals(actual);
 
129
        }
 
130
 
 
131
                /// <summary>Asserts that two objects are equal. If they are not
 
132
                /// an <see cref="AssertionFailedError"/> is thrown.</summary>
 
133
                static public void AssertEquals(string message, Object expected, Object actual)
 
134
                {
 
135
                        if (expected == null && actual == null)
 
136
                                return;
 
137
                        if (expected != null && ObjectsEqual( expected, actual ))
 
138
                                return;
 
139
                        Assertion.FailNotEquals(message, expected, actual);
 
140
                }
 
141
    
 
142
                /// <summary>Asserts that an object isn't null.</summary>
 
143
                static public void AssertNotNull(Object anObject) 
 
144
                {
 
145
                        Assertion.AssertNotNull(string.Empty, anObject);
 
146
                }
 
147
    
 
148
                /// <summary>Asserts that an object isn't null.</summary>
 
149
                static public void AssertNotNull(string message, Object anObject) 
 
150
                {
 
151
                        Assertion.Assert(message, anObject != null); 
 
152
                }
 
153
    
 
154
                /// <summary>Asserts that an object is null.</summary>
 
155
                static public void AssertNull(Object anObject) 
 
156
                {
 
157
                        Assertion.AssertNull(string.Empty, anObject);
 
158
                }
 
159
    
 
160
                /// <summary>Asserts that an object is null.</summary>
 
161
                static public void AssertNull(string message, Object anObject) 
 
162
                {
 
163
                        Assertion.Assert(message, anObject == null); 
 
164
                }
 
165
    
 
166
                /// <summary>Asserts that two objects refer to the same object. If they
 
167
                /// are not the same an <see cref="AssertionFailedError"/> is thrown.
 
168
                /// </summary>
 
169
                static public void AssertSame(Object expected, Object actual) 
 
170
                {
 
171
                        Assertion.AssertSame(string.Empty, expected, actual);
 
172
                }
 
173
    
 
174
                /// <summary>Asserts that two objects refer to the same object. 
 
175
                /// If they are not an <see cref="AssertionFailedError"/> is thrown.
 
176
                /// </summary>
 
177
                static public void AssertSame(string message, Object expected, Object actual)
 
178
                {
 
179
                        if (expected == actual)
 
180
                                return;
 
181
                        Assertion.FailNotSame(message, expected, actual);
 
182
                }
 
183
    
 
184
                /// <summary>Fails a test with no message.</summary>
 
185
                static public void Fail() 
 
186
                {
 
187
                        Assertion.Fail(string.Empty);
 
188
                }
 
189
    
 
190
                /// <summary>Fails a test with the given message.</summary>
 
191
                static public void Fail(string message) 
 
192
                {
 
193
                        if (message == null)
 
194
                                message = string.Empty;
 
195
                        throw new AssertionException(message);
 
196
                }
 
197
 
 
198
        /// <summary>
 
199
        /// Called when two objects have been compared and found to be
 
200
        /// different.
 
201
        /// </summary>
 
202
        /// <param name="message"></param>
 
203
        /// <param name="expected"></param>
 
204
        /// <param name="actual"></param>
 
205
                static private void FailNotEquals(string message, Object expected, Object actual) 
 
206
                {
 
207
            Assertion.Fail( 
 
208
                AssertionFailureMessage.FormatMessageForFailNotEquals( 
 
209
                    message, 
 
210
                    expected, 
 
211
                    actual) );
 
212
                }
 
213
    
 
214
                static private void FailNotSame(string message, Object expected, Object actual) 
 
215
                {
 
216
                        string formatted=string.Empty;
 
217
                        if (message != null)
 
218
                                formatted= message+" ";
 
219
                        Assertion.Fail(formatted+"expected same");
 
220
                }
 
221
        }
 
222
}
 
 
b'\\ No newline at end of file'