Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added first tests
  • Loading branch information
filipw committed Mar 28, 2015
commit 2d0a88b0249068c47b2eec04882ca30b549bfef4
2 changes: 1 addition & 1 deletion src/ScriptCs.Contracts/ScriptPackSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void AddScriptContextNamespace()
}
}

public IEnumerable<IScriptPackContext> Contexts
public virtual IEnumerable<IScriptPackContext> Contexts
{
get { return _contexts; }
}
Expand Down
8 changes: 5 additions & 3 deletions src/ScriptCs.Core/ReplCommands/ScriptPacksCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ public string CommandName
public object Execute(IRepl repl, object[] args)
{
var packContexts = repl.ScriptPackSession.Contexts;
var originalColor = _console.ForegroundColor;
_console.ForegroundColor = ConsoleColor.Yellow;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be moved down to inside the conditional, just before writing out the message to say there are no script packs. (See further comment below).


if (packContexts.IsNullOrEmpty())
{
_console.WriteLine("There are no script packs available in this REPL session");
_console.ForegroundColor = originalColor;
return null;
}

var importedNamespaces = repl.Namespaces.Union(repl.ScriptPackSession.Namespaces).ToArray();
var originalColor = _console.ForegroundColor;

foreach (var packContext in packContexts)
{
var contextType = packContext.GetType();

_console.ForegroundColor = ConsoleColor.Yellow;

_console.WriteLine(contextType.ToString());
_console.ForegroundColor = originalColor;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the yellow colour is set outside this loop, the first script pack name will be printed in yellow, but every subsequent one will be printed in green. The yellow should be set just before writing out the name of the script pack.

Also, since this a static state mutation, after setting the yellow, the WriteLine should really be a in a try with the resetting of the colour in a finally. Whilst contextType.ToString() is low risk, it's an interface and a module or whatnot could substitute in any bizarre exception throwing animal.

(Once I pull my finger out and produce a source package for https://github.com/colored-console/colored-console, we can use it to wrap up colour management for us.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, i think i messed it up at some point (look at the screenshot, it was ok)


Expand Down
48 changes: 48 additions & 0 deletions test/ScriptCs.Core.Tests/ReplCommands/ScriptPacksCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Linq;
using Moq;
using ScriptCs.Contracts;
using ScriptCs.ReplCommands;
using Xunit;

namespace ScriptCs.Tests.ReplCommands
{
public class ScriptPacksCommandTests
{
public class CommandNameProperty
{
[Fact]
public void ReturnsScriptPacks()
{
// act
var cmd = new ScriptPacksCommand(new Mock<IConsole>().Object);

// assert
Assert.Equal("scriptpacks", cmd.CommandName);
}
}

public class ExecuteMethod
{
[Fact]
public void ShouldExitIfThereAreNoScriptPacks()
{
// arrange
var console = new Mock<IConsole>();
var repl = new Mock<IRepl>();
var scriptPackSession = new Mock<ScriptPackSession>(Enumerable.Empty<IScriptPack>(), new string[0]);

scriptPackSession.Setup(x => x.Contexts).Returns((IEnumerable<IScriptPackContext>) null);
repl.Setup(x => x.ScriptPackSession).Returns(scriptPackSession.Object);

var cmd = new ScriptPacksCommand(console.Object);

// act
cmd.Execute(repl.Object, null);

// assert
console.Verify(x => x.WriteLine("There are no script packs available in this REPL session"));
}
}
}
}
1 change: 1 addition & 0 deletions test/ScriptCs.Core.Tests/ScriptCs.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="FileSystemMigratorTests.cs" />
<Compile Include="PackageReferenceTests.cs" />
<Compile Include="ReplCommands\HelpCommandTests.cs" />
<Compile Include="ReplCommands\ScriptPacksCommandTests.cs" />
<Compile Include="ScriptLibraryComposerTests.cs" />
<Compile Include="ReferenceLineProcessorTests.cs" />
<Compile Include="LoadLineProcessorTests.cs" />
Expand Down