|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Vbe.Interop; |
| 9 | +using Rubberduck.Reflection; |
| 10 | + |
| 11 | +namespace Rubberduck.UnitTesting |
| 12 | +{ |
| 13 | + [ComVisible(true)] |
| 14 | + [ComDefaultInterface(typeof(ITestRunner))] |
| 15 | + [Guid(ClassId)] |
| 16 | + [ProgId(ProgId)] |
| 17 | + public class TestRunner : ITestRunner |
| 18 | + { |
| 19 | + private const string ClassId = "C46C141F-30C8-4A8A-B84B-EC04F1CC559B"; |
| 20 | + private const string ProgId = "Rubberduck.TestRunner"; |
| 21 | + |
| 22 | + private readonly TestEngine _engine = new TestEngine(); |
| 23 | + |
| 24 | + public TestRunner() |
| 25 | + { |
| 26 | + _engine.MethodCleanup += TestEngineMethodCleanup; |
| 27 | + _engine.MethodInitialize += TestEngineMethodInitialize; |
| 28 | + _engine.ModuleCleanup += TestEngine_ModuleCleanup; |
| 29 | + _engine.ModuleInitialize += TestEngine_ModuleInitialize; |
| 30 | + } |
| 31 | + |
| 32 | + private void TestEngineMethodCleanup(object sender, TestModuleEventArgs e) |
| 33 | + { |
| 34 | + var module = e.QualifiedModuleName.Component.CodeModule; |
| 35 | + module.Parent.RunMethodsWithAttribute<TestCleanupAttribute>(); |
| 36 | + } |
| 37 | + |
| 38 | + private void TestEngineMethodInitialize(object sender, TestModuleEventArgs e) |
| 39 | + { |
| 40 | + var module = e.QualifiedModuleName.Component.CodeModule; |
| 41 | + module.Parent.RunMethodsWithAttribute<TestInitializeAttribute>(); |
| 42 | + } |
| 43 | + |
| 44 | + private void TestEngine_ModuleCleanup(object sender, TestModuleEventArgs e) |
| 45 | + { |
| 46 | + var module = e.QualifiedModuleName.Component.CodeModule; |
| 47 | + module.Parent.RunMethodsWithAttribute<ModuleCleanupAttribute>(); |
| 48 | + } |
| 49 | + |
| 50 | + private void TestEngine_ModuleInitialize(object sender, TestModuleEventArgs e) |
| 51 | + { |
| 52 | + var module = e.QualifiedModuleName.Component.CodeModule; |
| 53 | + module.Parent.RunMethodsWithAttribute<ModuleInitializeAttribute>(); |
| 54 | + } |
| 55 | + |
| 56 | + private void LoadAllTests(VBE vbe) |
| 57 | + { |
| 58 | + _engine.AllTests = vbe.VBProjects |
| 59 | + .Cast<VBProject>().Where(project => project.Protection != vbext_ProjectProtection.vbext_pp_locked) |
| 60 | + .SelectMany(project => project.TestMethods()) |
| 61 | + .ToDictionary(test => test, test => _engine.AllTests.ContainsKey(test) ? _engine.AllTests[test] : null); |
| 62 | + } |
| 63 | + |
| 64 | + public string RunAllTests(VBE vbe, string outputFilePath = null) |
| 65 | + { |
| 66 | + LoadAllTests(vbe); |
| 67 | + _engine.Run(_engine.AllTests.Keys); |
| 68 | + |
| 69 | + var results = OutputToString(); |
| 70 | + |
| 71 | + if (outputFilePath != null) |
| 72 | + { |
| 73 | + OutputToFile(outputFilePath, results); |
| 74 | + } |
| 75 | + |
| 76 | + return results; |
| 77 | + } |
| 78 | + |
| 79 | + private string OutputToString() |
| 80 | + { |
| 81 | + var builder = new StringBuilder(); |
| 82 | + builder.AppendLine("Rubberduck Unit Tests - " + string.Format("{0:G}", DateTime.Now)); |
| 83 | + |
| 84 | + foreach (var result in _engine.AllTests) |
| 85 | + { |
| 86 | + var item = string.Format("{0}\t{1}ms\t{2} {3}", result.Key.QualifiedMemberName, result.Value.Duration, result.Value.Outcome, result.Value.Output); |
| 87 | + builder.AppendLine(item); |
| 88 | + } |
| 89 | + |
| 90 | + return builder.ToString(); |
| 91 | + } |
| 92 | + |
| 93 | + private void OutputToFile(string path, string results) |
| 94 | + { |
| 95 | + File.AppendAllText(path, results); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments