Skip to content

Commit 7e27df7

Browse files
committed
Acceptance tests for dynamic library support. #18
Also a fix for a bug found by the tests.
1 parent a9b6ff3 commit 7e27df7

File tree

6 files changed

+185
-2
lines changed

6 files changed

+185
-2
lines changed

src/robotremoteserver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def _get_kwargs_support(self, run_keyword):
234234
return len(spec.args) > 3 # self, name, args, kwargs=None
235235

236236
def run_keyword(self, name, args, kwargs=None):
237-
args = [name, args, kwargs]
237+
args = [name, args, kwargs] if kwargs else [name, args]
238238
return KeywordRunner(self._run_keyword).run_keyword(args)
239239

240240
def get_keyword_arguments(self, name):

test/atest/dynamic.robot

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
*** Settings ***
2+
Resource resource.robot
3+
Suite Setup Start And Import Remote Library Dynamic.py
4+
Suite Teardown Stop Remote Library
5+
6+
*** Test Cases ***
7+
Passing
8+
Passing
9+
Passing one arg accepted
10+
11+
Failing
12+
[Documentation] FAIL The message
13+
Failing The message
14+
15+
Logging
16+
[Documentation] LOG 1 INFO Hello, world! LOG 2 DEBUG Hi!
17+
Logging Hello, world!
18+
Logging Hi! DEBUG
19+
20+
Returning
21+
${result} = Returning
22+
Should Be Equal ${result} Hello, world!
23+
24+
Named arguments
25+
[Documentation] FAIL Bye!! LOG 2 INFO Hi, again! LOG 3 FAIL Bye!!
26+
Passing arg=ignored
27+
Logging level=INFO message=Hi, again!
28+
Failing message=Bye!!
29+
30+
Kwargs
31+
[Template] Kwargs
32+
${EMPTY}
33+
a: 1 a=1
34+
c=3 a=1 expected=a: 1, b: 2, c: 3 b=2
35+
36+
Called with invalid arguments when arguments are known
37+
[Documentation] FAIL Keyword 'Remote.Passing' expected 0 to 1 arguments, got 6.
38+
Passing more than one arg not accepted
39+
40+
Called with invalid arguments when arguments are not known
41+
[Documentation] FAIL GLOB: TypeError: *returning* *0* *3*
42+
Returning too many arguments

test/atest/hybrid.robot

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
*** Settings ***
2+
Resource resource.robot
3+
Suite Setup Start And Import Remote Library Hybrid.py
4+
Suite Teardown Stop Remote Library
5+
6+
*** Test Cases ***
7+
Passing
8+
Passing
9+
Passing one arg accepted
10+
11+
Failing
12+
[Documentation] FAIL The message
13+
Failing The message
14+
15+
Logging
16+
[Documentation] LOG 1 INFO Hello, world! LOG 2 DEBUG Hi!
17+
Logging Hello, world!
18+
Logging Hi! DEBUG
19+
20+
Returning
21+
${result} = Returning
22+
Should Be Equal ${result} Hello, world!
23+
24+
Named arguments
25+
[Documentation] FAIL Bye!! LOG 2 INFO Hi, again! LOG 3 FAIL Bye!!
26+
Passing arg=ignored
27+
Logging level=INFO message=Hi, again!
28+
Failing message=Bye!!
29+
30+
Kwargs
31+
[Template] Kwargs
32+
${EMPTY}
33+
a: 1 a=1
34+
c=3 a=1 expected=a: 1, b: 2, c: 3 b=2
35+
36+
Called with invalid arguments
37+
[Documentation] FAIL Keyword 'Remote.Passing' expected 0 to 1 arguments, got 6.
38+
Passing more than one arg not accepted

test/libs/Dynamic.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Dynamic(object):
2+
kws = {'passing': ['arg=None'],
3+
'failing': ['message'],
4+
'logging': ['message', 'level=INFO'],
5+
'returning': None,
6+
'kwargs': ['expected', '**kws']}
7+
8+
def get_keyword_names(self):
9+
return list(self.kws)
10+
11+
def run_keyword(self, name, args, kwargs=None):
12+
kw = globals()[name]
13+
return kw(*args, **(kwargs or {}))
14+
15+
def get_keyword_arguments(self, name):
16+
return self.kws[name]
17+
18+
19+
def passing(arg=None):
20+
assert not arg or '=' not in arg
21+
22+
23+
def failing(message):
24+
raise AssertionError(message)
25+
26+
27+
def logging(message, level='INFO'):
28+
print('*%s* %s' % (level, message))
29+
30+
31+
def returning():
32+
return 'Hello, world!'
33+
34+
35+
def kwargs(expected, **kws):
36+
actual = ', '.join('%s: %s' % (k, kws[k]) for k in sorted(kws))
37+
assert actual == expected
38+
39+
40+
if __name__ == '__main__':
41+
import sys
42+
from robotremoteserver import RobotRemoteServer
43+
44+
RobotRemoteServer(Dynamic(), '127.0.0.1', *sys.argv[1:])
45+

test/libs/Hybrid.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Hybrid(object):
2+
3+
def get_keyword_names(self):
4+
return ['passing', 'failing', 'logging', 'returning', 'kwargs']
5+
6+
def __getattr__(self, name):
7+
try:
8+
return globals()[name]
9+
except KeyError:
10+
raise AttributeError(name)
11+
12+
13+
def passing(arg=None):
14+
assert not arg or '=' not in arg
15+
16+
17+
def failing(message):
18+
raise AssertionError(message)
19+
20+
21+
def logging(message, level='INFO'):
22+
print('*%s* %s' % (level, message))
23+
24+
25+
def returning():
26+
return 'Hello, world!'
27+
28+
29+
def kwargs(expected, **kws):
30+
actual = ', '.join('%s: %s' % (k, kws[k]) for k in sorted(kws))
31+
assert actual == expected
32+
33+
34+
if __name__ == '__main__':
35+
import sys
36+
from robotremoteserver import RobotRemoteServer
37+
38+
RobotRemoteServer(Hybrid(), '127.0.0.1', *sys.argv[1:])
39+

test/utest/test_robotremoteserver.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def get_keyword_names(self):
6666

6767
def run_keyword(self, name, args, kwargs=None):
6868
kw = getattr(self.library, name)
69-
return kw(*args, **kwargs)
69+
return kw(*args, **(kwargs or {}))
7070

7171
def not_included(self):
7272
"""Not returned by get_keyword_names"""
@@ -76,6 +76,17 @@ def streams(self):
7676
return self.library.streams
7777

7878

79+
class DynamicLibraryWithoutKwargs(DynamicLibrary):
80+
81+
def run_keyword(self, name, args):
82+
kwargs = dict(item for item in self._pop_kwargs(args))
83+
return DynamicLibrary.run_keyword(self, name, args, kwargs)
84+
85+
def _pop_kwargs(self, args):
86+
while args and '=' in args[-1]:
87+
yield args.pop().split('=', 1)
88+
89+
7990
class TestStaticApi(unittest.TestCase):
8091
library = StaticLibrary()
8192

@@ -155,5 +166,13 @@ class TestDynamicApi(TestStaticApi):
155166
library = DynamicLibrary()
156167

157168

169+
class TestDynamicApiWithoutKwargs(TestStaticApi):
170+
library = DynamicLibraryWithoutKwargs()
171+
172+
def _run(self, kw, *args, **kwargs):
173+
args = list(args) + ['%s=%s' % item for item in kwargs.items()]
174+
return self.server.run_keyword(kw, args)
175+
176+
158177
if __name__ == '__main__':
159178
unittest.main()

0 commit comments

Comments
 (0)