|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | +using Rubberduck.UnitTesting; |
| 6 | +using Rubberduck.VBEditor; |
| 7 | +using Moq; |
| 8 | +using Rubberduck.VBEditor.VBEHost; |
| 9 | + |
| 10 | +namespace RubberduckTests.UnitTesting |
| 11 | +{ |
| 12 | + [TestClass] |
| 13 | + public class EngineTests |
| 14 | + { |
| 15 | + private TestEngine _engine; |
| 16 | + private Mock<IHostApplication> _hostAppMock; |
| 17 | + private readonly QualifiedModuleName _moduleName = new QualifiedModuleName("VBAProject", "TestModule1"); |
| 18 | + |
| 19 | + private TestMethod _successfulMethod; |
| 20 | + private TestMethod _failedMethod; |
| 21 | + private TestMethod _inconclusiveMethod; |
| 22 | + private TestMethod _notRunMethod; |
| 23 | + |
| 24 | + private bool _wasEventRaised; |
| 25 | + private int _eventCount; |
| 26 | + |
| 27 | + [TestInitialize] |
| 28 | + public void Intialize() |
| 29 | + { |
| 30 | + _wasEventRaised = false; |
| 31 | + _eventCount = 0; |
| 32 | + |
| 33 | + _engine = new TestEngine(); |
| 34 | + _hostAppMock = new Mock<IHostApplication>(); |
| 35 | + |
| 36 | + _successfulMethod = new TestMethod(new QualifiedMemberName(_moduleName, "TestMethod1"), _hostAppMock.Object); |
| 37 | + _failedMethod = new TestMethod(new QualifiedMemberName(_moduleName, "TestMethod2"), _hostAppMock.Object); |
| 38 | + _inconclusiveMethod = new TestMethod(new QualifiedMemberName(_moduleName, "TestMethod3"), _hostAppMock.Object); |
| 39 | + _notRunMethod = new TestMethod(new QualifiedMemberName(_moduleName, "TestMethod4"), _hostAppMock.Object); |
| 40 | + |
| 41 | + var tests = new Dictionary<TestMethod, TestResult> |
| 42 | + { |
| 43 | + {_successfulMethod, new TestResult(TestOutcome.Succeeded)}, |
| 44 | + {_failedMethod, new TestResult(TestOutcome.Failed)}, |
| 45 | + {_inconclusiveMethod, new TestResult(TestOutcome.Inconclusive)}, |
| 46 | + {_notRunMethod, null} |
| 47 | + }; |
| 48 | + |
| 49 | + _engine.AllTests = tests; |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + public void TestEngine_FailedTests() |
| 54 | + { |
| 55 | + var actual = _engine.FailedTests().First(); |
| 56 | + |
| 57 | + Assert.AreEqual(_failedMethod, actual); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void TestEngine_SuccessfulTests() |
| 62 | + { |
| 63 | + var actual = _engine.PassedTests().First(); |
| 64 | + |
| 65 | + Assert.AreEqual(_successfulMethod, actual); |
| 66 | + } |
| 67 | + |
| 68 | + [TestMethod] |
| 69 | + public void TestEngine_NotRunTests() |
| 70 | + { |
| 71 | + var actual = _engine.NotRunTests().First(); |
| 72 | + |
| 73 | + Assert.AreEqual(_notRunMethod, actual); |
| 74 | + } |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + public void TestEngine_LastRunTests_ReturnsAllRunTests() |
| 78 | + { |
| 79 | + var actual = _engine.LastRunTests().ToList(); |
| 80 | + var expected = new List<TestMethod>() |
| 81 | + { |
| 82 | + _failedMethod, _inconclusiveMethod, _successfulMethod |
| 83 | + }; |
| 84 | + |
| 85 | + CollectionAssert.AreEquivalent(expected, actual); |
| 86 | + } |
| 87 | + |
| 88 | + [TestMethod] |
| 89 | + public void TestEngine_LastRunTests_Successful() |
| 90 | + { |
| 91 | + var actual = _engine.LastRunTests(TestOutcome.Succeeded).First(); |
| 92 | + |
| 93 | + Assert.AreEqual(_successfulMethod, actual); |
| 94 | + } |
| 95 | + |
| 96 | + [TestMethod] |
| 97 | + public void TestEngine_LastRunTests_Failed() |
| 98 | + { |
| 99 | + var actual = _engine.LastRunTests(TestOutcome.Failed).First(); |
| 100 | + |
| 101 | + Assert.AreEqual(_failedMethod, actual); |
| 102 | + } |
| 103 | + |
| 104 | + [TestMethod] |
| 105 | + public void TestEngine_LastRunTests_Inconclusive() |
| 106 | + { |
| 107 | + var actual = _engine.LastRunTests(TestOutcome.Inconclusive).First(); |
| 108 | + |
| 109 | + Assert.AreEqual(_inconclusiveMethod, actual); |
| 110 | + } |
| 111 | + |
| 112 | + [TestMethod] |
| 113 | + public void TestEngine_Run_ModuleIntialize_IsRunOnce() |
| 114 | + { |
| 115 | + //arrange |
| 116 | + _engine.ModuleInitialize += CatchEvent; |
| 117 | + |
| 118 | + var tests = _engine.AllTests.Keys; |
| 119 | + |
| 120 | + //act |
| 121 | + _engine.Run(tests); |
| 122 | + |
| 123 | + Assert.IsTrue(_wasEventRaised, "Module Intialize was not run."); |
| 124 | + Assert.AreEqual(1, _eventCount, "Module Intialzie expected to be run once."); |
| 125 | + } |
| 126 | + |
| 127 | + [TestMethod] |
| 128 | + public void TestEngine_Run_ModuleCleanup_IsRunOnce() |
| 129 | + { |
| 130 | + //arrange |
| 131 | + _engine.ModuleCleanup += CatchEvent; |
| 132 | + |
| 133 | + //act |
| 134 | + _engine.Run(_engine.AllTests.Keys); |
| 135 | + |
| 136 | + //assert |
| 137 | + Assert.IsTrue(_wasEventRaised, "Module Cleanup was not run."); |
| 138 | + Assert.AreEqual(1, _eventCount, "Module Cleanup expected to be run once."); |
| 139 | + } |
| 140 | + |
| 141 | + [TestMethod] |
| 142 | + public void TestEngine_Run_MethodIntialize_IsRunForEachTestMethod() |
| 143 | + { |
| 144 | + //arrange |
| 145 | + var expectedCount = _engine.AllTests.Count; |
| 146 | + _engine.MethodInitialize += CatchEvent; |
| 147 | + |
| 148 | + //act |
| 149 | + _engine.Run(_engine.AllTests.Keys); |
| 150 | + |
| 151 | + //assert |
| 152 | + Assert.IsTrue(_wasEventRaised, "Method Intialize was not run."); |
| 153 | + Assert.AreEqual(expectedCount, _eventCount, "Method Intialized was expected to be run {0} times", expectedCount); |
| 154 | + } |
| 155 | + |
| 156 | + [TestMethod] |
| 157 | + public void TestEngine_Run_MethodCleanup_IsRunForEachTestMethod() |
| 158 | + { |
| 159 | + //arrange |
| 160 | + var expectedCount = _engine.AllTests.Count; |
| 161 | + _engine.MethodCleanup += CatchEvent; |
| 162 | + |
| 163 | + //act |
| 164 | + _engine.Run(_engine.AllTests.Keys); |
| 165 | + |
| 166 | + //assert |
| 167 | + Assert.IsTrue(_wasEventRaised, "Method Intialize was not run."); |
| 168 | + Assert.AreEqual(expectedCount, _eventCount, "Method Intialized was expected to be run {0} times", expectedCount); |
| 169 | + } |
| 170 | + |
| 171 | + [TestMethod] |
| 172 | + public void TestEngine_Run_TestCompleteIsRaisedForEachTestMethod() |
| 173 | + { |
| 174 | + //arrange |
| 175 | + var expectedCount = _engine.AllTests.Count; |
| 176 | + _engine.TestComplete += EngineOnTestComplete; |
| 177 | + |
| 178 | + //act |
| 179 | + _engine.Run(_engine.AllTests.Keys); |
| 180 | + |
| 181 | + //assert |
| 182 | + Assert.IsTrue(_wasEventRaised, "TestComplete event was not raised."); |
| 183 | + Assert.AreEqual(expectedCount, _eventCount, "TestComplete event was expected to be raised {0} times.", expectedCount); |
| 184 | + } |
| 185 | + |
| 186 | + [TestMethod] |
| 187 | + public void TestEngine_Run_WhenTestListIsEmpty_Bail() |
| 188 | + { |
| 189 | + //arrange |
| 190 | + _engine.MethodInitialize += CatchEvent; |
| 191 | + |
| 192 | + //act |
| 193 | + _engine.Run(new List<TestMethod>()); |
| 194 | + |
| 195 | + //assert |
| 196 | + Assert.IsFalse(_wasEventRaised, "No methods should run when passed an empty list of tests."); |
| 197 | + } |
| 198 | + |
| 199 | + private void EngineOnTestComplete(object sender, TestCompletedEventArgs testCompletedEventArgs) |
| 200 | + { |
| 201 | + CatchEvent(); |
| 202 | + } |
| 203 | + |
| 204 | + private void CatchEvent(object sender, TestModuleEventArgs e) |
| 205 | + { |
| 206 | + CatchEvent(); |
| 207 | + } |
| 208 | + |
| 209 | + private void CatchEvent() |
| 210 | + { |
| 211 | + _wasEventRaised = true; |
| 212 | + _eventCount++; |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments