Skip to content

Commit abf520a

Browse files
committed
Add tests to wrapper docorators and lamda
1 parent f8fac47 commit abf520a

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

atest/DynamicTypesLibrary.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import sys
23

34
from robotlibcore import DynamicCore, keyword
@@ -7,6 +8,13 @@ def def_deco(func):
78
return func
89

910

11+
def deco_wraps(func):
12+
@functools.wraps(func)
13+
def wrapper(*args, **kwargs):
14+
return func(*args, **kwargs)
15+
return wrapper
16+
17+
1018
class DynamicTypesLibrary(DynamicCore):
1119

1220
def __init__(self, arg=False):
@@ -61,3 +69,9 @@ def is_python_3(self):
6169
@def_deco
6270
def keyword_with_def_deco(self):
6371
return 1
72+
73+
@deco_wraps
74+
@keyword
75+
@deco_wraps
76+
def keyword_wrapped(self, number=1, arg=''):
77+
return number, arg

src/robotlibcore.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,13 @@ def get_keyword_source(self, keyword_name):
210210

211211
def __get_keyword_line(self, method):
212212
try:
213-
source, line_number = inspect.getsourcelines(method)
213+
lines, line_number = inspect.getsourcelines(method)
214214
except (OSError, IOError, TypeError):
215215
return None
216-
for line in source:
217-
if line.strip().startswith('def'):
218-
return line_number
219-
line_number += 1
216+
for increment, line in enumerate(lines):
217+
if line.strip().startswith('def '):
218+
return line_number + increment
219+
return line_number
220220

221221
def __get_keyword_path(self, method):
222222
try:

utest/test_get_keyword_source.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
from os import path
33

44
import pytest
5+
from mockito.matchers import Any
6+
57
from DynamicLibrary import DynamicLibrary
68
from DynamicTypesLibrary import DynamicTypesLibrary
7-
from mockito.matchers import Any
9+
from robotlibcore import PY2
810

911

1012
@pytest.fixture(scope='module')
@@ -21,6 +23,7 @@ def lib_types():
2123
def cur_dir():
2224
return path.dirname(__file__)
2325

26+
2427
@pytest.fixture(scope='module')
2528
def lib_path(cur_dir):
2629
return path.normpath(path.join(cur_dir, '..', 'atest', 'DynamicLibrary.py'))
@@ -46,6 +49,12 @@ def test_location_in_class(lib, lib_path_components):
4649
assert source == '%s:15' % lib_path_components
4750

4851

52+
@pytest.mark.skipif(PY2, reason='Only applicable on Python 3')
53+
def test_decorator_wrapper(lib_types, lib_path_types):
54+
source = lib_types.get_keyword_source('keyword_wrapped')
55+
assert source == '%s:76' % lib_path_types
56+
57+
4958
def test_location_in_class_custom_keyword_name(lib, lib_path_components):
5059
source = lib.get_keyword_source('Custom name')
5160
assert source == '%s:19' % lib_path_components
@@ -72,7 +81,7 @@ def test_no_path_and_no_line_number(lib, when):
7281

7382
def test_def_in_decorator(lib_types, lib_path_types):
7483
source = lib_types.get_keyword_source('keyword_with_def_deco')
75-
assert source == '%s:62' % lib_path_types
84+
assert source == '%s:70' % lib_path_types
7685

7786

7887
def test_error_in_getfile(lib, when):
@@ -84,4 +93,4 @@ def test_error_in_getfile(lib, when):
8493
def test_error_in_line_number(lib, when, lib_path):
8594
when(inspect).getsourcelines(Any()).thenRaise(IOError('Some message'))
8695
source = lib.get_keyword_source('keyword_in_main')
87-
assert source == lib_path
96+
assert source == lib_path

0 commit comments

Comments
 (0)