Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better interception validation

  • Loading branch information...
commit 9f865a1028b66ca91baa78120061cc1522caa226 1 parent dbfa809
@adrianaisemberg adrianaisemberg authored
View
4 CLAP/Interception/IVerbInterceptor.cs
@@ -13,7 +13,7 @@ public interface IVerbInterceptor : IPreVerbInterceptor, IPostVerbInterceptor
/// </summary>
public interface IPreVerbInterceptor
{
- void Intercept(PreVerbExecutionContext context);
+ void BeforeVerbExecution(PreVerbExecutionContext context);
}
/// <summary>
@@ -21,6 +21,6 @@ public interface IPreVerbInterceptor
/// </summary>
public interface IPostVerbInterceptor
{
- void Intercept(PostVerbExecutionContext context);
+ void AfterVerbExecution(PostVerbExecutionContext context);
}
}
View
101 CLAP/ParserRunner.cs
@@ -254,7 +254,7 @@ private void RunInternal(string[] args, object obj)
var interceptor = (IPostVerbInterceptor)Activator.CreateInstance(interception.InterceptorType);
- interceptor.Intercept(postVerbExecutionContext);
+ interceptor.AfterVerbExecution(postVerbExecutionContext);
}
}
}
@@ -297,7 +297,7 @@ private void RunInternal(string[] args, object obj)
var interceptor = (IPreVerbInterceptor)Activator.CreateInstance(interception.InterceptorType);
- interceptor.Intercept(preVerbExecutionContext);
+ interceptor.BeforeVerbExecution(preVerbExecutionContext);
}
}
}
@@ -353,21 +353,10 @@ internal static void Validate(Type type)
//
ValidateDefinedEmptyHandlers(type);
- // no more that one pre/post interception methods
+ // validate pre/post interceptors
//
- var preInterceptionMethods = type.GetMethodsWith<PreVerbExecutionAttribute>();
-
- if (preInterceptionMethods.Count() > 1)
- {
- throw new MoreThanOnePreVerbInterceptorException();
- }
-
- var postInterceptionMethods = type.GetMethodsWith<PostVerbExecutionAttribute>();
-
- if (postInterceptionMethods.Count() > 1)
- {
- throw new MoreThanOnePostVerbInterceptorException();
- }
+ ValidateDefinedPreInterceptors(type);
+ ValidateDefinedPostInterceptors(type);
}
private static void ValidateDefinedEmptyHandlers(Type type)
@@ -839,6 +828,86 @@ private static void ValidateDefinedErrorHandlers(Type type)
}
}
+ private static void ValidateDefinedPreInterceptors(Type type)
+ {
+ // no more that one pre/post interception methods
+ //
+ var preInterceptionMethods = type.GetMethodsWith<PreVerbExecutionAttribute>();
+
+ var preInterceptionMethodsCount = preInterceptionMethods.Count();
+
+ if (preInterceptionMethodsCount == 0)
+ {
+ return;
+ }
+ else if (preInterceptionMethodsCount > 1)
+ {
+ throw new MoreThanOnePreVerbInterceptorException();
+ }
+
+ // there is only one defined interceptor
+ //
+ var method = preInterceptionMethods.First();
+
+ var parameters = method.GetParameters();
+
+ if (parameters.Length > 1)
+ {
+ throw new ArgumentMismatchException(
+ "Method '{0}' is marked as [PreVerbExecution] so it should have a single parameter of type CLAP.PreVerbExecutionContext".FormatWith(method));
+ }
+ else
+ {
+ var parameter = parameters.First();
+
+ if (parameter.ParameterType != typeof(PreVerbExecutionContext))
+ {
+ throw new ArgumentMismatchException(
+ "Method '{0}' is marked as [PreVerbExecution] so it should have a single parameter of type CLAP.PreVerbExecutionContext".FormatWith(method));
+ }
+ }
+ }
+
+ private static void ValidateDefinedPostInterceptors(Type type)
+ {
+ // no more that one pre/post interception methods
+ //
+ var postInterceptionMethods = type.GetMethodsWith<PostVerbExecutionAttribute>();
+
+ var postInterceptionMethodsCount = postInterceptionMethods.Count();
+
+ if (postInterceptionMethodsCount == 0)
+ {
+ return;
+ }
+ else if (postInterceptionMethodsCount > 1)
+ {
+ throw new MoreThanOnePostVerbInterceptorException();
+ }
+
+ // there is only one defined interceptor
+ //
+ var method = postInterceptionMethods.First();
+
+ var parameters = method.GetParameters();
+
+ if (parameters.Length > 1)
+ {
+ throw new ArgumentMismatchException(
+ "Method '{0}' is marked as [PostVerbExecution] so it should have a single parameter of type CLAP.PostVerbExecutionContext".FormatWith(method));
+ }
+ else
+ {
+ var parameter = parameters.First();
+
+ if (parameter.ParameterType != typeof(PostVerbExecutionContext))
+ {
+ throw new ArgumentMismatchException(
+ "Method '{0}' is marked as [PostVerbExecution] so it should have a single parameter of type CLAP.PostVerbExecutionContext".FormatWith(method));
+ }
+ }
+ }
+
private Method GetDefaultVerb()
{
var verbs = GetVerbs();
View
27 ConsoleTest/Program.cs
@@ -18,6 +18,33 @@ static void Main(string[] args)
p.Register.ParameterHandler<List<string>>("blonda,blnd", list => { });
p.Register.ParameterHandler<List<string>>("kramer,krmr,k", list => { }, "a sample description");
+
+ p.Register.PreVerbInterceptor(context =>
+ {
+ // Log the method name
+ //
+ //Log.Write("START: " + context.Method.MethodInfo.Name);
+
+ // Add the current time to the context
+ //
+ context.UserContext.Add("START", DateTime.Now);
+ });
+
+ p.Register.PostVerbInterceptor(context =>
+ {
+ // Log the method name
+ //
+ //Log.Write("END: " + context.Method.MethodInfo.Name);
+
+ // Take the "START" user context value
+ //
+ var start = (DateTime)context.UserContext["START"];
+
+ // Log the time it took to execute the verb
+ //
+ //Log.Write(DateTime.Now.Subtract(start).ToString());
+ });
+
p.RunStatic(args);
}
}
View
4 Tests/Samples.cs
@@ -954,14 +954,14 @@ class Inter : IVerbInterceptor
public PreVerbExecutionContext PreContext;
public PostVerbExecutionContext PostContext;
- public void Intercept(PreVerbExecutionContext context)
+ public void BeforeVerbExecution(PreVerbExecutionContext context)
{
PreContext = context;
context.UserContext.Add("a", 54);
}
- public void Intercept(PostVerbExecutionContext context)
+ public void AfterVerbExecution(PostVerbExecutionContext context)
{
PostContext = context;
}
Please sign in to comment.
Something went wrong with that request. Please try again.