'''
### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ###
                   308-304 - Object-Oriented Software Design
                                 ASSIGNMENT  1
   TestCC.py ---
      Testing unit for class CellCoordinate.

   last modified: 01/24/02
                       ===============================
                             Copyright (c)  2002
                            Jean-Sébastien BOLDUC
                             (jseb@cs.mcgill.ca)

                        McGill University  (Montréal)
                       ===============================

### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ###
'''

# Testing other implementations from the command line:
from sys import argv
if len(argv) > 1:
  exec('from ' + argv[-1] + ' import *')
  del argv[-1] # Don't pass this argument to unittest
else:
  from CCoord import * # JSeb's implementation

import unittest

# Valid coordinates (positive integers only)
goodCoords = range(1, 20)
# Invalid coordinates
badCoords = [-1, 0, None, 'a', 1.5, [1, 2] ]


class CellCoordConstructor(unittest.TestCase):

  def testGoodInitialization(self): # test for success
    '''CellCoordinate __init__ should succeed with positive integers'''
    for i in goodCoords:
      for j in goodCoords:
        self.assert_(CellCoordinate(i, j))
    # also test the default parameter:
    self.assert_(CellCoordinate())

  def testBadInitialization(self): # test for failure
    '''CellCoordinate __init__ should raise a KeyError on bad coordinates'''
    for i in badCoords:
      for j in goodCoords:
        self.assertRaises(KeyError, CellCoordinate, i, j)
    for i in goodCoords:
      for j in badCoords:
        self.assertRaises(KeyError, CellCoordinate, i, j)
    for i in badCoords:
      for j in badCoords:
        self.assertRaises(KeyError, CellCoordinate, i, j)


class CellCoordGetSetRow(unittest.TestCase):

  X = CellCoordinate()

  def testGoodSetting(self): # test for success
    '''setRow should succeed with positive integers'''
    for i in goodCoords:
      self.assertEqual(self.X.setRow(i), None)
    # also test the default parameter:
    self.assertEqual(self.X.setRow(), None)

  def testBadSetting(self): # test for failure
    '''setRow should raise a KeyError on bad coordinate'''
    for i in badCoords:
      self.assertRaises(KeyError, self.X.setRow, i)

  def testSanity1(self): # test for sanity
    '''getRow should return value previously set by setRow'''
    for i in goodCoords:
      self.X.setRow(i)
      # test it twice, to make sure the data is not corrupted
      # by the getRow method:
      self.assertEqual(self.X.getRow(), i)
      self.assertEqual(self.X.getRow(), i)

  def testSanity2(self): # test for sanity
    '''CellCoordinate's row should not be corrupted by failed setRow's'''
    self.X.setRow(10)
    for i in badCoords:
      try:
        self.X.setRow(i)
      except KeyError:
        pass
      self.assertEqual(self.X.getRow(), 10)


class CellCoordGetSetColumn(unittest.TestCase):

  X = CellCoordinate()

  def testGoodSetting(self): # test for success
    '''setColumn should succeed with positive integers'''
    for i in goodCoords:
      self.assertEqual(self.X.setColumn(i), None)
    # also test the default parameter:
    self.assertEqual(self.X.setColumn(), None)

  def testBadSetting(self): # test for failure
    '''setColumn should raise a KeyError on bad coordinate'''
    for i in badCoords:
      self.assertRaises(KeyError, self.X.setColumn, i)

  def testSanity1(self): # test for sanity
    '''getColumn should return value previously set by setColumn'''
    for i in goodCoords:
      self.X.setColumn(i)
      # test it twice, to make sure the data is not corrupted
      # by the getColumn method:
      self.assertEqual(self.X.getColumn(), i)
      self.assertEqual(self.X.getColumn(), i)

  def testSanity2(self): # test for sanity
    '''CellCoordinate's column should not be corrupted by failed setColumn's'''
    self.X.setColumn(10)
    for i in badCoords:
      try:
        self.X.setColumn(i)
      except KeyError:
        pass
      self.assertEqual(self.X.getColumn(), 10)


class CellCoordSTR(unittest.TestCase):

  X = CellCoordinate()

  def testStringRepres(self): # test for success
    '''CellCoordinate's string representation should be (row, column)'''
    for i in goodCoords:
      for j in goodCoords:
        self.X.setRow(i)
        self.X.setColumn(j)
        s ="<CellCoordinate:%d,%d.>" % (i, j)
        self.assertEqual( str(self.X), s)


### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ###

if __name__ == "__main__":
  unittest.main()