From ce3851099c06285a0b1181d3204bfa50cc83fb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Tue, 29 Jan 2019 10:59:40 +0100 Subject: Fixes VSTS Bug 766267: No keyboard shortcut to run test from the text editor https://devdiv.visualstudio.com/DevDiv/_workitems/edit/766267 --- .../Commands/UnitTestCommands.cs | 15 +- .../MonoDevelop.UnitTesting.addin.xml | 3 + .../Services/AbstractUnitTestEditorExtension.cs | 158 +++++++++++++-------- 3 files changed, 109 insertions(+), 67 deletions(-) (limited to 'main/src/addins/MonoDevelop.UnitTesting') diff --git a/main/src/addins/MonoDevelop.UnitTesting/Commands/UnitTestCommands.cs b/main/src/addins/MonoDevelop.UnitTesting/Commands/UnitTestCommands.cs index 5f776ccc60..6d0a84150d 100644 --- a/main/src/addins/MonoDevelop.UnitTesting/Commands/UnitTestCommands.cs +++ b/main/src/addins/MonoDevelop.UnitTesting/Commands/UnitTestCommands.cs @@ -44,7 +44,7 @@ namespace MonoDevelop.UnitTesting.Commands GoToFailure, RerunTest, } - + public enum TestChartCommands { ShowResults, @@ -55,13 +55,20 @@ namespace MonoDevelop.UnitTesting.Commands ShowFailedTests, ShowIgnoredTests } - + public enum NUnitProjectCommands { AddAssembly } - - class RunAllTestsHandler: CommandHandler + + public enum TextEditorCommands + { + RunTestAtCaret, + DebugTestAtCaret, + SelectTestAtCaret + } + + class RunAllTestsHandler : CommandHandler { protected override void Run () { diff --git a/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.addin.xml b/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.addin.xml index c2158343b2..4fd591a678 100644 --- a/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.addin.xml +++ b/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.addin.xml @@ -75,6 +75,9 @@ + + + diff --git a/main/src/addins/MonoDevelop.UnitTesting/Services/AbstractUnitTestEditorExtension.cs b/main/src/addins/MonoDevelop.UnitTesting/Services/AbstractUnitTestEditorExtension.cs index 58449a06a8..c83ed71e25 100644 --- a/main/src/addins/MonoDevelop.UnitTesting/Services/AbstractUnitTestEditorExtension.cs +++ b/main/src/addins/MonoDevelop.UnitTesting/Services/AbstractUnitTestEditorExtension.cs @@ -37,6 +37,8 @@ using MonoDevelop.Ide.Editor.Extension; using MonoDevelop.Ide.Editor; using MonoDevelop.Projects; using Mono.Addins; +using MonoDevelop.Components.Commands; +using MonoDevelop.UnitTesting.Commands; namespace MonoDevelop.UnitTesting { @@ -137,6 +139,36 @@ namespace MonoDevelop.UnitTesting List currentMarker = new List(); + TestRunner GetTestRunnerAtCaret (bool debug) + { + var line = Editor.GetLine (Editor.CaretLine); + if (line == null) + return null; + foreach (var marker in Editor.GetLineMarkers (line)) { + if (marker is IUnitTestMarker result) + return new TestRunner (result.UnitTest.UnitTestIdentifier, DocumentContext.Project, debug); + } + return null; + } + + [CommandHandler (TextEditorCommands.RunTestAtCaret)] + protected void OnRunTestAtCaret () + { + GetTestRunnerAtCaret (false)?.Run (this, EventArgs.Empty); + } + + [CommandHandler (TextEditorCommands.DebugTestAtCaret)] + protected void OnDebugTestAtCaret () + { + GetTestRunnerAtCaret (true)?.Run (this, EventArgs.Empty); + } + + [CommandHandler (TextEditorCommands.SelectTestAtCaret)] + protected void OnSelectTestAtCaret () + { + GetTestRunnerAtCaret (false)?.Select (this, EventArgs.Empty); + } + class UnitTestMarkerHostImpl : UnitTestMarkerHost { readonly AbstractUnitTestTextEditorExtension ext; @@ -277,82 +309,82 @@ namespace MonoDevelop.UnitTesting #endregion - class TestRunner + } + class TestRunner + { + readonly string testCase; + readonly bool debug; + IBuildTarget project; + + public TestRunner (string testCase, IBuildTarget project, bool debug) { - readonly string testCase; - readonly bool debug; - IBuildTarget project; - - public TestRunner (string testCase, IBuildTarget project, bool debug) - { - this.testCase = testCase; - this.debug = debug; - this.project = project; - } + this.testCase = testCase; + this.debug = debug; + this.project = project; + } - bool TimeoutHandler () - { - var test = UnitTestService.SearchTestByDocumentId (testCase); - if (test != null) { - RunTest (test); - timeoutHandler = 0; - } else { - return true; - } - return false; + bool TimeoutHandler () + { + var test = UnitTestService.SearchTestByDocumentId (testCase); + if (test != null) { + RunTest (test); + timeoutHandler = 0; + } else { + return true; } + return false; + } - internal async void Run (object sender, EventArgs e) - { - if (IdeApp.ProjectOperations.IsBuilding (IdeApp.ProjectOperations.CurrentSelectedSolution) || - IdeApp.ProjectOperations.IsRunning (IdeApp.ProjectOperations.CurrentSelectedSolution)) - return; + internal async void Run (object sender, EventArgs e) + { + if (IdeApp.ProjectOperations.IsBuilding (IdeApp.ProjectOperations.CurrentSelectedSolution) || + IdeApp.ProjectOperations.IsRunning (IdeApp.ProjectOperations.CurrentSelectedSolution)) + return; - var foundTest = UnitTestService.SearchTestByDocumentId (testCase); - if (foundTest != null) { - RunTest (foundTest); - return; - } + var foundTest = UnitTestService.SearchTestByDocumentId (testCase); + if (foundTest != null) { + RunTest (foundTest); + return; + } - bool buildBeforeExecuting = IdeApp.Preferences.BuildBeforeRunningTests; + bool buildBeforeExecuting = IdeApp.Preferences.BuildBeforeRunningTests; - if (buildBeforeExecuting) { - await IdeApp.ProjectOperations.Build (project).Task; - await UnitTestService.RefreshTests (CancellationToken.None); - } - - foundTest = UnitTestService.SearchTestByDocumentId (testCase); - if (foundTest != null) - RunTest (foundTest); - else - UnitTestService.ReportExecutionError (GettextCatalog.GetString ("Unit test '{0}' could not be loaded.", testCase)); + if (buildBeforeExecuting) { + await IdeApp.ProjectOperations.Build (project).Task; + await UnitTestService.RefreshTests (CancellationToken.None); } - internal void Select (object sender, EventArgs e) - { - var test = UnitTestService.SearchTestByDocumentId (testCase); - if (test == null) - return; - UnitTestService.CurrentSelectedTest = test; - } + foundTest = UnitTestService.SearchTestByDocumentId (testCase); + if (foundTest != null) + RunTest (foundTest); + else + UnitTestService.ReportExecutionError (GettextCatalog.GetString ("Unit test '{0}' could not be loaded.", testCase)); + } - void RunTest (UnitTest test) - { - var debugModeSet = Runtime.ProcessService.GetDebugExecutionMode (); - Core.Execution.IExecutionHandler ctx = null; - if (debug && debugModeSet != null) { - foreach (var executionMode in debugModeSet.ExecutionModes) { - if (test.CanRun (executionMode.ExecutionHandler)) { - ctx = executionMode.ExecutionHandler; - break; - } + internal void Select (object sender, EventArgs e) + { + var test = UnitTestService.SearchTestByDocumentId (testCase); + if (test == null) + return; + UnitTestService.CurrentSelectedTest = test; + } + + void RunTest (UnitTest test) + { + var debugModeSet = Runtime.ProcessService.GetDebugExecutionMode (); + Core.Execution.IExecutionHandler ctx = null; + if (debug && debugModeSet != null) { + foreach (var executionMode in debugModeSet.ExecutionModes) { + if (test.CanRun (executionMode.ExecutionHandler)) { + ctx = executionMode.ExecutionHandler; + break; } } - // NUnitService.Instance.RunTest (test, ctx); - var pad = IdeApp.Workbench.GetPad (); - var content = (TestPad)pad.Content; - content.RunTest (test, ctx); } + // NUnitService.Instance.RunTest (test, ctx); + var pad = IdeApp.Workbench.GetPad (); + var content = (TestPad)pad.Content; + content.RunTest (test, ctx); } } } -- cgit v1.2.3 From b2183a05fccb8acabc6324e81afb2b5bf90baa89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Tue, 29 Jan 2019 15:03:20 +0100 Subject: [UnitTest] Removed some dead code. --- .../Services/AbstractUnitTestEditorExtension.cs | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'main/src/addins/MonoDevelop.UnitTesting') diff --git a/main/src/addins/MonoDevelop.UnitTesting/Services/AbstractUnitTestEditorExtension.cs b/main/src/addins/MonoDevelop.UnitTesting/Services/AbstractUnitTestEditorExtension.cs index c83ed71e25..27464ad5b2 100644 --- a/main/src/addins/MonoDevelop.UnitTesting/Services/AbstractUnitTestEditorExtension.cs +++ b/main/src/addins/MonoDevelop.UnitTesting/Services/AbstractUnitTestEditorExtension.cs @@ -323,18 +323,6 @@ namespace MonoDevelop.UnitTesting this.project = project; } - bool TimeoutHandler () - { - var test = UnitTestService.SearchTestByDocumentId (testCase); - if (test != null) { - RunTest (test); - timeoutHandler = 0; - } else { - return true; - } - return false; - } - internal async void Run (object sender, EventArgs e) { if (IdeApp.ProjectOperations.IsBuilding (IdeApp.ProjectOperations.CurrentSelectedSolution) || -- cgit v1.2.3