
    h6                     x   d Z dZddlZddlZddlZddlZddlZ	 ddlmZ	 n# e
$ r ddlZ	Y nw xY w ej        d          Z ej                    j        Z e            Z e            Zd Zd Zd Zd Z G d	 d
e          Zd Zd Zd Zd Zd Z G d de          Zd Z  G d dej!        e          Z!d Z"dS )a  Adds support for parameterized tests to Python's unittest TestCase class.

A parameterized test is a method in a test case that is invoked with different
argument tuples.

A simple example:

  class AdditionExample(_parameterized.TestCase):
    @_parameterized.parameters(
       (1, 2, 3),
       (4, 5, 9),
       (1, 1, 3))
    def testAddition(self, op1, op2, result):
      self.assertEqual(result, op1 + op2)


Each invocation is a separate test case and properly isolated just
like a normal test method, with its own setUp/tearDown cycle. In the
example above, there are three separate testcases, one of which will
fail due to an assertion error (1 + 1 != 3).

Parameters for individual test cases can be tuples (with positional parameters)
or dictionaries (with named parameters):

  class AdditionExample(_parameterized.TestCase):
    @_parameterized.parameters(
       {'op1': 1, 'op2': 2, 'result': 3},
       {'op1': 4, 'op2': 5, 'result': 9},
    )
    def testAddition(self, op1, op2, result):
      self.assertEqual(result, op1 + op2)

If a parameterized test fails, the error message will show the
original test name (which is modified internally) and the arguments
for the specific invocation, which are part of the string returned by
the shortDescription() method on test cases.

The id method of the test, used internally by the unittest framework,
is also modified to show the arguments. To make sure that test names
stay the same across several invocations, object representations like

  >>> class Foo(object):
  ...  pass
  >>> repr(Foo())
  '<__main__.Foo object at 0x23d8610>'

are turned into '<__main__.Foo>'. For even more descriptive names,
especially in test logs, you can use the named_parameters decorator. In
this case, only tuples are supported, and the first parameters has to
be a string (or an object that returns an apt name when converted via
str()):

  class NamedExample(_parameterized.TestCase):
    @_parameterized.named_parameters(
       ('Normal', 'aa', 'aaa', True),
       ('EmptyPrefix', '', 'abc', True),
       ('BothEmpty', '', '', True))
    def testStartsWith(self, prefix, string, result):
      self.assertEqual(result, strings.startswith(prefix))

Named tests also have the benefit that they can be run individually
from the command line:

  $ testmodule.py NamedExample.testStartsWithNormal
  .
  --------------------------------------------------------------------
  Ran 1 test in 0.000s

  OK

Parameterized Classes
=====================
If invocation arguments are shared across test methods in a single
TestCase class, instead of decorating all test methods
individually, the class itself can be decorated:

  @_parameterized.parameters(
    (1, 2, 3)
    (4, 5, 9))
  class ArithmeticTest(_parameterized.TestCase):
    def testAdd(self, arg1, arg2, result):
      self.assertEqual(arg1 + arg2, result)

    def testSubtract(self, arg2, arg2, result):
      self.assertEqual(result - arg1, arg2)

Inputs from Iterables
=====================
If parameters should be shared across several test cases, or are dynamically
created from other sources, a single non-tuple iterable can be passed into
the decorator. This iterable will be used to obtain the test cases:

  class AdditionExample(_parameterized.TestCase):
    @_parameterized.parameters(
      c.op1, c.op2, c.result for c in testcases
    )
    def testAddition(self, op1, op2, result):
      self.assertEqual(result, op1 + op2)


Single-Argument Test Methods
============================
If a test method takes only one argument, the single argument does not need to
be wrapped into a tuple:

  class NegativeNumberExample(_parameterized.TestCase):
    @_parameterized.parameters(
       -1, -3, -4, -5
    )
    def testIsNegative(self, arg):
      self.assertTrue(IsNegative(arg))
