-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathNumberWithUnit_PromptTests.cs
More file actions
257 lines (226 loc) · 11.3 KB
/
NumberWithUnit_PromptTests.cs
File metadata and controls
257 lines (226 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Recognizers.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading.Tasks;
namespace Bot.Builder.Community.Dialogs.Prompts.Tests
{
[TestClass]
public class NumberWithUnit_PromptTests
{
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void NumberPromptWithEmptyIdShouldFail()
{
var emptyId = "";
var numberPrompt = new NumberWithUnitPrompt(emptyId, NumberWithUnitPromptType.Currency);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void NumberPromptWithNullIdShouldFail()
{
var nullId = "";
nullId = null;
var numberPrompt = new NumberWithUnitPrompt(nullId, NumberWithUnitPromptType.Currency);
}
[TestMethod]
public async Task NumberWithUnitPrompt_Type_Currency()
{
var convoState = new ConversationState(new MemoryStorage());
var dialogState = convoState.CreateProperty<DialogState>("dialogState");
var adapter = new TestAdapter()
.Use(new AutoSaveStateMiddleware(convoState));
// Create new DialogSet.
var dialogs = new DialogSet(dialogState);
// Create and add number prompt to DialogSet.
var numberPrompt = new NumberWithUnitPrompt("CurrencyPrompt", NumberWithUnitPromptType.Currency, defaultLocale: 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 (!turnContext.Responded && results.Status == DialogTurnStatus.Empty && results.Status != DialogTurnStatus.Complete)
{
var options = new PromptOptions { Prompt = new Activity { Type = ActivityTypes.Message, Text = "Enter a currency." } };
await dc.PromptAsync("CurrencyPrompt", options, cancellationToken);
}
else if (results.Status == DialogTurnStatus.Complete)
{
var currencyPromptResult = (NumberWithUnitResult)results.Result;
if (currencyPromptResult != null)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Bot received Value: {currencyPromptResult.Value}, Unit: {currencyPromptResult.Unit}"), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Nothing recognized"), cancellationToken);
}
}
})
.Send("hello")
.AssertReply("Enter a currency.")
.Send("42 dollars")
.AssertReply("Bot received Value: 42, Unit: Dollar")
.Send("hello")
.AssertReply("Enter a currency.")
.Send("twenty five dollars")
.AssertReply("Bot received Value: 25, Unit: Dollar")
.Send("hello")
.AssertReply("Enter a currency.")
.Send("?0.5")
.AssertReply("Bot received Value: 50.5, Unit: Pound")
.StartTestAsync();
}
[TestMethod]
public async Task NumberWithUnitPrompt_Type_Temperature()
{
var convoState = new ConversationState(new MemoryStorage());
var dialogState = convoState.CreateProperty<DialogState>("dialogState");
var adapter = new TestAdapter()
.Use(new AutoSaveStateMiddleware(convoState));
// Create new DialogSet.
var dialogs = new DialogSet(dialogState);
// Create and add number prompt to DialogSet.
var numberPrompt = new NumberWithUnitPrompt("NumberPrompt", NumberWithUnitPromptType.Temperature, defaultLocale: 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 (!turnContext.Responded && results.Status == DialogTurnStatus.Empty && results.Status != DialogTurnStatus.Complete)
{
var options = new PromptOptions { Prompt = new Activity { Type = ActivityTypes.Message, Text = "Enter a temperature." } };
await dc.PromptAsync("NumberPrompt", options, cancellationToken);
}
else if (results.Status == DialogTurnStatus.Complete)
{
var numberWithUnitResult = (NumberWithUnitResult)results.Result;
if (numberWithUnitResult != null)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Bot received Value: {numberWithUnitResult.Value}, Unit: {numberWithUnitResult.Unit}"), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Nothing recognized"), cancellationToken);
}
}
})
.Send("hello")
.AssertReply("Enter a temperature.")
.Send("25 degrees celsius")
.AssertReply("Bot received Value: 25, Unit: C")
.Send("hello")
.AssertReply("Enter a temperature.")
.Send("50C")
.AssertReply("Bot received Value: 50, Unit: C")
.Send("hello")
.AssertReply("Enter a temperature.")
.Send("75F")
.AssertReply("Bot received Value: 75, Unit: F")
.Send("hello")
.AssertReply("Enter a temperature.")
.Send("100 degrees")
.AssertReply("Bot received Value: 100, Unit: Degree")
.StartTestAsync();
}
[TestMethod]
public async Task NumberWithUnitPrompt_Type_Age()
{
var convoState = new ConversationState(new MemoryStorage());
var dialogState = convoState.CreateProperty<DialogState>("dialogState");
var adapter = new TestAdapter()
.Use(new AutoSaveStateMiddleware(convoState));
// Create new DialogSet.
var dialogs = new DialogSet(dialogState);
// Create and add number prompt to DialogSet.
var numberPrompt = new NumberWithUnitPrompt("NumberPrompt", NumberWithUnitPromptType.Age, defaultLocale: 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 (!turnContext.Responded && results.Status == DialogTurnStatus.Empty && results.Status != DialogTurnStatus.Complete)
{
var options = new PromptOptions { Prompt = new Activity { Type = ActivityTypes.Message, Text = "Enter an age." } };
await dc.PromptAsync("NumberPrompt", options, cancellationToken);
}
else if (results.Status == DialogTurnStatus.Complete)
{
var numberWithUnitResult = (NumberWithUnitResult)results.Result;
if (numberWithUnitResult != null)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Bot received Value: {numberWithUnitResult.Value}, Unit: {numberWithUnitResult.Unit}"), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Nothing recognized"), cancellationToken);
}
}
})
.Send("hello")
.AssertReply("Enter an age.")
.Send("25 years old")
.AssertReply("Bot received Value: 25, Unit: Year")
.Send("hello")
.AssertReply("Enter an age.")
.Send("three months old")
.AssertReply("Bot received Value: 3, Unit: Month")
.Send("hello")
.AssertReply("Enter an age.")
.Send("twenty five years of age")
.AssertReply("Bot received Value: 25, Unit: Year")
.StartTestAsync();
}
[TestMethod]
public async Task NumberWithUnitPrompt_Type_Dimension()
{
var convoState = new ConversationState(new MemoryStorage());
var dialogState = convoState.CreateProperty<DialogState>("dialogState");
var adapter = new TestAdapter()
.Use(new AutoSaveStateMiddleware(convoState));
// Create new DialogSet.
var dialogs = new DialogSet(dialogState);
// Create and add number prompt to DialogSet.
var numberPrompt = new NumberWithUnitPrompt("NumberPrompt", NumberWithUnitPromptType.Dimension, defaultLocale: 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 (!turnContext.Responded && results.Status == DialogTurnStatus.Empty && results.Status != DialogTurnStatus.Complete)
{
var options = new PromptOptions { Prompt = new Activity { Type = ActivityTypes.Message, Text = "Enter an age." } };
await dc.PromptAsync("NumberPrompt", options, cancellationToken);
}
else if (results.Status == DialogTurnStatus.Complete)
{
var numberWithUnitResult = (NumberWithUnitResult)results.Result;
if (numberWithUnitResult != null)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Bot received Value: {numberWithUnitResult.Value}, Unit: {numberWithUnitResult.Unit}"), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Nothing recognized"), cancellationToken);
}
}
})
.Send("hello")
.AssertReply("Enter an age.")
.Send("6 miles")
.AssertReply("Bot received Value: 6, Unit: Mile")
.Send("hello")
.AssertReply("Enter an age.")
.Send("three and a half metres")
.AssertReply("Bot received Value: 3.5, Unit: Meter")
.Send("hello")
.AssertReply("Enter an age.")
.Send("twenty five kilometers")
.AssertReply("Bot received Value: 25, Unit: Kilometer")
.StartTestAsync();
}
}
}