Skip to content

Sound #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ nose==1.3.7
pylint==1.7.4
coverage==4.4.2
colorama==0.3.9
opencensus-ext-azure==1.1.1
opencensus-ext-azure==1.1.1
playsound
PyObjC
Binary file added sound/explosion.mp3
Binary file not shown.
Binary file added sound/explosion2.mp3
Binary file not shown.
Binary file added sound/fireworks.mp3
Binary file not shown.
Binary file added sound/splash.mp3
Binary file not shown.
Binary file added sound/splash2.mp3
Binary file not shown.
32 changes: 31 additions & 1 deletion tests/test_battleship.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
import unittest

from torpydo.battleship import parse_position
from torpydo.battleship import parse_position, is_fleet_down, get_random_position
from torpydo.ship import Color, Letter, Position, Ship
from unittest.mock import patch

class TestBattleship(unittest.TestCase):
def setUp(self):
self.ships = []
self.ships.append(init_ship(Ship("Test", 2, Color.RED), [Position(Letter.A, 1), Position(Letter.A, 2)]))

def test_parse_position_true(self):
self.assertTrue(parse_position("A1"))

def test_is_fleet_down(self):
self.assertFalse(is_fleet_down(self.ships))
self.ships[0].is_sunk = True
self.assertTrue(is_fleet_down(self.ships))

@patch("torpydo.battleship.NUMBER_ROWS", 2)
@patch("torpydo.battleship.NUMBER_COL", 2)
def test_random_duplicate_position(self):
for i in range(10):
board = []
x = [
get_random_position(board),
get_random_position(board),
get_random_position(board),
get_random_position(board),
]
y = list(set(x))
self.assertTrue(len(y) == 4)

def init_ship(ship: Ship, positions: list):
ship.positions = positions

return ship

if '__main__' == __name__:
unittest.main()
8 changes: 8 additions & 0 deletions tests/test_colours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from torpydo.battleship import right_colour
from colorama import Fore

def test_right_colour_hit():
assert right_colour(True) == Fore.RED

def test_right_colour_water():
assert right_colour(False) == Fore.BLUE
1 change: 1 addition & 0 deletions tests/test_game_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def setUp(self):

def test_check_is_hit_true(self):
self.assertTrue(GameController.check_is_hit(self.ships, Position(Letter.A, 1)))
self.assertTrue(self.ships[0].positions[0].is_shot)

def test_check_is_hit_false(self):
self.assertFalse(GameController.check_is_hit(self.ships, Position(Letter.B, 1)))
Expand Down
44 changes: 44 additions & 0 deletions tests/test_random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from torpydo.battleship import overlaps, set_direction, set_forward_rear
from torpydo.ship import Letter, Position, Ship, Color

def test_not_overlaps():
ship = Ship("test",5,Color.RED)
ship.add_position("B1")
assert overlaps(
[
Position(Letter.A,1),
Position(Letter.A,2)
],
fleet=[
ship
]
) == False

def test_overlaps():
ship = Ship("test",5, Color.RED)
ship.add_position("A2")
assert overlaps(
[
Position(Letter.A,1),
Position(Letter.A,2)
],
fleet=[
ship
]
) == True

def test_set_forward_read():
assert set_forward_rear(5,3,6) == 0
assert set_forward_rear(5,1,7) == 1
assert set_forward_rear(2,3,8) == 1
assert set_forward_rear(3,5,7) == 1
assert set_forward_rear(3,5,6) == -1

def test_set_direction():
axis, st_value = set_direction(Position(Letter.A,1))
assert axis == "horizontal"
assert st_value == 1
axis, st_value = set_direction(Position(Letter.A,2))
assert axis == "vertical"
assert st_value == 2

23 changes: 23 additions & 0 deletions tests/test_ship.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest

from torpydo.ship import Color, Letter, Position, Ship
from torpydo.game_controller import GameController

class TestBattleship(unittest.TestCase):
def setUp(self):
self.ships = []
self.ships.append(init_ship(Ship("Test", 2, Color.RED), [Position(Letter.A, 1), Position(Letter.A, 2)]))

def test_check_sunk(self):
self.assertFalse(self.ships[0].is_sunk)
GameController.check_is_hit(self.ships, Position(Letter.A, 1))
self.assertFalse(self.ships[0].is_sunk)
GameController.check_is_hit(self.ships, Position(Letter.A, 2))
self.assertTrue(self.ships[0].is_sunk)

def init_ship(ship: Ship, positions: list):
ship.positions = positions

return ship
if '__main__' == __name__:
unittest.main()
12 changes: 12 additions & 0 deletions tests/test_sound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from unittest import mock
from torpydo.battleship import is_hit_sound

def test_call_right_sound_fire():
with mock.patch("torpydo.battleship.playsound") as mf:
is_hit_sound(True)
mf.assert_called_once_with('sound/explosion2.mp3')

def test_call_right_sound_water():
with mock.patch("torpydo.battleship.playsound") as mf:
is_hit_sound(False)
mf.assert_called_once_with('sound/splash2.mp3', block=False)
Empty file added torpydo/__init__.py
Empty file.
Loading