Skip to content

Commit

Permalink
Fix SelectScoreScorable StackOverflowException
Browse files Browse the repository at this point in the history
  • Loading branch information
msft-shahins committed Jan 23, 2017
1 parent 6c34994 commit 1076168
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CSharp/Library/Microsoft.Bot.Builder/Scorables/Scorables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ public static IScorable<Item, Score> First<Item, Score>(this IEnumerable<IScorab
/// <summary>
/// Fold an enumeration of scorables using a score comparer.
/// </summary>
public static IScorable<Item, Score> Fold<Item, Score>(this IEnumerable<IScorable<Item, Score>> scorables, IComparer<Score> comparer, FoldScorable<Item, Score>.OnStageDelegate onStage = null)
public static IScorable<Item, Score> Fold<Item, Score>(this IEnumerable<IScorable<Item, Score>> scorables, IComparer<Score> comparer = null, FoldScorable<Item, Score>.OnStageDelegate onStage = null)
{
comparer = comparer ?? Comparer<Score>.Default;

IScorable<Item, Score> scorable;
if (TryReduce(ref scorables, out scorable))
{
Expand Down Expand Up @@ -313,7 +315,7 @@ public SelectScoreScorable(IScorable<Item, SourceScore> scorable, Func<Item, Sou

TargetScore IScorable<Item, TargetScore>.GetScore(Item item, object state)
{
IScorable<Item, SourceScore> source = this;
IScorable<Item, SourceScore> source = this.inner;
var sourceScore = source.GetScore(item, state);
var targetScore = this.selector(item, sourceScore);
return targetScore;
Expand Down
50 changes: 50 additions & 0 deletions CSharp/Tests/Microsoft.Bot.Builder.Tests/ScorableDialogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,56 @@ protected override Task DoneAsync(IActivity item, string state, CancellationToke
}
}

[TestClass]
public sealed class SelectScoreScorableTests : DialogTestBase
{
[TestMethod]
public async Task SelectScoreScorable_Scaling()
{
var echo = Chain.PostToChain().Select(msg => $"echo: {msg.Text}").PostToUser().Loop();

var scorable = new[]
{
Actions
.Bind(async (IBotToUser botToUser, CancellationToken token) =>
{
await botToUser.PostAsync("10");
})
.When(new Regex("10.*"))
.Normalize()
.SelectScore((r, s) => s * 0.9),

Actions
.Bind(async (IBotToUser botToUser, CancellationToken token) =>
{
await botToUser.PostAsync("1");
})
.When(new Regex("10.*"))
.Normalize()
.SelectScore((r, s) => s * 0.1)

}.Fold();

echo = echo.WithScorable(scorable);

using (var container = Build(Options.ResolveDialogFromContainer, scorable))
{
var builder = new ContainerBuilder();
builder
.RegisterInstance(echo)
.As<IDialog<object>>();
builder.Update(container);

await AssertScriptAsync(container,
"hello",
"echo: hello",
"10",
"10"
);
}
}
}

[TestClass]
public sealed class CalculatorScorableTests : DialogTestBase
{
Expand Down

0 comments on commit 1076168

Please sign in to comment.