Skip to content

Commit 80077b9

Browse files
lmartorellacomick
authored andcommitted
- Added support for Robot var args and named arguments
1 parent 288e678 commit 80077b9

3 files changed

Lines changed: 73 additions & 1 deletion

File tree

lib/robotremote.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ function Server(libraries, options, listeningCallback) {
5353
var that = this;
5454
var rpcWrap = function (keyword) {
5555
return function (method, params, response) {
56-
params.push(response);
56+
// Normally params is an array with the function name and an array of parameters
57+
// In case of named arguments (kwargs), params contains three items, the last one contains the
58+
// named arguments. In that case build a proper kwargs object.
59+
if (typeof params[2] === "object") {
60+
params[1].push(params[2]);
61+
params[2] = response;
62+
} else {
63+
params.push(response);
64+
}
5765
keyword.apply(that, params);
5866
};
5967
};

test/robotremote.robot

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ Run Synchronous Keyword With Return Value And Multiple Arguments
2020
${result}= Concatenate Arguments Bau Miao
2121
Should Be Equal ${result} BauMiao
2222

23+
Run Synchronous Keyword With Return Value And Variable Arguments
24+
${result}= Concatenate Arguments With Var Arguments prefix One Two
25+
Should Be Equal ${result} prefix["One","Two"]
26+
27+
Run Synchronous Keyword With Return Value And Named Arguments
28+
${result}= Concatenate Arguments With Named Arguments prefix a=1 b=${2}
29+
Should Be Equal ${result} prefix{"a":"1","b":2}
30+
31+
Run Synchronous Keyword With Return Value And Both Named and Variable Arguments
32+
${result}= Concatenate Arguments With Var And Named Arguments prefix One Two a=1 b=${2}
33+
Should Be Equal ${result} prefix["One","Two",{"a":"1","b":2}]
34+
2335
Run Synchronous Failing Keyword
2436
Run Keyword And Expect Error Error Just Fail
2537

@@ -31,6 +43,18 @@ Run Asynchronous Keyword With Return Value And Multiple Arguments
3143
${result}= Concatenate Arguments Async Bau Miao
3244
Should Be Equal ${result} BauMiao
3345

46+
Run Asynchronous Keyword With Return Value And Variable Arguments
47+
${result}= Concatenate Arguments With Var Arguments Async prefix One Two
48+
Should Be Equal ${result} prefix["One","Two"]
49+
50+
Run Aynchronous Keyword With Return Value And Named Arguments
51+
${result}= Concatenate Arguments With Named Arguments Async prefix a=1 b=${2}
52+
Should Be Equal ${result} prefix{"a":"1","b":2}
53+
54+
Run Asynchronous Keyword With Return Value And Both Named and Variable Arguments
55+
${result}= Concatenate Arguments With Var And Named Arguments Async prefix One Two a=1 b=${2}
56+
Should Be Equal ${result} prefix["One","Two",{"a":"1","b":2}]
57+
3458
Run Asynchronous Failing Keyword
3559
Run Keyword And Expect Error Error Just Fail Async
3660

test/testlibrary.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ lib.concatenateArgumentsWithCommentsInArgs = function (arg1, arg2 /*, skipped ar
1717
return arg1 + arg2;
1818
};
1919

20+
lib.concatenateArgumentsWithVarArguments = function (prefix, ...args) {
21+
return prefix + JSON.stringify(args);
22+
};
23+
lib.concatenateArgumentsWithVarArguments.args = ['prefix', '*args'];
24+
25+
lib.concatenateArgumentsWithNamedArguments = function (prefix, kwargs) {
26+
return prefix + JSON.stringify(kwargs);
27+
};
28+
lib.concatenateArgumentsWithNamedArguments.args = ['prefix', '**kwargs'];
29+
30+
// For language constraints, var arguments should be declared at the end, so the last
31+
// args will be kwargs
32+
lib.concatenateArgumentsWithVarAndNamedArguments = function (prefix, ...args) {
33+
return prefix + JSON.stringify(args);
34+
};
35+
lib.concatenateArgumentsWithVarAndNamedArguments.args = ['prefix', '*args', '**kwargs'];
36+
2037
lib.justFail = function () {
2138
throw new Error();
2239
};
@@ -33,6 +50,29 @@ lib.concatenateArgumentsAsync = function (arg1, arg2) {
3350
});
3451
};
3552

53+
lib.concatenateArgumentsWithVarArgumentsAsync = function (prefix, ...args) {
54+
return new Promise(function (resolve) {
55+
resolve(prefix + JSON.stringify(args));
56+
});
57+
};
58+
lib.concatenateArgumentsWithVarArgumentsAsync.args = ['prefix', '*args'];
59+
60+
lib.concatenateArgumentsWithNamedArgumentsAsync = function (prefix, kwargs) {
61+
return new Promise(function (resolve) {
62+
resolve(prefix + JSON.stringify(kwargs));
63+
});
64+
};
65+
lib.concatenateArgumentsWithNamedArgumentsAsync.args = ['prefix', '**kwargs'];
66+
67+
// For language constraints, var arguments should be declared at the end, so the last
68+
// args will be kwargs
69+
lib.concatenateArgumentsWithVarAndNamedArgumentsAsync = function (prefix, ...args) {
70+
return new Promise(function (resolve) {
71+
resolve(prefix + JSON.stringify(args));
72+
});
73+
};
74+
lib.concatenateArgumentsWithVarAndNamedArgumentsAsync.args = ['prefix', '*args', '**kwargs'];
75+
3676
lib.justFailAsync = function () {
3777
return new Promise(function (resolve, reject) {
3878
reject(new Error());

0 commit comments

Comments
 (0)