public static void TestReduceHelper <Item, Score>(params IScorable <Item, Score>[] test) { IEnumerable <IScorable <Item, Score> > scorables = test; IScorable <Item, Score> scorable; var success = Scorable.TryReduce(ref scorables, out scorable); var filtered = test.Where(Scorable.Keep).ToArray(); switch (filtered.Length) { case 0: Assert.IsTrue(success); Assert.IsInstanceOfType(scorable, typeof(NullScorable <Item, Score>)); break; case 1: Assert.IsTrue(success); Assert.AreEqual(1, filtered.Length); Assert.AreEqual(filtered[0], scorable); break; default: Assert.IsFalse(success); Assert.IsNull(scorable); CollectionAssert.AreEqual(filtered, (ICollection)scorables); break; } }
IScorable <Item, Score> IScorableFactory <Item, Score> .ScorableFor(IEnumerable <MethodInfo> methods) { var levels = from method in methods // note, this is non-deterministic across executions, which seems lame let defaultOrder = method.Name.GetHashCode() let attributes = InheritedAttributes.For <ScorableGroupAttribute>(method) let orders = attributes.Select(order => order.Order).DefaultIfEmpty(defaultOrder) from order in orders group method by order into g orderby g.Key select g; var scorables = from level in levels from factory in this.factories let scorable = factory.ScorableFor(level) where Scorable.Keep(scorable) select scorable; var winner = scorables.ToArray().Fold(this.comparer, this.onStage); return(winner); }
public async Task Calculate_Script_Scorable_As_Action() { var echo = Chain.PostToChain().Select(msg => $"echo: {msg.Text}").PostToUser().Loop(); var scorable = Scorable .Bind((string expression, IBotData data, IDialogStack stack, IMessageActivity activity, CancellationToken token) => { var dialog = new CalculatorDialog(); activity.Text = expression; return(stack.InterruptAsync(dialog, activity, token)); }).When(new Regex(@".*calculate\s*(?<expression>.*)")).Normalize(); echo = echo.WithScorable(scorable); using (var container = Build(Options.ResolveDialogFromContainer)) { var builder = new ContainerBuilder(); builder .RegisterInstance(echo) .As <IDialog <object> >(); builder.Update(container); await AssertScriptAsync(container, "hello", "echo: hello", "calculate 2 + 3", "5", "world", "echo: world", "2 + 3", "echo: 2 + 3", "calculate 4 / 2", "2" ); } }