public void NumberPromptWithNullIdShouldFail()
        {
            var nullId = "";

            nullId = null;
            var numberPrompt = new NumberPrompt <int>(nullId);
        }
Esempio n. 2
0
        public void NumberPromptWithUnsupportedTypeShouldFail()
        {
            var nullId = string.Empty;

            nullId = null;
            var numberPrompt = new NumberPrompt <short>("prompt");
        }
        public async Task DecimalNumberPrompt()
        {
            ConversationState convoState = new ConversationState(new MemoryStorage());
            var testProperty             = convoState.CreateProperty("test", () => new Dictionary <string, object>());

            TestAdapter adapter = new TestAdapter()
                                  .Use(convoState);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var state  = await testProperty.GetAsync(turnContext);
                var prompt = new NumberPrompt <decimal>(Culture.English);

                var dialogCompletion = await prompt.ContinueAsync(turnContext, state);
                if (!dialogCompletion.IsActive && !dialogCompletion.IsCompleted)
                {
                    await prompt.BeginAsync(turnContext, state, new PromptOptions {
                        PromptString = "Enter a number."
                    });
                }
                else if (dialogCompletion.IsCompleted)
                {
                    var numberResult = (NumberResult <decimal>)dialogCompletion.Result;
                    await turnContext.SendActivityAsync($"Bot received the number '{numberResult.Value}'.");
                }
            })
            .Send("hello")
            .AssertReply("Enter a number.")
            .Send("3.14")
            .AssertReply("Bot received the number '3.14'.")
            .StartTestAsync();
        }
        public void SetDialogConfiguration(DialogSet dialogSet)
        {
            AddPictureOfTheDayDialog(dialogSet);
            AddMarsRoverDataDialog(dialogSet);
            AddAsteroidDataDialog(dialogSet);

            //mars rover date prompt
            DateTimePrompt earthDatePrompt = new DateTimePrompt("EarthDatePrompt", DateValidatorAsync);

            dialogSet.Add(earthDatePrompt);

            NumberPrompt <int> numberOfPicturesPrompt = new NumberPrompt <int>("NumberOfPicturesPrompt", NumberValidatorAsync);

            dialogSet.Add(numberOfPicturesPrompt);

            //asteroid start && end date prompts
            DateTimePrompt startDatePrompt = new DateTimePrompt("StartDatePrompt", DateValidatorAsync);

            dialogSet.Add(startDatePrompt);
            DateTimePrompt endDatePrompt = new DateTimePrompt("EndDatePrompt", DateValidatorAsync);

            dialogSet.Add(endDatePrompt);

            //add choice options dialog
            var choicePrompt = new ChoicePrompt("GetChoices");

            dialogSet.Add(choicePrompt);

            //add generic confirmation dialog
            var confirmationPrompt = new ConfirmPrompt("ConfirmationDialog");

            dialogSet.Add(confirmationPrompt);
        }
Esempio n. 5
0
        public async Task NumberPrompt()
        {
            TestAdapter adapter = new TestAdapter()
                                  .Use(new ConversationState <Dictionary <string, object> >(new MemoryStorage()));

            await new TestFlow(adapter, async(turnContext) =>
            {
                var state  = ConversationState <Dictionary <string, object> > .Get(turnContext);
                var prompt = new NumberPrompt <int>(Culture.English);

                var dialogCompletion = await prompt.Continue(turnContext, state);
                if (!dialogCompletion.IsActive && !dialogCompletion.IsCompleted)
                {
                    await prompt.Begin(turnContext, state, new PromptOptions {
                        PromptString = "Enter a number."
                    });
                }
                else if (dialogCompletion.IsCompleted)
                {
                    var numberResult = (NumberResult <int>)dialogCompletion.Result;
                    await turnContext.SendActivity($"Bot received the number '{numberResult.Value}'.");
                }
            })
            .Send("hello")
            .AssertReply("Enter a number.")
            .Send("42")
            .AssertReply("Bot received the number '42'.")
            .StartTest();
        }
