Example #1
0
 def test_TypeExpressions(self):
         t_legendary = MgSupertype(MgSupertype.SupertypeEnum.Legendary)
         t_human = MgSubtype(MgSubtype.CreatureSubtypeEnum.Human)
         t_cleric = MgSubtype(MgSubtype.CreatureSubtypeEnum.Cleric)
         
         texpr = MgTypeExpression(t_legendary,t_human,t_cleric)
         
         self.assertTrue(t_legendary.isTraversable())
         self.assertEqual(len(texpr.getTraversalSuccessors()),3)
         
         self.assertTrue(texpr.isChild(t_legendary))
         self.assertEqual(t_legendary.getParent(),texpr)
         
         self.assertEqual(texpr.unparseToString().lower(),"legendary human cleric")
         
         ##test pluralization
         #texpr.setPlural(True)
         #self.assertEqual(texpr.unparseToString().lower(),"legendary human clerics")
         
         #test comma delimitation.
         nonvampire = MgNonExpression(MgSubtype(MgSubtype.CreatureSubtypeEnum.Vampire))
         nonwerewolf = MgNonExpression(MgSubtype(MgSubtype.CreatureSubtypeEnum.Werewolf))
         nonzombie = MgNonExpression(MgSubtype(MgSubtype.CreatureSubtypeEnum.Zombie))
         t_creature = MgType(MgType.TypeEnum.Creature)
         
         commaExpr = MgTypeExpression(nonvampire,nonwerewolf,nonzombie,t_creature)
         commaExpr.setCommaDelimited(True)
         self.assertEqual(commaExpr.unparseToString().lower(),"non-vampire, non-werewolf, non-zombie creature")
Example #2
0
class MgTypeLine(core.MgNode):
    """The type line is the middle part of a Magic card
        that lists the supertypes, types, and subtypes of that card."""
    def __init__(self):
        """Default constructor for an empty type line."""
        self._traversable = True
        self._supertypes = MgTypeExpression()
        self._supertypes.setParent(self)
        self._types = MgTypeExpression()
        self._types.setParent(self)
        self._subtypes = MgTypeExpression()
        self._subtypes.setParent(self)

    def __init__(self, supertypes=None, types=None, subtypes=None):
        """Constructor that allows you to supply MgTypeExpression objects."""
        self._traversable = True
        if supertypes is not None:
            self._supertypes = supertypes
        else:
            self._supertypes = MgTypeExpression()
        self._supertypes.setParent(self)
        if types is not None:
            self._types = types
        else:
            self._types = MgTypeExpression()
        self._types.setParent(self)
        if subtypes is not None:
            self._subtypes = subtypes
        else:
            self._subtypes = MgTypeExpression()
        self._subtypes.setParent(self)

    def getSupertypes(self):
        """Gets the type expression for supertypes."""
        return self._supertypes

    def getTypes(self):
        """Gets the type expression for types."""
        return self._types

    def getSubtypes(self):
        """Gets the type expression for subtypes."""
        return self._subtypes

    def hasSupertype(self, t):
        """Checks whether a supertype is on the type line."""
        return self._supertypes.isChild(t)

    def hasType(self, t):
        """Checks whether a type is on the type line."""
        return self._types.isChild(t)

    def hasSubtype(self, t):
        """Checks whether a subtype is on the type line."""
        return self._subtypes.isChild(t)

    def isChild(self, child):
        return child in {self._supertypes, self._types, self._subtypes}

    def getTraversalSuccessors(self):
        return [
            ts for ts in {self._supertypes, self._types, self._subtypes}
            if ts.isTraversable()
        ]

    def unparseToString(self):
        if self._subtypes.getLength() == 0:
            return "{0} {1}".format(self._supertypes.unparseToString(),
                                    self._types.unparseToString())
        else:
            return "{0} {1} — {2}".format(self._supertypes.unparseToString(),
                                          self._types.unparseToString(),
                                          self._subtypes.unparseToString())