Unit tests, mana cost math fixes
This commit is contained in:
parent
e02a22fe23
commit
93957b0ff7
2 changed files with 37 additions and 5 deletions
11
mtg.py
11
mtg.py
|
@ -1,6 +1,5 @@
|
||||||
import copy
|
import copy
|
||||||
import random
|
import random
|
||||||
import cards
|
|
||||||
from observable import Observable
|
from observable import Observable
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
|
@ -82,13 +81,15 @@ class ManaCost:
|
||||||
elif isinstance(other, Mana):
|
elif isinstance(other, Mana):
|
||||||
result.mana = other
|
result.mana = other
|
||||||
elif isinstance(other, ManaCost):
|
elif isinstance(other, ManaCost):
|
||||||
result.any = other.many
|
result.any = self.any + other.any
|
||||||
result.mana = other.many
|
result.mana = self.mana + other.mana
|
||||||
return result
|
return result
|
||||||
def __sub__(self, other):
|
def __sub__(self, other):
|
||||||
|
result = ManaCost()
|
||||||
if isinstance(other, ManaCost):
|
if isinstance(other, ManaCost):
|
||||||
result.any = self.any - right.any
|
result.any = self.any - other.any
|
||||||
result.mana = self.mana - right.mana
|
result.mana = self.mana - other.mana
|
||||||
|
return result
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '{0}{1}'.format(
|
return '{0}{1}'.format(
|
||||||
self.any if self.any > 0 or self.mana.converted() == 0 else '',
|
self.any if self.any > 0 or self.mana.converted() == 0 else '',
|
||||||
|
|
31
test.py
Normal file
31
test.py
Normal 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()
|
Loading…
Reference in a new issue