Esempio n. 6
0
        public async Task NumberPromptValidator()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(new AutoSaveStateMiddleware(convoState));

            var dialogs = new DialogSet(dialogState);

            PromptValidator <int> validator = (promptContext, cancellationToken) =>
            {
                var result = promptContext.Recognized.Value;

                if (result < 100 && result > 0)
                {
                    return(Task.FromResult(true));
                }

                return(Task.FromResult(false));
            };
            var numberPrompt = new NumberPrompt <int>("NumberPrompt", validator, Culture.English);

            dialogs.Add(numberPrompt);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);

                var results = await dc.ContinueDialogAsync(cancellationToken);
                if (results.Status == DialogTurnStatus.Empty)
                {
                    var options = new PromptOptions
                    {
                        Prompt = new Activity {
                            Type = ActivityTypes.Message, Text = "Enter a number."
                        },
                        RetryPrompt = new Activity {
                            Type = ActivityTypes.Message, Text = "You must enter a positive number less than 100."
                        },
                    };
                    await dc.PromptAsync("NumberPrompt", options, cancellationToken);
                }
                else if (results.Status == DialogTurnStatus.Complete)
                {
                    var numberResult = (int)results.Result;
                    await turnContext.SendActivityAsync(MessageFactory.Text($"Bot received the number '{numberResult}'."), cancellationToken);
                }
            })
            .Send("hello")
            .AssertReply("Enter a number.")
            .Send("150")
            .AssertReply("You must enter a positive number less than 100.")
            .Send("64")
            .AssertReply("Bot received the number '64'.")
            .StartTestAsync();
        }
Esempio n. 7
0
        public async Task NumberPromptValidator()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(convoState);

            var dialogs = new DialogSet(dialogState);

            PromptValidator <int> validator = async(ctx, promptContext) =>
            {
                var result = (int)promptContext.Recognized.Value;

                if (result < 100 && result > 0)
                {
                    promptContext.End(result);
                }
            };
            var numberPrompt = new NumberPrompt <int>("NumberPrompt", validator, Culture.English);

            dialogs.Add(numberPrompt);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dc = await dialogs.CreateContextAsync(turnContext);

                var results = await dc.ContinueAsync();
                if (!turnContext.Responded && !results.HasActive && !results.HasResult)
                {
                    var options = new PromptOptions {
                        Prompt = new Activity {
                            Type = ActivityTypes.Message, Text = "Enter a number."
                        },
                        RetryPrompt = new Activity {
                            Type = ActivityTypes.Message, Text = "You must enter a positive number less than 100."
                        }
                    };
                    await dc.PromptAsync("NumberPrompt", options);
                }
                else if (!results.HasActive && results.HasResult)
                {
                    var numberResult = (int)results.Result;
                    await turnContext.SendActivityAsync($"Bot received the number '{numberResult}'.");
                }
            })
            .Send("hello")
            .AssertReply("Enter a number.")
            .Send("150")
            .AssertReply("You must enter a positive number less than 100.")
            .Send("64")
            .AssertReply("Bot received the number '64'.")
            .StartTestAsync();
        }
        public async Task NumberPromptValidator()
        {
            ConversationState convoState = new ConversationState(new MemoryStorage());
            var testProperty             = convoState.CreateProperty <Dictionary <string, object> >("test");

            TestAdapter adapter = new TestAdapter()
                                  .Use(convoState);


            PromptValidator <NumberResult <int> > validator = async(ctx, result) =>
            {
                if (result.Value < 0)
                {
                    result.Status = PromptStatus.TooSmall;
                }
                if (result.Value > 100)
                {
                    result.Status = PromptStatus.TooBig;
                }
                await Task.CompletedTask;
            };

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var state  = await testProperty.GetAsync(turnContext, () => new Dictionary <string, object>());
                var prompt = new NumberPrompt <int>(Culture.English, validator);

                var dialogCompletion = await prompt.ContinueAsync(turnContext, state);
                if (!dialogCompletion.IsActive && !dialogCompletion.IsCompleted)
                {
                    await prompt.BeginAsync(turnContext, state,
                                            new PromptOptions
                    {
                        PromptString      = "Enter a number.",
                        RetryPromptString = "You must enter a positive number less than 100."
                    });
                }
                else if (dialogCompletion.IsCompleted)
                {
                    var numberResult = (NumberResult <int>)dialogCompletion.Result;
                    await turnContext.SendActivityAsync($"Bot received the number '{numberResult.Value}'.");
                }
            })
            .Send("hello")
            .AssertReply("Enter a number.")
            .Send("150")
            .AssertReply("You must enter a positive number less than 100.")
            .Send("64")
            .AssertReply("Bot received the number '64'.")
            .StartTestAsync();
        }
        public async Task NumberPrompt()
        {
            var activities = TranscriptUtilities.GetFromTestContext(TestContext);


            PromptValidatorEx.PromptValidator <NumberResult <int> > validator = async(ctx, result) =>
            {
                if (result.Value < 0)
                {
                    result.Status = PromptStatus.TooSmall;
                }
                if (result.Value > 100)
                {
                    result.Status = PromptStatus.TooBig;
                }
                await Task.CompletedTask;
            };

            var convState    = new ConversationState(new MemoryStorage());
            var testProperty = convState.CreateProperty <Dictionary <string, object> >("test", () => new Dictionary <string, object>());

            TestAdapter adapter = new TestAdapter()
                                  .Use(convState);

            await new TestFlow(adapter, async(turnContext) =>
            {
                if (turnContext.Activity.Type == ActivityTypes.Message)
                {
                    var state  = await testProperty.GetAsync(turnContext);
                    var prompt = new NumberPrompt <int>(Culture.English, validator);

                    var dialogCompletion = await prompt.ContinueAsync(turnContext, state);
                    if (!dialogCompletion.IsActive && !dialogCompletion.IsCompleted)
                    {
                        await prompt.BeginAsync(turnContext, state,
                                                new PromptOptions
                        {
                            PromptString      = "Enter a number.",
                            RetryPromptString = "You must enter a valid positive number less than 100."
                        });
                    }
                    else if (dialogCompletion.IsCompleted)
                    {
                        var numberResult = (NumberResult <int>)dialogCompletion.Result;
                        await turnContext.SendActivityAsync($"Bot received the number '{numberResult.Value}'.");
                    }
                }
            })
            .Test(activities)
            .StartTestAsync();
        }
