Skip to content

Commit

Permalink
interception cover
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianaisemberg committed Oct 27, 2011
1 parent 973c170 commit d17ae0b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
6 changes: 3 additions & 3 deletions CLAP/Parser.cs
Expand Up @@ -1123,9 +1123,9 @@ private bool HandleError(Exception ex, object target)
{ {
if (m_registration.RegisteredErrorHandler != null) if (m_registration.RegisteredErrorHandler != null)
{ {
m_registration.RegisteredErrorHandler(ex); var rethrow = m_registration.RegisteredErrorHandler(ex);
#warning TODO: return bool from the error handler
return m_registration.RegisteredErrorHandlerRethrow; return rethrow;
} }
else else
{ {
Expand Down
15 changes: 8 additions & 7 deletions CLAP/ParserRegistration.cs
Expand Up @@ -16,14 +16,11 @@ public sealed class ParserRegistration
#region Properties #region Properties


internal Dictionary<string, GlobalParameterHandler> RegisteredGlobalHandlers { get; private set; } internal Dictionary<string, GlobalParameterHandler> RegisteredGlobalHandlers { get; private set; }

#warning TODO: only one help handler
internal Dictionary<string, Action<string>> RegisteredHelpHandlers { get; private set; } internal Dictionary<string, Action<string>> RegisteredHelpHandlers { get; private set; }
internal Action RegisteredEmptyHandler { get; private set; } internal Action RegisteredEmptyHandler { get; private set; }
internal Action<Exception> RegisteredErrorHandler { get; private set; } internal Func<Exception, bool> RegisteredErrorHandler { get; private set; }
internal Action<PreVerbExecutionContext> RegisteredPreVerbInterceptor { get; private set; } internal Action<PreVerbExecutionContext> RegisteredPreVerbInterceptor { get; private set; }
internal Action<PostVerbExecutionContext> RegisteredPostVerbInterceptor { get; private set; } internal Action<PostVerbExecutionContext> RegisteredPostVerbInterceptor { get; private set; }
internal bool RegisteredErrorHandlerRethrow { get; private set; }


#endregion Properties #endregion Properties


Expand Down Expand Up @@ -74,18 +71,22 @@ public void EmptyHandler(Action handler)


public void ErrorHandler(Action<Exception> handler) public void ErrorHandler(Action<Exception> handler)
{ {
ErrorHandler(handler, false); ErrorHandler(new Func<Exception, bool>(ex =>
{
handler(ex);
return false;
}));
} }


public void ErrorHandler(Action<Exception> handler, bool rethrow) public void ErrorHandler(Func<Exception, bool> handler)
{ {
if (RegisteredErrorHandler != null) if (RegisteredErrorHandler != null)
{ {
throw new MoreThanOneErrorHandlerException(); throw new MoreThanOneErrorHandlerException();
} }


RegisteredErrorHandler = handler; RegisteredErrorHandler = handler;
RegisteredErrorHandlerRethrow = rethrow;
} }


public void PreVerbInterceptor(Action<PreVerbExecutionContext> interceptor) public void PreVerbInterceptor(Action<PreVerbExecutionContext> interceptor)
Expand Down
61 changes: 57 additions & 4 deletions Tests/Tests.cs
Expand Up @@ -1279,7 +1279,9 @@ public void Execute_HandleError_Registered_NoRethrow()
p.Register.ErrorHandler(ex => p.Register.ErrorHandler(ex =>
{ {
handled = true; handled = true;
}, false);
return false;
});


p.Run(new string[] { }); p.Run(new string[] { });


Expand Down Expand Up @@ -1311,7 +1313,9 @@ public void Execute_HandleError_Registered_Rethrow()
p.Register.ErrorHandler(ex => p.Register.ErrorHandler(ex =>
{ {
handled = true; handled = true;
}, true);
return true;
});


try try
{ {
Expand All @@ -1338,7 +1342,7 @@ public void Execute_HandleError_Registered_UnhandledParameters()
p.Register.ErrorHandler(ex => p.Register.ErrorHandler(ex =>
{ {
handled = true; handled = true;
}, false); });


p.Run(new[] p.Run(new[]
{ {
Expand All @@ -1363,7 +1367,7 @@ public void Execute_HandleError_Registered_ValidationError()
p.Register.ErrorHandler(ex => p.Register.ErrorHandler(ex =>
{ {
handled = true; handled = true;
}, false); });


p.Run(new[] { "morethan5", "/n=1" }, sample); p.Run(new[] { "morethan5", "/n=1" }, sample);


Expand Down Expand Up @@ -1948,13 +1952,33 @@ public void Interception_MoreThanOnePre_Exception()
Parser.Run<Sample_45>(new[] { "foo" }); Parser.Run<Sample_45>(new[] { "foo" });
} }


[Test]
[ExpectedException(typeof(MoreThanOnePreVerbInterceptorException))]
public void Interception_RegisterMoreThanOnePre_Exception()
{
var p = new Parser<Sample_02>();

p.Register.PreVerbInterceptor(c => { });
p.Register.PreVerbInterceptor(c => { });
}

[Test] [Test]
[ExpectedException(typeof(MoreThanOnePostVerbInterceptorException))] [ExpectedException(typeof(MoreThanOnePostVerbInterceptorException))]
public void Interception_MoreThanOnePost_Exception() public void Interception_MoreThanOnePost_Exception()
{ {
Parser.Run<Sample_46>(new[] { "foo" }); Parser.Run<Sample_46>(new[] { "foo" });
} }


[Test]
[ExpectedException(typeof(MoreThanOnePostVerbInterceptorException))]
public void Interception_RegisterMoreThanOnePost_Exception()
{
var p = new Parser<Sample_02>();

p.Register.PostVerbInterceptor(c => { });
p.Register.PostVerbInterceptor(c => { });
}

[Test] [Test]
public void Interception_ExecutionFailed_PostCalled() public void Interception_ExecutionFailed_PostCalled()
{ {
Expand Down Expand Up @@ -2000,5 +2024,34 @@ public void Interception_ExecutionFailed_PostCalled_WithErrorHandler_ReThrow()


Assert.IsTrue(s.Context.Failed); Assert.IsTrue(s.Context.Failed);
} }

[Test]
public void Interception_Registered_Called()
{
var printer = new Printer();
var sample = new Sample_02 { Printer = printer };

var p = new Parser<Sample_02>();

var preInterceptorCalled = false;
var postInterceptorCalled = false;

p.Register.PreVerbInterceptor(c => preInterceptorCalled = true);
p.Register.PostVerbInterceptor(c => postInterceptorCalled = true);

p.Run(new[]
{
"print",
"/c=5",
"/msg=test",
"/prefix=hello_",
}, sample);

Assert.AreEqual(5, printer.PrintedTexts.Count);
Assert.IsTrue(printer.PrintedTexts.All(t => t.Equals("hello_test")));

Assert.IsTrue(preInterceptorCalled);
Assert.IsTrue(postInterceptorCalled);
}
} }
} }

0 comments on commit d17ae0b

Please sign in to comment.