z!tmarek@google.com (Torsten Marek)    Nz0\<([a-zA-Z0-9_\-\.]+) object at 0x[a-fA-F0-9]+\>c                 R    t                               dt          |                     S )Nz<\1>)ADDR_REsubreprobjs    i/var/www/html/prod/cognitive/venv/lib/python3.11/site-packages/google/protobuf/internal/_parameterized.py
_CleanReprr
      s    	Wd3ii	(	((    c                 $    | j         d| j        S )N.)
__module____name__)clss    r	   	_StrClassr      s    NNNCLL	11r   c                 b    t          | t          j                  ot          | t                     S N)
isinstancecollections_abcIterablestrr   s    r	   _NonStringIterabler      s,    
S/2
3
3 #c""
"$r   c                 &   t          | t          j                  r1d                    d |                                 D                       S t          |           r(d                    t          t          |                     S t          | f          S )Nz, c              3   D   K   | ]\  }}|d t          |          V  dS )=N)r
   ).0argnamevalues      r	   	<genexpr>z'_FormatParameterList.<locals>.<genexpr>   sV       D D'% !(E):):):; D D D D D Dr   )	r   r   Mappingjoinitemsr   mapr
   _FormatParameterList)testcase_paramss    r	   r$   r$      s    !899 499 D D+:+@+@+B+BD D D D D D/** 499S_55666 2333r   c                   $    e Zd ZdZd Zd Zd ZdS )_ParameterizedTestIterz9Callable and iterable class for producing new test cases.c                 0    || _         || _        || _        dS )a\  Returns concrete test functions for a test and a list of parameters.

    The naming_type is used to determine the name of the concrete
    functions as reported by the unittest framework. If naming_type is
    _FIRST_ARG, the testcases must be tuples, and the first element must
    have a string representation that is a valid Python identifier.

    Args:
      test_method: The decorated test method.
      testcases: (list of tuple/dict) A list of parameter
                 tuples/dicts for individual test invocations.
      naming_type: The test naming type, either _NAMED or _ARGUMENT_REPR.
    N)_test_method	testcases_naming_type)selftest_methodr*   naming_types       r	   __init__z_ParameterizedTestIter.__init__   s!     $DDN#Dr   c                      t          d          )NzYou appear to be running a parameterized test case without having inherited from parameterized.TestCase. This is bad because none of your test cases are actually being run.)RuntimeError)r,   argskwargss      r	   __call__z_ParameterizedTestIter.__call__   s    
 A B B Br   c                 V    | j         | j        fdfd| j        D             S )Nc                     t          j                   fd            }t          u r5d|_        |xj        t           d                   z  c_         dd           n4t          u rdt                     d|_        nt          d          |j        dt                     d|_
        j
        r|xj
        dj
        z  c_
        |S )	Nc                     t          t          j                  r | fi  d S t                    r | gR   d S  |            d S r   )r   r   r    r   )r,   r-   r%   s    r	   BoundParamTestzS_ParameterizedTestIter.__iter__.<locals>.MakeBoundParamTest.<locals>.BoundParamTest   s|    o'>?? 	-
+d
.
.o
.
.
.
.
.00 	-
+d
-_
-
-
-
-
-
-
+dO
,
,
,
,
,r   Tr      ()z is not a valid naming type.
)	functoolswraps
_FIRST_ARG__x_use_name__r   r   _ARGUMENT_REPRr$   __x_extra_id__r1   __doc__)r%   r8   r.   r-   s   ` r	   MakeBoundParamTestz;_ParameterizedTestIter.__iter__.<locals>.MakeBoundParamTest   s    {##- - - - - $#- 

	"	" )-%3q'9#:#::)!""-.((( !1111)4%% {{{LMMM 
!
!
!#7#H#H#H#H Jn		 BK,?,?"AAr   c              3   .   K   | ]} |          V  d S r    )r   crD   s     r	   r   z2_ParameterizedTestIter.__iter__.<locals>.<genexpr>   s/      ::aq!!::::::r   )r)   r+   r*   )r,   rD   r.   r-   s    @@@r	   __iter__z_ParameterizedTestIter.__iter__   sQ    #K#K     > ;:::4>::::r   N)r   r   __qualname__rC   r/   r4   rH   rF   r   r	   r'   r'      sJ        AA$ $ $$B B B#; #; #; #; #;r   r'   c                 `    t          |           dk    ot          | d         t                     S )z<True iff testcases contains only a single non-tuple element.r9   r   )lenr   tupler*   s    r	   _IsSingletonListrN      s)    	Y1		DZ	!e%D%D!DDr   c                    t          | dd           rJ d| d            i x| _        }| j                                                                        D ]\  }}|                    t          j        j                  rxt          |t          j                  r^t          | |           i }t          |||t          |||                     |                                D ]\  }}t          | ||           d S )N
_id_suffixzCannot add parameters to z*, which already has parameterized methods.)getattrrP   __dict__copyr"   
startswithunittest
TestLoadertestMethodPrefixr   typesFunctionTypedelattr _UpdateClassDictForParamTestCaser'   setattr)class_objectr*   r.   	id_suffixnamer   methodsmeths           r	   _ModifyClassrb      s'   \<66 E E E5A\\DE E	6 )+*,I  (--//5577 	* 	*idC+<== *sE.//*lD!!!g&
9d
 i
=
=? ? ?   * **$dD))))	* 	*r   c                       fd}t                    r't          d                   s
J d            d         |S )zImplementation of the parameterization decorators.

  Args:
    naming_type: The naming type.
    testcases: Testcase parameters.

  Returns:
    A function for modifying the decorated object.
  c                     t          | t                    r<t          | t          t          j                  st                    n           | S t          |           S r   )r   typerb   r   Sequencelistr'   )r   r.   r*   s    r	   _Applyz#_ParameterDecorator.<locals>._Apply  si    #t A
!+I7O!P!P $y///
	  
 j#CK@@@r   r   z7Single parameter argument must be a non-string iterable)rN   r   )r.   r*   rh   s   `` r	   _ParameterDecoratorri     su    	A 	A 	A 	A 	A 	A i   il++ C CAC C+!I	-r   c                  ,    t          t          |           S )ai  A decorator for creating parameterized tests.

  See the module docstring for a usage example.
  Args:
    *testcases: Parameters for the decorated method, either a single
                iterable, or a list of tuples/dicts/objects (for tests
                with only one argument).

  Returns:
     A test generator to be handled by TestGeneratorMetaclass.
  )ri   rA   rM   s    r	   
parametersrk     s     
^Y	7	77r   c                  ,    t          t          |           S )a  A decorator for creating parameterized tests.

  See the module docstring for a usage example. The first element of
  each parameter tuple should be a string and will be appended to the
  name of the test method.

  Args:
    *testcases: Parameters for the decorated method, either a single
                iterable, or a list of tuples.

  Returns:
     A test generator to be handled by TestGeneratorMetaclass.
  )ri   r?   rM   s    r	   named_parametersrm   .  s     
Z	3	33r   c                       e Zd ZdZd ZdS )TestGeneratorMetaclassa  Metaclass for test cases with test generators.

  A test generator is an iterable in a testcase that produces callables. These
  callables must be single-argument methods. These methods are injected into
  the class namespace and the original iterable is removed. If the name of the
  iterable conforms to the test pattern, the injected methods will be picked
  up as tests by the unittest framework.

  In general, it is supposed to be used in conjunction with the
  parameters decorator.
  c                 t   i x|d<   }|                                                                 D ]n\  }}|                    t          j        j                  rEt          |          r6t          |          }|                    |           t          ||||           ot                              | |||          S )NrP   )rS   r"   rT   rU   rV   rW   r   iterpopr[   re   __new__)mcs
class_namebasesdctr^   r_   r   iterators           r	   rs   zTestGeneratorMetaclass.__new__L  s    $&&C	XXZZ%%'' I I	c
//(->
?
? I
S
!
!I99(ixHHH<<Z444r   N)r   r   rI   rC   rs   rF   r   r	   ro   ro   ?  s-        
 
	5 	5 	5 	5 	5r   ro   c                 
   t          |          D ]r\  }}t          |          sJ d|            t          |dd          r|j        }nd|t          |fz  }|| vsJ d|d            || |<   t          |dd          ||<   sd	S )
a  Adds individual test cases to a dictionary.

  Args:
    dct: The target dictionary.
    id_suffix: The dictionary for mapping names to test IDs.
    name: The original name of the test case.
    iterator: The iterator generating the individual test cases.
  z*Test generators must yield callables, got r@   Fz%s%s%dz!Name of parameterized test case "z" not uniquerB    N)	enumeratecallablerQ   r   
_SEPARATOR)rw   r^   r_   rx   idxfuncnew_names          r	   r[   r[   X  s     X&& 
> 
>ic4D>>     >t%u-- 4hhT:s33h3=EXXG CM!$(8"==Ih
> 
>r   c                   $    e Zd ZdZd Zd Zd ZdS )TestCasez9Base class for test cases using the parameters decorator.c                 L    | j                             t                    d         S )Nr   )_testMethodNamesplitr}   r,   s    r	   _OriginalNamezTestCase._OriginalNameq  s    %%j11!44r   c                 Z    |                                  dt          | j                  dS )Nz (r;   )r   r   	__class__r   s    r	   __str__zTestCase.__str__t  s/    **,,,,i.G.G.G.GHHr   c                     t          | j                  d|                                 | j                            | j        d          S )zReturns the descriptive ID of the test.

    This is used internally by the unittesting framework to get a name
    for the test to be used in reports.

    Returns:
      The test id.
    r   rz   )r   r   r   rP   getr   r   s    r	   idzTestCase.idw  sP     "$.1111**,,,++D,@"EEEG Gr   N)r   r   rI   rC   r   r   r   rF   r   r	   r   r   n  sO        AA5 5 5I I IG G G G Gr   r   )	metaclassc                 d    t          d| j        t          fi           } |d| t          fi           S )a$  Returns a new base class with a cooperative metaclass base.

  This enables the TestCase to be used in combination
  with other base classes that have custom metaclasses, such as
  mox.MoxTestBase.

  Only works with metaclasses that do not override type.__new__.

  Example:

    import google3
    import mox

    from google.protobuf.internal import _parameterized

    class ExampleTest(parameterized.CoopTestCase(mox.MoxTestBase)):
      ...

  Args:
    other_base_class: (class) A test case base class.

  Returns:
    A new class object.
  CoopMetaclassCoopTestCase)re   __metaclass__ro   r   )other_base_classr   s     r	   r   r     sJ    2 % "$ $) 
"B
( 
( (r   )#rC   
__author__r=   rerX   rU   uuidcollections.abcabcr   ImportErrorcollectionscompiler   uuid1hexr}   objectr?   rA   r
   r   r   r$   r'   rN   rb   ri   rk   rm   re   ro   r[   r   r   rF   r   r	   <module>r      s  o ob 1
     				   (+++++++ ( ( (''''''( "*H
I
ITZ\\
VXX
) ) )2 2 2$ $ $
4 4 4>; >; >; >; >;V >; >; >;BE E E
* * *&  :8 8 84 4 4"5 5 5 5 5T 5 5 52> > >,G G G G Gx ,B G G G G.( ( ( ( (s   ! 	--