Esempio n. 10
0
        public async Task NumberPrompt_Validator()
        {
            TestAdapter adapter = new TestAdapter()
                                  .Use(new ConversationState <TestState>(new MemoryStorage()));

            await new TestFlow(adapter, async(context) =>
            {
                var state        = ConversationState <TestState> .Get(context);
                var numberPrompt = new NumberPrompt <int>(Culture.English, async(ctx, result) =>
                {
                    if (result.Value < 0)
                    {
                        result.Status = RecognitionStatus.TooSmall;
                    }
                    if (result.Value > 100)
                    {
                        result.Status = RecognitionStatus.TooBig;
                    }
                });
                if (!state.InPrompt)
                {
                    state.InPrompt = true;
                    await numberPrompt.Prompt(context, "Gimme:");
                }
                else
                {
                    var numberResult = await numberPrompt.Recognize(context);
                    if (numberResult.Succeeded())
                    {
                        Assert.IsInstanceOfType(numberResult.Value, typeof(int));
                        Assert.IsTrue(numberResult.Value < 100);
                        Assert.IsNotNull(numberResult.Text);
                        context.Reply(numberResult.Value.ToString());
                    }
                    else
                    {
                        context.Reply(numberResult.Status.ToString());
                    }
                }
            })
            .Send("hello")
            .AssertReply("Gimme:")
            .Send("asdf df 123")
            .AssertReply(RecognitionStatus.TooBig.ToString())
            .Send(" asdf asd 12 adsfsdf ")
            .AssertReply("12")
            .StartTest();
        }
