/
AddTestMethodExpectedErrorCommand.cs
95 lines (87 loc) · 3.36 KB
/
AddTestMethodExpectedErrorCommand.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
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 AddTestMethodExpectedErrorCommand : CommandBase
{
private readonly IVBE _vbe;
private readonly RubberduckParserState _state;
private readonly ITestCodeGenerator _codeGenerator;
public AddTestMethodExpectedErrorCommand(IVBE vbe, RubberduckParserState state, ITestCodeGenerator codeGenerator)
{
_vbe = vbe;
_state = state;
_codeGenerator = codeGenerator;
AddToCanExecuteEvaluation(SpecialEvaluateCanExecute);
}
private bool SpecialEvaluateCanExecute(object parameter)
{
using (var pane = _vbe.ActiveCodePane)
{
if (_state.Status != ParserState.Ready || pane is null || pane.IsWrappingNullReference)
{
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 component = _vbe.SelectedVBComponent)
{
using(var selectedModule = component.CodeModule)
{
return testModules.Any(a => _state.ProjectsProvider.Component(a.QualifiedModuleName).HasEqualCodeModule(selectedModule));
}
}
}
catch (COMException)
{
return false;
}
}
protected override void OnExecute(object parameter)
{
using (var pane = _vbe.ActiveCodePane)
{
if (pane?.IsWrappingNullReference ?? true)
{
return;
}
using (var activeModule = pane.CodeModule)
{
var declaration = _state.GetTestModules().FirstOrDefault(f =>
{
var component = _state.ProjectsProvider.Component(f.QualifiedName.QualifiedModuleName);
using (var thisModule = component.CodeModule)
{
return thisModule.Equals(activeModule);
}
});
if (declaration != null)
{
using (var component = activeModule.Parent)
{
activeModule.InsertLines(activeModule.CountOfLines, _codeGenerator.GetNewTestMethodCodeErrorExpected(component));
}
}
}
}
_state.OnParseRequested(this);
}
}
}