Skip to content

Commit 0a26480

Browse files
authored
Merge pull request #1833 from Hosch250/Issue1806
Unit test tweaks
2 parents a5f4eab + d1683bf commit 0a26480

File tree

7 files changed

+48
-28
lines changed

7 files changed

+48
-28
lines changed

RetailCoder.VBE/UI/Command/RunAllTestsCommand.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,25 @@ public RunAllTestsCommand(RubberduckParserState state, ITestEngine engine, TestE
2424

2525
public override void Execute(object parameter)
2626
{
27-
_state.StateChanged += StateChanged;
27+
if (!_state.IsDirty())
28+
{
29+
RunTests();
30+
}
31+
else
32+
{
33+
_model.TestsRefreshed += TestsRefreshed;
34+
_model.Refresh();
35+
}
36+
}
2837

29-
_model.Refresh();
38+
private void TestsRefreshed(object sender, EventArgs e)
39+
{
40+
RunTests();
3041
}
3142

32-
private void StateChanged(object sender, ParserStateEventArgs e)
43+
private void RunTests()
3344
{
34-
if (e.State != ParserState.Ready) { return; }
45+
_model.TestsRefreshed -= TestsRefreshed;
3546

3647
var stopwatch = new Stopwatch();
3748

@@ -41,9 +52,8 @@ private void StateChanged(object sender, ParserStateEventArgs e)
4152
stopwatch.Start();
4253
_engine.Run(_model.Tests);
4354
stopwatch.Stop();
44-
55+
4556
_model.IsBusy = false;
46-
_state.StateChanged -= StateChanged;
4757

4858
OnRunCompleted(new TestRunEventArgs(stopwatch.ElapsedMilliseconds));
4959
}

RetailCoder.VBE/UI/UnitTesting/TestExplorerControl.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@
593593
</DataTemplate>
594594
</DataGridTemplateColumn.CellTemplate>
595595
</DataGridTemplateColumn>
596-
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_QualifiedModuleName}" Binding="{Binding QualifiedMemberName.QualifiedModuleName}" />
597-
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_MethodName}" Binding="{Binding QualifiedMemberName.MemberName}" />
596+
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_QualifiedModuleName}" Binding="{Binding Declaration.QualifiedName.QualifiedModuleName}" />
597+
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_MethodName}" Binding="{Binding Declaration.IdentifierName}" />
598598
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_Message}" Binding="{Binding Result.Output}" Width="*" />
599599
<DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=TestExplorer_Duration}" Binding="{Binding Result.Duration, StringFormat={}{0}ms}" />
600600
</DataGrid.Columns>

RetailCoder.VBE/UI/UnitTesting/TestExplorerModel.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Collections.ObjectModel;
34
using System.Linq;
@@ -67,6 +68,8 @@ private void State_StateChanged(object sender, ParserStateEventArgs e)
6768
}
6869
}
6970
});
71+
72+
OnTestsRefreshed();
7073
}
7174

7275
private readonly ObservableCollection<TestMethod> _tests = new ObservableCollection<TestMethod>();
@@ -155,5 +158,15 @@ private set
155158
OnPropertyChanged();
156159
}
157160
}
161+
162+
public event EventHandler<EventArgs> TestsRefreshed;
163+
private void OnTestsRefreshed()
164+
{
165+
var handler = TestsRefreshed;
166+
if (handler != null)
167+
{
168+
handler.Invoke(this, EventArgs.Empty);
169+
}
170+
}
158171
}
159172
}

RetailCoder.VBE/UnitTesting/NewTestMethodCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void NewExpectedErrorTestMethod()
116116

117117
private string GetNextTestMethodName(VBComponent component)
118118
{
119-
var names = component.GetTests(_vbe, _state).Select(test => test.QualifiedMemberName.MemberName);
119+
var names = component.GetTests(_vbe, _state).Select(test => test.Declaration.IdentifierName);
120120
var index = names.Count(n => n.StartsWith(TestMethodBaseName)) + 1;
121121

122122
return string.Concat(TestMethodBaseName, index);

RetailCoder.VBE/UnitTesting/TestEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ public void Run(IEnumerable<TestMethod> tests)
6060
return;
6161
}
6262

