Skip to content

Commit f726824

Browse files
committed
Added initial TestModuleWithStubsCommand to Code Explorer
1 parent 580572d commit f726824

File tree

7 files changed

+116
-0
lines changed

7 files changed

+116
-0
lines changed

RetailCoder.VBE/Navigation/CodeExplorer/CodeExplorerViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public CodeExplorerViewModel(FolderHelper folderHelper, RubberduckParserState st
6464
OpenDesignerCommand = commands.OfType<OpenDesignerCommand>().SingleOrDefault();
6565

6666
AddTestModuleCommand = commands.OfType<UI.CodeExplorer.Commands.AddTestModuleCommand>().SingleOrDefault();
67+
AddTestModuleWithStubsCommand = commands.OfType<AddTestModuleWithStubsCommand>().SingleOrDefault();
68+
6769
AddStdModuleCommand = commands.OfType<AddStdModuleCommand>().SingleOrDefault();
6870
AddClassModuleCommand = commands.OfType<AddClassModuleCommand>().SingleOrDefault();
6971
AddUserFormCommand = commands.OfType<AddUserFormCommand>().SingleOrDefault();
@@ -491,6 +493,7 @@ private void SwitchNodeState(CodeExplorerItemViewModel node, bool expandedState)
491493
public CommandBase OpenCommand { get; }
492494

493495
public CommandBase AddTestModuleCommand { get; }
496+
public CommandBase AddTestModuleWithStubsCommand { get; }
494497
public CommandBase AddStdModuleCommand { get; }
495498
public CommandBase AddClassModuleCommand { get; }
496499
public CommandBase AddUserFormCommand { get; }

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@
395395
<Compile Include="UI\About\AboutDialog.Designer.cs">
396396
<DependentUpon>AboutDialog.cs</DependentUpon>
397397
</Compile>
398+
<Compile Include="UI\CodeExplorer\Commands\AddTestModuleWithStubsCommand.cs" />
398399
<Compile Include="UI\CodeExplorer\Commands\CodeExplorerCommandAttribute.cs" />
399400
<Compile Include="UI\CodeExplorer\Commands\CommitCommand.cs" />
400401
<Compile Include="UI\CodeExplorer\Commands\AddUserFormCommand.cs" />

RetailCoder.VBE/UI/CodeExplorer/CodeExplorerControl.xaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<BitmapImage x:Key="UndoImage" UriSource="../../Resources/arrow-circle-left.png" />
2626
<BitmapImage x:Key="PrintImage" UriSource="../../Resources/printer.png" />
2727
<BitmapImage x:Key="AddTestModuleImage" UriSource="../../Resources/Custom/PNG/ListOfTests.png" />
28+
<BitmapImage x:Key="AddTestModuleWithStubsImage" UriSource="../../Resources/Custom/PNG/ListOfTests.png" />
2829
<BitmapImage x:Key="AddStdModuleImage" UriSource="../../Resources/Custom/PNG/AddModule.png" />
2930
<BitmapImage x:Key="AddClassModuleImage" UriSource="../../Resources/Custom/PNG/AddClass.png" />
3031
<BitmapImage x:Key="AddUserFormImage" UriSource="../../Resources/Custom/PNG/AddForm.png" />
@@ -161,6 +162,13 @@
161162
<Image Height="16" Source="{StaticResource AddTestModuleImage}" />
162163
</MenuItem.Icon>
163164
</MenuItem>
165+
<MenuItem Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=CodeExplorer_AddTestModuleWithStubsText}"
166+
Command="{Binding AddTestModuleWithStubsCommand}"
167+
CommandParameter="{Binding SelectedItem}">
168+
<MenuItem.Icon>
169+
<Image Height="16" Source="{StaticResource AddTestModuleWithStubsImage}" />
170+
</MenuItem.Icon>
171+
</MenuItem>
164172
<Separator />
165173
<MenuItem Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=CodeExplorer_AddStdModuleText}"
166174
Command="{Binding AddStdModuleCommand}"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Runtime.InteropServices;
2+
using NLog;
3+
using Rubberduck.Navigation.CodeExplorer;
4+
using Rubberduck.Parsing.Symbols;
5+
using Rubberduck.UI.Command;
6+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
7+
8+
namespace Rubberduck.UI.CodeExplorer.Commands
9+
{
10+
[CodeExplorerCommand]
11+
public class AddTestModuleWithStubsCommand : CommandBase
12+
{
13+
private readonly IVBE _vbe;
14+
private readonly Command.AddTestModuleCommand _newUnitTestModuleCommand;
15+
16+
public AddTestModuleWithStubsCommand(IVBE vbe, Command.AddTestModuleCommand newUnitTestModuleCommand) : base(LogManager.GetCurrentClassLogger())
17+
{
18+
_vbe = vbe;
19+
_newUnitTestModuleCommand = newUnitTestModuleCommand;
20+
}
21+
22+
protected override bool EvaluateCanExecute(object parameter)
23+
{
24+
try
25+
{
26+
return GetDeclaration(parameter) != null || _vbe.VBProjects.Count == 1;
27+
}
28+
catch (COMException)
29+
{
30+
return false;
31+
}
32+
}
33+
34+
protected override void OnExecute(object parameter)
35+
{
36+
if (parameter != null)
37+
{
38+
_newUnitTestModuleCommand.Execute(GetDeclaration(parameter));
39+
}
40+
else
41+
{
42+
_newUnitTestModuleCommand.Execute(_vbe.ActiveVBProject);
43+
}
44+
}
45+
46+
private Declaration GetDeclaration(object parameter)
47+
{
48+
var node = parameter as CodeExplorerItemViewModel;
49+
while (node != null && !(node is ICodeExplorerDeclarationViewModel))
50+
{
51+
node = node.Parent;
52+
}
53+
54+
return ((ICodeExplorerDeclarationViewModel)node)?.Declaration;
55+
}
56+
}
57+
}