Esempio n. 11
0
        public async Task ContinueCallPromptResult()
        {
            // Arrange
            var target1  = new NumberPrompt <int>("", async(turnContext, o) => Check.That(o).Equals(15), null);
            var context  = A.Fake <ITurnContext>();
            var activity = new Activity()
            {
                ActivityType = ActivityType.Message, Text = "15"
            };

            A.CallTo(() => context.Activity).Returns(activity);
            // Act
            await target1.Continue(context);

            // Assert
        }
        public async Task NumberPromptRetry()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(convoState);

            var dialogs = new DialogSet(dialogState);

            var numberPrompt = new NumberPrompt <int>("NumberPrompt", defaultLocale: Culture.English);

            dialogs.Add(numberPrompt);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);

                var results = await dc.ContinueAsync(cancellationToken);
                if (results.Status == DialogTurnStatus.Empty)
                {
                    var options = new PromptOptions {
                        Prompt = new Activity {
                            Type = ActivityTypes.Message, Text = "Enter a number."
                        },
                        RetryPrompt = new Activity {
                            Type = ActivityTypes.Message, Text = "You must enter a number."
                        }
                    };
                    await dc.PromptAsync("NumberPrompt", options, cancellationToken);
                }
                else if (results.Status == DialogTurnStatus.Complete)
                {
                    var numberResult = (int)results.Result;
                    await turnContext.SendActivityAsync(MessageFactory.Text($"Bot received the number '{numberResult}'."), cancellationToken);
                }
            })
            .Send("hello")
            .AssertReply("Enter a number.")
            .Send("hello")
            .AssertReply("You must enter a number.")
            .Send("64")
            .AssertReply("Bot received the number '64'.")
            .StartTestAsync();
        }
        public async Task WaterfallPrompt()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(convoState);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var state   = await dialogState.GetAsync(turnContext, () => new DialogState());
                var dialogs = new DialogSet(dialogState);
                dialogs.Add(Create_Waterfall2());
                var numberPrompt = new NumberPrompt <int>("number", defaultLocale: Culture.English);
                dialogs.Add(numberPrompt);

                var dc = await dialogs.CreateContextAsync(turnContext);

                await dc.ContinueAsync();

                if (!turnContext.Responded)
                {
                    await dc.BeginAsync("test-waterfall");
                }
            })
            .Send("hello")
            .AssertReply("step1")
            .AssertReply("Enter a number.")
            .Send("hello again")
            .AssertReply("It must be a number")
            .Send("42")
            .AssertReply("Thanks for '42'")
            .AssertReply("step2")
            .AssertReply("Enter a number.")
            .Send("apple")
            .AssertReply("It must be a number")
            .Send("orange")
            .AssertReply("It must be a number")
            .Send("64")
            .AssertReply("Thanks for '64'")
            .AssertReply("step3")
            .StartTestAsync();
        }
Esempio n. 14
0
        public async Task CultureThruActivityNumberPrompt()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(new AutoSaveStateMiddleware(convoState));

            var dialogs = new DialogSet(dialogState);

            var numberPrompt = new NumberPrompt <double>("NumberPrompt", defaultLocale: Culture.Dutch);

            dialogs.Add(numberPrompt);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);

                var results = await dc.ContinueDialogAsync(cancellationToken);
                if (results.Status == DialogTurnStatus.Empty)
                {
                    var options = new PromptOptions
                    {
                        Prompt = new Activity {
                            Type = ActivityTypes.Message, Text = "Enter a number."
                        },
                    };
                    await dc.PromptAsync("NumberPrompt", options);
                }
                else if (results.Status == DialogTurnStatus.Complete)
                {
                    var numberResult = (double)results.Result;
                    Assert.AreEqual(3.14, numberResult);
                }
            })
            .Send("hello")
            .AssertReply("Enter a number.")
            .Send(new Activity {
                Type = ActivityTypes.Message, Text = "3,14", Locale = Culture.Dutch
            })
            .StartTestAsync();
        }
Esempio n. 15
0
        public async Task NumberPrompt()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(convoState);

            // Create new DialogSet.
            var dialogs = new DialogSet(dialogState);

            // Create and add number prompt to DialogSet.
            var numberPrompt = new NumberPrompt <int>("NumberPrompt", defaultLocale: Culture.English);

            dialogs.Add(numberPrompt);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dc = await dialogs.CreateContextAsync(turnContext);

                var results = await dc.ContinueAsync();
                if (!turnContext.Responded && !results.HasActive && !results.HasResult)
                {
                    var options = new PromptOptions {
                        Prompt = new Activity {
                            Type = ActivityTypes.Message, Text = "Enter a number."
                        }
                    };
                    await dc.PromptAsync("NumberPrompt", options);
                }
                else if (!results.HasActive && results.HasResult)
                {
                    var numberResult = (int)results.Result;
                    await turnContext.SendActivityAsync($"Bot received the number '{numberResult}'.");
                }
            })
            .Send("hello")
            .AssertReply("Enter a number.")
            .Send("42")
            .AssertReply("Bot received the number '42'.")
            .StartTestAsync();
        }
Esempio n. 16
0
        public async Task NumberPromptDefaultsToEnUsLocale()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(new AutoSaveStateMiddleware(convoState));

            var dialogs = new DialogSet(dialogState);

            var numberPrompt = new NumberPrompt <double>("NumberPrompt");

            dialogs.Add(numberPrompt);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);

                var results = await dc.ContinueDialogAsync(cancellationToken);
                if (results.Status == DialogTurnStatus.Empty)
                {
                    var options = new PromptOptions
                    {
                        Prompt = new Activity {
                            Type = ActivityTypes.Message, Text = "Enter a number."
                        },
                    };
                    await dc.PromptAsync("NumberPrompt", options);
                }
                else if (results.Status == DialogTurnStatus.Complete)
                {
                    var numberResult = (double)results.Result;
                    await turnContext.SendActivityAsync(MessageFactory.Text($"Bot received the number '{numberResult}'."), cancellationToken);
                }
            })
            .Send("hello")
            .AssertReply("Enter a number.")
            .Send("3.14")
            .AssertReply("Bot received the number '3.14'.")
            .StartTestAsync();
        }