63-
var modules = testMethods.GroupBy(test => test.QualifiedMemberName.QualifiedModuleName);
63+
var modules = testMethods.GroupBy(test => test.Declaration.QualifiedName.QualifiedModuleName);
6464
foreach (var module in modules)
6565
{
6666
var testInitialize = module.Key.FindTestInitializeMethods(_state).ToList();
6767
var testCleanup = module.Key.FindTestCleanupMethods(_state).ToList();
6868

6969
var moduleTestMethods = testMethods
70-
.Where(test => test.QualifiedMemberName.QualifiedModuleName.ProjectId == module.Key.ProjectId
71-
&& test.QualifiedMemberName.QualifiedModuleName.ComponentName == module.Key.ComponentName);
70+
.Where(test => test.Declaration.QualifiedName.QualifiedModuleName.ProjectId == module.Key.ProjectId
71+
&& test.Declaration.QualifiedName.QualifiedModuleName.ComponentName == module.Key.ComponentName);
7272

7373
Run(module.Key.FindModuleInitializeMethods(_state));
7474
foreach (var test in moduleTestMethods)

RetailCoder.VBE/UnitTesting/TestMethod.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class TestMethod : ViewModelBase, IEquatable<TestMethod>, INavigateSource
2121
public TestMethod(Declaration declaration, VBE vbe)
2222
{
2323
_declaration = declaration;
24-
_qualifiedMemberName = declaration.QualifiedName;
2524
_hostApp = vbe.HostApplication();
2625
}
2726

@@ -31,11 +30,9 @@ public TestMethod(Declaration declaration, VBE vbe)
3130
public void SetDeclaration(Declaration declaration)
3231
{
3332
_declaration = declaration;
33+
OnPropertyChanged("Declaration");
3434
}
3535

36-
private readonly QualifiedMemberName _qualifiedMemberName;
37-
public QualifiedMemberName QualifiedMemberName { get { return _qualifiedMemberName; } }
38-
3936
public void Run()
4037
{
4138
_assertResults.Clear(); //clear previous results to account for changes being made
@@ -46,7 +43,7 @@ public void Run()
4643
try
4744
{
4845
AssertHandler.OnAssertCompleted += HandleAssertCompleted;
49-
_hostApp.Run(QualifiedMemberName);
46+
_hostApp.Run(Declaration.QualifiedName);
5047
AssertHandler.OnAssertCompleted -= HandleAssertCompleted;
5148

5249
result = EvaluateResults();
@@ -93,13 +90,13 @@ public NavigateCodeEventArgs GetNavigationArgs()
9390
{
9491
try
9592
{
96-
var moduleName = QualifiedMemberName.QualifiedModuleName;
97-
var methodName = QualifiedMemberName.MemberName;
93+
var moduleName = Declaration.QualifiedName.QualifiedModuleName;
94+
var methodName = Declaration.IdentifierName;
9895
var module = moduleName.Component.CodeModule;
9996

100-
var startLine = module.get_ProcStartLine(methodName, vbext_ProcKind.vbext_pk_Proc);
101-
var endLine = startLine + module.get_ProcCountLines(methodName, vbext_ProcKind.vbext_pk_Proc);
102-
var endLineColumns = module.get_Lines(endLine, 1).Length;
97+
var startLine = module.ProcStartLine[methodName, vbext_ProcKind.vbext_pk_Proc];
98+
var endLine = startLine + module.ProcCountLines[methodName, vbext_ProcKind.vbext_pk_Proc];
99+
var endLineColumns = module.Lines[endLine, 1].Length;
103100

104101
var selection = new Selection(startLine, 1, endLine, endLineColumns == 0 ? 1 : endLineColumns);
105102
return new NavigateCodeEventArgs(new QualifiedSelection(moduleName, selection));
@@ -112,29 +109,29 @@ public NavigateCodeEventArgs GetNavigationArgs()
112109

113110
public object[] ToArray()
114111
{
115-
return new object[] { QualifiedMemberName.QualifiedModuleName.ProjectTitle, QualifiedMemberName.QualifiedModuleName.ComponentTitle, QualifiedMemberName.MemberName,
112+
return new object[] { Declaration.QualifiedName.QualifiedModuleName.ProjectTitle, Declaration.QualifiedName.QualifiedModuleName.ComponentTitle, Declaration.IdentifierName,
116113
_result.Outcome.ToString(), _result.Output, _result.StartTime.ToString(CultureInfo.InvariantCulture), _result.EndTime.ToString(CultureInfo.InvariantCulture), _result.Duration };
117114
}
118115

119116
public bool Equals(TestMethod other)
120117
{
121-
return QualifiedMemberName.Equals(other.QualifiedMemberName);
118+
return Declaration.QualifiedName.Equals(other.Declaration.QualifiedName);
122119
}
123120

124121
public override bool Equals(object obj)
125122
{
126123
return obj is TestMethod
127-
&& ((TestMethod)obj).QualifiedMemberName.Equals(QualifiedMemberName);
124+
&& ((TestMethod)obj).Declaration.QualifiedName.Equals(Declaration.QualifiedName);
128125
}
129126

130127
public override int GetHashCode()
131128
{
132-
return QualifiedMemberName.GetHashCode();
129+
return Declaration.QualifiedName.GetHashCode();
133130
}
134131

135132
public override string ToString()
136133
{
137-
return string.Format("{0}: {1} ({2}ms) {3}", QualifiedMemberName, Result.Outcome, Result.Duration, Result.Output);
134+
return string.Format("{0}: {1} ({2}ms) {3}", Declaration.QualifiedName, Result.Outcome, Result.Duration, Result.Output);
138135
}
139136
}
140137
}

RetailCoder.VBE/UnitTesting/UnitTestHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static IEnumerable<TestMethod> GetTests(this VBComponent component, VBE v
2222
// apparently, sometimes it thinks the components are different but knows the modules are the same
2323
// if the modules are the same, then the component is the same as far as we are concerned
2424
return GetAllTests(vbe, state)
25-
.Where(test => test.QualifiedMemberName.QualifiedModuleName.Component.CodeModule == component.CodeModule);
25+
.Where(test => test.Declaration.QualifiedName.QualifiedModuleName.Component.CodeModule == component.CodeModule);
2626
}
2727

2828
public static bool IsTestMethod(RubberduckParserState state, Declaration item)

0 commit comments

Comments
 (0)