From 82cdd964ad676220f78607b7b27de74c618daa51 Mon Sep 17 00:00:00 2001 From: Nicolas Vandeginste Date: Thu, 29 Feb 2024 16:30:30 +0100 Subject: [PATCH] Fixes issues #38. --- .../Services/ParameterProviderTests.cs | 42 ------------------- .../fill_primary_constructor_with_mock.cs | 28 +++++++++++++ ...fill_primary_constructor_with_mock.cs.gold | 33 +++++++++++++++ .../FillParamWithMockContextAction.cs | 2 +- ...ParamWithMockLocalVariableContextAction.cs | 2 +- .../FillWithMockFieldsContextAction.cs | 4 +- .../FillWithMockLocalVariableContextAction.cs | 2 +- .../Services/CsharpMemberProvider.cs | 30 +------------ .../Services/ICsharpMemberProvider.cs | 2 +- 9 files changed, 69 insertions(+), 76 deletions(-) delete mode 100644 Abc.MoqComplete/Abc.MoqComplete.Tests/ContextAction/Services/ParameterProviderTests.cs create mode 100644 Abc.MoqComplete/Abc.MoqComplete.Tests/data/ContextAction/fill_primary_constructor_with_mock.cs create mode 100644 Abc.MoqComplete/Abc.MoqComplete.Tests/data/ContextAction/fill_primary_constructor_with_mock.cs.gold diff --git a/Abc.MoqComplete/Abc.MoqComplete.Tests/ContextAction/Services/ParameterProviderTests.cs b/Abc.MoqComplete/Abc.MoqComplete.Tests/ContextAction/Services/ParameterProviderTests.cs deleted file mode 100644 index d2cf68a..0000000 --- a/Abc.MoqComplete/Abc.MoqComplete.Tests/ContextAction/Services/ParameterProviderTests.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Linq; -using Abc.MoqComplete.ContextActions.Services; -using NUnit.Framework; - -namespace Abc.MoqComplete.Tests.ContextAction.Services -{ - [TestFixture] - public class ParameterProviderTests - { - private CsharpMemberProvider _provider; - - [SetUp] - public void SetUp() - { - _provider = new CsharpMemberProvider(); - } - - [Test] - public void should_parse_generic_parameters() - { - var constructorString = - "CSharpConstructor:CurrencyChoiceHasSignificantSpreadRequirement(Func,ICurrencyAmountConverter,ICorporateActionRepository)"; - var expected = new[] { "Func", "ICurrencyAmountConverter", "ICorporateActionRepository" }; - - var parameters = _provider.GetConstructorParameters(constructorString).ToArray(); - - Assert.That(parameters, Is.EquivalentTo(expected)); - } - - [Test] - public void should_parse_non_generic_parameters() - { - var constructorString = - "CSharpConstructor:TestConstructor(ICurrencyAmountConverter,ICorporateActionRepository)"; - var expected = new[] { "ICurrencyAmountConverter", "ICorporateActionRepository" }; - - var parameters = _provider.GetConstructorParameters(constructorString).ToArray(); - - Assert.That(parameters, Is.EquivalentTo(expected)); - } - } -} diff --git a/Abc.MoqComplete/Abc.MoqComplete.Tests/data/ContextAction/fill_primary_constructor_with_mock.cs b/Abc.MoqComplete/Abc.MoqComplete.Tests/data/ContextAction/fill_primary_constructor_with_mock.cs new file mode 100644 index 0000000..be8558d --- /dev/null +++ b/Abc.MoqComplete/Abc.MoqComplete.Tests/data/ContextAction/fill_primary_constructor_with_mock.cs @@ -0,0 +1,28 @@ +using NUnit.Framework; +using System; +using Moq; + +namespace ConsoleApp1.Tests +{ + public class Test1(ITest test, ITest2 test2) + { + + public interface ITest + { + void Coco(ITest test, ITest test2, string test3); + } + public interface ITest2 + { + } + } + + [TestFixture] + public class TestClass + { + [Test] + public void METHOD() + { + var t = new Test1({ caret }); + } + } +} \ No newline at end of file diff --git a/Abc.MoqComplete/Abc.MoqComplete.Tests/data/ContextAction/fill_primary_constructor_with_mock.cs.gold b/Abc.MoqComplete/Abc.MoqComplete.Tests/data/ContextAction/fill_primary_constructor_with_mock.cs.gold new file mode 100644 index 0000000..bb7474f --- /dev/null +++ b/Abc.MoqComplete/Abc.MoqComplete.Tests/data/ContextAction/fill_primary_constructor_with_mock.cs.gold @@ -0,0 +1,33 @@ +using NUnit.Framework; +using System; +using Moq; + +namespace ConsoleApp1.Tests +{ + public class Test1(ITest test, ITest2 test2) + { + + public interface ITest + { + void Coco(ITest test, ITest test2, string test3); + } + public interface ITest2 + { + } + } + + [TestFixture] + public class TestClass + { + private Mock myTest; + private Mock myTest2; + + [Test] + public void METHOD() + { + myTest = new Mock(); + myTest2 = new Mock(); + var t = new Test1(myTest.Object, myTest2.Object{caret}); + } + } +} \ No newline at end of file diff --git a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillParamWithMockContextAction.cs b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillParamWithMockContextAction.cs index 4be5fa8..aee6d2b 100644 --- a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillParamWithMockContextAction.cs +++ b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillParamWithMockContextAction.cs @@ -111,7 +111,7 @@ public override bool IsAvailable(IUserDataHolder cache) protected override Action ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { var argumentList = _selectedElement.ArgumentList; - var parameters = _csharpMemberProvider.GetConstructorParameters(_constructor.ToString()).ToArray(); + var parameters = _csharpMemberProvider.GetConstructorParameters(_constructor).ToArray(); var mockFieldsByType = _csharpMemberProvider.GetClassFields(_classBody, _selectedElement.Language); var shortName = _constructor.Parameters[_parameterNumber].ShortName; var currentParam = parameters[_parameterNumber]; diff --git a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillParamWithMockLocalVariableContextAction.cs b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillParamWithMockLocalVariableContextAction.cs index 18c7aa8..9b22327 100644 --- a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillParamWithMockLocalVariableContextAction.cs +++ b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillParamWithMockLocalVariableContextAction.cs @@ -49,7 +49,7 @@ protected override Action ExecutePsiTransaction(ISolution solution var @class = (IClass)selectedElement.TypeReference?.Resolve().DeclaredElement; var parameterCount = selectedElement.ArgumentList?.Arguments.Count(x => x.Kind != ParameterKind.UNKNOWN); var constructor = @class.Constructors.ToArray().FirstOrDefault(x => !x.IsParameterless && x.Parameters.Count > parameterCount); - var parameters = csharpMemberProvider.GetConstructorParameters(constructor.ToString()).ToArray(); + var parameters = csharpMemberProvider.GetConstructorParameters(constructor).ToArray(); var parameterNumber = csharpMemberProvider.GetCurrentParameterNumber(selectedElement, _dataProvider); var shortName = constructor.Parameters[parameterNumber].ShortName; var currentParam = parameters[parameterNumber]; diff --git a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillWithMockFieldsContextAction.cs b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillWithMockFieldsContextAction.cs index 86be24a..be57e69 100644 --- a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillWithMockFieldsContextAction.cs +++ b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillWithMockFieldsContextAction.cs @@ -58,7 +58,7 @@ public override bool IsAvailable(IUserDataHolder cache) && x.Parameters.All(_csharpMemberProvider.IsAbstractOrInterface)); if (_constructor == null) return false; - + var isAvailable = testProjectProvider.IsTestProject(_dataProvider.PsiModule) && _selectedElement != null && _selectedElement.Arguments.Count == 0; if (isAvailable) cache.PutKey(AnchorKey.FillWithMockContextActionKey); @@ -75,7 +75,7 @@ protected override Action ExecutePsiTransaction(ISolution solution if (classDeclaration == null || block == null) return null; - var parameters = _csharpMemberProvider.GetConstructorParameters(_constructor.ToString()).ToArray(); + var parameters = _csharpMemberProvider.GetConstructorParameters(_constructor).ToArray(); var naming = _dataProvider.PsiServices.Naming; var mockFieldsByType = _csharpMemberProvider.GetClassFields(classBody, _selectedElement.Language); diff --git a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillWithMockLocalVariableContextAction.cs b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillWithMockLocalVariableContextAction.cs index 8270480..7e2f942 100644 --- a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillWithMockLocalVariableContextAction.cs +++ b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/FillWithMock/FillWithMockLocalVariableContextAction.cs @@ -54,7 +54,7 @@ protected override Action ExecutePsiTransaction(ISolution solution if (constructor == null) return null; - var parameters = _csharpMemberProvider.GetConstructorParameters(constructor.ToString()).ToArray(); + var parameters = _csharpMemberProvider.GetConstructorParameters(constructor).ToArray(); for (int i = 0; i < constructor.Parameters.Count; i++) { diff --git a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/Services/CsharpMemberProvider.cs b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/Services/CsharpMemberProvider.cs index 35aa264..bd4ddf7 100644 --- a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/Services/CsharpMemberProvider.cs +++ b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/Services/CsharpMemberProvider.cs @@ -6,6 +6,7 @@ using JetBrains.ReSharper.Feature.Services.CSharp.ContextActions; using JetBrains.ReSharper.Psi; using JetBrains.ReSharper.Psi.CSharp.Tree; +using JetBrains.ReSharper.Psi.Util; using JetBrains.TextControl; using JetBrains.Util; @@ -14,34 +15,7 @@ namespace Abc.MoqComplete.ContextActions.Services [SolutionComponent] public class CsharpMemberProvider : ICsharpMemberProvider { - public IEnumerable GetConstructorParameters(string constructorString) - { - var index = 0; - var openedBracket = 0; - - while (constructorString[index] != '(') - index++; - - var sb = new StringBuilder(); - while (constructorString[index] != ')') - { - index++; - - if (constructorString[index] == '<') - openedBracket++; - - else if (constructorString[index] == '>') - openedBracket--; - - if (openedBracket ==0 && (constructorString[index] == ',' || constructorString[index] == ')')) - { - yield return sb.ToString(); - sb.Clear(); - } - else - sb.Append(constructorString[index]); - } - } + public IEnumerable GetConstructorParameters(IConstructor constructor) => constructor.Parameters.Select(s => s.Type.GetPresentableName(constructor.PresentationLanguage)); public Dictionary GetClassFields(IClassBody classBody, PsiLanguageType languageType) { diff --git a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/Services/ICsharpMemberProvider.cs b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/Services/ICsharpMemberProvider.cs index 77c5285..fa7957e 100644 --- a/Abc.MoqComplete/Abc.MoqComplete/ContextActions/Services/ICsharpMemberProvider.cs +++ b/Abc.MoqComplete/Abc.MoqComplete/ContextActions/Services/ICsharpMemberProvider.cs @@ -9,7 +9,7 @@ namespace Abc.MoqComplete.ContextActions.Services { public interface ICsharpMemberProvider { - IEnumerable GetConstructorParameters(string constructorString); + IEnumerable GetConstructorParameters(IConstructor constructor); Dictionary GetClassFields(IClassBody classBody, PsiLanguageType languageType); string GetGenericMock(string typeStr); bool IsAbstractOrInterface(IParameter parameter);