RetailCoder.VBE/UI/RubberduckUI.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RetailCoder.VBE/UI/RubberduckUI.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,4 +2100,8 @@ the projects in the VBE.</value>
21002100
<data name="EmptyUIRefreshMessage_Title" xml:space="preserve">
21012101
<value>Rubberduck doesn't see anything yet.</value>
21022102
</data>
2103+
<data name="CodeExplorer_AddTestModuleWithStubsText" xml:space="preserve">
2104+
<value>Test module with stubs</value>
2105+
<comment>TC</comment>
2106+
</data>
21032107
</root>

RubberduckTests/CodeExplorer/CodeExplorerTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,40 @@ public void AddTestModule()
158158
components.Verify(c => c.Add(ComponentType.StandardModule), Times.Once);
159159
}
160160

161+
[TestCategory("Code Explorer")]
162+
[TestMethod]
163+
public void AddTestModuleWithStubs()
164+
{
165+
var builder = new MockVbeBuilder();
166+
var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
167+
.AddComponent("Module1", ComponentType.StandardModule, "");
168+
169+
var components = project.MockVBComponents;
170+
171+
var vbe = builder.AddProject(project.Build()).Build();
172+
173+
var configLoader = new Mock<ConfigurationLoader>(null, null, null, null, null, null, null);
174+
configLoader.Setup(c => c.LoadConfiguration()).Returns(GetDefaultUnitTestConfig());
175+
176+
var state = new RubberduckParserState(vbe.Object, new DeclarationFinderFactory());
177+
var vbeWrapper = vbe.Object;
178+
var commands = new List<CommandBase>
179+
{
180+
new AddTestModuleWithStubsCommand(vbeWrapper, new Rubberduck.UI.Command.AddTestModuleCommand(vbeWrapper, state, configLoader.Object))
181+
};
182+
183+
var vm = new CodeExplorerViewModel(new FolderHelper(state), state, commands, _generalSettingsProvider.Object, _windowSettingsProvider.Object);
184+
185+
var parser = MockParser.Create(vbe.Object, state);
186+
parser.Parse(new CancellationTokenSource());
187+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
188+
189+
vm.SelectedItem = vm.Projects.First().Items.First().Items.First();
190+
vm.AddTestModuleWithStubsCommand.Execute(vm.SelectedItem);
191+
192+
components.Verify(c => c.Add(ComponentType.StandardModule), Times.Once);
193+
}
194+
161195
[TestCategory("Code Explorer")]
162196
[TestMethod]
163197
public void ImportModule()

0 commit comments

Comments
 (0)