Skip to content

Commit

Permalink
Merge pull request #95 from Pondidum/commandline-di
Browse files Browse the repository at this point in the history
CommandLine DI
  • Loading branch information
jeremydmiller committed Dec 2, 2015
2 parents 44ec1e1 + 7420fb4 commit 8f3ec2b
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
51 changes: 51 additions & 0 deletions src/FubuCore.Testing/CommandLine/ActivatorCommandCreatorTests.cs
@@ -0,0 +1,51 @@
using System;
using FubuCore.CommandLine;
using FubuTestingSupport;
using NUnit.Framework;

namespace FubuCore.Testing.CommandLine
{
public class ActivatorCommandCreatorTests
{
[Test]
public void builds_an_instance_with_no_ctor_parameters()
{
var creator = new ActivatorCommandCreator();
var instance = creator.Create(typeof (NoParamsCommand));

instance.ShouldBeOfType<NoParamsCommand>();
}

[Test]
public void throws_if_the_ctor_has_parameters()
{
var creator = new ActivatorCommandCreator();

Assert.Throws<MissingMethodException>(() => creator.Create(typeof (ParamsCommand)));
}

public class FakeModel
{
}

private class NoParamsCommand : FubuCommand<FakeModel>
{
public override bool Execute(FakeModel input)
{
throw new System.NotImplementedException();
}
}

private class ParamsCommand : FubuCommand<FakeModel>
{
public ParamsCommand(string testArgument)
{
}

public override bool Execute(FakeModel input)
{
throw new System.NotImplementedException();
}
}
}
}
18 changes: 17 additions & 1 deletion src/FubuCore.Testing/CommandLine/CommandFactoryTester.cs
Expand Up @@ -3,6 +3,7 @@
using FubuCore.CommandLine;
using FubuTestingSupport;
using NUnit.Framework;
using Rhino.Mocks;

namespace FubuCore.Testing.CommandLine
{
Expand Down Expand Up @@ -205,8 +206,23 @@ public void build_command_with_multiargs()
input.ThirdFlag.ShouldBeTrue();
}

}
[Test]
public void build_command_with_a_replacement_commandcreator()
{
var creator = MockRepository.GenerateMock<ICommandCreator>();
creator.Stub(c => c.Create(typeof (MyCommand)))
.Repeat.Once()
.Return(new MyCommand());

var factory = new CommandFactory(creator);
factory.RegisterCommands(GetType().Assembly);

var cmd = factory.Build("my");

creator.VerifyAllExpectations();
cmd.ShouldBeOfType<MyCommand>();
}
}

public class RebuildAuthorizationCommand : FubuCommand<MyCommandInput>
{
Expand Down
1 change: 1 addition & 0 deletions src/FubuCore.Testing/FubuCore.Testing.csproj
Expand Up @@ -81,6 +81,7 @@
<Compile Include="Binding\ConversionPropertyBinderTester.cs" />
<Compile Include="Binding\ConverterFamilyTester.cs" />
<Compile Include="Binding\ConvertProblemTester.cs" />
<Compile Include="CommandLine\ActivatorCommandCreatorTests.cs" />
<Compile Include="CommandLine\DumpCommandSmokeTester.cs" />
<Compile Include="Csv\ColumnDefinitionTester.cs" />
<Compile Include="Csv\ColumnExpressionTester.cs" />
Expand Down
12 changes: 12 additions & 0 deletions src/FubuCore/CommandLine/ActivatorCommandCreator.cs
@@ -0,0 +1,12 @@
using System;

namespace FubuCore.CommandLine
{
public class ActivatorCommandCreator : ICommandCreator
{
public IFubuCommand Create(Type commandType)
{
return Activator.CreateInstance(commandType).As<IFubuCommand>();
}
}
}
17 changes: 13 additions & 4 deletions src/FubuCore/CommandLine/CommandFactory.cs
Expand Up @@ -12,8 +12,19 @@ public class CommandFactory : ICommandFactory
{
private static readonly string[] _helpCommands = new []{"help", "?"};
private readonly Cache<string, Type> _commandTypes = new Cache<string, Type>();
private readonly ICommandCreator _commandCreator;
private string _appName;

public CommandFactory()
{
_commandCreator = new ActivatorCommandCreator();
}

public CommandFactory(ICommandCreator creator)
{
_commandCreator = creator;
}

public CommandRun BuildRun(string commandLine)
{
var args = StringTokenizer.Tokenize(commandLine);
Expand Down Expand Up @@ -125,15 +136,13 @@ public void RegisterCommands(Assembly assembly)

public IEnumerable<IFubuCommand> BuildAllCommands()
{
return _commandTypes.Select(x => {
return Activator.CreateInstance(x).As<IFubuCommand>();
});
return _commandTypes.Select(x => _commandCreator.Create(x));
}


public IFubuCommand Build(string commandName)
{
return (IFubuCommand) Activator.CreateInstance(_commandTypes[commandName.ToLower()]);
return _commandCreator.Create(_commandTypes[commandName.ToLower()]);
}


Expand Down
9 changes: 9 additions & 0 deletions src/FubuCore/CommandLine/ICommandCreator.cs
@@ -0,0 +1,9 @@
using System;

namespace FubuCore.CommandLine
{
public interface ICommandCreator
{
IFubuCommand Create(Type commandType);
}
}
2 changes: 2 additions & 0 deletions src/FubuCore/FubuCore.csproj
Expand Up @@ -113,10 +113,12 @@
<Compile Include="Binding\Values\NamedKeyValues.cs" />
<Compile Include="Binding\Values\ValueReport.cs" />
<Compile Include="Binding\Values\ValueReportBase.cs" />
<Compile Include="CommandLine\ActivatorCommandCreator.cs" />
<Compile Include="CommandLine\ArgPreprocessor.cs" />
<Compile Include="CommandLine\DumpUsagesCommand.cs" />
<Compile Include="CommandLine\EnumerableFlag.cs" />
<Compile Include="CommandLine\FlagAliases.cs" />
<Compile Include="CommandLine\ICommandCreator.cs" />
<Compile Include="CommandLine\InvalidUsageException.cs" />
<Compile Include="CommandLine\UsageReport.cs" />
<Compile Include="Configuration\SettingsData.cs" />
Expand Down

0 comments on commit 8f3ec2b

Please sign in to comment.