Skip to content

Commit f2fbb32

Browse files
lmartorellacomick
authored andcommitted
- Fixed arg extraction in case of comments in the middle
1 parent e50033c commit f2fbb32

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

lib/robotremote.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@ function Server(libraries, options, listeningCallback) {
1313
'stopRemoteServer': this.stopRemoteServer
1414
};
1515

16+
function getParamNames(func) {
17+
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
18+
var ARGUMENT_NAMES = /([^\s,]+)/g;
19+
20+
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
21+
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
22+
if(result === null)
23+
result = [];
24+
return result;
25+
}
26+
1627
// Load libraries
1728
libraries.forEach(function (lib) {
1829
for (var keywordName in lib) {
1930
if (lib.hasOwnProperty(keywordName)) {
2031
var keyword = lib[keywordName];
21-
// Parameters name for documenting purpose.
22-
var rawParameters = /\(([\s\S]*?)\)/.exec(keyword.toString())[1];
23-
keyword.args = rawParameters.length > 0 ? rawParameters.split(/\s*,\s*/) : [];
32+
// Get parameter count and names.
33+
keyword.args = getParamNames(keyword.toString());
2434
if (!keyword.doc) {
2535
keyword.doc = '';
2636
}

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ describe('Robot Remote Library', function () {
2424
});
2525

2626
it('client should fail to start if server is not running', function (done) {
27+
// In case of slow DNS this will fail after 2000ms
28+
this.timeout(5000);
2729
robot.createClient({host: 'localhost', port: nextPort()}).done(
2830
function (val) {
2931
throw new Error('client succeeded');
@@ -44,6 +46,14 @@ describe('Robot Remote Library', function () {
4446
);
4547
});
4648
});
49+
it('server should correctly manage keyword arguments', function (done) {
50+
var serverPort = nextPort();
51+
var server = new robot.Server([testLibrary], {host: 'localhost', port: serverPort, allowStop: true}, function () {
52+
assert.deepEqual(server.keywords['concatenateArguments'].args, ['arg1', 'arg2']);
53+
assert.deepEqual(server.keywords['concatenateArgumentsWithCommentsInArgs'].args, ['arg1', 'arg2']);
54+
done();
55+
});
56+
});
4757
it('keyword should run when called by a client', function (done) {
4858
var serverPort = nextPort();
4959

test/testlibrary.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ lib.concatenateArguments = function (arg1, arg2) {
1313
return arg1 + arg2;
1414
};
1515

16+
lib.concatenateArgumentsWithCommentsInArgs = function (arg1, arg2 /*, skipped arg */) {
17+
return arg1 + arg2;
18+
};
19+
1620
lib.justFail = function () {
1721
throw new Error();
1822
};

0 commit comments

Comments
 (0)