1 // ***********************************************************************
2 // Copyright (c) 2009 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 
24 using System;
25 using NUnit.Framework;
26 using NUnit.Framework.Api;
27 using NUnit.Framework.Internal;
28 
29 namespace NUnit.TestUtilities
30 {
31     public class TestAssert
32     {
33         #region IsRunnable
34 
35         /// <summary>
36         /// Verify that a test is runnable
37         /// </summary>
IsRunnable(Test test)38         public static void IsRunnable(Test test)
39         {
40             Assert.AreEqual(RunState.Runnable, test.RunState);
41         }
42 
43         /// <summary>
44         /// Verify that the first child test is runnable
45         /// </summary>
FirstChildIsRunnable(Test test)46         public static void FirstChildIsRunnable(Test test)
47         {
48             IsRunnable((Test)test.Tests[0]);
49         }
50 
51         /// <summary>
52         /// Verify that a Type can be used to create a
53         /// runnable fixture
54         /// </summary>
IsRunnable(Type type)55         public static void IsRunnable(Type type)
56         {
57             TestSuite suite = TestBuilder.MakeFixture(type);
58             Assert.NotNull(suite, "Unable to construct fixture");
59             Assert.AreEqual(RunState.Runnable, suite.RunState);
60         }
61 
62         /// <summary>
63         /// Verify that a Type is runnable, then run it and
64         /// verify the result.
65         /// </summary>
IsRunnable(Type type, ResultState resultState)66         public static void IsRunnable(Type type, ResultState resultState)
67         {
68             TestSuite suite = TestBuilder.MakeFixture(type);
69             Assert.NotNull(suite, "Unable to construct fixture");
70             Assert.AreEqual(RunState.Runnable, suite.RunState);
71             ITestResult result = TestBuilder.RunTest(suite);
72             Assert.AreEqual(resultState, result.ResultState);
73         }
74 
75         /// <summary>
76         /// Verify that a named test method is runnable
77         /// </summary>
IsRunnable(Type type, string name)78         public static void IsRunnable(Type type, string name)
79         {
80             Test test = TestBuilder.MakeTestCase(type, name);
81             Assert.That(test.RunState, Is.EqualTo(RunState.Runnable));
82         }
83 
84         /// <summary>
85         /// Verify that the first child (usually a test case)
86         /// of a named test method is runnable
87         /// </summary>
FirstChildIsRunnable(Type type, string name)88         public static void FirstChildIsRunnable(Type type, string name)
89         {
90             Test suite = TestBuilder.MakeTestCase(type, name);
91             TestAssert.FirstChildIsRunnable(suite);
92         }
93 
94         /// <summary>
95         /// Verify that a named test method is runnable, then
96         /// run it and verify the result.
97         /// </summary>
IsRunnable(Type type, string name, ResultState resultState)98         public static void IsRunnable(Type type, string name, ResultState resultState)
99         {
100             Test test = TestBuilder.MakeTestCase(type, name);
101             Assert.That(test.RunState, Is.EqualTo(RunState.Runnable));
102             object testObject = Activator.CreateInstance(type);
103             ITestResult result = TestBuilder.RunTest(test, testObject);
104             if (result.HasChildren) // In case it's a parameterized method
105                 result = (ITestResult)result.Children[0];
106             Assert.That(result.ResultState, Is.EqualTo(resultState));
107         }
108 
109         #endregion
110 
111         #region IsNotRunnable
IsNotRunnable(Test test)112         public static void IsNotRunnable(Test test)
113         {
114             Assert.AreEqual(RunState.NotRunnable, test.RunState);
115             ITestResult result = TestBuilder.RunTest(test, null);
116             Assert.AreEqual(ResultState.NotRunnable, result.ResultState);
117         }
118 
IsNotRunnable(Type type)119         public static void IsNotRunnable(Type type)
120         {
121             TestSuite fixture = TestBuilder.MakeFixture(type);
122             Assert.NotNull(fixture, "Unable to construct fixture");
123             IsNotRunnable(fixture);
124         }
125 
IsNotRunnable(Type type, string name)126         public static void IsNotRunnable(Type type, string name)
127         {
128             IsNotRunnable(TestBuilder.MakeTestCase(type, name));
129         }
130 
FirstChildIsNotRunnable(Test suite)131         public static void FirstChildIsNotRunnable(Test suite)
132         {
133             IsNotRunnable((Test)suite.Tests[0]);
134         }
135 
FirstChildIsNotRunnable(Type type, string name)136         public static void FirstChildIsNotRunnable(Type type, string name)
137         {
138             FirstChildIsNotRunnable(TestBuilder.MakeParameterizedMethodSuite(type, name));
139         }
140         #endregion
141 
TestAssert()142         private TestAssert() { }
143     }
144 }
145