Skip to content

Commit 5e16fd5

Browse files
committed
[ARO] Adding a feature to make tester able to create their own test
1 parent 26ee915 commit 5e16fd5

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ yarn-error.log
44
.coveralls.yml
55
.nyc_output
66
coverage
7-
*.ava.spec.js
7+
*.ava.spec.js
8+
.idea

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,28 @@ describe('BotTester', () => {
242242
.runTest();
243243
});
244244
```
245-
245+
# Make your own tests
246+
```javascript
247+
it("should make you able to write your own tests", () => {
248+
bot.dialog('/', (session) => {
249+
session.send("Hello");
250+
session.send("12");
251+
});
252+
return botTester.sendMessageToBot('Bonjour',(message) => {
253+
if (message.text === "Hello") {
254+
return true;
255+
} else {
256+
return new Error("Message : " + message.text + "is not equal to 'Hello'")
257+
}
258+
},(message)=>{
259+
if (parseInt(message.text,0) % 2 === 0) {
260+
return true;
261+
} else {
262+
return new Error("Message is not an even number : " + message.text);
263+
}
264+
}).runTest();
265+
});
266+
```
246267
# Address/multiuser cases
247268
```javascript
248269
describe('Address/multi user', () => {

src/ExpectedMessage.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import { IEvent, IMessage } from 'botbuilder';
2-
import { BotTesterExpectation } from './assertionLibraries/BotTesterExpectation';
3-
import { IConfig } from './config';
1+
import {IEvent, IMessage} from 'botbuilder';
2+
import {assert} from 'chai';
3+
import {BotTesterExpectation} from './assertionLibraries/BotTesterExpectation';
4+
import {IConfig} from './config';
45

56
export enum ExpectedMessageType {
67
String,
78
IMessage,
8-
Regex
9+
Regex,
10+
Function
911
}
1012

1113
/**
1214
* Types accepted for responses checkers
1315
*/
14-
export type PossibleExpectedMessageType = string | IMessage | RegExp | IEvent;
16+
export type PossibleExpectedMessageType = string | IMessage | RegExp | IEvent | Function;
1517

1618
/**
1719
* Response expectations area always collections. The collection is the set of possible responses, chosen at random. If the collection size
@@ -26,8 +28,10 @@ function getExpectedMessageType(expectedResponseCollection: PossibleExpectedMess
2628
return ExpectedMessageType.String;
2729
} else if (firstElt.constructor.name === 'RegExp') {
2830
return ExpectedMessageType.Regex;
29-
} else {
31+
} else if (firstElt.constructor.name === 'IMessage') {
3032
return ExpectedMessageType.IMessage;
33+
} else {
34+
return ExpectedMessageType.Function;
3135
}
3236
}
3337

@@ -42,10 +46,8 @@ export class ExpectedMessage {
4246
*/
4347
private readonly expectedResponseCollection: PossibleExpectedMessageCollections;
4448

45-
constructor(
46-
config: IConfig,
47-
expectedResponseCollection: PossibleExpectedMessageType | PossibleExpectedMessageCollections
48-
) {
49+
constructor(config: IConfig,
50+
expectedResponseCollection: PossibleExpectedMessageType | PossibleExpectedMessageCollections) {
4951
this.internalExpectation = new BotTesterExpectation(config);
5052

5153
if (!(expectedResponseCollection instanceof Array)) {
@@ -73,6 +75,9 @@ export class ExpectedMessage {
7375
// doing this check will highlight if the diff in text instead of a large IMessage diff
7476
this.deepMessageMatchCheck(outgoingMessage);
7577
break;
78+
case ExpectedMessageType.Function:
79+
this.deepMatchCheckWithFunction(outgoingMessage);
80+
break;
7681
default:
7782
this.internalExpectation.expect(outgoingMessage.type).toEqual('save');
7883
}
@@ -111,7 +116,7 @@ export class ExpectedMessage {
111116
const regexCollection: RegExp[] = this.expectedResponseCollection as RegExp[];
112117

113118
this.internalExpectation.expect(regexCollection.some((regex: RegExp) => regex.test(text)),
114-
`'${text}' did not match any regex in ${regexCollection}`).toBeTrue();
119+
`'${text}' did not match any regex in ${regexCollection}`).toBeTrue();
115120
}
116121

117122
/**
@@ -136,4 +141,30 @@ export class ExpectedMessage {
136141

137142
this.internalExpectation.expect(expectedResponseCollectionAsIMessage).toDeeplyInclude(outgoingMessage);
138143
}
144+
145+
/**
146+
* Verfy the incoming message with custom test defined by tester
147+
* If the function that tester defined return an error, make the test break
148+
* If the function return anything else, the test is considered as good
149+
* I've tryed to use promise as parameter, but in a promise we change scope, so the assert doesn't work
150+
* @param {IMessage} outgoingMessage outgoing message being compared
151+
*/
152+
private deepMatchCheckWithFunction(outgoingMessage: IMessage): void {
153+
const functionCollection: Function[] = this.expectedResponseCollection as Function[];
154+
let errorString = '';
155+
let success = false;
156+
functionCollection.forEach((func: Function) => {
157+
const result = func(outgoingMessage);
158+
if (result instanceof Error) {
159+
errorString += `\n -----------------ERROR-----------------\n\n\n'${result.message}' `;
160+
} else {
161+
success = true;
162+
}
163+
});
164+
// ErrorString here, can hold multiples error, if the bot send multiples message in one batching
165+
const error = `Bot should have relied response that matches with function but respond '${outgoingMessage}'` +
166+
` that create the following error(s) '${errorString}'`;
167+
this.internalExpectation.expect(success, error).toBeTrue();
168+
169+
}
139170
}

test/mocha.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
--require ts-node/register
22
--recursive
3-
test/*.spec.ts
3+
test/**/*mocha.spec.ts

0 commit comments

Comments
 (0)