Ticket #7921: categories_testsuite-nt.patch

File categories_testsuite-nt.patch, 64.2 KB (added by nthiery, 12 years ago)

Patch 2: stronger category tests

  • sage/algebras/steenrod_algebra.py

    # HG changeset patch
    # User Nicolas M. Thiery <nthiery@users.sf.net>
    # Date 1263761923 -3600
    # Node ID e605190b01c65e5fac335cd4239e6f8834bbdf30
    # Parent  c844f8d6197b2089c42e8e3763967fbce58a3a22
    #7921: Patch 2: stronger category tests
    
     - More category tests (_test_category, _test_elements)
     - Improvements to TestSuite
       - Partial support for nested test suites
       - Support for basic TestSuite(x) for x any Python object
     - Added TestSuite call for Spec and ref to #7946
     - Corresponding doctest updates
    
    diff --git a/sage/algebras/steenrod_algebra.py b/sage/algebras/steenrod_algebra.py
    a b class SteenrodAlgebra_generic(Algebra): 
    119119            sage: A = SteenrodAlgebra(3)
    120120            sage: A.category()
    121121            Category of algebras over Finite Field of size 3
    122             sage: TestSuite(A).run()
     122            sage: TestSuite(A).run() # todo: fix category inheritance for elements of A
     123              Failure in _test_category:
     124              ...
     125              ------------------------------------------------------------
     126              The following tests failed: _test_category
     127            Failure in _test_elements
     128            The following tests failed: _test_elements
    123129        """
    124130        from sage.rings.arith import is_prime
    125131        if not is_prime(p):
  • sage/categories/algebras_with_basis.py

    diff --git a/sage/categories/algebras_with_basis.py b/sage/categories/algebras_with_basis.py
    a b class AlgebrasWithBasis(Category_over_ba 
    6060        running ._test_additive_associativity() . . . pass
    6161        running ._test_an_element() . . . pass
    6262        running ._test_associativity() . . . pass
    63         running ._test_element_pickling() . . . pass
     63        running ._test_category() . . . pass
     64        running ._test_elements() . . .
     65          Running the test suite of self.an_element()
     66          running ._test_category() . . . pass
     67          running ._test_not_implemented_methods() . . . pass
     68          running ._test_pickling() . . . pass
     69          pass
    6470        running ._test_not_implemented_methods() . . . pass
    6571        running ._test_one() . . . pass
    6672        running ._test_pickling() . . . pass
  • sage/categories/coxeter_groups.py

    diff --git a/sage/categories/coxeter_groups.py b/sage/categories/coxeter_groups.py
    a b class CoxeterGroups(Category): 
    6868        sage: W = CoxeterGroups().example(); TestSuite(W).run(verbose = "True")
    6969        running ._test_an_element() . . . pass
    7070        running ._test_associativity() . . . pass
    71         running ._test_element_pickling() . . . pass
     71        running ._test_category() . . . pass
     72        running ._test_elements() . . .
     73          Running the test suite of self.an_element()
     74          running ._test_category() . . . pass
     75          running ._test_not_implemented_methods() . . . pass
     76          running ._test_pickling() . . . pass
     77          pass
    7278        running ._test_enumerated_set_contains() . . . pass
    7379        running ._test_enumerated_set_iter_cardinality() . . . pass
    7480        running ._test_enumerated_set_iter_list() . . . pass
  • sage/categories/examples/commutative_additive_monoids.py

    diff --git a/sage/categories/examples/commutative_additive_monoids.py b/sage/categories/examples/commutative_additive_monoids.py
    a b class FreeCommutativeAdditiveMonoid(Free 
    4545        sage: TestSuite(S).run(verbose = True)
    4646        running ._test_additive_associativity() . . . pass
    4747        running ._test_an_element() . . . pass
    48         running ._test_element_pickling() . . . pass
     48        running ._test_category() . . . pass
     49        running ._test_elements() . . .
     50          Running the test suite of self.an_element()
     51          running ._test_category() . . . pass
     52          running ._test_not_implemented_methods() . . . pass
     53          running ._test_pickling() . . . pass
     54          pass
    4955        running ._test_not_implemented_methods() . . . pass
    5056        running ._test_pickling() . . . pass
    5157        running ._test_some_elements() . . . pass
  • sage/categories/examples/commutative_additive_semigroups.py

    diff --git a/sage/categories/examples/commutative_additive_semigroups.py b/sage/categories/examples/commutative_additive_semigroups.py
    a b class FreeCommutativeAdditiveSemigroup(U 
    4343        sage: TestSuite(S).run(verbose = True)
    4444        running ._test_additive_associativity() . . . pass
    4545        running ._test_an_element() . . . pass
    46         running ._test_element_pickling() . . . pass
     46        running ._test_category() . . . pass
     47        running ._test_elements() . . .
     48          Running the test suite of self.an_element()
     49          running ._test_category() . . . pass
     50          running ._test_not_implemented_methods() . . . pass
     51          running ._test_pickling() . . . pass
     52          pass
    4753        running ._test_not_implemented_methods() . . . pass
    4854        running ._test_pickling() . . . pass
    4955        running ._test_some_elements() . . . pass
  • sage/categories/examples/finite_coxeter_groups.py

    diff --git a/sage/categories/examples/finite_coxeter_groups.py b/sage/categories/examples/finite_coxeter_groups.py
    a b class DihedralGroup(UniqueRepresentation 
    6969        sage: TestSuite(G).run(verbose = True)
    7070        running ._test_an_element() . . . pass
    7171        running ._test_associativity() . . . pass
    72         running ._test_element_pickling() . . . pass
     72        running ._test_category() . . . pass
     73        running ._test_elements() . . .
     74          Running the test suite of self.an_element()
     75          running ._test_category() . . . pass
     76          running ._test_not_implemented_methods() . . . pass
     77          running ._test_pickling() . . . pass
     78          pass
    7379        running ._test_enumerated_set_contains() . . . pass
    7480        running ._test_enumerated_set_iter_cardinality() . . . pass
    7581        running ._test_enumerated_set_iter_list() . . . pass
  • sage/categories/examples/finite_enumerated_sets.py

    diff --git a/sage/categories/examples/finite_enumerated_sets.py b/sage/categories/examples/finite_enumerated_sets.py
    a b class Example(UniqueRepresentation, Pare 
    3434
    3535        sage: TestSuite(C).run(verbose = True)
    3636        running ._test_an_element() . . . pass
    37         running ._test_element_pickling() . . . pass
     37        running ._test_category() . . . pass
     38        running ._test_elements() . . .
     39          Running the test suite of self.an_element()
     40          running ._test_pickling() . . . pass
     41          pass
    3842        running ._test_enumerated_set_contains() . . . pass
    3943        running ._test_enumerated_set_iter_cardinality() . . . pass
    4044        running ._test_enumerated_set_iter_list() . . . pass
  • sage/categories/examples/finite_monoids.py

    diff --git a/sage/categories/examples/finite_monoids.py b/sage/categories/examples/finite_monoids.py
    a b class IntegerModMonoid(UniqueRepresentat 
    3737        sage: TestSuite(S).run(verbose = True)
    3838        running ._test_an_element() . . . pass
    3939        running ._test_associativity() . . . pass
    40         running ._test_element_pickling() . . . pass
     40        running ._test_category() . . . pass
     41        running ._test_elements() . . .
     42          Running the test suite of self.an_element()
     43          running ._test_category() . . . pass
     44          running ._test_not_implemented_methods() . . . pass
     45          running ._test_pickling() . . . pass
     46          pass
    4147        running ._test_enumerated_set_contains() . . . pass
    4248        running ._test_enumerated_set_iter_cardinality() . . . pass
    4349        running ._test_enumerated_set_iter_list() . . . pass
  • sage/categories/examples/finite_semigroups.py

    diff --git a/sage/categories/examples/finite_semigroups.py b/sage/categories/examples/finite_semigroups.py
    a b class LeftRegularBand(UniqueRepresentati 
    8080        sage: TestSuite(S).run(verbose = True)
    8181        running ._test_an_element() . . . pass
    8282        running ._test_associativity() . . . pass
    83         running ._test_element_pickling() . . . pass
     83        running ._test_category() . . . pass
     84        running ._test_elements() . . .
     85          Running the test suite of self.an_element()
     86          running ._test_category() . . . pass
     87          running ._test_not_implemented_methods() . . . pass
     88          running ._test_pickling() . . . pass
     89          pass
    8490        running ._test_enumerated_set_contains() . . . pass
    8591        running ._test_enumerated_set_iter_cardinality() . . . pass
    8692        running ._test_enumerated_set_iter_list() . . . pass
    8793        running ._test_not_implemented_methods() . . . pass
    8894        running ._test_pickling() . . . pass
    8995        running ._test_some_elements() . . . pass
    90 
    9196    """
    9297
    9398    def __init__(self, alphabet=('a','b','c','d')):
  • sage/categories/examples/finite_weyl_groups.py

    diff --git a/sage/categories/examples/finite_weyl_groups.py b/sage/categories/examples/finite_weyl_groups.py
    a b class SymmetricGroup(UniqueRepresentatio 
    5050        sage: TestSuite(S).run(verbose = True)
    5151        running ._test_an_element() . . . pass
    5252        running ._test_associativity() . . . pass
    53         running ._test_element_pickling() . . . pass
     53        running ._test_category() . . . pass
     54        running ._test_elements() . . .
     55          Running the test suite of self.an_element()
     56          running ._test_category() . . . pass
     57          running ._test_not_implemented_methods() . . . pass
     58          running ._test_pickling() . . . pass
     59          pass
    5460        running ._test_enumerated_set_contains() . . . pass
    5561        running ._test_enumerated_set_iter_cardinality() . . . pass
    5662        running ._test_enumerated_set_iter_list() . . . pass
  • sage/categories/examples/infinite_enumerated_sets.py

    diff --git a/sage/categories/examples/infinite_enumerated_sets.py b/sage/categories/examples/infinite_enumerated_sets.py
    a b class NonNegativeIntegers(UniqueRepresen 
    5151
    5252        sage: TestSuite(NN).run(verbose = True)
    5353        running ._test_an_element() . . . pass
    54         running ._test_element_pickling() . . . pass
     54        running ._test_category() . . . pass
     55        running ._test_elements() . . .
     56          Running the test suite of self.an_element()
     57          running ._test_category() . . . pass
     58          running ._test_not_implemented_methods() . . . pass
     59          running ._test_pickling() . . . pass
     60          pass
    5561        running ._test_enumerated_set_contains() . . . pass
    5662        running ._test_enumerated_set_iter_cardinality() . . . pass
    5763        running ._test_enumerated_set_iter_list() . . . pass
  • sage/categories/examples/monoids.py

    diff --git a/sage/categories/examples/monoids.py b/sage/categories/examples/monoids.py
    a b class FreeMonoid(FreeSemigroup): 
    4646        sage: TestSuite(S).run(verbose = True)
    4747        running ._test_an_element() . . . pass
    4848        running ._test_associativity() . . . pass
    49         running ._test_element_pickling() . . . pass
     49        running ._test_category() . . . pass
     50        running ._test_elements() . . .
     51          Running the test suite of self.an_element()
     52          running ._test_category() . . . pass
     53          running ._test_not_implemented_methods() . . . pass
     54          running ._test_pickling() . . . pass
     55          pass
    5056        running ._test_not_implemented_methods() . . . pass
    5157        running ._test_one() . . . pass
    5258        running ._test_pickling() . . . pass
  • sage/categories/examples/semigroups.py

    diff --git a/sage/categories/examples/semigroups.py b/sage/categories/examples/semigroups.py
    a b class LeftZeroSemigroup(UniqueRepresenta 
    4646        sage: TestSuite(S).run(verbose = True)
    4747        running ._test_an_element() . . . pass
    4848        running ._test_associativity() . . . pass
    49         running ._test_element_pickling() . . . pass
     49        running ._test_category() . . . pass
     50        running ._test_elements() . . .
     51          Running the test suite of self.an_element()
     52          running ._test_category() . . . pass
     53          running ._test_not_implemented_methods() . . . pass
     54          running ._test_pickling() . . . pass
     55          pass
    5056        running ._test_not_implemented_methods() . . . pass
    5157        running ._test_pickling() . . . pass
    5258        running ._test_some_elements() . . . pass
    class FreeSemigroup(UniqueRepresentation 
    165171        sage: TestSuite(S).run(verbose = True)
    166172        running ._test_an_element() . . . pass
    167173        running ._test_associativity() . . . pass
    168         running ._test_element_pickling() . . . pass
     174        running ._test_category() . . . pass
     175        running ._test_elements() . . .
     176          Running the test suite of self.an_element()
     177          running ._test_category() . . . pass
     178          running ._test_not_implemented_methods() . . . pass
     179          running ._test_pickling() . . . pass
     180          pass
    169181        running ._test_not_implemented_methods() . . . pass
    170182        running ._test_pickling() . . . pass
    171183        running ._test_some_elements() . . . pass
    class SubQuotientOfLeftZeroSemigroup(Uni 
    326338        sage: TestSuite(S).run(verbose = True)
    327339        running ._test_an_element() . . . pass
    328340        running ._test_associativity() . . . pass
    329         running ._test_element_pickling() . . . pass
     341        running ._test_category() . . . pass
     342        running ._test_elements() . . .
     343          Running the test suite of self.an_element()
     344          running ._test_category() . . . pass
     345          running ._test_not_implemented_methods() . . . pass
     346          running ._test_pickling() . . . pass
     347          pass
    330348        running ._test_not_implemented_methods() . . . pass
    331349        running ._test_pickling() . . . pass
    332350        running ._test_some_elements() . . . pass
    class IncompleteSubQuotientSemigroup(Uni 
    528546              ...
    529547            AssertionError: Not implemented method: lift
    530548
    531             sage: TestSuite(S).run(verbose = True)   # todo: improve the error messages
     549            sage: TestSuite(S).run(verbose = True)
    532550            running ._test_an_element() . . . pass
    533551            running ._test_associativity() . . . fail
    534552            Traceback (most recent call last):
    535553              ...
    536554            NotImplementedError: <abstract method retract at ...>
    537555            ------------------------------------------------------------
    538             running ._test_element_pickling() . . . pass
     556            running ._test_category() . . . pass
     557            running ._test_elements() . . .
     558              Running the test suite of self.an_element()
     559              running ._test_category() . . . pass
     560              running ._test_not_implemented_methods() . . . pass
     561              running ._test_pickling() . . . pass
     562              pass
    539563            running ._test_not_implemented_methods() . . . fail
    540564            Traceback (most recent call last):
    541565              ...
  • sage/categories/examples/semigroups_cython.pyx

    diff --git a/sage/categories/examples/semigroups_cython.pyx b/sage/categories/examples/semigroups_cython.pyx
    a b cdef class LeftZeroSemigroupElement(Elem 
    130130        """
    131131        return cmp(self._value, other._value)
    132132
    133     def __getattr__(self, name):
    134         """
    135         EXAMPLES::
    136 
    137             sage: from sage.categories.examples.semigroups_cython import LeftZeroSemigroup
    138             sage: S = LeftZeroSemigroup()
    139             sage: x = S(3)
    140             sage: x.is_idempotent
    141             <bound method LeftZeroSemigroupElement.is_idempotent of 3>
    142             sage: x.is_idempotent()
    143             True
    144         """
    145         result = getattr(self.parent().category().element_class, name)
    146         if isinstance(result, instancemethod):
    147             return instancemethod(result.im_func, self, self.__class__)
    148         elif isinstance(result,  method_descriptor):
    149             return result # should bind the method descriptor to appropriate object
    150         else:
    151             return result
    152 
    153133    # Apparently, python looks for __mul__, __pow__, ... in the
    154134    # class of self rather than in self itself. No big deal, since
    155135    # those will usually be defined in a cython super class of
    class LeftZeroSemigroup(LeftZeroSemigrou 
    229209        sage: TestSuite(S).run(verbose = True)
    230210        running ._test_an_element() . . . pass
    231211        running ._test_associativity() . . . pass
    232         running ._test_element_pickling() . . . pass
     212        running ._test_category() . . . pass
     213        running ._test_elements() . . .
     214          Running the test suite of self.an_element()
     215          running ._test_category() . . . pass
     216          running ._test_not_implemented_methods() . . . pass
     217          running ._test_pickling() . . . pass
     218          pass
    233219        running ._test_not_implemented_methods() . . . pass
    234220        running ._test_pickling() . . . pass
    235221        running ._test_some_elements() . . . pass
    class LeftZeroSemigroup(LeftZeroSemigrou 
    237223    That's really the only method which is obtained from the category ... ::
    238224
    239225        sage: S(42).is_idempotent
    240         <bound method LeftZeroSemigroupElement.is_idempotent of 42>
     226        <bound method IdempotentSemigroups.element_class.is_idempotent of 42>
    241227        sage: S(42).is_idempotent()
    242228        True
    243229
    244         sage: S(42)._pow_                 # how to bind it?
     230        sage: S(42)._pow_                 # todo: not implemented (how to bind it?)
    245231        <method '_pow_' of 'sage.categories.examples.semigroups_cython.IdempotentSemigroupsElement' objects>
    246232        sage: S(42)^10                    # todo: not implemented (see __getattr__)
    247233        42
    248234
    249         sage: S(42).is_idempotent_cpdef   # how to bind it?
     235        sage: S(42).is_idempotent_cpdef   #  todo: not implemented (how to bind it?)
    250236        <method 'is_idempotent_cpdef' of 'sage.categories.examples.semigroups_cython.IdempotentSemigroupsElement' objects>
    251237        sage: S(42).is_idempotent_cpdef() # todo: not implemented (see __getattr__)
    252238        True
  • sage/categories/examples/sets_cat.py

    diff --git a/sage/categories/examples/sets_cat.py b/sage/categories/examples/sets_cat.py
    a b class PrimeNumbers(UniqueRepresentation, 
    5858
    5959        sage: TestSuite(P).run(verbose=True)
    6060        running ._test_an_element() . . . pass
    61         running ._test_element_pickling() . . . pass
     61        running ._test_category() . . . pass
     62        running ._test_elements() . . .
     63          Running the test suite of self.an_element()
     64          running ._test_category() . . . pass
     65          running ._test_not_implemented_methods() . . . pass
     66          running ._test_pickling() . . . pass
     67          pass
    6268        running ._test_not_implemented_methods() . . . pass
    6369        running ._test_pickling() . . . pass
    6470        running ._test_some_elements() . . . pass
    class PrimeNumbers_Inherits(PrimeNumbers 
    325331
    326332        sage: TestSuite(P).run(verbose=True)
    327333        running ._test_an_element() . . . pass
    328         running ._test_element_pickling() . . . pass
     334        running ._test_category() . . . pass
     335        running ._test_elements() . . .
     336          Running the test suite of self.an_element()
     337          running ._test_category() . . . pass
     338          running ._test_not_implemented_methods() . . . pass
     339          running ._test_pickling() . . . pass
     340          pass
    329341        running ._test_not_implemented_methods() . . . pass
    330342        running ._test_pickling() . . . pass
    331343        running ._test_some_elements() . . . pass
    class PrimeNumbers_Wrapper(PrimeNumbers_ 
    437449
    438450    TESTS::
    439451
    440         sage: TestSuite(P).run(verbose=True)
    441         running ._test_an_element() . . . pass
    442         running ._test_element_pickling() . . . pass
    443         running ._test_not_implemented_methods() . . . pass
    444         running ._test_pickling() . . . pass
    445         running ._test_some_elements() . . . pass
     452        sage: TestSuite(P).run()
    446453    """
    447454    def __init__(self):
    448455        """
    class PrimeNumbers_Facade(PrimeNumbers_A 
    598605
    599606    TESTS::
    600607
    601         sage: TestSuite(P).run(verbose=True)
     608        sage: TestSuite(P).run(verbose = True)
    602609        running ._test_an_element() . . . pass
    603         running ._test_element_pickling() . . . pass
     610        running ._test_category() . . . pass
     611        running ._test_elements() . . .
     612          Running the test suite of self.an_element()
     613          running ._test_category() . . . pass
     614          running ._test_not_implemented_methods() . . . pass
     615          running ._test_pickling() . . . pass
     616          pass
    604617        running ._test_not_implemented_methods() . . . pass
    605618        running ._test_pickling() . . . pass
    606619        running ._test_some_elements() . . . pass
  • sage/categories/finite_semigroups.py

    diff --git a/sage/categories/finite_semigroups.py b/sage/categories/finite_semigroups.py
    a b class FiniteSemigroups(Category): 
    4141
    4242        sage: C = FiniteSemigroups()
    4343        sage: TestSuite(C).run(verbose = True)
     44        running ._test_category() . . . pass
    4445        running ._test_not_implemented_methods() . . . pass
    4546        running ._test_pickling() . . . pass
    4647
  • sage/categories/finite_weyl_groups.py

    diff --git a/sage/categories/finite_weyl_groups.py b/sage/categories/finite_weyl_groups.py
    a b class FiniteWeylGroups(Category): 
    3333        sage: TestSuite(W).run(verbose = "True")
    3434        running ._test_an_element() . . . pass
    3535        running ._test_associativity() . . . pass
    36         running ._test_element_pickling() . . . pass
     36        running ._test_category() . . . pass
     37        running ._test_elements() . . .
     38          Running the test suite of self.an_element()
     39          running ._test_category() . . . pass
     40          running ._test_not_implemented_methods() . . . pass
     41          running ._test_pickling() . . . pass
     42          pass
    3743        running ._test_enumerated_set_contains() . . . pass
    3844        running ._test_enumerated_set_iter_cardinality() . . . pass
    3945        running ._test_enumerated_set_iter_list() . . . pass
  • sage/categories/hopf_algebras_with_basis.py

    diff --git a/sage/categories/hopf_algebras_with_basis.py b/sage/categories/hopf_algebras_with_basis.py
    a b class HopfAlgebrasWithBasis(Category_ove 
    6969        running ._test_additive_associativity() . . . pass
    7070        running ._test_an_element() . . . pass
    7171        running ._test_associativity() . . . pass
    72         running ._test_element_pickling() . . . pass
     72        running ._test_category() . . . pass
     73        running ._test_elements() . . .
     74          Running the test suite of self.an_element()
     75          running ._test_category() . . . pass
     76          running ._test_not_implemented_methods() . . . pass
     77          running ._test_pickling() . . . pass
     78          pass
    7379        running ._test_not_implemented_methods() . . . pass
    7480        running ._test_one() . . . pass
    7581        running ._test_pickling() . . . pass
  • sage/categories/modules_with_basis.py

    diff --git a/sage/categories/modules_with_basis.py b/sage/categories/modules_with_basis.py
    a b class ModuleMorphismByLinearity(Morphism 
    500500
    501501        TESTS::
    502502
    503             sage: TestSuite(phi).run()
     503            sage: TestSuite(phi).run() # known issue
     504            Failure in _test_category:
     505            ...
     506            The following tests failed: _test_category
     507
     508        Analysis: ``phi`` does not inherit from the element class of
     509        the category of its parent::
     510
     511            sage: isinstance(phi, phi.parent().category().element_class)
     512            False
     513
     514        To be fixed in the general morphism overhaul (#....), possibly
     515        by making sure to create ``phi`` through its parent.
     516
    504517        """
    505518        if codomain is None and hasattr(on_basis, 'codomain'):
    506519            codomain = on_basis.codomain()
    class DiagonalModuleMorphism(ModuleMorph 
    630643
    631644            sage: phi.__class__
    632645            <class 'sage.categories.modules_with_basis.DiagonalModuleMorphism'>
    633             sage: TestSuite(phi).run()
     646            sage: TestSuite(phi).run() # known issue; see ModuleMorphismByLinearity.__init__
     647            Failure in _test_category:
     648            ...
     649            The following tests failed: _test_category
    634650        """
    635651        assert codomain is not None
    636652        assert domain.basis().keys() == codomain.basis().keys()
  • sage/categories/primer.py

    diff --git a/sage/categories/primer.py b/sage/categories/primer.py
    a b Generic tests 
    335335    sage: TestSuite(S).run(verbose = True)
    336336    running ._test_an_element() . . . pass
    337337    running ._test_associativity() . . . pass
    338     running ._test_element_pickling() . . . pass
     338    running ._test_category() . . . pass
     339    running ._test_elements() . . .
     340      Running the test suite of self.an_element()
     341      running ._test_category() . . . pass
     342      running ._test_not_implemented_methods() . . . pass
     343      running ._test_pickling() . . . pass
     344      pass
    339345    running ._test_enumerated_set_contains() . . . pass
    340346    running ._test_enumerated_set_iter_cardinality() . . . pass
    341347    running ._test_enumerated_set_iter_list() . . . pass
  • sage/categories/semigroups.py

    diff --git a/sage/categories/semigroups.py b/sage/categories/semigroups.py
    a b class Semigroups(Category): 
    4141
    4242        sage: C = Semigroups()
    4343        sage: TestSuite(C).run(verbose=True)
     44        running ._test_category() . . . pass
    4445        running ._test_not_implemented_methods() . . . pass
    4546        running ._test_pickling() . . . pass
    4647
  • sage/categories/sets_cat.py

    diff --git a/sage/categories/sets_cat.py b/sage/categories/sets_cat.py
    a b Sets 
    1212#******************************************************************************
    1313
    1414from sage.misc.cachefunc import cached_method
     15from sage.misc.sage_unittest import TestSuite
    1516from sage.misc.abstract_method import abstract_method
    1617from sage.misc.lazy_attribute import lazy_attribute
    1718from sage.categories.category import Category, HomCategory
    class Sets(Category): 
    6768
    6869        sage: TestSuite(P).run(verbose=True)
    6970        running ._test_an_element() . . . pass
    70         running ._test_element_pickling() . . . pass
     71        running ._test_category() . . . pass
     72        running ._test_elements() . . .
     73          Running the test suite of self.an_element()
     74          running ._test_category() . . . pass
     75          running ._test_not_implemented_methods() . . . pass
     76          running ._test_pickling() . . . pass
     77          pass
    7178        running ._test_not_implemented_methods() . . . pass
    7279        running ._test_pickling() . . . pass
    7380        running ._test_some_elements() . . . pass
    class Sets(Category): 
    296303                AssertionError: self.an_element() is not in self
    297304            """
    298305            tester = self._tester(**options)
    299             tester.assert_(self.an_element() in self, "self.an_element() is not in self")
     306            an_element = self.an_element()
     307            tester.assert_(an_element in self, "self.an_element() is not in self")
     308
     309        def _test_elements(self, tester = None, **options):
     310            """
     311            Run generic tests on element(s) of ``self``.
     312
     313            See also: :class:`TestSuite`.
     314
     315            EXAMPLES::
     316
     317                sage: C = Sets().example()
     318                sage: C._test_elements(verbose = True)
     319                <BLANKLINE>
     320                  Running the test suite of self.an_element()
     321                  running ._test_category() . . . pass
     322                  running ._test_not_implemented_methods() . . . pass
     323                  running ._test_pickling() . . . pass
     324                <BLANKLINE>
     325
     326            Debugging tip: in case of failure of this test, run instead:
     327
     328                sage: TestSuite(C.an_element()).run()
     329
     330            Let us now implement a parent whose elements cannot be pickled::
     331
     332                sage: from sage.categories.examples.sets_cat import PrimeNumbers
     333                sage: class Bla(SageObject): pass
     334                sage: class CCls(PrimeNumbers):
     335                ...       def an_element(self):
     336                ...           return Bla()
     337                sage: CC = CCls()
     338                sage: CC._test_elements()
     339                  Failure in _test_pickling:
     340                  ...
     341                  PicklingError: Can't pickle <class '__main__.Bla'>: attribute lookup __main__.Bla failed
     342                  ...
     343                  The following tests failed: _test_pickling
     344            """
     345            # TODO: add native support for nested test suites to TestSuite
     346
     347            # The intention is to raise an exception only if this is
     348            # run as a sub-testsuite of a larger testsuite.
     349            is_sub_testsuite = (tester is not None)
     350            tester = self._tester(tester = tester, **options)
     351            # Or do we want to run the test on some_elements?
     352            an_element = self.an_element()
     353            tester.info("\n  Running the test suite of self.an_element()")
     354            TestSuite(an_element).run(verbose = tester._verbose, prefix = tester._prefix+"  ",
     355                                      raise_on_failure = is_sub_testsuite)
     356            tester.info(tester._prefix+"  ", newline = False)
    300357
    301358        def some_elements(self):
    302359            """
    class Sets(Category): 
    350407            for x in elements:
    351408                tester.assert_(x in self, "the object %s in self.some_elements() is not in self"%(x,))
    352409
    353         def _test_element_pickling(self, **options):
    354             """
    355             Run pickling/unpickling tests for elements of this parent.
    356 
    357             EXAMPLES::
    358 
    359                 sage: C = Sets().example()
    360                 sage: C._test_element_pickling()
    361 
    362             SEE ALSO: :class:`TestSuite`, :func:`dumps` :func:`loads`
    363 
    364             Let us now write a class whose elements cannot be pickled::
    365 
    366                 sage: from sage.categories.examples.sets_cat import PrimeNumbers
    367                 sage: class Bla(SageObject): pass
    368                 sage: class CCls(PrimeNumbers):
    369                 ...       def an_element(self):
    370                 ...           return Bla()
    371                 sage: CC = CCls()
    372                 sage: CC._test_element_pickling()
    373                 Traceback (most recent call last):
    374                 ...
    375                 PicklingError: Can't pickle <class '__main__.Bla'>: attribute lookup __main__.Bla failed
    376             """
    377             tester = self._tester(**options)
    378             from sage.misc.all import loads, dumps
    379             # if x is a SageObject, we could call x._test_pickling() instead
    380             x = self.an_element()
    381             tester.assertEqual(loads(dumps(x)), x)
    382 
    383410    class ElementMethods:
    384411        ##def equal(x,y):
    385412        ##def =(x,y):
    386         pass
     413
     414        # Used by Element._test_category
     415        _dummy_attribute = None
    387416
    388417    class HomCategory(HomCategory):
    389418        pass
  • sage/combinat/root_system/cartan_type.py

    diff --git a/sage/combinat/root_system/cartan_type.py b/sage/combinat/root_system/cartan_type.py
    a b class CartanType_standard_finite(UniqueR 
    12661266        TESTS::
    12671267
    12681268            sage: TestSuite(ct).run(verbose = True)
     1269            running ._test_category() . . . pass
    12691270            running ._test_not_implemented_methods() . . . pass
    12701271            running ._test_pickling() . . . pass
    12711272        """
  • sage/combinat/root_system/weyl_characters.py

    diff --git a/sage/combinat/root_system/weyl_characters.py b/sage/combinat/root_system/weyl_characters.py
    a b class WeylCharacter(AlgebraElement): 
    115115       
    116116            sage: R = WeylCharacterRing("B3", prefix = "R")
    117117            sage: r =  R(1,1,0)
    118             sage: TestSuite(r).run()
     118            sage: TestSuite(r).run(skip = "_test_category")
    119119        """
    120120        AlgebraElement.__init__(self, A)
    121121        self._hdict = hdict
    class WeylCharacterRing_class(Algebra): 
    621621        EXAMPLES::
    622622       
    623623            sage: R = WeylCharacterRing(['A',3])
    624             sage: TestSuite(R).run()
     624            sage: TestSuite(R).run(skip = "_test_elements") # see #7922
    625625        """
    626626        sage.structure.parent_base.ParentWithBase.__init__(self, base_ring, category = Algebras(base_ring))
    627627
    class WeightRingElement(AlgebraElement): 
    22152215       
    22162216            sage: A2 = WeylCharacterRing(['A',2])
    22172217            sage: a2 = WeightRing(A2)
    2218             sage: TestSuite(a2).run(skip = ["_test_element_pickling"]) # see #7922
     2218            sage: TestSuite(a2).run(skip = ["_test_elements"]) # see #7922
    22192219        """
    22202220        AlgebraElement.__init__(self, A)
    22212221        self._mdict = mdict
    class WeightRing(Algebra): 
    25032503       
    25042504            sage: R = WeylCharacterRing(['G',2], prefix = "R", base_ring = QQ)
    25052505            sage: S = WeightRing(R, prefix = "S")
    2506             sage: TestSuite(S).run(skip = ["_test_element_pickling"]) # see #7922
     2506            sage: TestSuite(S).run(skip = ["_test_elements"]) # see #7922
    25072507        """
    25082508        self._parent = A
    25092509        self._cartan_type = self._parent._cartan_type
  • sage/combinat/sf/sf.py

    diff --git a/sage/combinat/sf/sf.py b/sage/combinat/sf/sf.py
    a b class SymmetricFunctions(UniqueRepresent 
    215215
    216216        TESTS:
    217217
    218         There are a lot of missing features for this abstract parent. But some tests do pass:
     218        There are a lot of missing features for this abstract parent. But some tests do pass::
    219219
    220220            sage: TestSuite(Sym).run()
    221221            Failure ...
    222             The following tests failed: _test_additive_associativity, _test_an_element, _test_associativity, _test_element_pickling, _test_not_implemented_methods, _test_one, _test_prod, _test_some_elements, _test_zero
    223 
     222            The following tests failed: _test_additive_associativity, _test_an_element, _test_associativity, _test_elements, _test_not_implemented_methods, _test_one, _test_prod, _test_some_elements, _test_zero
    224223        """
    225224        assert(R in Rings())
    226225        self._base = R # Won't be needed when CategoryObject won't override anymore base_ring
  • sage/misc/nested_class_test.py

    diff --git a/sage/misc/nested_class_test.py b/sage/misc/nested_class_test.py
    a b for categories), but deriving only from  
    1212    sage: P = TestParent1()
    1313    sage: TestSuite(P).run()
    1414    Failure ...
    15     The following tests failed: _test_element_pickling, _test_pickling
     15    The following tests failed: _test_elements, _test_pickling
    1616
    1717They actually need to be in the NestedClassMetaclass. However, due to
    1818a technical detail, this is currently not directly supported::
  • sage/misc/sage_unittest.py

    diff --git a/sage/misc/sage_unittest.py b/sage/misc/sage_unittest.py
    a b class TestSuite(object): 
    2727    object, in alphabetic order::
    2828
    2929        sage: TestSuite(1).run(verbose = True)
     30        running ._test_category() . . . pass
    3031        running ._test_not_implemented_methods() . . . pass
    3132        running ._test_pickling() . . . pass
    3233
    class TestSuite(object): 
    4142        sage: TestSuite(S).run(verbose = True)
    4243        running ._test_an_element() . . . pass
    4344        running ._test_associativity() . . . pass
    44         running ._test_element_pickling() . . . pass
     45        running ._test_category() . . . pass
     46        running ._test_elements() . . .
     47          Running the test suite of self.an_element()
     48          running ._test_category() . . . pass
     49          running ._test_not_implemented_methods() . . . pass
     50          running ._test_pickling() . . . pass
     51          pass
    4552        running ._test_enumerated_set_contains() . . . pass
    4653        running ._test_enumerated_set_iter_cardinality() . . . pass
    4754        running ._test_enumerated_set_iter_list() . . . pass
    class TestSuite(object): 
    5360
    5461        sage: S._test_associativity()
    5562
     63    Debugging tip: in case of failure of some test, use `%pdb on` to
     64    turn on automatic debugging on error. Run the failing test
     65    independtly: the debugger will stop right where the first
     66    assertion fails. Then, introspection can be used to analyse what
     67    exactly the problem is. See also the ``catch = False`` option to
     68    :meth:`.run`.
     69
    5670    When meaningful, one can further customize on which elements
    5771    the tests are run. Here, we use it to *prove* that the
    5872    multiplication is indeed associative, by running the test on
    class TestSuite(object): 
    7286    run a :class:`TestSuite` on one of its instances in its doctest
    7387    (replacing the current ``loads(dumps(x))`` tests).
    7488
     89    Finally, running ``TestSuite`` on a standard Python object does
     90    some basic sanity checks:
     91
     92        sage: TestSuite(int(1)).run(verbose = True)
     93        running ._test_pickling() . . . pass
     94
    7595    TODO:
    7696
    77      - allow for customized behavior in case of failing assertion
     97     - Allow for customized behavior in case of failing assertion
    7898       (warning, error, statistic accounting).
    7999       This involves reimplementing the methods fail / failIf / ...
    80100       of unittest.TestCase in InstanceTester
    class TestSuite(object): 
    82102     - Don't catch the exceptions if ``TestSuite(..).run()`` is called
    83103       under the debugger, or with ``%pdb`` on (how to detect this? see
    84104       ``IPython.ipapi.get()``, ``IPython.Magic.shell.call_pdb``, ...)
     105       In the mean time, see the ``catch=False`` option.
    85106
    86107     - Run the tests according to the inheritance order, from most
    87108       generic to most specific, rather than alphabetically. Then, the
    class TestSuite(object): 
    90111
    91112     - Improve integration with doctests (statistics on failing/passing tests)
    92113
     114     - Add proper support for nested testsuites.
     115
    93116     - Integration with unittest:
    94117       Make TestSuite inherit from unittest.TestSuite?
    95118       Make ``.run(...)`` accept a result object
    class TestSuite(object): 
    112135            sage: TestSuite(ZZ)
    113136            Test suite for Integer Ring
    114137        """
     138        from sage.structure.sage_object import SageObject
     139        if not isinstance(instance, (SageObject,PythonObjectWithTests)):
     140            instance = PythonObjectWithTests(instance)
    115141        self._instance = instance
    116142
    117143    def __repr__(self):
    class TestSuite(object): 
    124150        return "Test suite for %s"%self._instance
    125151
    126152
    127     def run(self, category = None, skip = [], **options):
     153    def run(self, category = None, skip = [], catch = True, raise_on_failure = False, **options):
    128154        """
    129155        Run all the tests from this test suite:
    130156
    131157        INPUT:
    132158
    133159         - ``category`` - a category; reserved for future use
    134          - ``skip` ` - a string or list (or iterable) of strings
     160         - ``skip`` - a string or list (or iterable) of strings
     161         - ``raise_on_failure`` - a boolean (default: False)
     162         - ``catch` - a boolean (default: True)
    135163
    136164        All other options are passed down to the individual tests.
    137165
    class TestSuite(object): 
    142170        We now use the ``verbose`` option::
    143171
    144172            sage: TestSuite(1).run(verbose = True)
     173            running ._test_category() . . . pass
    145174            running ._test_not_implemented_methods() . . . pass
    146175            running ._test_pickling() . . . pass
    147176
    148177        Some tests may be skipped using the ``skip`` option::
    149178
    150179            sage: TestSuite(1).run(verbose = True, skip ="_test_pickling")
     180            running ._test_category() . . . pass
    151181            running ._test_not_implemented_methods() . . . pass
    152             sage: TestSuite(1).run(verbose = True, skip =["_test_pickling"])
     182            sage: TestSuite(1).run(verbose = True, skip =["_test_pickling", "_test_category"])
    153183            running ._test_not_implemented_methods() . . . pass
    154184
    155185        We now show (and test) some standard error reports::
    class TestSuite(object): 
    186216            AssertionError
    187217            ------------------------------------------------------------
    188218            running ._test_c() . . . pass
     219            running ._test_category() . . . pass
    189220            running ._test_d() . . . fail
    190221            Traceback (most recent call last):
    191222              ...
    class TestSuite(object): 
    202233            File "/opt/sage/local/lib/python/site-packages/sage/misc/sage_unittest.py", line 183, in run
    203234            test_method(tester = tester)
    204235
     236        The ``catch=False`` option prevents ``TestSuite`` from
     237        catching exceptions::
     238
     239            sage: TestSuite(Blah()).run(catch=False)
     240            Traceback (most recent call last):
     241              ...
     242              File ..., in _test_b
     243                def _test_b(self, tester): tester.fail()
     244              ...
     245            AssertionError
     246
     247        In conjonction with ``%pdb on``, this allows for the debbuger
     248        to jump directly to the first failure location.
    205249        """
    206250        if type(skip) == str:
    207251            skip = [skip]
    208252        else:
    209253            skip = tuple(skip)
    210         tester = self._instance._tester(**options)
     254
     255        # The class of exceptions that will be catched and reported;
     256        # other exceptions will get trough. None catches nothing.
     257        catch_exception = Exception if catch else None
     258
     259        tester = instance_tester(self._instance, **options)
    211260        failed = []
    212261        for method_name in dir(self._instance):
    213262            if method_name[0:6] == "_test_" and method_name not in skip:
    214263                # TODO: improve pretty printing
    215264                # could use the doc string of the test method?
    216                 tester.info("running .%s() . . . "%method_name, newline = False)
     265                tester.info(tester._prefix+"running .%s() . . . "%method_name, newline = False)
    217266                test_method = getattr(self._instance, method_name)
    218267                try:
    219268                    test_method(tester = tester)
    220269                    tester.info("pass")
    221                 except:
    222                     if tester._verbose:
    223                         tester.info("fail")
     270                except catch_exception as e:
     271                    failed.append(method_name)
     272                    if isinstance(e, TestSuiteFailure):
     273                        # The failure occured in a nested testsuite
     274                        # which has already reported the details of
     275                        # that failure
     276                        if not tester._verbose:
     277                            print tester._prefix+"Failure in %s"%method_name
    224278                    else:
    225                         print "Failure in %s:"%method_name
    226                     traceback.print_exc(file = sys.stdout)
    227                     print "-" * 60
    228                     failed.append(method_name)
     279                        if tester._verbose:
     280                            tester.info("fail")
     281                        else:
     282                            print tester._prefix+"Failure in %s:"%method_name
     283                        s = traceback.format_exc()
     284                        print tester._prefix + s.strip().replace("\n", "\n"+tester._prefix)
     285                        print tester._prefix + "-" * 60
    229286        if len(failed) > 0:
    230             print "The following tests failed: %s"%(", ".join(failed))
     287            print tester._prefix+"The following tests failed: %s"%(", ".join(failed))
     288            if raise_on_failure:
     289                raise TestSuiteFailure
     290
     291class TestSuiteFailure(AssertionError):
     292    pass
     293
     294def instance_tester(instance, tester = None, **options):
     295    """
     296    Returns a gadget attached to ``instance`` providing testing utilities.
     297
     298    EXAMPLES::
     299
     300        sage: from sage.misc.sage_unittest import instance_tester
     301        sage: tester = instance_tester(ZZ)
     302
     303        sage: tester.assert_(1 == 1)
     304        sage: tester.assert_(1 == 0)
     305        Traceback (most recent call last):
     306        ...
     307        AssertionError
     308        sage: tester.assert_(1 == 0, "this is expected to fail")
     309        Traceback (most recent call last):
     310        ...
     311        AssertionError: this is expected to fail
     312
     313        sage: tester.assertEquals(1, 1)
     314        sage: tester.assertEquals(1, 0)
     315        Traceback (most recent call last):
     316        ...
     317        AssertionError: 1 != 0
     318
     319    The available assertion testing facilities are the same as in
     320    :class:`unittest.TestCase`, which see (actually, by a slight
     321    abuse, tester is currently an instance of this class).
     322
     323    TESTS::
     324
     325        sage: instance_tester(ZZ, tester = tester) is tester
     326        True
     327    """
     328    if tester is None:
     329        return InstanceTester(instance, **options)
     330    else:
     331        assert len(options) == 0
     332        assert tester._instance is instance
     333        return tester
    231334
    232335class InstanceTester(unittest.TestCase):
    233336    """
    class InstanceTester(unittest.TestCase): 
    245348        Testing utilities for Integer Ring
    246349    """
    247350
    248     def __init__(self, instance, elements = None, verbose = False, **options):
     351    def __init__(self, instance, elements = None, verbose = False, prefix = "", **options):
    249352        """
    250353        A gadget attached to an instance providing it with testing utilities.
    251354
    class InstanceTester(unittest.TestCase): 
    263366        self._instance = instance
    264367        self._verbose = verbose
    265368        self._elements = elements
     369        self._prefix = prefix
    266370
    267371    def runTest(self):
    268372        """
    class InstanceTester(unittest.TestCase): 
    341445            return self._instance.some_elements()
    342446        else:
    343447            return self._elements
     448
     449class PythonObjectWithTests(object):
     450    """
     451    Utility class for running basis tests on a plain Python object
     452    (that is not in SageObject). More test methods can be added here.
     453
     454    EXAMPLES::
     455
     456            sage: TestSuite("bla").run()
     457
     458    """
     459    def __init__(self, instance):
     460        """
     461        EXAMPLES::
     462
     463            sage: from sage.misc.sage_unittest import PythonObjectWithTests
     464            sage: x = PythonObjectWithTests(int(1)); x
     465            <sage.misc.sage_unittest.PythonObjectWithTests object at ...>
     466            sage: TestSuite(x).run()
     467        """
     468
     469        self._instance = instance
     470
     471    def _test_pickling(self, **options):
     472        """
     473        Checks that the instance in self can be pickled and unpickled properly.
     474
     475        EXAMPLES::
     476
     477            sage: from sage.misc.sage_unittest import PythonObjectWithTests
     478            sage: PythonObjectWithTests(int(1))._test_pickling()
     479
     480        SEE ALSO: :func:`dumps` :func:`loads`
     481        """
     482        tester = instance_tester(self, **options)
     483        from sage.misc.all import loads, dumps
     484        tester.assertEqual(loads(dumps(self._instance)), self._instance)
  • sage/modular/hecke/module.py

    diff --git a/sage/modular/hecke/module.py b/sage/modular/hecke/module.py
    a b class HeckeModule_free_module(HeckeModul 
    471471
    472472            sage: M = sage.modular.hecke.module.HeckeModule_free_module(QQ, 12, -4); M
    473473            <class 'sage.modular.hecke.module.HeckeModule_free_module_with_category'>
    474             sage: TestSuite(M).run(skip = ["_test_additive_associativity", "_test_an_element", "_test_element_pickling", "_test_pickling", "_test_some_elements", "_test_zero"]) # is this supposed to be an abstract parent without elements?
     474            sage: TestSuite(M).run(skip = ["_test_additive_associativity", "_test_an_element", "_test_elements", "_test_pickling", "_test_some_elements", "_test_zero"]) # is this supposed to be an abstract parent without elements?
    475475        """
    476476        HeckeModule_generic.__init__(self, base_ring, level)
    477477        self.__weight = weight
  • sage/rings/number_field/class_group.py

    diff --git a/sage/rings/number_field/class_group.py b/sage/rings/number_field/class_group.py
    a b class ClassGroup(AbelianGroup_class): 
    4040
    4141            sage: G.category()
    4242            Category of groups
    43             sage: TestSuite(G).run() # todo: improve that!
     43            sage: TestSuite(G).run() # see #7945
    4444            Failure in _test_an_element:
    4545              ...
    4646            AssertionError: self.an_element() is not in self
    4747            ...
    48             The following tests failed: _test_an_element, _test_element_pickling, _test_inverse, _test_some_elements
     48            The following tests failed: _test_an_element, _test_elements, _test_inverse, _test_some_elements
    4949        """
    5050        self.__number_field = number_field
    5151        self.__gens = Sequence([FractionalIdealClass(x, self) for x in gens], immutable=True,
  • sage/rings/residue_field.pyx

    diff --git a/sage/rings/residue_field.pyx b/sage/rings/residue_field.pyx
    a b class ResidueField_generic(Field): 
    305305
    306306        TESTS::
    307307
    308             sage: TestSuite(k).run(skip = ["_test_element_pickling", "_test_pickling"]) # see #7929
    309             sage: TestSuite(F).run(skip = ["_test_element_pickling", "_test_pickling"]) # see #7929
     308            sage: TestSuite(k).run(skip = ["_test_elements", "_test_pickling"]) # see #7929
     309            sage: TestSuite(F).run(skip = ["_test_elements", "_test_pickling"]) # see #7929
    310310        """
    311311        self.p = p
    312312        self.f = f
  • sage/schemes/generic/scheme.py

    diff --git a/sage/schemes/generic/scheme.py b/sage/schemes/generic/scheme.py
    a b class Scheme(ParentWithBase): 
    6060            sage: I = (x^2 - y^2)*R
    6161            sage: RmodI = R.quotient(I)
    6262            sage: X = Spec(RmodI)
    63             sage: TestSuite(X).run(skip = ["_test_an_element", "_test_element_pickling", "_test_some_elements"])
     63            sage: TestSuite(X).run(skip = ["_test_an_element", "_test_elements", "_test_some_elements", "_test_category"]) # See #7946
    6464
    6565        A scheme is in the category of all schemes over the base scheme of self::
    6666
  • sage/schemes/generic/spec.py

    diff --git a/sage/schemes/generic/spec.py b/sage/schemes/generic/spec.py
    a b class Spec(scheme.AffineScheme): 
    4747        Spectrum of Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
    4848        sage: X = Spec(PolynomialRing(GF(49,'a'), 3, 'x')); X
    4949        Spectrum of Multivariate Polynomial Ring in x0, x1, x2 over Finite Field in a of size 7^2
    50         sage: loads(X.dumps()) == X
    51         True
     50        sage: TestSuite(X).run(skip = ["_test_an_element", "_test_elements", "_test_some_elements", "_test_category"]) # see # 7946
     51
    5252        sage: A = Spec(ZZ); B = Spec(ZZ)
    5353        sage: A is B
    5454        False
  • sage/schemes/jacobians/abstract_jacobian.py

    diff --git a/sage/schemes/jacobians/abstract_jacobian.py b/sage/schemes/jacobians/abstract_jacobian.py
    a b class Jacobian_generic(Scheme): 
    7575
    7676        Note: this is an abstract parent, so we skip element tests::
    7777
    78             sage: TestSuite(J).run(skip =["_test_an_element", "_test_element_pickling", "_test_some_elements"])
     78            sage: TestSuite(J).run(skip =["_test_an_element", "_test_elements", "_test_some_elements"])
    7979
    8080        ::
    8181
  • sage/structure/category_object.pyx

    diff --git a/sage/structure/category_object.pyx b/sage/structure/category_object.pyx
    a b cdef int bad_parent_warnings = 0 
    5050
    5151import generators
    5252import sage_object
    53 from sage.categories.category import JoinCategory
     53from sage.categories.category import Category, JoinCategory
    5454
    5555def guess_category(obj):
    5656    # this should be obsolete if things declare their categories
    cdef class CategoryObject(sage_object.Sa 
    157157    def category(self):
    158158        if self._category is None:
    159159            # COERCE TODO: we shouldn't need this
    160             from sage.categories.all import Objects
     160            from sage.categories.objects import Objects
    161161            self._category = Objects()
    162162        return self._category
    163163
     164    def categories(self):
     165        """
     166        EXAMPLES::
     167
     168            sage: ZZ.categories()
     169            [Category of euclidean domains,
     170             Category of principal ideal domains,
     171             Category of gcd domains,
     172             Category of integral domains,
     173             Category of commutative rings,
     174             Category of domains,
     175             Category of rings,
     176             Category of rngs,
     177             Category of commutative additive groups,
     178             Category of commutative additive monoids,
     179             Category of commutative additive semigroups,
     180             Category of monoids,
     181             Category of semigroups,
     182             Category of sets,
     183             Category of objects]
     184        """
     185        return self.category().all_super_categories()
    164186
    165187    ##############################################################################
    166188    # Generators
  • sage/structure/element.pyx

    diff --git a/sage/structure/element.pyx b/sage/structure/element.pyx
    a b import traceback 
    181181cdef MethodType
    182182from types import MethodType
    183183
    184 from sage.categories.sets_cat   import Sets
     184from sage.categories.category   import Category
    185185from sage.structure.parent      cimport Parent
     186from sage.structure.parent      import is_extension_type
    186187
    187188# This classes uses element.pxd.  To add data members, you
    188189# must change that file.
    cdef class Element(sage_object.SageObjec 
    433434        from sage.categories.all import Elements
    434435        return Elements(self._parent)
    435436
     437
     438    def _test_category(self, **options):
     439        """
     440        Run generic tests on the method :meth:`.category`.
     441
     442        See also: :class:`TestSuite`.
     443
     444        EXAMPLES::
     445
     446            sage: 3._test_category()
     447
     448        Let us now write a broken :meth:`.category` method::
     449
     450            sage: from sage.categories.examples.sets_cat import PrimeNumbers
     451            sage: class CCls(PrimeNumbers):
     452            ...       def an_element(self):
     453            ...           return 18
     454            sage: CC = CCls()
     455            sage: CC._test_an_element()
     456            Traceback (most recent call last):
     457            ...
     458            AssertionError: self.an_element() is not in self
     459        """
     460        from sage.categories.objects    import Objects
     461        tester = self._tester(**options)
     462        sage_object.SageObject._test_category(self, tester = tester)
     463        category = self.category()
     464        # Tests that self inherits methods from the categories
     465        if not is_extension_type(self.__class__):
     466            # For usual Python classes, that should be done with
     467            # standard inheritance
     468            tester.assert_(isinstance(self, self.parent().category().element_class))
     469        else:
     470            # For extension types we just check that inheritance
     471            # occurs on a dummy attribute of Sets().ElementMethods
     472            tester.assert_(hasattr(self, "_dummy_attribute"))
     473
     474
    436475    def parent(self, x=None):
    437476        """
    438477        Returns parent of this element; or, if the optional argument x is
  • sage/structure/parent.pyx

    diff --git a/sage/structure/parent.pyx b/sage/structure/parent.pyx
    a b A simple example of registering coercion 
    7070cimport element
    7171cimport sage.categories.morphism as morphism
    7272cimport sage.categories.map as map
     73from sage.structure.sage_object import SageObject
    7374from sage.misc.lazy_attribute import lazy_attribute
    7475from sage.categories.sets_cat import Sets
    7576from copy import copy
    cdef class Parent(category_object.Catego 
    496497            self._category = Sets()
    497498        return self._category
    498499
    499     def categories(self):
     500    def _test_category(self, **options):
    500501        """
     502        Run generic tests on the method :meth:`.category`.
     503
     504        See also: :class:`TestSuite`.
     505
    501506        EXAMPLES::
    502507
    503             sage: ZZ.categories()
    504             [Category of euclidean domains,
    505              Category of principal ideal domains,
    506              Category of gcd domains,
    507              Category of integral domains,
    508              Category of commutative rings,
    509              Category of domains,
    510              Category of rings,
    511              Category of rngs,
    512              Category of commutative additive groups,
    513              Category of commutative additive monoids,
    514              Category of commutative additive semigroups,
    515              Category of monoids,
    516              Category of semigroups,
    517              Category of sets,
    518              Category of objects]
     508            sage: C = Sets().example()
     509            sage: C._test_category()
     510
     511        Let us now write a parent with broken categories:
     512
     513            sage: class MyParent(Parent):
     514            ...       def __init__(self):
     515            ...           pass
     516            sage: P = MyParent()
     517            sage: P._test_category()
     518            Traceback (most recent call last):
     519            ...
     520            AssertionError: category of self improperly initialized
     521
     522        To fix this, :meth:`MyParent.__init__` should initialize the
     523        category of ``self`` by calling :meth:`._init_category` or
     524        ``Parent.__init__(self, category = ...)``.
    519525        """
    520         return self.category().all_super_categories()
     526        tester = self._tester(**options)
     527        SageObject._test_category(self, tester = tester)
     528        category = self.category()
     529        tester.assert_(category.is_subcategory(Sets()))
     530        # Tests that self inherits methods from the categories
     531        if not is_extension_type(self.__class__):
     532            # For usual Python classes, that should be done with
     533            # standard inheritance
     534            tester.assert_(isinstance(self, category.parent_class),
     535                           "category of self improperly initialized"%self)
     536        else:
     537            # For extension types we just check that inheritance
     538            # occurs on one specific method.
     539            # _test_an_element from Sets().ParentMethods is a good
     540            # candidate because it's unlikely to be overriden in self.
     541            tester.assert_(hasattr(self, "_test_an_element"),
     542                           "category of self improperly initialized"%self)
    521543
    522544    cdef int init_coerce(self, bint warn=True) except -1:
    523545        if self._coerce_from_hash is None:
    cdef class Parent(category_object.Catego 
    553575            running ._test_additive_associativity() . . . pass
    554576            running ._test_an_element() . . . pass
    555577            running ._test_associativity() . . . pass
    556             running ._test_element_pickling() . . . pass
     578            running ._test_category() . . . pass
     579            running ._test_elements() . . .
     580              Running the test suite of self.an_element()
     581              running ._test_category() . . . pass
     582              running ._test_not_implemented_methods() . . . pass
     583              running ._test_pickling() . . . pass
     584              pass
    557585            running ._test_not_implemented_methods() . . . pass
    558586            running ._test_one() . . . pass
    559587            running ._test_pickling() . . . pass
    cdef class Parent(category_object.Catego 
    583611            ['_test_additive_associativity',
    584612             '_test_an_element',
    585613             '_test_associativity',
    586              '_test_element_pickling',
     614             '_test_category',
     615             '_test_elements',
    587616             '_test_not_implemented_methods',
    588617             '_test_one',
    589              '_test_pickling', 
     618             '_test_pickling',
    590619             '_test_prod',
    591620             '_test_some_elements',
    592621             '_test_zero']
  • sage/structure/sage_object.pyx

    diff --git a/sage/structure/sage_object.pyx b/sage/structure/sage_object.pyx
    a b cdef class SageObject: 
    201201        from sage.categories.all import Objects
    202202        return Objects()
    203203
     204    def _test_category(self, **options):
     205        """
     206        Run generic tests on the method :meth:`.category`.
     207
     208        See also: :class:`TestSuite`.
     209
     210        EXAMPLES::
     211
     212            sage: O = SageObject()
     213            sage: O._test_category()
     214
     215        Let us now write a broken :meth:`.category` method::
     216
     217            sage: class CCls(SageObject):
     218            ...       def category(self):
     219            ...           return 3
     220            sage: CC = CCls()
     221            sage: CC._test_category()
     222            Traceback (most recent call last):
     223            ...
     224            AssertionError
     225        """
     226        from sage.categories.category import Category
     227        from sage.categories.objects import Objects
     228        tester = self._tester(**options)
     229        category = self.category()
     230        tester.assert_(isinstance(category, Category))
     231        tester.assert_(category.is_subcategory(Objects()))
     232        tester.assert_(self in category)
     233
    204234##     def category(self):
    205235##         try:
    206236##             return self.__category
    cdef class SageObject: 
    215245    # Test framework
    216246    #############################################################################
    217247
     248    def _tester(self, **options):
     249        """
     250        Returns a gadget attached to ``self`` providing testing utilities.
     251
     252        This is used by :class:`sage.misc.sage_unittest.TestSuite` and the
     253        ``_test_*`` methods.
     254
     255        EXAMPLES::
     256
     257            sage: tester = ZZ._tester()
     258
     259            sage: tester.assert_(1 == 1)
     260            sage: tester.assert_(1 == 0)
     261            Traceback (most recent call last):
     262            ...
     263            AssertionError
     264            sage: tester.assert_(1 == 0, "this is expected to fail")
     265            Traceback (most recent call last):
     266            ...
     267            AssertionError: this is expected to fail
     268
     269            sage: tester.assertEquals(1, 1)
     270            sage: tester.assertEquals(1, 0)
     271            Traceback (most recent call last):
     272            ...
     273            AssertionError: 1 != 0
     274
     275        The available assertion testing facilities are the same as in
     276        :class:`unittest.TestCase`, which see (actually, by a slight
     277        abuse, tester is currently an instance of this class).
     278
     279        TESTS::
     280
     281            sage: ZZ._tester(tester = tester) is tester
     282            True
     283        """
     284        from sage.misc.sage_unittest import instance_tester
     285        return instance_tester(self, **options)
    218286
    219287    def _test_not_implemented_methods(self, **options):
    220288        """
    cdef class SageObject: 
    275343        from sage.misc.all import loads, dumps
    276344        tester.assertEqual(loads(dumps(self)), self)
    277345
    278     def _tester(self, tester = None, **options):
    279         """
    280         Returns a gadget attached to ``self`` providing testing utilities.
    281 
    282         This is used by :class:`sage.misc.sage_unittest.TestSuite` and the
    283         ``_test_*`` methods.
    284 
    285         EXAMPLES::
    286 
    287             sage: tester = ZZ._tester()
    288 
    289             sage: tester.assert_(1 == 1)
    290             sage: tester.assert_(1 == 0)
    291             Traceback (most recent call last):
    292             ...
    293             AssertionError
    294             sage: tester.assert_(1 == 0, "this is expected to fail")
    295             Traceback (most recent call last):
    296             ...
    297             AssertionError: this is expected to fail
    298 
    299             sage: tester.assertEquals(1, 1)
    300             sage: tester.assertEquals(1, 0)
    301             Traceback (most recent call last):
    302             ...
    303             AssertionError: 1 != 0
    304 
    305         The available assertion testing facilities are the same as in
    306         :class:`unittest.TestCase`, which see (actually, by a slight
    307         abuse, tester is currently an instance of this class).
    308 
    309         TESTS::
    310 
    311             sage: ZZ._tester(tester = tester) is tester
    312             True
    313         """
    314         if tester is None:
    315             from sage.misc.sage_unittest import InstanceTester
    316             return InstanceTester(self, **options)
    317         else:
    318             assert len(options) == 0
    319             assert tester._instance is self
    320             return tester
    321 
    322346    #############################################################################
    323347    # Coercions to interface objects
    324348    #############################################################################