Esempio n. 17
0
        public async Task ContinueGetValue()
        {
            // Arrange
            var target1  = new NumberPrompt <int>("", null, null);
            var target2  = new NumberPrompt <double>("", null, null);
            var context  = A.Fake <ITurnContext>();
            var activity = new Activity()
            {
                ActivityType = ActivityType.Message, Text = "15"
            };

            A.CallTo(() => context.Activity).Returns(activity);
            // Act
            await target1.Continue(context);

            await target2.Continue(context);

            // Assert
            Check.That(target1.Value).Equals(15);
            Check.That(target2.Value).Equals(15.0);
        }
Esempio n. 18
0
        public async Task NumberPrompt_Float()
        {
            TestAdapter adapter = new TestAdapter()
                                  .Use(new ConversationState <TestState>(new MemoryStorage()));

            await new TestFlow(adapter, async(context) =>
            {
                var state        = ConversationState <TestState> .Get(context);
                var numberPrompt = new NumberPrompt <float>(Culture.English);
                if (!state.InPrompt)
                {
                    state.InPrompt = true;
                    await numberPrompt.Prompt(context, "Gimme:");
                }
                else
                {
                    var numberResult = await numberPrompt.Recognize(context);
                    if (numberResult.Succeeded())
                    {
                        Assert.IsTrue(numberResult.Value != float.NaN);
                        Assert.IsNotNull(numberResult.Text);
                        Assert.IsInstanceOfType(numberResult.Value, typeof(float));
                        context.Reply(numberResult.Value.ToString());
                    }
                    else
                    {
                        context.Reply(numberResult.Status.ToString());
                    }
                }
            })
            .Send("hello")
            .AssertReply("Gimme:")
            .Send("test test test")
            .AssertReply(RecognitionStatus.NotRecognized.ToString())
            .Send("asdf df 123")
            .AssertReply("123")
            .Send(" asdf asd 123.43 adsfsdf ")
            .AssertReply("123.43")
            .StartTest();
        }
        public async Task NumberPrompt_Int()
        {
            TestAdapter adapter = new TestAdapter()
                                  .Use(new ConversationState <TestState>(new MemoryStorage()));

            await new TestFlow(adapter, async(context) =>
            {
                var state        = ConversationState <TestState> .Get(context);
                var numberPrompt = new NumberPrompt <int>(Culture.English);
                if (!state.InPrompt)
                {
                    state.InPrompt = true;
                    await numberPrompt.Prompt(context, "Gimme:");
                }
                else
                {
                    var result = await numberPrompt.Recognize(context);
                    if (result == null)
                    {
                        context.Reply("null");
                    }
                    else
                    {
                        Assert.IsInstanceOfType(result.Value, typeof(int));
                        Assert.IsNotNull(result.Text);
                        context.Reply(result.Value.ToString());
                    }
                }
            })
            .Send("hello")
            .AssertReply("Gimme:")
            .Send("test test test")
            .AssertReply("null")
            .Send("asdf df 123")
            .AssertReply("123")
            .Send(" asdf asd 123.43 adsfsdf ")
            .AssertReply("null")
            .StartTest();
        }
Esempio n. 20
0
        public void ParseNumber(string prompt, NumberPrompt numberPrompt)
        {
            Output.WriteLine(prompt);

            IsWaitingForNumber = true;
            mNumberPrompt      = numberPrompt;

            /*
             * int result;
             *
             * while (true)
             * {
             *  string response = Console.ReadLine().Trim();
             *  if (int.TryParse(response, out result))
             *  {
             *      value = result;
             *      return true;
             *  }
             *  else
             *      Output.Write("Please enter a whole number.> ");
             * }
             */
        }
Esempio n. 21
0
 public SimplePromptBot()
 {
     namePrompt = new TextPrompt();
     agePrompt  = new NumberPrompt <int>(Culture.English, ValidateAge);
 }
Esempio n. 22
0
 public void NumberPromptWithEmptyIdShouldFail()
 {
     var emptyId      = string.Empty;
     var numberPrompt = new NumberPrompt <int>(emptyId);
 }