1
+ from tests import unittest
2
+
3
+ from jmespath import compat
4
+ from jmespath import exceptions
5
+ from jmespath import functions
6
+
7
+ class TestFunctionSignatures (unittest .TestCase ):
8
+
9
+ def setUp (self ):
10
+ self ._functions = functions .Functions ()
11
+
12
+ def test_signature_with_monotype_argument (self ):
13
+ (function_name , signature ) = self ._make_test ("_function_with_monotyped_arguments" )
14
+ self ._functions ._validate_arguments (['string' ], signature , function_name )
15
+ self .assertRaises (
16
+ exceptions .ArityError , lambda :
17
+ self ._functions ._validate_arguments ([], signature , function_name )
18
+ )
19
+
20
+ def test_signature_with_optional_arguments (self ):
21
+ (function_name , signature ) = self ._make_test ("_function_with_optional_arguments" )
22
+ self ._functions ._validate_arguments (['string' ], signature , function_name )
23
+ self ._functions ._validate_arguments (['string' , 42 ], signature , function_name )
24
+ self ._functions ._validate_arguments (['string' , 43 ], signature , function_name )
25
+ self .assertRaises (
26
+ exceptions .ArityError , lambda :
27
+ self ._functions ._validate_arguments ([], signature , function_name )
28
+ )
29
+ self .assertRaises (
30
+ exceptions .ArityError , lambda :
31
+ self ._functions ._validate_arguments (['string' , 42 , 43 , 44 ], signature , function_name )
32
+ )
33
+
34
+ def test_signature_with_variadic_arguments (self ):
35
+ (function_name , signature ) = self ._make_test ("_function_with_variadic_arguments" )
36
+ self ._functions ._validate_arguments (['string' , 'text1' ], signature , function_name )
37
+ self ._functions ._validate_arguments (['string' , 'text1' , 'text2' ], signature , function_name )
38
+ self .assertRaises (
39
+ exceptions .VariadictArityError , lambda :
40
+ self ._functions ._validate_arguments (['string' ], signature , function_name )
41
+ )
42
+
43
+ def _make_test (self , funcName ):
44
+ for name , method in compat .get_methods (TestFunctionSignatures ):
45
+ print (name )
46
+ if name != funcName :
47
+ continue
48
+ signature = getattr (method , 'signature' , None )
49
+ return (funcName , signature )
50
+ return None
51
+
52
+ ## arg1 can only be of type 'string'
53
+ ## this signature supports testing simplified syntax
54
+ ## where 'type' is a string instead of an array of strings
55
+ @functions .signature ({'type' : 'string' })
56
+ def _function_with_monotyped_arguments (self , arg1 ):
57
+ return None
58
+
59
+ @functions .signature ({'type' : 'string' }, {'type' : 'string' , 'variadic' : True })
60
+ def _function_with_variadic_arguments (self , arg1 , * arguments ):
61
+ return None
62
+
63
+ @functions .signature ({'type' : 'string' }, {'type' : 'number' , 'optional' : True }, {'type' : 'number' , 'optional' : True })
64
+ def _function_with_optional_arguments (self , arg1 , opt1 , opt2 ):
65
+ return None
66
+
67
+
68
+ if __name__ == '__main__' :
69
+ unittest .main ()
0 commit comments