~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to addins/RowTest/NUnitExtension.RowTest.AddIn.UnitTests/RowTestAddInTest.cs

  • Committer: charliepoole
  • Date: 2008-05-05 22:17:22 UTC
  • Revision ID: vcs-imports@canonical.com-20080505221722-7bdjujqed8pk6al3
Add back RowTestExtension as a separately compiled assembly, bundled
with NUnit, and modify build script to handle addins.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// *********************************************************************
 
2
// Copyright 2007, Andreas Schlapsi
 
3
// This is free software licensed under the MIT license. 
 
4
// *********************************************************************
 
5
using System;
 
6
using System.Reflection;
 
7
using NUnit.Core;
 
8
using NUnit.Core.Extensibility;
 
9
using NUnit.Framework;
 
10
using NUnit.Framework.SyntaxHelpers;
 
11
#if !NMOCK2
 
12
using NUnit.Mocks;
 
13
#endif
 
14
 
 
15
namespace NUnitExtension.RowTest.AddIn.UnitTests
 
16
{
 
17
        [TestFixture]
 
18
        public class RowTestAddInTest : BaseTestFixture
 
19
        {
 
20
        private Test dummy = null; // OK unless builder starts using the suite argument
 
21
 
 
22
#if NMOCK2
 
23
                private NMock2.Mockery _mocks;
 
24
                
 
25
                [SetUp]
 
26
                public void SetUp()
 
27
                {
 
28
                        _mocks = new NMock2.Mockery();
 
29
                }
 
30
#endif
 
31
                [Test]
 
32
                public void Install_Successful()
 
33
                {
 
34
#if NMOCK2
 
35
                        IExtensionHost extensionHostMock = (IExtensionHost)_mocks.NewMock(typeof(IExtensionHost));
 
36
                        IExtensionPoint extensionPointMock = (IExtensionPoint)_mocks.NewMock(typeof(IExtensionPoint));
 
37
                        RowTestAddIn addIn = new RowTestAddIn();
 
38
 
 
39
                        NMock2.Expect.Once.On(extensionHostMock)
 
40
                                        .Method("GetExtensionPoint").With("TestCaseBuilders")
 
41
                                        .Will(NMock2.Return.Value(extensionPointMock));
 
42
                        
 
43
                        NMock2.Expect.Once.On(extensionPointMock)
 
44
                                        .Method("Install").With(addIn);
 
45
 
 
46
                        bool installed = addIn.Install(extensionHost);
 
47
 
 
48
                        _mocks.VerifyAllExpectationsHaveBeenMet();
 
49
                        Assert.That(installed, Is.True);
 
50
#else
 
51
            DynamicMock extensionHostMock = new DynamicMock(typeof(IExtensionHost));
 
52
                    IExtensionHost extensionHost = (IExtensionHost)extensionHostMock.MockInstance;
 
53
            DynamicMock extensionPointMock = new DynamicMock(typeof(IExtensionPoint));
 
54
                    IExtensionPoint extensionPoint = (IExtensionPoint)extensionPointMock.MockInstance;
 
55
            RowTestAddIn addIn = new RowTestAddIn();
 
56
 
 
57
            extensionHostMock.ExpectAndReturn("GetExtensionPoint", extensionPointMock.MockInstance, "TestCaseBuilders");
 
58
            extensionPointMock.Expect("Install", addIn);
 
59
 
 
60
            bool installed = addIn.Install(extensionHost);
 
61
 
 
62
            extensionPointMock.Verify();
 
63
            extensionHostMock.Verify();
 
64
            Assert.That(installed, Is.True);
 
65
#endif
 
66
                }
 
67
                
 
68
                [Test]
 
69
                public void Install_Failure()
 
70
                {
 
71
#if NMOCK2
 
72
                        IExtensionHost extensionHostMock = (IExtensionHost)_mocks.NewMock(typeof(IExtensionHost));
 
73
                        RowTestAddIn addIn = new RowTestAddIn();
 
74
                        
 
75
                        NMock2.Expect.Once.On(extensionHostMock)
 
76
                                        .Method("GetExtensionPoint").With("TestCaseBuilders")
 
77
                                        .Will(NMock2.Return.Value(null));
 
78
                        
 
79
                        bool installed = addIn.Install(extensionHostMock);
 
80
                        
 
81
                        _mocks.VerifyAllExpectationsHaveBeenMet();
 
82
                        Assert.That(installed, Is.False);
 
83
#else
 
84
            DynamicMock extensionHostMock = new DynamicMock(typeof(IExtensionHost));
 
85
                    IExtensionHost extensionHost = (IExtensionHost) extensionHostMock.MockInstance;
 
86
            RowTestAddIn addIn = new RowTestAddIn();
 
87
 
 
88
            extensionHostMock.ExpectAndReturn("GetExtensionPoint",null,"TestCaseBuilders");
 
89
 
 
90
                    bool installed = addIn.Install(extensionHost);
 
91
 
 
92
            extensionHostMock.Verify();
 
93
            Assert.That(installed, Is.False);
 
94
#endif
 
95
                }
 
96
                
 
97
                [Test]
 
98
                public void CanBuildFrom_False()
 
99
                {
 
100
                        RowTestAddIn addIn = new RowTestAddIn();
 
101
                        MethodInfo method = GetTestClassMethod("MethodWithoutRowTestAttribute");
 
102
 
 
103
                        bool canBuildFrom = addIn.CanBuildFrom(method);
 
104
                        
 
105
                        Assert.That(canBuildFrom, Is.False);
 
106
                }
 
107
                
 
108
                [Test]
 
109
                public void CanBuildFrom_True()
 
110
                {
 
111
                        RowTestAddIn addIn = new RowTestAddIn();
 
112
                        MethodInfo method = GetTestClassMethod("MethodWithRowTestAttribute");
 
113
 
 
114
            bool canBuildFrom = addIn.CanBuildFrom(method);
 
115
                        
 
116
                        Assert.That(canBuildFrom, Is.True);
 
117
                }
 
118
                
 
119
                [Test]
 
120
                public void BuildFrom_WithoutRows()
 
121
                {
 
122
                        RowTestAddIn addIn = new RowTestAddIn();
 
123
                        MethodInfo method = GetTestClassMethod("MethodWithRowTestAttribute");
 
124
 
 
125
            Test test = addIn.BuildFrom(method);
 
126
                        
 
127
                        Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
 
128
                        RowTestSuite suite = (RowTestSuite) test;
 
129
                        
 
130
                        Assert.That(suite.TestCount, Is.EqualTo(0));
 
131
                }
 
132
                
 
133
                [Test]
 
134
                public void BuildFrom_WithRows()
 
135
                {
 
136
                        RowTestAddIn addIn = new RowTestAddIn();
 
137
                        MethodInfo method = GetRowTestMethodWith2Rows();
 
138
 
 
139
            Test test = addIn.BuildFrom(method);
 
140
                        
 
141
                        Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
 
142
                        RowTestSuite suite = (RowTestSuite) test;
 
143
                        
 
144
                        Assert.That(suite.TestCount, Is.EqualTo(2));
 
145
                        Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
 
146
                        Assert.That(suite.Tests[1], Is.InstanceOfType(typeof(RowTestCase)));
 
147
                        
 
148
                        RowTestCase testCase1 = (RowTestCase) suite.Tests[0];
 
149
                        RowTestCase testCase2 = (RowTestCase) suite.Tests[1];
 
150
                        
 
151
                        Assert.That(testCase1.Arguments.Length, Is.EqualTo(2));
 
152
                        Assert.That(testCase2.Arguments.Length, Is.EqualTo(2));
 
153
                }
 
154
                
 
155
                [Test]
 
156
                public void BuildFrom_WithTestName()
 
157
                {
 
158
                        RowTestAddIn addIn = new RowTestAddIn();
 
159
                        MethodInfo method = GetRowTestMethodWithTestName();
 
160
 
 
161
            Test test = addIn.BuildFrom(method);
 
162
                        
 
163
                        Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
 
164
                        RowTestSuite suite = (RowTestSuite) test;
 
165
                        
 
166
                        Assert.That(suite.TestCount, Is.EqualTo(1));
 
167
                        Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
 
168
                        
 
169
                        RowTestCase testCase = (RowTestCase) suite.Tests[0];
 
170
 
 
171
                        Assert.That(testCase.TestName.Name, Is.EqualTo("UnitTest(4, 5)"));
 
172
                }
 
173
                
 
174
                [Test]
 
175
                public void BuildFrom_WithExpectedException()
 
176
                {
 
177
                        RowTestAddIn addIn = new RowTestAddIn();
 
178
                        MethodInfo method = GetRowTestMethodWithExpectedException();
 
179
 
 
180
            Test test = addIn.BuildFrom(method);
 
181
                        
 
182
                        Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
 
183
                        RowTestSuite suite = (RowTestSuite) test;
 
184
                        
 
185
                        Assert.That(suite.TestCount, Is.EqualTo(1));
 
186
                        Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
 
187
                        
 
188
                        RowTestCase testCase = (RowTestCase) suite.Tests[0];
 
189
 
 
190
                        Assert.That(testCase.ExceptionExpected, Is.True);
 
191
                        Assert.That(testCase.ExpectedExceptionType, Is.EqualTo(typeof(InvalidOperationException)));
 
192
                }
 
193
                
 
194
                [Test]
 
195
                public void BuildFrom_WithExpectedExceptionAndExceptionMessage()
 
196
                {
 
197
                        RowTestAddIn addIn = new RowTestAddIn();
 
198
                        MethodInfo method = GetRowTestMethodWithExpectedExceptionAndExceptionMessage();
 
199
 
 
200
            Test test = addIn.BuildFrom(method);
 
201
                        
 
202
                        Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
 
203
                        RowTestSuite suite = (RowTestSuite) test;
 
204
                        
 
205
                        Assert.That(suite.TestCount, Is.EqualTo(1));
 
206
                        Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
 
207
                        
 
208
                        RowTestCase testCase = (RowTestCase) suite.Tests[0];
 
209
 
 
210
                        Assert.That(testCase.ExceptionExpected, Is.True);
 
211
                        Assert.That(testCase.ExpectedExceptionType, Is.EqualTo(typeof(InvalidOperationException)));
 
212
                        Assert.That(testCase.ExpectedMessage, Is.EqualTo("Expected Exception Message."));
 
213
                }
 
214
                
 
215
                [Test]
 
216
                public void BuildFrom_SetsNameCorrectly()
 
217
                {
 
218
                        RowTestAddIn addIn = new RowTestAddIn();
 
219
                        Type testClass = typeof(TestClass);
 
220
                        MethodInfo method = testClass.GetMethod(Method_RowTestMethodWith2Rows, BindingFlags.Public | BindingFlags.Instance);
 
221
 
 
222
            Test test = addIn.BuildFrom(method);
 
223
                        
 
224
                        Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
 
225
                        RowTestSuite suite = (RowTestSuite) test;
 
226
                        
 
227
                        Assert.That(suite.TestName.Name, Is.EqualTo(method.Name));
 
228
                        Assert.That(suite.TestName.FullName, Is.EqualTo(testClass.FullName + "." + method.Name));
 
229
                }
 
230
                
 
231
                [Test]
 
232
                public void BuildFrom_SetsCommonNUnitAttributes()
 
233
                {
 
234
                        RowTestAddIn addin = new RowTestAddIn();
 
235
                        MethodInfo method = GetTestClassMethod("RowTestMethodWithCategory");
 
236
 
 
237
            Test test = addin.BuildFrom(method);
 
238
 
 
239
                        Assert.That(test.Categories, Is.Not.Null);
 
240
                        Assert.That(test.Categories.Count, Is.EqualTo(1));
 
241
                        Assert.That(test.Categories[0], Is.EqualTo("Category"));
 
242
                }
 
243
                
 
244
                [Test]
 
245
                public void BuildFrom_WithSpecialValueNull()
 
246
                {
 
247
                        RowTestAddIn addIn = new RowTestAddIn();
 
248
                        MethodInfo method = GetTestClassMethod("RowTestMethodWithSpecialValue");
 
249
 
 
250
            Test test = addIn.BuildFrom(method);
 
251
                        
 
252
                        Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
 
253
                        RowTestSuite suite = (RowTestSuite) test;
 
254
                        
 
255
                        Assert.That(suite.TestCount, Is.EqualTo(1));
 
256
                        Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
 
257
                        
 
258
                        RowTestCase testCase = (RowTestCase) suite.Tests[0];
 
259
                        
 
260
                        Assert.That(testCase.Arguments.Length, Is.EqualTo(1));
 
261
                        Assert.That(testCase.Arguments[0], Is.Null);
 
262
                }
 
263
                
 
264
                [Test]
 
265
                public void BuildFrom_WithNull()
 
266
                {
 
267
                        RowTestAddIn addIn = new RowTestAddIn();
 
268
                        MethodInfo method = GetTestClassMethod("RowTestMethodWithNullArgument");
 
269
 
 
270
            Test test = addIn.BuildFrom(method);
 
271
                        
 
272
                        Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
 
273
                        RowTestSuite suite = (RowTestSuite) test;
 
274
                        
 
275
                        Assert.That(suite.TestCount, Is.EqualTo(1));
 
276
                        Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
 
277
                        
 
278
                        RowTestCase testCase = (RowTestCase) suite.Tests[0];
 
279
                        
 
280
                        Assert.That(testCase.Arguments, Is.Null);
 
281
                }
 
282
        }
 
283
}