Skip to content

Commit 83246b2

Browse files
author
Comick
committed
moving tests and examples to async
1 parent 692a4fb commit 83246b2

File tree

5 files changed

+62
-60
lines changed

5 files changed

+62
-60
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ examplelibrary.js:
2020
```js
2121
'use strict';
2222

23-
var readdir = require('promise').denodeify(require('fs').readdir),
24-
robot = require('../lib/robotremote'),
25-
assert = require('assert');
23+
const { readdir } = require('fs').promises;
24+
const robot = require('../lib/robotremote');
25+
const assert = require('assert');
2626

2727
var lib = module.exports;
2828

@@ -39,11 +39,9 @@ var lib = module.exports;
3939
*
4040
* @param path directory path to count item in.
4141
*/
42-
lib.countItemsInDirectory = function (path) {
43-
return readdir(path).then(function (files) {
44-
return files.length;
45-
});
46-
};
42+
lib.countItemsInDirectory = async function(path) {
43+
return (await readdir(path)).length;
44+
}
4745
// The doc attribute is used for inspection on the command line of client and doc generation.
4846
// It's optional and defaults to empty string when missing.
4947
lib.countItemsInDirectory.doc = 'Returns the number of items in the directory specified by `path`.';
@@ -67,15 +65,15 @@ lib.countItemsInDirectory.doc = 'Returns the number of items in the directory sp
6765
* @param str1
6866
* @param str2
6967
*/
70-
lib.stringsShouldBeEqual = function (str1, str2) {
68+
lib.stringsShouldBeEqual = (str1, str2) => {
7169
this.output.warn('Comparing \'%s\' to \'%s\'', str1, str2);
7270
assert.equal(str1, str2, 'Given strings are not equal');
7371
};
7472

7573

7674
// Run this keyword library if the library itself is called explicitly.
7775
if (!module.parent) {
78-
var server = new robot.Server([lib], { host: 'localhost', port: 8270 });
76+
const server = new robot.Server([lib], { host: 'localhost', port: 8270 });
7977
}
8078
```
8179

bin/botclient.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
var robot = require('../lib/robotremote'),
5-
repl = require('repl');
4+
const robot = require('../lib/robotremote');
5+
const repl = require('repl');
66

7+
const options = {
8+
host: process.argv[2] || 'localhost',
9+
port: parseInt(process.argv[3], 10) || 8270
10+
};
11+
const serverString = options.host + ':' + options.port;
712

8-
var options = {host: process.argv[2] || 'localhost', port: parseInt(process.argv[3], 10) || 8270};
9-
var serverString = options.host + ':' + options.port;
10-
11-
robot.createClient(options).then(keywords -> {
13+
async function reloadKeywords(serverString) {
14+
let keywords = await robot.createClient(options);
1215
console.log('Connected to remote server at "' + serverString + '"');
1316
console.log('Available keywords: ' + Object.keys(keywords).join(', '));
14-
repl.start(serverString + '> ').context.keywords = keywords;
15-
}, err -> {
16-
console.log('Could not connected to remote server at "' + serverString + '"');
17-
throw err;
18-
});
17+
return keywords;
18+
}
19+
20+
(async () => {
21+
try {
22+
let keywords = await reloadKeywords(serverString);
23+
const replServer = repl.start(serverString + '> ')
24+
replServer.context.keywords = keywords;
25+
replServer.defineCommand('reload', {
26+
help: 'Reload remote keyword definitions',
27+
async action() {
28+
replServer.context.keywords = await reloadKeywords(serverString);
29+
this.displayPrompt();
30+
}
31+
});
32+
} catch (err) {
33+
console.log('Could not connected to remote server at "' + serverString + '"');
34+
throw err;
35+
}
36+
})();
1937

example/examplelibrary.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
var readdir = require('promise').denodeify(require('fs').readdir),
4-
robot = require('../lib/robotremote'),
5-
assert = require('assert');
3+
const { readdir } = require('fs').promises;
4+
const robot = require('../lib/robotremote');
5+
const assert = require('assert');
66

77
var lib = module.exports;
88

