Skip to content

Commit

Permalink
Fix TodoItems injection annotations and unit test data and settings i…
Browse files Browse the repository at this point in the history
…solation within `TodoItemsCountTests`
  • Loading branch information
ulrichb committed Nov 11, 2024
1 parent 4b0cf68 commit 8da011a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using JetBrains.Application;
using JetBrains.Application.Parts;
using JetBrains.DataFlow;
using Roflcopter.Plugin.TodoItems;

namespace Roflcopter.Plugin.Tests.TodoItems
{
[ShellComponent]
[ShellComponent(Instantiation.DemandAnyThreadSafe)]
internal class TestTodoItemsCountConsumer : ITodoItemsCountConsumer
{
[CanBeNull]
Expand Down
38 changes: 20 additions & 18 deletions Src/Roflcopter.Plugin.Tests/TodoItems/TodoItemsCountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using JetBrains.Application.Components;
using JetBrains.Application.Settings;
using JetBrains.Diagnostics;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.TestFramework;
using NUnit.Framework;
Expand Down Expand Up @@ -37,7 +38,7 @@ public void TodoItemsCountsWithCondition()
Test((consumer, settings) =>
{
var definitionText = "Todo\n Todo [Important] ";
RunGuarded(() => settings.SetValue((TodoItemsCountSettings s) => s.Definitions, definitionText));
settings.SetValue((TodoItemsCountSettings s) => s.Definitions, definitionText);
Assert.That(consumer.TodoItemsCounts.NotNull().Select(x => (x.Definition.ToString(), x.Count)), Is.EqualTo(new[]
{
Expand All @@ -52,7 +53,7 @@ public void TodoItemsCounts_WithNonMatchingName()
{
Test((consumer, settings) =>
{
RunGuarded(() => settings.SetValue((TodoItemsCountSettings s) => s.Definitions, "Todo\nNON_MATCHING"));
settings.SetValue((TodoItemsCountSettings s) => s.Definitions, "Todo\nNON_MATCHING");
Assert.That(consumer.TodoItemsCounts.NotNull().Select(x => (x.Definition.ToString(), x.Count)), Is.EqualTo(new[]
{
Expand All @@ -67,7 +68,7 @@ public void TodoItemsCounts_WithDuplicateDefinition()
{
Test((consumer, settings) =>
{
RunGuarded(() => settings.SetValue((TodoItemsCountSettings s) => s.Definitions, "Todo\nTodo"));
settings.SetValue((TodoItemsCountSettings s) => s.Definitions, "Todo\nTodo");
Assert.That(consumer.TodoItemsCounts.NotNull().Select(x => (x.Definition.ToString(), x.Count)), Is.EqualTo(new[]
{
Expand Down Expand Up @@ -114,23 +115,24 @@ public void TodoItemsCounts_ConsumerUpdateRequestSignal()

private void Test(Action<TestTodoItemsCountConsumer, IContextBoundSettingsStore> action)
{
var files = new[] { "Sample.cs", "Sample.xml" };
// Use an explicit short-lived lifetime for the "settings transaction" because a) it must be scoped per test,
// and b) the `base.TestLifetime` is destroyed within the test tear-down phase which would raise
// "target dispatcher [...] does not support asynchronous execution or cross-thread marshalling" errors
// (b/c TearDown is not `RunWithAsyncBehaviorAllowed`).

ExecuteWithinSettingsTransaction(settings =>
using var temporarySettingsLifetimeDefinition = Lifetime.Define("TemporarySettings");

ExecuteWithinSettingsTransactionGuarded(temporarySettingsLifetimeDefinition.Lifetime, (_, settings) =>
{
WithSingleProject(
files.Select(x => GetTestDataFilePath2(x).FullPath),
(_, solution, _) =>
{
Assert.That(solution.GetComponent<TodoItemsCountProvider>, Is.Not.Null);
var consumer = ShellInstance.GetComponent<TestTodoItemsCountConsumer>();
action(consumer, settings);
});
// Disable to solve issues with TodoItemsCountProvider-updates during termination of
// the "settings transaction":
settings.SetValue((TodoItemsCountSettings s) => s.IsEnabled, false);
var projectFiles = new[] { "Sample.cs", "Sample.xml" }.Select(x => GetTestDataFilePath2(x).FullPath);
WithSingleProject(projectFiles, (_, solution, _) =>
{
Assert.That(solution.GetComponent<TodoItemsCountProvider>, Is.Not.Null);
var consumer = ShellInstance.GetComponent<TestTodoItemsCountConsumer>();
action(consumer, settings);
});
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions Src/Roflcopter.Plugin.Tests/test/data/TodoItems/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public class Sample
{
// TODO

// TODO 1 before Important after
// TODO 2 before Important after
// TODO 3 before imPOrTaNt after
// TODO: 1 before Important after
// TODO: 2 before Important after
// TODO: 3 before imPOrTaNt after

// BUG: 1
// BUG: 2
Expand Down
2 changes: 1 addition & 1 deletion Src/Roflcopter.Plugin.Tests/test/data/TodoItems/Sample.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<root>
<!-- TODO in XML -->
<!-- TODO: in XML -->
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Input;
using JetBrains.Annotations;
using JetBrains.Application;
using JetBrains.Application.Parts;
using JetBrains.Application.Threading;
using JetBrains.Application.UI.Actions;
using JetBrains.Application.UI.ActionSystem.ActionBar;
Expand All @@ -21,7 +22,7 @@

namespace Roflcopter.Plugin.TodoItems.Presentation
{
[ShellComponent]
[ShellComponent(Instantiation.DemandAnyThreadSafe)]
[ExcludeFromCodeCoverage /* manually tested UI code */]
public class TodoItemsCountPresenter : IActionBarPatcher, ITodoItemsCountConsumer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using JetBrains.Application.Parts;
using JetBrains.Application.Settings;
using JetBrains.Application.Settings.Extentions;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;

namespace Roflcopter.Plugin.TodoItems
{
[SolutionComponent]
[SolutionComponent(Instantiation.DemandAnyThreadSafe)]
public class TodoItemsCountDefinitionsCachedSettingsReader : ICachedSettingsReader<IReadOnlyCollection<TodoItemsCountDefinition>>
{
private readonly ISettingsStore _settingsStore;
Expand Down
3 changes: 2 additions & 1 deletion Src/Roflcopter.Plugin/TodoItems/TodoItemsCountProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using JetBrains.Application.Parts;
using JetBrains.Application.Settings;
using JetBrains.Application.Settings.Extentions;
using JetBrains.DataFlow;
Expand All @@ -16,7 +17,7 @@

namespace Roflcopter.Plugin.TodoItems
{
[SolutionComponent]
[SolutionComponent(Instantiation.ContainerAsyncPrimaryThread /* req to inject `MultiplexingTodoManager` */)]
public class TodoItemsCountProvider
{
private static readonly ILogger Logger = JetBrains.Util.Logging.Logger.GetLogger(typeof(TodoItemsCountProvider));
Expand Down

0 comments on commit 8da011a

Please sign in to comment.