-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IErrorTag implementation that lazily-creates tooltips (#3690)
* Add IErrorTag implementation that lazily-creates tooltips Relates to #2798
- Loading branch information
1 parent
9360d20
commit 01463d3
Showing
5 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/IssueViz.UnitTests/Editor/ErrorTagging/SonarErrorTagUnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using SonarLint.VisualStudio.Core.Analysis; | ||
using SonarLint.VisualStudio.IssueVisualization.Editor.ErrorTagging; | ||
|
||
namespace SonarLint.VisualStudio.IssueVisualization.UnitTests.Editor.ErrorTagging | ||
{ | ||
[TestClass] | ||
public class SonarErrorTagUnitTests | ||
{ | ||
[TestMethod] | ||
public void Ctor_SimplePropertySet_TooltipNotCreated() | ||
{ | ||
var tooltipProvider = new Mock<IErrorTagTooltipProvider>(); | ||
|
||
var testSubject = CreateTestSubject("my error type", tooltipProvider.Object); | ||
|
||
testSubject.ErrorType.Should().Be("my error type"); | ||
tooltipProvider.Invocations.Should().HaveCount(0); | ||
} | ||
|
||
[TestMethod] | ||
public void ToolTipContent_CreatedOnDemandAndCreatedOnlyOnce() | ||
{ | ||
var analysisIssue = Mock.Of<IAnalysisIssueBase>(); | ||
|
||
var expectedTooltipObject = new object(); | ||
var tooltipProvider = new Mock<IErrorTagTooltipProvider>(); | ||
tooltipProvider.Setup(x => x.Create(analysisIssue)).Returns(expectedTooltipObject); | ||
|
||
var testSubject = CreateTestSubject(tooltipProvider: tooltipProvider.Object, | ||
analysisIssue: analysisIssue); | ||
|
||
// Sanity check before accessing the property | ||
tooltipProvider.Invocations.Should().HaveCount(0); | ||
|
||
// 1. First access - should create the tooltip | ||
var actual1 = testSubject.ToolTipContent; | ||
|
||
actual1.Should().BeSameAs(expectedTooltipObject); | ||
tooltipProvider.Invocations.Should().HaveCount(1); | ||
|
||
// 2. Subsequent access - should return the original object | ||
var actual2 = testSubject.ToolTipContent; | ||
|
||
actual2.Should().BeSameAs(expectedTooltipObject); | ||
tooltipProvider.Invocations.Should().HaveCount(1); | ||
} | ||
|
||
private static SonarErrorTag CreateTestSubject(string errorType = null, | ||
IErrorTagTooltipProvider tooltipProvider = null, | ||
IAnalysisIssueBase analysisIssue = null) | ||
{ | ||
errorType ??= "any"; | ||
analysisIssue ??= Mock.Of<IAnalysisIssueBase>(); | ||
tooltipProvider ??= Mock.Of<IErrorTagTooltipProvider>(); | ||
|
||
return new SonarErrorTag(errorType, analysisIssue, tooltipProvider); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System; | ||
using Microsoft.VisualStudio.Text.Tagging; | ||
using SonarLint.VisualStudio.Core.Analysis; | ||
|
||
namespace SonarLint.VisualStudio.IssueVisualization.Editor.ErrorTagging | ||
{ | ||
/// <summary> | ||
/// Implementation of <see cref="IErrorTag"/> that lazily creates tooltips. | ||
/// </summary> | ||
/// <remarks> | ||
/// Mitigation for a performance issue. See https://github.com/SonarSource/sonarlint-visualstudio/issues/2798 | ||
/// </remarks> | ||
internal class SonarErrorTag : IErrorTag | ||
{ | ||
private readonly Lazy<object> tooltipFactory; | ||
|
||
public SonarErrorTag(string errorType, IAnalysisIssueBase analysisIssue, IErrorTagTooltipProvider errorTagTooltipProvider) | ||
{ | ||
ErrorType = errorType; | ||
tooltipFactory = new Lazy<object>(() => errorTagTooltipProvider.Create(analysisIssue)); | ||
} | ||
|
||
public string ErrorType { get; } | ||
|
||
public object ToolTipContent => tooltipFactory.Value; | ||
} | ||
} |