Unit tests, mana cost math fixes

This commit is contained in:
Correl Roush 2010-06-16 14:42:22 -04:00
parent e02a22fe23
commit 93957b0ff7
2 changed files with 37 additions and 5 deletions

11
mtg.py
View file

@ -1,6 +1,5 @@
import copy
import random
import cards
from observable import Observable
class Game:
@ -82,13 +81,15 @@ class ManaCost:
elif isinstance(other, Mana):
result.mana = other
elif isinstance(other, ManaCost):
result.any = other.many
result.mana = other.many
result.any = self.any + other.any
result.mana = self.mana + other.mana
return result
def __sub__(self, other):
result = ManaCost()
if isinstance(other, ManaCost):
result.any = self.any - right.any
result.mana = self.mana - right.mana
result.any = self.any - other.any
result.mana = self.mana - other.mana
return result
def __repr__(self):
return '{0}{1}'.format(
self.any if self.any > 0 or self.mana.converted() == 0 else '',

31
test.py Normal file
View file

@ -0,0 +1,31 @@
import unittest
from mtg import *
class TestManaMath(unittest.TestCase):
def test_add_mana(self):
self.assertEquals('RR', str(Mana('r') + Mana('r')))
def test_subtract_mana(self):
self.assertEquals('R', str(Mana('rr') - Mana('r')))
def test_subtract_mana_insufficient(self):
self.assertRaises(Exception, lambda: Mana('r') - Mana('rr'))
self.assertRaises(Exception, lambda: Mana('r') - Mana('g'))
class TestManaCostMath(unittest.TestCase):
def test_add_manacost(self):
self.assertEquals('3R', str(ManaCost('2R') + ManaCost('1')))
def test_subtract_manacost(self):
self.assertEquals('1G', str(ManaCost('4GR') - ManaCost('3R')))
def test_subtract_manacost_from_mana(self):
result = Mana('UUBWWR') - ManaCost('1RW')
# Can't check the exact string, since there's no way of knowing which
# colors will be used for the additional mana requirement
self.assertEquals(3, result.converted())
self.assertEquals(0, result.mana['R'])
self.assertEquals(1, result.mana['W'])
def test_subtract_manacost_from_mana_insufficient_addtl(self):
self.assertRaises(Exception, lambda: Mana('RRW'), ManaCost('3R'))
def test_subtract_manacost_from_mana_insufficient_color(self):
self.assertRaises(Exception, lambda: Mana('RRW'), ManaCost('2U'))
if __name__ == '__main__':
unittest.main()