def test_load_must_raise_if_equipment_arg_is_loaded_elsewhere(self): with self.assertRaises(AssertionError): t = TransportShip(10,20,9,12) c = Container(1,1) t.load(c) self.assertRaises(AssertionError, self.transport_ship.load(c))
def __init__(self, volume, mass, volume_capacity, weight_capacity, max_nb_weapons): TransportShip.__init__(self, volume, mass, volume_capacity, weight_capacity) WarShip.__init__(self, volume, mass, max_nb_weapons) HybridShip._nb_instances += 1 self.name = "MR-" + str(HybridShip._nb_instances)
def test_load_must_raise_if_weapon_arg_is_loaded_elsewhere(self): with self.assertRaises(AssertionError): t = TransportShip(10, 20, 9, 12) p = Phaser(1, 1) t.load(p) self.assertRaises(AssertionError, self.war_ship.load(p)) self.assertRaises(AssertionError, self.war_ship.load(t))
class EquipmentTest(unittest.TestCase): def setUp(self): self.transport_ship = TransportShip(100, 200, 90, 1000) self.war_ship = WarShip(100, 200, 5) def tearDown(self): self.transport_ship = None self.war_ship = None def test_mass_must_be_the_mass_sum_of_all_the_equipments_loaded_plus_the_transport_ship_mass(self): c = Container(1,1) base_mass = self.transport_ship.mass self.transport_ship.load(c) self.assertGreater(self.transport_ship.mass, base_mass) self.assertEqual(self.transport_ship.mass, base_mass + c.mass) def test_ship_must_only_unload_equipment_already_loaded(self): c = Container(1,1) p = Phaser(1,1) self.transport_ship.load(c) self.war_ship.load(p) equipments_length_before_unloading_ts = len(self.transport_ship.equipments) equipments_length_before_unloading_ws = len(self.war_ship.equipments) self.transport_ship.unload(c) self.war_ship.unload(p) self.assertGreater(equipments_length_before_unloading_ts, len(self.transport_ship.equipments)) self.assertGreater(equipments_length_before_unloading_ws, len(self.war_ship.equipments)) def test_equipment_unloaded_must_have_None_location(self): c = Container(1, 1) p = Phaser(1, 1) self.transport_ship.load(c) self.war_ship.load(p) self.transport_ship.unload(c) self.war_ship.unload(p) self.assertIsNone(c.location) self.assertIsNone(p.location)
def test_equipment_location_can_only_be_a_ship_object(self): with self.assertRaises(AssertionError): c = Container(1, 1) ts = TransportShip(10, 10, 9, 20) c.location = 10 c.location = ts self.assertEqual(ts, c.location)
def test_hybrid_can_load_any_equipment(self): c = Container(1, 1) p = Phaser(1, 1) ts = TransportShip(10, 20, 9, 34) self.hs.load(c) self.hs.load(p) self.hs.load(ts) self.assertEqual(self.hs.equipments.index(c), 0) self.assertEqual(self.hs.equipments.index(p), 1) self.assertEqual(self.hs.equipments.index(ts), 2)
def test_equipment_constructor(self): ts = TransportShip(10, 10, 9, 20) self.assertEqual(self.phaser.name, "P-1") self.assertEqual(self.phaser.volume, 100) self.assertEqual(self.phaser.mass, 200) self.assertIsNone(self.phaser.location) self.assertEqual(self.container.name, "C-1") self.assertEqual(self.container.volume, 100) self.assertEqual(self.container.mass, 200) self.assertIsNone(self.container.location) self.assertEqual(self.transport_ship.name, "VT-1") self.assertEqual(self.transport_ship.volume, 100) self.assertEqual(self.transport_ship.mass, 200) self.assertIsNone(self.transport_ship.location)
def test_load_anything_else_than_a_weapon_must_raise(self): with self.assertRaises(AssertionError): c = Container(1, 1) ts = TransportShip(13, 24, 12, 45) self.assertRaises(AssertionError, self.war_ship.load(c))
def setUp(self): self.transport_ship = TransportShip(100, 200, 90, 1000) self.war_ship = WarShip(100, 200, 5)
def setUp(self): self.phaser = Phaser(100, 200) self.container = Container(100, 200) self.transport_ship = TransportShip(100, 200, 90, 1000)
def setUp(self): self.phasers = [] self.vc1 = LightWeightWarShip(10, 50, 2) print 'Soit un vaisseau de combat leger ' + self.vc1.name + '(volume=10, masse=50, 2 armes max)' print 'On equipe ' + self.vc1.name + ' avec deux phasers (volume=1, masse=1)' p = Phaser(1, 1) p1 = Phaser(1, 1) self.vc1.load(p) self.vc1.load(p1) self.phasers.append(p) self.phasers.append(p1) self.vt2 = TransportShip(100, 100, 90, 300) print 'Soit un vaisseau de transport ' + self.vt2.name + \ ' (volume=100, masse=100, cap_volume=90, cap_masse=300)' print 'On charge ' + self.vt2.name + ' avec 5 conteneurs (volume=1, masse= 1)' for i in range (0, 5): self.vt2.load(Container(1,1)) print 'On charge ' + self.vt2.name + ' avec ' + self.vc1.name + ' comme du fret' self.vt2.load(self.vc1) print 'On desequipe un des phasers de ' + self.vc1.name + ' et ' \ 'on le charge dans ' + self.vt2.name + ' comme du fret' self.vc1.unload(p) self.vt2.load(p) self.mr63 = HybridShip(200, 150, 180, 600, 5) print 'Soit ' + self.mr63.name + ' un vieux modele de vaisseau de transport lourd capable de se battre ' \ '(volume=200, masse=150, cap_volume=180, cap_masse=600, 5 armes max)' print 'On equipe ' + self.mr63.name + ' avec deux blasters (volume=2, masse=2, gaz=50%),' \ ' deux phasers (volume=1, masse=1),' \ ' le vaisseau ' + self.vt2.name + \ ' et 4 conteneurs (volume=10, masse=100)' b = Blaster(2, 2, 50) b2 = Blaster(2, 2, 50) self.mr63.load(b) self.mr63.load(b2) p2 = Phaser(1,1) p3 = Phaser(1,1) self.phasers.append(p2) self.phasers.append(p3) self.mr63.load(p2) self.mr63.load(p3) self.mr63.load(self.vt2) for i in range(0, 4): self.mr63.load(Container(10, 100)) print 'On veut connaitre le niveau de gaz des blasters equipes par ' + self.mr63.name + ', puis les recharger' for equipment in self.mr63.equipments: if isinstance(equipment, Blaster): print 'Niveau de gaz de ' + equipment.name + ': ' + str(equipment.gaz_level) equipment.recharge() print 'Niveau de gaz de ' + equipment.name + ' apres rechargement: ' + str(equipment.gaz_level)
class TeacherScenarioTest(unittest.TestCase): def setUp(self): self.phasers = [] self.vc1 = LightWeightWarShip(10, 50, 2) print 'Soit un vaisseau de combat leger ' + self.vc1.name + '(volume=10, masse=50, 2 armes max)' print 'On equipe ' + self.vc1.name + ' avec deux phasers (volume=1, masse=1)' p = Phaser(1, 1) p1 = Phaser(1, 1) self.vc1.load(p) self.vc1.load(p1) self.phasers.append(p) self.phasers.append(p1) self.vt2 = TransportShip(100, 100, 90, 300) print 'Soit un vaisseau de transport ' + self.vt2.name + \ ' (volume=100, masse=100, cap_volume=90, cap_masse=300)' print 'On charge ' + self.vt2.name + ' avec 5 conteneurs (volume=1, masse= 1)' for i in range (0, 5): self.vt2.load(Container(1,1)) print 'On charge ' + self.vt2.name + ' avec ' + self.vc1.name + ' comme du fret' self.vt2.load(self.vc1) print 'On desequipe un des phasers de ' + self.vc1.name + ' et ' \ 'on le charge dans ' + self.vt2.name + ' comme du fret' self.vc1.unload(p) self.vt2.load(p) self.mr63 = HybridShip(200, 150, 180, 600, 5) print 'Soit ' + self.mr63.name + ' un vieux modele de vaisseau de transport lourd capable de se battre ' \ '(volume=200, masse=150, cap_volume=180, cap_masse=600, 5 armes max)' print 'On equipe ' + self.mr63.name + ' avec deux blasters (volume=2, masse=2, gaz=50%),' \ ' deux phasers (volume=1, masse=1),' \ ' le vaisseau ' + self.vt2.name + \ ' et 4 conteneurs (volume=10, masse=100)' b = Blaster(2, 2, 50) b2 = Blaster(2, 2, 50) self.mr63.load(b) self.mr63.load(b2) p2 = Phaser(1,1) p3 = Phaser(1,1) self.phasers.append(p2) self.phasers.append(p3) self.mr63.load(p2) self.mr63.load(p3) self.mr63.load(self.vt2) for i in range(0, 4): self.mr63.load(Container(10, 100)) print 'On veut connaitre le niveau de gaz des blasters equipes par ' + self.mr63.name + ', puis les recharger' for equipment in self.mr63.equipments: if isinstance(equipment, Blaster): print 'Niveau de gaz de ' + equipment.name + ': ' + str(equipment.gaz_level) equipment.recharge() print 'Niveau de gaz de ' + equipment.name + ' apres rechargement: ' + str(equipment.gaz_level) def resolveLocation(self, equipment): if equipment.location is None: return if isinstance(equipment, Weapon) and equipment.equipped: print equipment.name + ' est equipe par ' + equipment.location.name else: print equipment.name + ' est dans la soute de ' + equipment.location.name self.resolveLocation(equipment.location) def tearDown(self): self.vc1 = None self.mr63 = None self.vt2 = None self.phasers = None def test_total_mass_total_volume_of_mr_63_and_where_are_the_phasers(self): print 'On veut savoir quelle est la masse total de ' + self.mr63.name + \ ' (un bon 713) et quel est le volume disponible dans MR-63 (il en reste pour 34)' print 'Masse de ' + self.mr63.name + ': ' + str(self.mr63.mass) print 'Volume disponible de ' + self.mr63.name + ': ' + str(self.mr63.volume_capacity_remaining) print 'On veut savoir ou est chacun des phasers (deux sont dans ' + self.mr63.name + \ ', un dans la soute de ' + self.vt2.name + ', le dernier est equipe par ' + \ self.vc1.name + ')' for phaser in self.phasers: self.resolveLocation(phaser)
def setUp(self): self.transport_ship = TransportShip(100, 200, 90, 200)
class TransportShipTest(unittest.TestCase): def setUp(self): self.transport_ship = TransportShip(100, 200, 90, 200) def tearDown(self): self.transport_ship = None def test_constructor(self): self.assertEqual(self.transport_ship.volume_capacity, 90) self.assertEqual(self.transport_ship.weight_capacity, 200) def test_location_setter_overriding(self): with self.assertRaises(AssertionError): w = Blaster(1,1,3) try: self.transport_ship.location = w except ValueError as e: self.assertEquals(e.args, ('A Ship can only be located in a TransportShip',)) raise def test_must_raise_if_volume_capacity_gt_volume(self): with self.assertRaises(AssertionError): self.assertRaises(AssertionError, TransportShip(10,20,30,40)) def test_load_equipments_list_must_contain_equipment_in_arg(self): c = Container(1,1) c2 = Container(1,1) self.transport_ship.load(c) self.transport_ship.load(c2) self.assertEqual(self.transport_ship.equipments.index(c), 0) self.assertEqual(self.transport_ship.equipments.index(c2), 1) def test_load_must_raise_if_volume_capacity_exceed(self): with self.assertRaises(AssertionError): c = Container(1000,1000) self.assertRaises(AssertionError, self.transport_ship.load(c)) def test_load_must_raise_if_weight_capacity_exceed(self): with self.assertRaises(AssertionError): c = Container(10,1000) self.assertRaises(AssertionError, self.transport_ship.load(c)) def test_load_must_raise_if_arg_is_not_an_equipment_object(self): with self.assertRaises(AssertionError): arg = 10 self.assertRaises(AssertionError, self.transport_ship.load(arg)) def test_load_must_raise_if_equipment_arg_is_loaded_elsewhere(self): with self.assertRaises(AssertionError): t = TransportShip(10,20,9,12) c = Container(1,1) t.load(c) self.assertRaises(AssertionError, self.transport_ship.load(c)) def test_volume_and_weight_capacity_remaining_must_decrease_after_equipment_loading(self): base_volume_capacity = self.transport_ship.volume_capacity base_weight_capacity = self.transport_ship.weight_capacity c = Container(1,1) self.transport_ship.load(c) self.assertLess(self.transport_ship.volume_capacity_remaining, base_volume_capacity) self.assertLess(self.transport_ship.weight_capacity_remaining, base_weight_capacity) def test_equipment_location_must_be_set_to_the_current_loader_after_loading(self): c = Container(1,1) self.assertIsNone(c.location) self.transport_ship.load(c) self.assertEqual(self.transport_ship, c.location) def test_ship_weight_and_volume_capacity_remaining_must_increase_after_unloading(self): c = Container(1,1) self.transport_ship.load(c) weight_capacity_remaining_before_unloading = self.transport_ship.weight_capacity_remaining volume_capacity_remaining_before_unloading = self.transport_ship.volume_capacity_remaining self.transport_ship.unload(c) self.assertGreater(self.transport_ship.weight_capacity_remaining, weight_capacity_remaining_before_unloading) self.assertGreater(self.transport_ship.volume_capacity_remaining, volume_capacity_remaining_before_unloading)
def test_must_raise_if_volume_capacity_gt_volume(self): with self.assertRaises(AssertionError): self.assertRaises(AssertionError, TransportShip(10,20,30,40))