Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/UnitTestGenerator/Commands/GenerateUnitTestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@
using System.Linq;
using Microsoft.CodeAnalysis;
using MonoDevelop.Components.Commands;
using MonoDevelop.Ide.Composition;
using UnitTestGenerator.Dialogs;
using UnitTestGenerator.Helpers;
using UnitTestGenerator.Services.Interfaces;

namespace UnitTestGenerator.Commands
{
public class GenerateUnitTestCommand : CommandHandler
{
readonly ITestGeneratorService _testGeneratorService;
readonly IConfigurationService _configurationService;

public GenerateUnitTestCommand()
{
_testGeneratorService = CompositionManager.GetExportedValue<ITestGeneratorService>();
_configurationService = CompositionManager.GetExportedValue<IConfigurationService>();
}

protected override void Update(CommandInfo info)
{
info.Bypass = false;
info.Visible = false;
info.Enabled = false;

var utHelper = new UnitTestHelper();
try
{
var method = utHelper.GetActiveMethodDeclarationSyntax();
var method = _testGeneratorService.GetActiveMethodDeclarationSyntax();

if (method == null)
return;
Expand Down Expand Up @@ -50,19 +58,19 @@ protected override void Update(CommandInfo info)

protected override void Run()
{
var config = new ConfigurationHelper().GetConfiguration();
var config = _configurationService.GetConfiguration();
if (string.IsNullOrWhiteSpace(config.UnitTestProjectName))
{
var dialog = new ConfigureUnitTestProjectDialog();
return;
}

var utHelper = new UnitTestHelper();
var currentMethod = utHelper.GetActiveMethodDeclarationSyntax();

var currentMethod = _testGeneratorService.GetActiveMethodDeclarationSyntax();
if (currentMethod == null)
return;
var generatedTestModel = utHelper.CreateGeneratedTestModel(currentMethod);
var document = utHelper.OpenDocument(generatedTestModel);
var generatedTestModel = _testGeneratorService.CreateGeneratedTestModel(currentMethod);
var document = _testGeneratorService.OpenDocument(generatedTestModel);



Expand Down
10 changes: 6 additions & 4 deletions src/UnitTestGenerator/Dialogs/AddUnitTestDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
using MonoDevelop.Ide.Composition;
using UnitTestGenerator.Helpers;
using UnitTestGenerator.Services.Interfaces;

namespace UnitTestGenerator.Dialogs
{
Expand All @@ -18,12 +20,13 @@ public class AddUnitTestDialog : Dialog
readonly Entry _unitTestName;
readonly Button _confirm;
readonly MonoDevelop.Ide.Gui.Document _document;
readonly ITestGeneratorService _testGeneratorService;

public AddUnitTestDialog(MonoDevelop.Ide.Gui.Document document, MethodDeclarationSyntax currentMethod)
{
_testGeneratorService = CompositionManager.GetExportedValue<ITestGeneratorService>();
_currentMethod = currentMethod;
_document = document;
var config = new ConfigurationHelper().GetConfiguration();
WindowPosition = WindowPosition.CenterAlways;
Title = "Add Unit test method";

Expand Down Expand Up @@ -57,9 +60,8 @@ protected override void OnShown()
}

void Confirm_Clicked(object sender, EventArgs e)
{
var utHelper = new UnitTestHelper();
utHelper.GenerateUnitTest(_unitTestName.Text, _currentMethod, _document);
{
_testGeneratorService.GenerateUnitTest(_unitTestName.Text, _currentMethod, _document);
Hide();
}

Expand Down
11 changes: 6 additions & 5 deletions src/UnitTestGenerator/Dialogs/ConfigureUnitTestProjectDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
using System.Linq;
using Gtk;
using MonoDevelop.Ide;
using UnitTestGenerator.Helpers;
using MonoDevelop.Ide.Composition;
using UnitTestGenerator.Services.Interfaces;

namespace UnitTestGenerator.Dialogs
{
Expand All @@ -13,13 +14,14 @@ public class ConfigureUnitTestProjectDialog : Dialog
readonly Button _confirm;
readonly Label _selectedProjectLabel;
readonly List<string> _projects;
readonly IConfigurationService _configurationService;
string _selectedProject = "";

public ConfigureUnitTestProjectDialog()
{
WindowPosition = WindowPosition.CenterAlways;
Title = "Configure UnitTest project";

_configurationService = CompositionManager.GetExportedValue<IConfigurationService>();


//TreeView setup
Expand Down Expand Up @@ -90,10 +92,9 @@ void Confirm_Clicked(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(_selectedProject))
{
var configHelper = new ConfigurationHelper();
var config = configHelper.GetConfiguration();
var config = _configurationService.GetConfiguration();
config.UnitTestProjectName = _selectedProject;
configHelper.Save(config);
_configurationService.Save(config);
}
Hide();
}
Expand Down
2 changes: 1 addition & 1 deletion src/UnitTestGenerator/Properties/AddinInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
[assembly: Addin(
"UnitTestGenerator",
Namespace = "UnitTestGenerator",
Version = "0.1"
Version = "0.1.1"
)]

[assembly: AddinName("UnitTestGenerator")]
Expand Down
5 changes: 5 additions & 0 deletions src/UnitTestGenerator/Properties/Manifest.addin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
<CommandItem
id="UnitTestGenerator.Commands.GenerateUnitTestCommand" />
</Extension>
<!-- Add Command to (Old) TextEditor -->
<Extension
path="/MonoDevelop/SourceEditor2/ContextMenu/Editor">
<CommandItem
id="UnitTestGenerator.Commands.GenerateUnitTestCommand" />
</Extension>
<!-- MEF Catalog -->
<Extension path="/MonoDevelop/Ide/Composition">
<Assembly file="UnitTestGenerator.dll" />
</Extension>
</ExtensionModel>
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
using System.Composition;
using System.IO;
using System.Linq;
using MonoDevelop.Ide;
using MonoDevelop.Projects;
using Newtonsoft.Json;
using UnitTestGenerator.Models;
using UnitTestGenerator.Services.Interfaces;

namespace UnitTestGenerator.Helpers
namespace UnitTestGenerator.Services.Implementations
{
public class ConfigurationHelper
[Export(typeof(IConfigurationService))]
public class ConfigurationService : IConfigurationService
{
public Configuration GetConfiguration()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System.IO;
using System.Composition;
using System.IO;
using UnitTestGenerator.Services.Interfaces;

namespace UnitTestGenerator.Helpers
namespace UnitTestGenerator.Services.Implementations
{
public static class FileGenerator
[Export(typeof(IFileService))]
public class FileService : IFileService
{
public static void GenerateFile(string namespaceId, string classId, string filePath, string templateName)
public void GenerateFile(string namespaceId, string classId, string filePath, string templateName)
{
if (templateName == "XFUnitTestNUnit")
{
GenerateXFUnitTestNUnitFile(namespaceId, classId, filePath);
}
}

static void GenerateXFUnitTestNUnitFile(string namespaceId, string classId, string filePath)
public void GenerateXFUnitTestNUnitFile(string namespaceId, string classId, string filePath)
{
string[] lines = { "using System;",
"using Moq;",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Composition;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
Expand All @@ -10,17 +11,29 @@
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using MonoDevelop.Ide;
using MonoDevelop.Ide.Composition;
using MonoDevelop.Projects;
using UnitTestGenerator.Helpers;
using UnitTestGenerator.Models;
using UnitTestGenerator.Services.Interfaces;

namespace UnitTestGenerator.Helpers
namespace UnitTestGenerator.Services.Implementations
{
public class UnitTestHelper
[Export(typeof(ITestGeneratorService))]
public class TestGeneratorService : ITestGeneratorService
{
readonly IConfigurationService _configurationService;
readonly IFileService _fileService;
public TestGeneratorService()
{
_configurationService = CompositionManager.GetExportedValue<IConfigurationService>();
_fileService = CompositionManager.GetExportedValue<IFileService>();
}

public GeneratedTest CreateGeneratedTestModel(MethodDeclarationSyntax method)
{
//Get the configuration model
var config = new ConfigurationHelper().GetConfiguration();
var config = _configurationService.GetConfiguration();
var generatedTest = new GeneratedTest();

var projects = IdeApp.Workspace.GetAllProjects();
Expand Down Expand Up @@ -92,7 +105,7 @@ public MethodDeclarationSyntax GetActiveMethodDeclarationSyntax()

public MonoDevelop.Ide.Gui.Document OpenDocument(GeneratedTest generatedTestModel)
{
var config = new ConfigurationHelper().GetConfiguration();
var config = _configurationService.GetConfiguration();
var projects = IdeApp.Workspace.GetAllProjects();
var unitTestProject = projects.FirstOrDefault(p => p.Name == config.UnitTestProjectName);

Expand All @@ -104,7 +117,7 @@ public MonoDevelop.Ide.Gui.Document OpenDocument(GeneratedTest generatedTestMode
{
try
{
FileGenerator.GenerateFile(generatedTestModel.Namespace, generatedTestModel.Name, generatedTestModel.FilePath, "XFUnitTestNUnit");
_fileService.GenerateFile(generatedTestModel.Namespace, generatedTestModel.Name, generatedTestModel.FilePath, "XFUnitTestNUnit");
file = new ProjectFile(generatedTestModel.FilePath, BuildAction.Compile)
{
Visible = true,
Expand All @@ -123,7 +136,6 @@ public MonoDevelop.Ide.Gui.Document OpenDocument(GeneratedTest generatedTestMode

public void GenerateUnitTest(string unitTestName, MethodDeclarationSyntax currentMethod, MonoDevelop.Ide.Gui.Document document)
{
//TODO: Investigate maybe using DocumentEditor to do changes instead of updaing syntax root
var isTask = false;
if (currentMethod.ReturnType is GenericNameSyntax taskSomethingReturnType)
{
Expand Down Expand Up @@ -196,8 +208,6 @@ public MethodDeclarationSyntax GenerateUnitTestMethodDeclaration(string returnTy

public UsingDirectiveSyntax GenerateTaskUsingSyntax()
{
//return SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Threading.Tasks"))
// .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
var qualifiedName = SyntaxFactory.ParseName(" System.Threading.Tasks");
var usingSmnt = SyntaxFactory.UsingDirective(qualifiedName);
return usingSmnt;
Expand Down
11 changes: 11 additions & 0 deletions src/UnitTestGenerator/Services/Interfaces/IConfigurationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using UnitTestGenerator.Models;

namespace UnitTestGenerator.Services.Interfaces
{
public interface IConfigurationService
{
Configuration GetConfiguration();
void Save(Configuration configuration);
}
}
9 changes: 9 additions & 0 deletions src/UnitTestGenerator/Services/Interfaces/IFileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
namespace UnitTestGenerator.Services.Interfaces
{
public interface IFileService
{
void GenerateFile(string namespaceId, string classId, string filePath, string templateName);
void GenerateXFUnitTestNUnitFile(string namespaceId, string classId, string filePath);
}
}
17 changes: 17 additions & 0 deletions src/UnitTestGenerator/Services/Interfaces/ITestGeneratorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using UnitTestGenerator.Models;

namespace UnitTestGenerator.Services.Interfaces
{
public interface ITestGeneratorService
{
GeneratedTest CreateGeneratedTestModel(MethodDeclarationSyntax method);
MethodDeclarationSyntax GetActiveMethodDeclarationSyntax();
MonoDevelop.Ide.Gui.Document OpenDocument(GeneratedTest generatedTestModel);
void GenerateUnitTest(string unitTestName, MethodDeclarationSyntax currentMethod, MonoDevelop.Ide.Gui.Document document);
MethodDeclarationSyntax GenerateUnitTestMethodDeclaration(string returnTypeName, SyntaxTokenList modifiers, string methodName);
UsingDirectiveSyntax GenerateTaskUsingSyntax();
}
}
16 changes: 12 additions & 4 deletions src/UnitTestGenerator/UnitTestGenerator.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<ReleaseVersion>0.1.1</ReleaseVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoDevelop.Addins" Version="0.4.7" />
Expand All @@ -12,17 +14,23 @@
<Folder Include="Helpers\" />
<Folder Include="Models\" />
<Folder Include="Templates\" />
<Folder Include="Services\" />
<Folder Include="Services\Interfaces\" />
<Folder Include="Services\Implementations\" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Templates\XFUnitTestNUnit.cs" />
</ItemGroup>
<ItemGroup>
<AddinFile Include="Templates\XFUnitTestNUnit.cs" />
<AddinFile Include="Templates\XFUnitTestNUnit.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AddinFile>
<AddinFile Include="Templates\XFUnitTestNUnit.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AddinFile>
</ItemGroup>
<ItemGroup>
<None Update="Templates\XFUnitTestNUnit.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Remove="Templates\XFUnitTestNUnit.xml" />
</ItemGroup>
<ItemGroup>
<AddinReference Include="MonoDevelop.CSharpBinding" />
Expand Down