Skip to content

Commit

Permalink
Simplified ExceptionWalker (+Tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed May 31, 2012
1 parent 216120c commit b03aa12
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/shell.me/ShellMe.Console.Tests/CommandInitTests.cs
Expand Up @@ -75,5 +75,18 @@ public void RunsTwoTimesInteractiveAndThenIgnoresLastCommandBecauseOfPreviousExi
Assert.AreEqual("Enter commands or type exit to close", console.OutputQueue[3]);
Assert.AreEqual(4, console.OutputQueue.Count);
}

[Test]
public void PrintsExceptionsToTheConsole()
{
var console = new TestConsole(new List<string>());
var commandFactory = new CommandFactory(new[] { new ExceptionCommand() });
var commandLoop = new CommandLoop(console, commandFactory);
commandLoop.Start(new[] { "--RaiseException" });

Assert.AreEqual("Unexpected error happended while proceeding the command: RaiseException", console.OutputQueue[0]);
Assert.AreEqual("Foo", console.OutputQueue[1]);
Assert.AreEqual("Bar", console.OutputQueue[2]);
}
}
}
19 changes: 19 additions & 0 deletions src/shell.me/ShellMe.Console.Tests/ExceptionCommand.cs
@@ -0,0 +1,19 @@
using System;
using ShellMe.Console.CommandHandling;
using ShellMe.Console.Configuration;

namespace ShellMe.Console.Tests
{
class ExceptionCommand : BaseCommand
{
public override string Name
{
get { return "RaiseException"; }
}

public override void Run()
{
throw new NotImplementedException("Foo", new Exception("Bar"));
}
}
}
Expand Up @@ -44,6 +44,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CommandInitTests.cs" />
<Compile Include="ExceptionCommand.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestCommand.cs" />
</ItemGroup>
Expand Down
12 changes: 4 additions & 8 deletions src/shell.me/ShellMe.Console/ExceptionWalker.cs
Expand Up @@ -5,7 +5,7 @@ namespace ShellMe.Console
{
class ExceptionWalker
{
private readonly Exception _exception;
private Exception _exception;

public ExceptionWalker(Exception exception)
{
Expand All @@ -14,14 +14,10 @@ public ExceptionWalker(Exception exception)

public IEnumerable<string> GetExceptionMessages()
{
Func<Exception, Exception> getInnerException = (ex) => ex.InnerException;
yield return _exception.Message;
Exception innerException = getInnerException(_exception);
while (innerException != null)
while (_exception != null)
{
yield return innerException.Message;
if (innerException != null)
innerException = getInnerException(innerException);
yield return _exception.Message;
_exception = _exception.InnerException;
}
}
}
Expand Down

0 comments on commit b03aa12

Please sign in to comment.