public static bool Matches(Type controller, string methodName, string statement) { var method = GetMethod(controller, methodName); var statementHandler = new ReflectionStatementDefinition(method, controller); return(statementHandler.Matches(new MatchingContext() { Statement = statement, }).Success); }
public bool ItMatchesStatements(string statment, string statement) { var m = GetMethod(statment); var load = new ReflectionStatementDefinition(m, typeof(TestStatements)); var match = load.Matches(new MatchingContext() { Statement = statement }); return(match.Let(x => x.Success, false)); }
public bool ItExecutesStatements(string statement, string rawStatement, int scope) { var load = new ReflectionStatementDefinition(GetMethod(statement), typeof(TestStatements)); var linked = load.Link(rawStatement); Assert.IsNotNull(linked); return(linked.Execute(new InvokationContext() { Scope = scope, Context = new ContextBundle(), })); }
public static InvokationResult Invoke <T>(Type controller, string methodName, T scope, string statement, ContextBundle context = null, Func <object, bool> proceed = null) { if (proceed == null) { proceed = o => false; } context = context ?? new ContextBundle(); var method = GetMethod(controller, methodName); var statementHandler = new ReflectionStatementDefinition(method, controller); var invokationResult = new InvokationResult() { Context = context, Statement = statement, MatchingCriteria = statementHandler.GetMatchingCriteria(), Attachments = new ContextBundle() }; invokationResult.Match = statementHandler.Matches(new MatchingContext() { Statement = statement, ScopeType = typeof(T) }); if (!invokationResult.Matched) { return(invokationResult); } var link = statementHandler.Link(statement); if (link == null) { throw new Exception("If matched, should have linked, but didn't"); } invokationResult.Result = link.Execute(new InvokationContext() { Context = invokationResult.Context, Attachments = invokationResult.Attachments, Scope = scope, Proceed = proceed }); return(invokationResult); }
public static StatementDocumentation FromReflection(ReflectionStatementDefinition reflectionStatementDefinition, ICommentDocumentation commentDocumentation) { var methodDocs = commentDocumentation.ForMethod(reflectionStatementDefinition.MethodInfo); return(new StatementDocumentation() { Parameters = (reflectionStatementDefinition.Parameters ?? Enumerable.Empty <StatementParameter>()) .Select(p => new ParameterDocumentation() { Name = p.Name, Type = p.Type, Description = methodDocs.With(d => d.Parameters.With(p.Name)), TransformsTo = (p as ProceedParameter).With(z => z.TransformsTo) }) .ToList(), Description = methodDocs.With(x => x.Summary), ScopeType = reflectionStatementDefinition.ScopeType, Title = ExtractTitle(reflectionStatementDefinition) }); }
[TestCase("R4", "I haz 10 cheeseburgers", "1", 1, ExpectedException = typeof(CannotLinkStatementException), TestName = "It errors when cannot make parameter")] //too many public bool ItExecutesAggregates(string statement, string rawStatement, string strScope, int expectedCalls) { var scope = strScope.Split(',').Select(int.Parse); var load = new ReflectionStatementDefinition(GetAggregateMethod(statement), typeof(TestAggregates)); var link = load.Link(rawStatement); Assert.IsNotNull(link); var calls = 0; var result = link.Execute(new InvokationContext() { Scope = scope, Proceed = o => { calls++; return(((int)o) % 2 == 0); } }); Assert.AreEqual(expectedCalls, calls); return(result); }
private static string ExtractTitle(ReflectionStatementDefinition reflectionStatementDefinition) { //Use attribute if present var titleAttr = reflectionStatementDefinition .MethodInfo.GetCustomAttributes(typeof(TitleAttribute), true) .OfType <TitleAttribute>() .Select(x => x.Title) .FirstOrDefault(x => !string.IsNullOrEmpty(x)); if (titleAttr != null) { return(titleAttr); } var expression = reflectionStatementDefinition.MatchingCriteria.ToString(); if (Regex.IsMatch(expression, @"\(\?")) { return(reflectionStatementDefinition.MethodInfo.Name); //Compilcated regexes just use method name } return(ParseTitle(expression, reflectionStatementDefinition.Parameters)); }
public void ItDoesNotReUseControllers() { var load = new ReflectionStatementDefinition(GetMethod("R8"), typeof(TestStatements)); var linked = load.Link("R8"); var att1 = new ContextBundle(); var att2 = new ContextBundle(); linked.Execute(new InvokationContext() { Scope = 9, Attachments = att1 }); linked.Execute(new InvokationContext() { Scope = 9, Attachments = att2 }); var ctr1 = att1.Get <TestStatements>("controller"); var ctr2 = att2.Get <TestStatements>("controller"); Assert.AreNotSame(ctr1, ctr2); }
public string ItNormalizesMethodNames(string name) { return(ReflectionStatementDefinition.NormalizeMethodName(name)); }