/
AddTestMethodCommand.cs
97 lines (86 loc) · 3.17 KB
/
AddTestMethodCommand.cs
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
using System.Linq;
using System.Runtime.InteropServices;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.UI.Command;
using Rubberduck.UnitTesting;
using Rubberduck.UnitTesting.CodeGeneration;
using Rubberduck.VBEditor.Extensions;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
namespace Rubberduck.UI.UnitTesting.Commands
{
/// <summary>
/// A command that adds a new test method stub to the active code pane.
/// </summary>
[ComVisible(false)]
public class AddTestMethodCommand : CommandBase
{
private readonly IVBE _vbe;
private readonly RubberduckParserState _state;
private readonly ITestCodeGenerator _codeGenerator;
public AddTestMethodCommand(IVBE vbe, RubberduckParserState state, ITestCodeGenerator codeGenerator)
{
_vbe = vbe;
_state = state;
_codeGenerator = codeGenerator;
AddToCanExecuteEvaluation(SpecialEvaluateCanExecute);
}
private bool SpecialEvaluateCanExecute(object parameter)
{
if (_state.Status != ParserState.Ready)
{
return false;
}
using (var activePane = _vbe.ActiveCodePane)
{
if (activePane?.IsWrappingNullReference ?? true)
{
return false;
}
}
var testModules = _state.AllUserDeclarations.Where(d =>
d.DeclarationType == DeclarationType.ProceduralModule &&
d.Annotations.Any(a => a is TestModuleAnnotation));
try
{
// the code modules consistently match correctly, but the components don't
using( var activeCodePane = _vbe.ActiveCodePane)
{
using( var activePaneCodeModule = activeCodePane.CodeModule)
{
return testModules.Any(a => _state.ProjectsProvider.Component(a.QualifiedModuleName).HasEqualCodeModule(activePaneCodeModule));
}
}
}
catch (COMException)
{
return false;
}
}
protected override void OnExecute(object parameter)
{
using (var pane = _vbe.ActiveCodePane)
{
if (pane?.IsWrappingNullReference ?? true)
{
return;
}
using (var module = pane.CodeModule)
{
var declaration = _state.GetTestModules()
.FirstOrDefault(f => _state.ProjectsProvider.Component(f.QualifiedModuleName).HasEqualCodeModule(module));
if (declaration == null)
{
return;
}
using (var component = module.Parent)
{
module.InsertLines(module.CountOfLines, _codeGenerator.GetNewTestMethodCode(component));
}
}
}
_state.OnParseRequested(this);
}
}
}