@@ -19,11 +19,9 @@ var lib = module.exports;
1919
*
2020
* @param path directory path to count item in.
2121
*/
22-
lib.countItemsInDirectory = function (path) {
23-
return readdir(path).then(function (files) {
24-
return files.length;
25-
});
26-
};
22+
lib.countItemsInDirectory = async function(path) {
23+
return (await readdir(path)).length;
24+
}
2725
// The doc attribute is used for inspection on the command line of client and doc generation.
2826
// It's optional and defaults to empty string when missing.
2927
lib.countItemsInDirectory.doc = 'Returns the number of items in the directory specified by `path`.';
@@ -42,18 +40,18 @@ lib.countItemsInDirectory.doc = 'Returns the number of items in the directory sp
4240
* Each keyword also have the output writer, which enables logging at various levels.
4341
* Here warn level is showed as an example.
4442
* All robot levels are supported including messages with timestamp through timestamp`Level` function.
45-
* See http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.8.5#logging-information
43+
* See http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#logging-information
4644
*
4745
* @param str1
4846
* @param str2
4947
*/
50-
lib.stringsShouldBeEqual = function (str1, str2) {
48+
lib.stringsShouldBeEqual = (str1, str2) => {
5149
this.output.warn('Comparing \'%s\' to \'%s\'', str1, str2);
5250
assert.equal(str1, str2, 'Given strings are not equal');
5351
};
5452

5553

5654
// Run this keyword library if the library itself is called explicitly.
5755
if (!module.parent) {
58-
var server = new robot.Server([lib], { host: 'localhost', port: 8270 });
56+
const server = new robot.Server([lib], { host: 'localhost', port: 8270 });
5957
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"test",
1212
"ATDD"
1313
],
14-
"version": "1.5.0",
14+
"version": "1.5.1",
1515
"preferGlobal": false,
1616
"homepage": "https://github.com/comick/node-robotremoteserver",
1717
"author": {

test/testlibrary.js

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,50 +37,38 @@ lib.justFail = function () {
3737
throw new Error();
3838
};
3939

40-
lib.doNothingAsync = function () {
41-
return new Promise(function (resolve) {
42-
resolve();
43-
});
40+
lib.doNothingAsync = async function () {
41+
return;
4442
};
4543

46-
lib.concatenateArgumentsAsync = function (arg1, arg2) {
47-
return new Promise(function (resolve) {
48-
resolve(arg1 + arg2);
49-
});
44+
lib.concatenateArgumentsAsync = async function (arg1, arg2) {
45+
return arg1 + arg2;
5046
};
5147

52-
lib.concatenateArgumentsWithVarArgumentsAsync = function (prefix, ...args) {
53-
return new Promise(function (resolve) {
54-
resolve(prefix + JSON.stringify(args));
55-
});
48+
lib.concatenateArgumentsWithVarArgumentsAsync = async function (prefix, ...args) {
49+
return prefix + JSON.stringify(args);
5650
};
5751
lib.concatenateArgumentsWithVarArgumentsAsync.args = ['prefix', '*args'];
5852

59-
lib.concatenateArgumentsWithNamedArgumentsAsync = function (prefix, kwargs) {
60-
return new Promise(function (resolve) {
61-
resolve(prefix + JSON.stringify(kwargs));
62-
});
53+
lib.concatenateArgumentsWithNamedArgumentsAsync = async function (prefix, kwargs) {
54+
return prefix + JSON.stringify(kwargs);
6355
};
6456
lib.concatenateArgumentsWithNamedArgumentsAsync.args = ['prefix', '**kwargs'];
6557

6658
// For language constraints, var arguments should be declared at the end, so the last
6759
// args will be kwargs
68-
lib.concatenateArgumentsWithVarAndNamedArgumentsAsync = function (prefix, ...args) {
69-
return new Promise(function (resolve) {
70-
resolve(prefix + JSON.stringify(args));
71-
});
60+
lib.concatenateArgumentsWithVarAndNamedArgumentsAsync = async function (prefix, ...args) {
61+
return prefix + JSON.stringify(args);
7262
};
7363
lib.concatenateArgumentsWithVarAndNamedArgumentsAsync.args = ['prefix', '*args', '**kwargs'];
7464

75-
lib.justFailAsync = function () {
76-
return new Promise(function (resolve, reject) {
77-
reject(new Error());
78-
});
65+
lib.justFailAsync = async function () {
66+
throw new Error();
7967
};
8068

81-
lib.neverReturn = function () {
82-
return new Promise(function (resolve, reject) {
83-
});
69+
lib.neverReturn = async function () {
70+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
71+
await sleep(1000 * 60 * 60);
8472
};
8573

8674
// Run this keyword library if the library itself is called explicitly.

0 commit comments

Comments
 (0)