def test_income_shoot_border(self): f = Field(4, 4) cell = f.get(2, 2) cell.mark_border() self.assertFalse(ShipService.shoot_to(f, 2, 2)) self.assertTrue(cell.is_shooted) self.assertTrue(cell.is_border)
def test_income_shoot_hit(self): f = Field(4, 4) cell = f.get(2, 2) cell.mark_ship() self.assertTrue(ShipService.shoot_to(f, 2, 2)) self.assertTrue(cell.is_shooted) self.assertTrue(cell.is_ship)
def test_draw_border(self): f = Field(5, 5) f.draw_border([(1, 1), (1, 2)]) for c in f.cells: if (c.x, c.y) in ((1, 1), (1, 2)): self.assertEqual(c.value, 'border') else: self.assertEqual(c.value, 'empty')
def test_set(self): f = Field(5, 5) f.set(2, 2, 'ship') self.assertTrue(f.get(2, 2).is_ship) self.assertFalse(f.get(2, 2).is_shooted) f.set(2, 2, 'border', True) self.assertTrue(f.get(2, 2).is_border) self.assertTrue(f.get(2, 2).is_shooted)
def test_get_ship_by_cell_surrounded(self): f = Field() f.get(3, 3).mark_ship() f.get(1, 3).mark_ship() f.get(3, 1).mark_ship() f.get(5, 3).mark_ship() f.get(3, 5).mark_ship() self.assertEqual(ShipService.get_ship_by_cell(f, 3, 3), [(3, 3)])
def test_income_shoot_outrange(self): f = Field(4, 4) with self.assertRaises(CoordOutOfRange): ShipService.shoot_to(f, 22, 2) with self.assertRaises(CoordOutOfRange): ShipService.shoot_to(f, 2, -2)
def test_init(self): f = Field(5, 3) self.assertEqual(f.max_y, 3) self.assertEqual(f.max_x, 5) self.assertEqual(len(f.cells), 15) for c in f.cells: self.assertEqual(c.value, 'empty')
def test_get_ship_outrange(self): f = Field(3, 3) with self.assertRaises(CoordOutOfRange): ShipService.get_ship_if_killed(f, -1, 2) with self.assertRaises(CoordOutOfRange): ShipService.get_ship_if_killed(f, 0, 32)
def test_is_suitable_vector(self): f = Field(5, 5) self.assertTrue(f.is_suitable_ship_vektor(1, 1, 3)) self.assertTrue(f.is_suitable_ship_vektor(1, 1, 3, True)) self.assertFalse(f.is_suitable_ship_vektor(3, 3, 3)) self.assertFalse(f.is_suitable_ship_vektor(3, 3, 3, True)) self.assertFalse(f.is_suitable_ship_vektor(-1, 1, 3)) self.assertFalse(f.is_suitable_ship_vektor(1, 11, 3, True))
def test_pit_ships_random(self): f = Field(5, 5) ShipService.put_ships_random(f, fleet=[5, 1]) ships = set() for c in f.cells: if c.is_ship: ships.add(tuple(ShipService.get_ship_by_cell(f, c.x, c.y))) self.assertEqual(2, len(ships)) self.assertEqual(sum([len(i) for i in ships]), 6)
def test_put_ship(self): f = Field(3, 3) ShipService.put_ship(f, 0, 0, 2, True) for cell in f.cells: if (cell.x, cell.y) in ((0, 0), (0, 1)): self.assertTrue(cell.is_ship) elif (cell.x, cell.y) in ((1, 0), (1, 1), (1, 2), (0, 2)): self.assertTrue(cell.is_border) else: self.assertTrue(cell.is_empty)
def test_is_fleet_killed(self): f = Field(4, 4) self.assertTrue(ShipService.is_fleet_killed(f)) f.get(2, 2).mark_ship() self.assertFalse(ShipService.is_fleet_killed(f)) f.get(2, 2).shoot() self.assertTrue(ShipService.is_fleet_killed(f))
def test_set_outrange(self): f = Field(5, 5) with self.assertRaises(CoordOutOfRange): f.set(-1, 2, 'ship') with self.assertRaises(CoordOutOfRange): f.set(2, 44, 'ship')
def test_is_coord_correct(self): f = Field(5, 5) self.assertTrue(f.is_correct_coord(3, 3)) self.assertTrue(f.is_correct_coord(0, 0)) self.assertTrue(f.is_correct_coord(4, 4)) self.assertFalse(f.is_correct_coord(-1, 4)) self.assertFalse(f.is_correct_coord(1, 44)) self.assertFalse(f.is_correct_coord(1, 5))
def test_get_ship_by_cell_v(self): f = Field(5, 5) f.get(2, 1).mark_ship() f.get(2, 2).mark_ship() f.get(2, 3).mark_ship() self.assertFalse( set(ShipService.get_ship_by_cell(f, 2, 2)).difference([(2, 1), (2, 2), (2, 3)]))
def test_get_available_vectors(self): f = Field(3, 3) f.get(0, 0).mark_ship() f.get(0, 1).mark_border() f.get(1, 0).mark_border() f.get(1, 1).mark_border() self.assertEqual(ShipService.get_available_vectors(f, 3), [(2, 0, 3, True), (0, 2, 3, False)]) self.assertEqual(ShipService.get_available_vectors(f, 2), [(2, 0, 2, True), (2, 1, 2, True), (0, 2, 2, False), (1, 2, 2, False)]) self.assertEqual(ShipService.get_available_vectors(f, 1), [(2, 0, 1, True), (2, 0, 1, False), (2, 1, 1, True), (2, 1, 1, False), (0, 2, 1, True), (0, 2, 1, False), (1, 2, 1, True), (1, 2, 1, False), (2, 2, 1, True), (2, 2, 1, False)])
def test_get_ship_if_killed(self): f = Field(5, 5) f.get(0, 1).mark_ship() f.get(0, 0).mark_ship() self.assertEqual(ShipService.get_ship_if_killed(f, 0, 1), {}) f.get(0, 1).shoot() self.assertEqual(ShipService.get_ship_if_killed(f, 0, 0), {}) f.get(0, 0).shoot() resp = ShipService.get_ship_if_killed(f, 0, 1) self.assertEqual(len(resp['ship']), 2) self.assertEqual(len(resp['border']), 4) self.assertFalse(set(resp['ship']).difference([(0, 0), (0, 1)])) self.assertFalse( set(resp['border']).difference([(1, 0), (1, 1), (1, 2), (0, 2)]))
def test_ribs_for_coords_real(self): f = Field(4, 4) self.assertFalse({(0, 1), (1, 0)}.difference(Matrix.ribs_for_coord(f, 0, 0))) self.assertFalse({(3, 2), (2, 3)}.difference(Matrix.ribs_for_coord(f, 3, 3)))
def test_get(self): f = Field(5, 5) cell = f.get(3, 3) self.assertEqual(cell.x, 3) self.assertEqual(cell.y, 3)
def test_set_unkonw_value(self): f = Field(5, 5) f.set(2, 2, 'ship') self.assertTrue(f.get(2, 2).is_ship) with self.assertRaises(UnknownCellValue): f.set(1, 1, 'unknown')
def test_with_filter(self): field = Field(4, 4) coord = Matrix.coords_by_vektor(field, 3, 3, 3, True) self.assertEqual(coord, [(3, 3)])
def test_filter_negative(self): field = Field(4, 4) coord = Matrix.coords_by_vektor(field, -1, 1, 3) self.assertEqual(coord, [(0, 1), (1, 1)])
def test_income_shoot_missed(self): f = Field(4, 4) cell = f.get(2, 2) self.assertFalse(ShipService.shoot_to(f, 2, 2)) self.assertTrue(cell.is_shooted) self.assertTrue(cell.is_empty)
def test_corners_for_coords_real(self): f = Field(4, 4) self.assertFalse({(1, 1)}.difference(Matrix.conrers_for_coord(f, 0, 0))) self.assertFalse({(3, 3)}.difference(Matrix.conrers_for_coord(f, 2, 2)))