public
Description: Examples of writing specs in C# with NUnit.
Homepage:
Clone URL: git://github.com/ignu/specish.git
specish / Specish / nunit_spec_extensions.cs
100644 193 lines (159 sloc) 5.982 kb
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
using System;
using System.Collections;
using System.Text.RegularExpressions;
using NUnit.Framework;
 
namespace Specish
{
    public delegate void MethodThatThrows();
 
    /// <summary>
    /// Extention methods that make unit test asserts more fluid and readable.
    ///
    /// Modified from CodeIncubator: http://code.google.com/p/codeincubator/
    /// </summary>
    public static class NUnitSpecExtensions
    {
        public static void ShouldBeFalse(this bool condition)
        {
            Assert.IsFalse(condition);
        }
 
        public static void ShouldBeTrue(this bool condition)
        {
            Assert.IsTrue(condition);
        }
 
        public static object ShouldEqual(this object actual, object expected)
        {
            Assert.AreEqual(expected, actual);
            return expected;
        }
 
        public static object ShouldNotEqual(this object actual, object expected)
        {
            Assert.AreNotEqual(expected, actual);
            return expected;
        }
 
        public static void ShouldBeNull(this object anObject)
        {
            Assert.IsNull(anObject);
        }
 
        public static void ShouldNotBeNull(this object anObject)
        {
            Assert.IsNotNull(anObject);
        }
 
        public static void ShouldNotBeNullOrEmpty(this string aString)
        {
            Assert.That(!String.IsNullOrEmpty(aString), "String should not be null or empty");
        }
 
        public static object ShouldBeTheSameAs(this object actual, object expected)
        {
            Assert.AreSame(expected, actual);
            return expected;
        }
 
        public static object ShouldNotBeTheSameAs(this object actual, object expected)
        {
            Assert.AreNotSame(expected, actual);
            return expected;
        }
 
        public static void ShouldBeOfType(this object actual, Type expected)
        {
            Assert.IsInstanceOfType(expected, actual);
        }
 
        public static void ShouldNotBeOfType(this object actual, Type expected)
        {
            Assert.IsNotInstanceOfType(expected, actual);
        }
 
        public static void ShouldContain(this IList actual, object expected)
        {
            Assert.Contains(expected, actual);
        }
 
        public static IComparable ShouldBeGreaterThanOrEqualTo(this IComparable arg1, IComparable arg2)
        {
            Assert.GreaterOrEqual(arg1, arg2);
            return arg2;
        }
 
        public static IComparable ShouldBeGreaterThan(this IComparable arg1, IComparable arg2)
        {
            Assert.Greater(arg1, arg2);
            return arg2;
        }
 
        public static IComparable ShouldBeLessThan(this IComparable arg1, IComparable arg2)
        {
            Assert.Less(arg1, arg2);
            return arg2;
        }
 
        public static void ShouldBeEmpty(this ICollection collection)
        {
            Assert.IsEmpty(collection);
        }
 
        public static void ShouldBeEmpty(this string aString)
        {
            Assert.IsEmpty(aString);
        }
 
        public static void ShouldNotBeEmpty(this ICollection collection)
        {
            Assert.IsNotEmpty(collection);
        }
 
        public static void ShouldNotBeEmpty(this string aString)
        {
            Assert.IsNotEmpty(aString);
        }
 
        public static void ShouldContain(this string actual, string expected)
        {
            StringAssert.Contains(expected, actual);
        }
 
        public static void ShouldNotContain(this string actual, string expected)
        {
            Assert.That(actual.IndexOf(expected) < 0);
        }
 
        public static string ShouldBeEqualIgnoringCase(this string actual, string expected)
        {
            StringAssert.AreEqualIgnoringCase(expected, actual);
            return expected;
        }
 
        public static void ShouldEndWith(this string actual, string expected)
        {
            StringAssert.EndsWith(expected, actual);
        }
 
        public static void ShouldStartWith(this string actual, string expected)
        {
            StringAssert.StartsWith(expected, actual);
        }
 
        public static void ShouldContainErrorMessage(this Exception exception, string expected)
        {
            StringAssert.Contains(expected, exception.Message);
        }
 
        public static Exception ShouldBeThrownBy(this Type exceptionType, MethodThatThrows method)
        {
            Exception exception = null;
 
            try
            {
                method();
            }
            catch (Exception e)
            {
                Assert.AreEqual(exceptionType, e.GetType());
                exception = e;
            }
 
            if (exception == null)
            {
                Assert.Fail(String.Format("Expected {0} to be thrown.", exceptionType.FullName));
            }
 
            return exception;
        }
 
        public static void ShouldEqualSqlDate(this DateTime actual, DateTime expected)
        {
            TimeSpan timeSpan = actual - expected;
            Assert.Less(Math.Abs(timeSpan.TotalMilliseconds), 3);
        }
 
        public static void ShouldMatch(this string actual, string pattern)
        {
            Regex regex = new Regex(pattern);
            Assert.That(regex.Matches(actual).Count > 0, "Regular Expression (" + pattern + ") does not match: \r\n" + actual);
        }
 
        public static void ShouldContainExactly(this string actual, int count, string pattern)
        {
            Regex regex = new Regex(pattern);
            Assert.That(regex.Matches(actual).Count == count,
                string.Format("Regular Expression ({0}) does not contain {1} matches in : {2}",
                    pattern, count, actual));
        }
    }
 
}