1+ import inspect
2+ from os import path
3+
4+ import pytest
5+ from DynamicLibrary import DynamicLibrary
6+ from DynamicTypesLibrary import DynamicTypesLibrary
7+ from mockito .matchers import Any
8+
9+
10+ @pytest .fixture (scope = 'module' )
11+ def lib ():
12+ return DynamicLibrary ()
13+
14+
15+ @pytest .fixture (scope = 'module' )
16+ def lib_types ():
17+ return DynamicTypesLibrary ()
18+
19+
20+ @pytest .fixture (scope = 'module' )
21+ def cur_dir ():
22+ return path .dirname (__file__ )
23+
24+ @pytest .fixture (scope = 'module' )
25+ def lib_path (cur_dir ):
26+ return path .normpath (path .join (cur_dir , '..' , 'atest' , 'DynamicLibrary.py' ))
27+
28+
29+ @pytest .fixture (scope = 'module' )
30+ def lib_path_components (cur_dir ):
31+ return path .normpath (path .join (cur_dir , '..' , 'atest' , 'librarycomponents.py' ))
32+
33+
34+ @pytest .fixture (scope = 'module' )
35+ def lib_path_types (cur_dir ):
36+ return path .normpath (path .join (cur_dir , '..' , 'atest' , 'DynamicTypesLibrary.py' ))
37+
38+
39+ def test_location_in_main (lib , lib_path ):
40+ source = lib .get_keyword_source ('keyword_in_main' )
41+ assert source == '%s:20' % lib_path
42+
43+
44+ def test_location_in_class (lib , lib_path_components ):
45+ source = lib .get_keyword_source ('method' )
46+ assert source == '%s:15' % lib_path_components
47+
48+
49+ def test_location_in_class_custom_keyword_name (lib , lib_path_components ):
50+ source = lib .get_keyword_source ('Custom name' )
51+ assert source == '%s:19' % lib_path_components
52+
53+
54+ def test_no_line_number (lib , lib_path , when ):
55+ when (lib )._DynamicCore__get_keyword_line (Any ()).thenReturn (None )
56+ source = lib .get_keyword_source ('keyword_in_main' )
57+ assert source == lib_path
58+
59+
60+ def test_no_path (lib , when ):
61+ when (lib )._DynamicCore__get_keyword_path (Any ()).thenReturn (None )
62+ source = lib .get_keyword_source ('keyword_in_main' )
63+ assert source == ':20'
64+
65+
66+ def test_no_path_and_no_line_number (lib , when ):
67+ when (lib )._DynamicCore__get_keyword_path (Any ()).thenReturn (None )
68+ when (lib )._DynamicCore__get_keyword_line (Any ()).thenReturn (None )
69+ source = lib .get_keyword_source ('keyword_in_main' )
70+ assert source is None
71+
72+
73+ def test_def_in_decorator (lib_types , lib_path_types ):
74+ source = lib_types .get_keyword_source ('keyword_with_def_deco' )
75+ assert source == '%s:62' % lib_path_types
76+
77+
78+ def test_error_in_getfile (lib , when ):
79+ when (inspect ).getfile (Any ()).thenRaise (TypeError ('Some message' ))
80+ source = lib .get_keyword_source ('keyword_in_main' )
81+ assert source is None
82+
83+
84+ def test_error_in_line_number (lib , when , lib_path ):
85+ when (inspect ).getsourcelines (Any ()).thenRaise (IOError ('Some message' ))
86+ source = lib .get_keyword_source ('keyword_in_main' )
87+ assert source == lib_path
0 commit comments