- Writes tests for aircox streamer application; - Add test utilities in aircox Co-authored-by: bkfox <thomas bkfox net> Reviewed-on: #110
This commit is contained in:
		
							
								
								
									
										33
									
								
								aircox/test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								aircox/test.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
			
		||||
"""This module provide test utilities."""
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
__all__ = ("interface",)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def interface(obj, funcs):
 | 
			
		||||
    """Override provided object's functions using dict of funcs, as
 | 
			
		||||
    ``{func_name: return_value}``.
 | 
			
		||||
 | 
			
		||||
    Attribute ``obj.calls`` is a dict with all call done using those
 | 
			
		||||
    methods, as ``{func_name: (args, kwargs) | list[(args, kwargs]]}``.
 | 
			
		||||
    """
 | 
			
		||||
    if not isinstance(getattr(obj, "calls", None), dict):
 | 
			
		||||
        obj.calls = {}
 | 
			
		||||
    for attr, value in funcs.items():
 | 
			
		||||
        interface_wrap(obj, attr, value)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def interface_wrap(obj, attr, value):
 | 
			
		||||
    obj.calls[attr] = None
 | 
			
		||||
 | 
			
		||||
    def wrapper(*a, **kw):
 | 
			
		||||
        call = obj.calls.get(attr)
 | 
			
		||||
        if call is None:
 | 
			
		||||
            obj.calls[attr] = (a, kw)
 | 
			
		||||
        elif isinstance(call, tuple):
 | 
			
		||||
            obj.calls[attr] = [call, (a, kw)]
 | 
			
		||||
        else:
 | 
			
		||||
            call.append((a, kw))
 | 
			
		||||
        return value
 | 
			
		||||
 | 
			
		||||
    setattr(obj, attr, wrapper)
 | 
			
		||||
		Reference in New Issue
	
	Block a user