/
TestExplorerViewModel.cs
604 lines (486 loc) · 22.8 KB
/
TestExplorerViewModel.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using NLog;
using Rubberduck.Common;
using Rubberduck.Interaction.Navigation;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Rewriter;
using Rubberduck.Parsing.VBA;
using Rubberduck.Settings;
using Rubberduck.SettingsProvider;
using Rubberduck.UI.Command;
using Rubberduck.UI.Command.ComCommands;
using Rubberduck.UI.Settings;
using Rubberduck.UI.UnitTesting.ComCommands;
using Rubberduck.UI.UnitTesting.Commands;
using Rubberduck.UI.UnitTesting.ViewModels;
using Rubberduck.UnitTesting;
using Rubberduck.VBEditor.Utility;
namespace Rubberduck.UI.UnitTesting
{
public enum TestExplorerGrouping
{
None,
Outcome,
Category,
Location
}
[Flags]
public enum TestExplorerOutcomeFilter
{
None = 0,
Unknown = 1,
Fail = 1 << 1,
Inconclusive = 1 << 2,
Succeeded = 1 << 3,
All = Unknown | Fail | Inconclusive | Succeeded
}
internal sealed class TestExplorerViewModel : ViewModelBase, INavigateSelection, IDisposable
{
private readonly IClipboardWriter _clipboard;
private readonly ISettingsFormFactory _settingsFormFactory;
public TestExplorerViewModel(ISelectionService selectionService,
TestExplorerModel model,
IClipboardWriter clipboard,
// ReSharper disable once UnusedParameter.Local - left in place because it will likely be needed for app wide font settings, etc.
IConfigurationService<Configuration> configService,
ISettingsFormFactory settingsFormFactory,
IRewritingManager rewritingManager,
IAnnotationUpdater annotationUpdater)
{
_clipboard = clipboard;
_settingsFormFactory = settingsFormFactory;
NavigateCommand = new NavigateCommand(selectionService);
RunSingleTestCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteSingleTestCommand, CanExecuteSingleTest);
RunSelectedTestsCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteSelectedTestsCommand, CanExecuteSelectedCommands);
RunSelectedGroupCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteRunSelectedGroupCommand, CanExecuteGroupCommand);
CancelTestRunCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteCancelTestRunCommand, CanExecuteCancelTestRunCommand);
ResetResultsCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteResetResultsCommand, CanExecuteResetResultsCommand);
CopyResultsCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteCopyResultsCommand);
OpenTestSettingsCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), OpenSettings);
CollapseAllCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteCollapseAll);
ExpandAllCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteExpandAll);
IgnoreTestCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteIgnoreTestCommand, CanExecuteIgnoreTestCommand);
UnignoreTestCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteUnignoreTestCommand, CanExecuteUnignoreTestCommand);
IgnoreSelectedTestsCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteIgnoreSelectedTestsCommand, CanExecuteIgnoreSelectedTests);
UnignoreSelectedTestsCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteUnignoreSelectedTestsCommand, CanExecuteUnignoreSelectedTests);
IgnoreGroupCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteIgnoreGroupCommand, CanExecuteIgnoreGroupCommand);
UnignoreGroupCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteUnignoreGroupCommand, CanExecuteUnignoreGroupCommand);
RewritingManager = rewritingManager;
AnnotationUpdater = annotationUpdater;
Model = model;
if (CollectionViewSource.GetDefaultView(Model.Tests) is ListCollectionView tests)
{
tests.SortDescriptions.Add(new SortDescription("QualifiedName.QualifiedModuleName.Name", ListSortDirection.Ascending));
tests.SortDescriptions.Add(new SortDescription("QualifiedName.MemberName", ListSortDirection.Ascending));
tests.IsLiveFiltering = true;
tests.IsLiveGrouping = true;
Tests = tests;
}
OnPropertyChanged(nameof(Tests));
TestGrouping = TestExplorerGrouping.Outcome;
OutcomeFilter = TestExplorerOutcomeFilter.All;
}
public TestExplorerModel Model { get; }
public ICollectionView Tests { get; }
public INavigateSource SelectedItem => MouseOverTest;
private TestMethodViewModel _mouseOverTest;
public TestMethodViewModel MouseOverTest
{
get => _mouseOverTest;
set
{
if (ReferenceEquals(_mouseOverTest, value))
{
return;
}
_mouseOverTest = value;
OnPropertyChanged();
RefreshContextMenu();
}
}
private CollectionViewGroup _mouseOverGroup;
public CollectionViewGroup MouseOverGroup
{
get => _mouseOverGroup;
set
{
if (ReferenceEquals(_mouseOverGroup, value))
{
return;
}
_mouseOverGroup = value;
OnPropertyChanged();
RefreshContextMenu();
}
}
private void RefreshContextMenu()
{
OnPropertyChanged(nameof(CanExecuteUnignoreTestCommand));
OnPropertyChanged(nameof(CanExecuteIgnoreTestCommand));
OnPropertyChanged(nameof(CanExecuteUnignoreGroupCommand));
OnPropertyChanged(nameof(CanExecuteIgnoreGroupCommand));
}
private static readonly Dictionary<TestExplorerGrouping, PropertyGroupDescription> GroupDescriptions = new Dictionary<TestExplorerGrouping, PropertyGroupDescription>
{
{ TestExplorerGrouping.Outcome, new PropertyGroupDescription("Result.Outcome", new TestResultToOutcomeTextConverter()) },
{ TestExplorerGrouping.Location, new PropertyGroupDescription("QualifiedName.QualifiedModuleName.Name") },
{ TestExplorerGrouping.Category, new PropertyGroupDescription("Method.Category.Name") }
};
private TestExplorerGrouping _grouping = TestExplorerGrouping.None;
public TestExplorerGrouping TestGrouping
{
get => _grouping;
set
{
if (value == _grouping)
{
return;
}
_grouping = value;
Tests.GroupDescriptions.Clear();
Tests.GroupDescriptions.Add(GroupDescriptions[_grouping]);
Tests.Refresh();
OnPropertyChanged();
}
}
private TestExplorerOutcomeFilter _outcomeFilter = TestExplorerOutcomeFilter.All;
public TestExplorerOutcomeFilter OutcomeFilter
{
get => _outcomeFilter;
set
{
if (value == _outcomeFilter)
{
return;
}
_outcomeFilter = value;
OnPropertyChanged();
Tests.Filter = FilterResults;
}
}
private string _testNameFilter = string.Empty;
public string TestNameFilter
{
get => _testNameFilter;
set
{
if (_testNameFilter != value)
{
_testNameFilter = value;
OnPropertyChanged();
Tests.Filter = FilterResults;
OnPropertyChanged(nameof(Tests));
}
}
}
private bool _expanded;
public bool ExpandedState
{
get => _expanded;
set
{
_expanded = value;
OnPropertyChanged();
}
}
/// <summary>
/// Filtering for displaying the correct tests.
/// Uses both <see cref="OutcomeFilter"/> and <see cref="TestNameFilter"/>
/// </summary>
private bool FilterResults(object unitTest)
{
var testMethodViewModel = unitTest as TestMethodViewModel;
var passesNameFilter = testMethodViewModel.QualifiedName.MemberName.ToUpper().Contains(TestNameFilter?.ToUpper() ?? string.Empty);
Enum.TryParse(testMethodViewModel.Result.Outcome.ToString(), out TestExplorerOutcomeFilter convertedOutcome);
var passesOutcomeFilter = (OutcomeFilter & convertedOutcome) == convertedOutcome;
return passesNameFilter && passesOutcomeFilter;
}
public IRewritingManager RewritingManager { get; }
public IAnnotationUpdater AnnotationUpdater { get; }
private TestMethod _mousedOverTestMethod => ((TestMethodViewModel)SelectedItem).Method;
public bool CanExecuteUnignoreTestCommand(object obj) => SelectedItem != null && _mousedOverTestMethod.IsIgnored;
public bool CanExecuteIgnoreTestCommand(object obj) => SelectedItem != null && !_mousedOverTestMethod.IsIgnored;
public bool CanExecuteIgnoreSelectedTests(object obj)
{
if (!Model.IsBusy && obj is IList viewModels && viewModels.Count > 0)
{
return viewModels.Cast<TestMethodViewModel>().Count(test => test.Method.IsIgnored) != viewModels.Count;
}
return false;
}
public bool CanExecuteUnignoreSelectedTests(object obj)
{
if (!Model.IsBusy && obj is IList viewModels && viewModels.Count > 0)
{
return viewModels.Cast<TestMethodViewModel>().Any(test => test.Method.IsIgnored);
}
return false;
}
public bool CanExecuteIgnoreGroupCommand(object obj)
{
var groupItems = MouseOverGroup?.Items
?? GroupContainingSelectedTest(MouseOverTest).Items;
return groupItems.Cast<TestMethodViewModel>().Count(test => test.Method.IsIgnored) != groupItems.Count;
}
public bool CanExecuteUnignoreGroupCommand(object obj)
{
var groupItems = MouseOverGroup?.Items
?? GroupContainingSelectedTest(MouseOverTest)?.Items;
return groupItems != null
&& groupItems.Cast<TestMethodViewModel>().Any(test => test.Method.IsIgnored);
}
#region Commands
public ReparseCommand RefreshCommand { get; set; }
public RunAllTestsCommand RunAllTestsCommand { get; set; }
public RepeatLastRunCommand RepeatLastRunCommand { get; set; }
public RunNotExecutedTestsCommand RunNotExecutedTestsCommand { get; set; }
// no way to run skipped tests. Those are skipped until reparsing anyways, so it's k
public RunInconclusiveTestsCommand RunInconclusiveTestsCommand { get; set; }
public RunFailedTestsCommand RunFailedTestsCommand { get; set; }
public RunSucceededTestsCommand RunPassedTestsCommand { get; set; }
public CommandBase RunSingleTestCommand { get; }
public CommandBase RunSelectedTestsCommand { get; }
public CommandBase RunSelectedGroupCommand { get; }
public CommandBase CancelTestRunCommand { get; }
public CommandBase ResetResultsCommand { get; }
public AddTestModuleCommand AddTestModuleCommand { get; set; }
public AddTestMethodCommand AddTestMethodCommand { get; set; }
public AddTestMethodExpectedErrorCommand AddErrorTestMethodCommand { get; set; }
public CommandBase CopyResultsCommand { get; }
public CommandBase OpenTestSettingsCommand { get; }
public INavigateCommand NavigateCommand { get; }
public CommandBase CollapseAllCommand { get; }
public CommandBase ExpandAllCommand { get; }
public CommandBase IgnoreTestCommand { get; }
public CommandBase UnignoreTestCommand { get; }
public CommandBase IgnoreGroupCommand { get; }
public CommandBase UnignoreGroupCommand { get; }
public CommandBase IgnoreSelectedTestsCommand { get; }
public CommandBase UnignoreSelectedTestsCommand { get; }
#endregion
#region Delegates
private bool CanExecuteSingleTest(object obj)
{
return !Model.IsBusy && MouseOverTest != null;
}
private bool CanExecuteSelectedCommands(object obj)
{
return !Model.IsBusy && obj is IList viewModels && viewModels.Count > 0;
}
private bool CanExecuteGroupCommand(object obj)
{
return !Model.IsBusy && (MouseOverTest != null || MouseOverGroup != null);
}
private bool CanExecuteResetResultsCommand(object obj)
{
return !Model.IsBusy && Tests.OfType<TestMethodViewModel>().Any(test => test.Result.Outcome != TestOutcome.Unknown);
}
private bool CanExecuteCancelTestRunCommand(object obj)
{
return Model.IsBusy;
}
private void ExecuteCollapseAll(object parameter)
{
ExpandedState = false;
}
private void ExecuteExpandAll(object parameter)
{
ExpandedState = true;
}
private void ExecuteSingleTestCommand(object obj)
{
if (MouseOverTest == null)
{
return;
}
Model.ExecuteTests(new List<TestMethodViewModel> { MouseOverTest });
}
private void ExecuteSelectedTestsCommand(object obj)
{
if (Model.IsBusy || !(obj is IList viewModels && viewModels.Count > 0))
{
return;
}
var models = viewModels.OfType<TestMethodViewModel>().ToList();
if (!models.Any())
{
return;
}
Model.ExecuteTests(models);
}
private void ExecuteRunSelectedGroupCommand(object obj)
{
var tests = GroupContainingSelectedTest(MouseOverTest);
if (tests is null)
{
return;
}
Model.ExecuteTests(tests.Items.OfType<TestMethodViewModel>().ToList());
}
private CollectionViewGroup GroupContainingSelectedTest(TestMethodViewModel selectedTest)
{
return selectedTest is null
? MouseOverGroup
: Tests.Groups.OfType<CollectionViewGroup>().FirstOrDefault(group => group.Items.Contains(selectedTest));
}
private void ExecuteCancelTestRunCommand(object parameter)
{
Model.CancelTestRun();
}
private void ExecuteResetResultsCommand(object parameter)
{
foreach (var test in Tests.OfType<TestMethodViewModel>())
{
test.Result = new TestResult(TestOutcome.Unknown);
}
Tests.Refresh();
}
private void ExecuteIgnoreTestCommand(object parameter)
{
var rewriteSession = RewritingManager.CheckOutCodePaneSession();
var testMethod = parameter is null
? _mousedOverTestMethod
: (TestMethod)parameter;
AnnotationUpdater.AddAnnotation(rewriteSession, testMethod.Declaration, new IgnoreTestAnnotation());
rewriteSession.TryRewrite();
}
private void ExecuteUnignoreTestCommand(object parameter)
{
var testMethod = parameter is null
? _mousedOverTestMethod
: (TestMethod)parameter;
var ignoreTestAnnotations = testMethod.Declaration.Annotations
.Where(pta => pta.Annotation is IgnoreTestAnnotation);
var rewriteSession = RewritingManager.CheckOutCodePaneSession();
AnnotationUpdater.RemoveAnnotations(rewriteSession, ignoreTestAnnotations);
rewriteSession.TryRewrite();
}
private void ExecuteIgnoreGroupCommand(object parameter)
{
var rewriteSession = RewritingManager.CheckOutCodePaneSession();
var testGroup = GroupContainingSelectedTest(MouseOverTest);
var ignoreTestAnnotation = new IgnoreTestAnnotation();
foreach (TestMethodViewModel test in testGroup.Items)
{
if (!test.Method.IsIgnored)
{
AnnotationUpdater.AddAnnotation(rewriteSession, test.Method.Declaration, ignoreTestAnnotation);
}
}
rewriteSession.TryRewrite();
}
private void ExecuteUnignoreGroupCommand(object parameter)
{
var rewriteSession = RewritingManager.CheckOutCodePaneSession();
var testGroup = GroupContainingSelectedTest(MouseOverTest);
foreach (TestMethodViewModel test in testGroup.Items)
{
var ignoreTestAnnotations = test.Method.Declaration.Annotations
.Where(pta => pta.Annotation is IgnoreTestAnnotation);
foreach (var ignoreTestAnnotation in ignoreTestAnnotations)
{
AnnotationUpdater.RemoveAnnotation(rewriteSession, ignoreTestAnnotation);
}
}
rewriteSession.TryRewrite();
}
private void ExecuteUnignoreSelectedTestsCommand(object parameter)
{
if (Model.IsBusy || !(parameter is IList viewModels && viewModels.Count > 0))
{
return;
}
var ignoredModels = viewModels.OfType<TestMethodViewModel>()
.Where(model => model.Method.IsIgnored);
if (!ignoredModels.Any())
{
return;
}
var rewriteSession = RewritingManager.CheckOutCodePaneSession();
foreach (var test in ignoredModels)
{
var ignoreTestAnnotations = test.Method.Declaration.Annotations
.Where(pta => pta.Annotation is IgnoreTestAnnotation);
AnnotationUpdater.RemoveAnnotations(rewriteSession, ignoreTestAnnotations);
}
rewriteSession.TryRewrite();
}
private void ExecuteIgnoreSelectedTestsCommand(object parameter)
{
if (Model.IsBusy || !(parameter is IList viewModels && viewModels.Count > 0))
{
return;
}
var unignoredModels = viewModels.OfType<TestMethodViewModel>()
.Where(model => !model.Method.IsIgnored);
if (!unignoredModels.Any())
{
return;
}
var rewriteSession = RewritingManager.CheckOutCodePaneSession();
var ignoreTestAnnotation = new IgnoreTestAnnotation();
foreach (var test in unignoredModels)
{
AnnotationUpdater.AddAnnotation(rewriteSession, test.Method.Declaration, ignoreTestAnnotation);
}
rewriteSession.TryRewrite();
}
private void ExecuteCopyResultsCommand(object parameter)
{
const string XML_SPREADSHEET_DATA_FORMAT = "XML Spreadsheet";
ColumnInfo[] columnInfos = { new ColumnInfo("Project"), new ColumnInfo("Component"), new ColumnInfo("Method"), new ColumnInfo("Outcome"), new ColumnInfo("Output"),
new ColumnInfo("Start Time"), new ColumnInfo("End Time"), new ColumnInfo("Duration (ms)", hAlignment.Right) };
// FIXME do that to the TestMethodViewModel
var aResults = Model.Tests.Select(test => test.ToArray()).ToArray();
var title = string.Format($"Rubberduck Test Results - {DateTime.Now.ToString(CultureInfo.InvariantCulture)}");
var textResults = title + Environment.NewLine + string.Join(string.Empty, aResults.Select(result => result.ToString() + Environment.NewLine).ToArray());
var csvResults = ExportFormatter.Csv(aResults, title, columnInfos);
var htmlResults = ExportFormatter.HtmlClipboardFragment(aResults, title, columnInfos);
var rtfResults = ExportFormatter.RTF(aResults, title);
using (var strm1 = ExportFormatter.XmlSpreadsheetNew(aResults, title, columnInfos))
{
//Add the formats from richest formatting to least formatting
_clipboard.AppendStream(DataFormats.GetDataFormat(XML_SPREADSHEET_DATA_FORMAT).Name, strm1);
_clipboard.AppendString(DataFormats.Rtf, rtfResults);
_clipboard.AppendString(DataFormats.Html, htmlResults);
_clipboard.AppendString(DataFormats.CommaSeparatedValue, csvResults);
_clipboard.AppendString(DataFormats.UnicodeText, textResults);
_clipboard.Flush();
}
}
// TODO - FIXME
//KEEP THIS, AS IT MAKES FOR THE BASIS OF A USEFUL *SUMMARY* REPORT
//private void ExecuteCopyResultsCommand(object parameter)
//{
// var results = string.Join(Environment.NewLine, _model.LastRun.Select(test => test.ToString()));
// var passed = _model.LastRun.Count(test => test.Result.Outcome == TestOutcome.Succeeded) + " " + TestOutcome.Succeeded;
// var failed = _model.LastRun.Count(test => test.Result.Outcome == TestOutcome.Failed) + " " + TestOutcome.Failed;
// var inconclusive = _model.LastRun.Count(test => test.Result.Outcome == TestOutcome.Inconclusive) + " " + TestOutcome.Inconclusive;
// var ignored = _model.LastRun.Count(test => test.Result.Outcome == TestOutcome.Ignored) + " " + TestOutcome.Ignored;
// var duration = RubberduckUI.UnitTest_TotalDuration + " - " + TotalDuration;
// var resource = "Rubberduck Unit Tests - {0}{6}{1} | {2} | {3} | {4}{6}{5} ms{6}";
// var text = string.Format(resource, DateTime.Now, passed, failed, inconclusive, ignored, duration, Environment.NewLine) + results;
// _clipboard.Write(text);
//}
private void OpenSettings(object param)
{
using (var window = _settingsFormFactory.Create(SettingsViews.UnitTestSettings))
{
window.ShowDialog();
_settingsFormFactory.Release(window);
}
}
#endregion
public void Dispose()
{
Model.Dispose();
}
}
}