Permalink
Browse files

interception cover

  • Loading branch information...
1 parent 973c170 commit d17ae0b33109c311d8dd7ac1929c607709fb3b60 @adrianaisemberg adrianaisemberg committed Oct 27, 2011
Showing with 68 additions and 14 deletions.
  1. +3 −3 CLAP/Parser.cs
  2. +8 −7 CLAP/ParserRegistration.cs
  3. +57 −4 Tests/Tests.cs
View
@@ -1123,9 +1123,9 @@ private bool HandleError(Exception ex, object target)
{
if (m_registration.RegisteredErrorHandler != null)
{
- m_registration.RegisteredErrorHandler(ex);
-#warning TODO: return bool from the error handler
- return m_registration.RegisteredErrorHandlerRethrow;
+ var rethrow = m_registration.RegisteredErrorHandler(ex);
+
+ return rethrow;
}
else
{
View
@@ -16,14 +16,11 @@ public sealed class ParserRegistration
#region Properties
internal Dictionary<string, GlobalParameterHandler> RegisteredGlobalHandlers { get; private set; }
-
-#warning TODO: only one help handler
internal Dictionary<string, Action<string>> RegisteredHelpHandlers { 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<PostVerbExecutionContext> RegisteredPostVerbInterceptor { get; private set; }
- internal bool RegisteredErrorHandlerRethrow { get; private set; }
#endregion Properties
@@ -74,18 +71,22 @@ public void EmptyHandler(Action 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)
{
throw new MoreThanOneErrorHandlerException();
}
RegisteredErrorHandler = handler;
- RegisteredErrorHandlerRethrow = rethrow;
}
public void PreVerbInterceptor(Action<PreVerbExecutionContext> interceptor)
View
@@ -1279,7 +1279,9 @@ public void Execute_HandleError_Registered_NoRethrow()
p.Register.ErrorHandler(ex =>
{
handled = true;
- }, false);
+
+ return false;
+ });
p.Run(new string[] { });
@@ -1311,7 +1313,9 @@ public void Execute_HandleError_Registered_Rethrow()
p.Register.ErrorHandler(ex =>
{
handled = true;
- }, true);
+
+ return true;
+ });
try
{
@@ -1338,7 +1342,7 @@ public void Execute_HandleError_Registered_UnhandledParameters()
p.Register.ErrorHandler(ex =>
{
handled = true;
- }, false);
+ });
p.Run(new[]
{
@@ -1363,7 +1367,7 @@ public void Execute_HandleError_Registered_ValidationError()
p.Register.ErrorHandler(ex =>
{
handled = true;
- }, false);
+ });
p.Run(new[] { "morethan5", "/n=1" }, sample);
@@ -1949,13 +1953,33 @@ public void Interception_MoreThanOnePre_Exception()
}
[Test]
+ [ExpectedException(typeof(MoreThanOnePreVerbInterceptorException))]
+ public void Interception_RegisterMoreThanOnePre_Exception()
+ {
+ var p = new Parser<Sample_02>();
+
+ p.Register.PreVerbInterceptor(c => { });
+ p.Register.PreVerbInterceptor(c => { });
+ }
+
+ [Test]
[ExpectedException(typeof(MoreThanOnePostVerbInterceptorException))]
public void Interception_MoreThanOnePost_Exception()
{
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]
public void Interception_ExecutionFailed_PostCalled()
{
var s = new Sample_47();
@@ -2000,5 +2024,34 @@ public void Interception_ExecutionFailed_PostCalled_WithErrorHandler_ReThrow()
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.