def setUp(self):
        """Set up tests."""
        initLocale()
        controller = MagicMock()
        controller.getLifemapSize = MagicMock(return_value=(50, 50))
        ruleSetup = (
            'GameOfLIfe.txt',
            0,
            Colors(['White', 'Black']),
            ['Moore', '0', ''],
            {
                (0, (5, 3)): 1,
                (1, (5, 3)): 1,
                (1, (6, 2)): 1
            },
            ['White', 'Black'],
        )
        controller.getRuleFiles = MagicMock(return_value=[ruleSetup])

        self.view = View(controller)
Exemple #2
0
 def __init__(self, latent_size: int):
     super().__init__()
     self.__sequential_blocks = [
         LinearGeneratorBlock(latent_size, 256),
         LinearGeneratorBlock(256, 256),
         LinearGeneratorBlock(256, 256),
         nn.Linear(256, 28 * 28),
         nn.Sigmoid(),
         View(-1, 1, 28, 28)
     ]
     self.main = nn.Sequential(*self.__sequential_blocks)
Exemple #3
0
 def __init__(self, latent_size: int):
     super().__init__()
     self.__sequential_blocks = [
         nn.Linear(latent_size, 1024 * 8 * 8),
         View(-1, 1024, 8, 8),
         CnnDecoderBlock(1024, 512),
         CnnDecoderBlock(512, 256),
         CnnDecoderBlock(256, 128),
         nn.ConvTranspose2d(128, 3, 3, 1, 1),
         nn.Tanh(),
         MapTanhZeroOne()
     ]
     self.main = nn.Sequential(*self.__sequential_blocks)
 def __init__(self, latent_size: int):
     super().__init__()
     self.__sequential_blocks = [
         nn.Linear(latent_size, 256 * 7 * 7),
         nn.ReLU(True),
         View(-1, 256, 7, 7),
         CnnDecoderBlock(256, 128),
         CnnDecoderBlock(128, 64),
         nn.ConvTranspose2d(64, 3, 3, 1, 1),
         nn.Tanh(),
         MapTanhZeroOne()
     ]
     self.main = nn.Sequential(*self.__sequential_blocks)
Exemple #5
0
    def __init__(self, noise_dim: int):
        super().__init__()

        self.__sequential_blocks = [
            LinearGeneratorBlock(noise_dim, 512),
            LinearGeneratorBlock(512, 512),
            LinearGeneratorBlock(512, 512),
            LinearGeneratorBlock(512, 512),
            LinearGeneratorBlock(512, 512),
            LinearGeneratorBlock(512, 512),
            nn.Linear(512, 28 * 28),
            nn.Sigmoid(),
            View(-1, 1, 28, 28)
        ]
        self.main = nn.Sequential(*self.__sequential_blocks)
Exemple #6
0
 def __init__(self, noise_dim: int):
     super().__init__()
     self.__sequential_blocks = [
         LinearGeneratorBlock(noise_dim, 128),
         LinearGeneratorBlock(128, 128),
         LinearGeneratorBlock(128, 128),
         LinearGeneratorBlock(128, 256 * 8 * 8),
         View(-1, 256, 8, 8),
         CnnDecoderBlock(256, 128),
         CnnDecoderBlock(128, 64),
         nn.ConvTranspose2d(64, 3, 3, 1, 1),
         nn.Tanh(),
         MapTanhZeroOne()
     ]
     self.main = nn.Sequential(*self.__sequential_blocks)
class ViewTests(unittest.TestCase):
    """View unit tests."""
    def setUp(self):
        """Set up tests."""
        initLocale()
        controller = MagicMock()
        controller.getLifemapSize = MagicMock(return_value=(50, 50))
        ruleSetup = (
            'GameOfLIfe.txt',
            0,
            Colors(['White', 'Black']),
            ['Moore', '0', ''],
            {
                (0, (5, 3)): 1,
                (1, (5, 3)): 1,
                (1, (6, 2)): 1
            },
            ['White', 'Black'],
        )
        controller.getRuleFiles = MagicMock(return_value=[ruleSetup])

        self.view = View(controller)

    def test_windowsTransfer(self):
        """Test windows transfer."""
        with self.assertRaises(AttributeError):
            self.view.cvsCells
        self.view.showMainWindow()
        self.assertEqual(type(self.view.cvsCells), Canvas)

    def test_inboundsDraw(self):
        """Inbounds draw test."""
        self.view.controller.getLifemapSize = MagicMock(return_value=(10, 10))
        self.view.showMainWindow()

        i, j = 1, 2
        colorIndex = 0
        obje = self.view.draw(i, j, colorIndex)

        CELL_SIZE = self.view.CELL_SIZE
        x0, y0 = j * CELL_SIZE, i * CELL_SIZE
        x1, y1 = x0 + CELL_SIZE, y0 + CELL_SIZE
        self.assertEqual(self.view.cvsCells.coords(obje), [x0, y0, x1, y1])

    def test_outboundsDraw(self):
        """Outbounds draw test."""
        self.view.controller.getLifemapSize = MagicMock(return_value=(10, 10))
        self.view.showMainWindow()
        object = self.view.draw(10, 10, 0)
        self.assertEqual(object, None)

    def test_inboundsMouseDraw(self):
        """Test mouse draw."""
        self.view.controller.getLifemapSize = MagicMock(return_value=(5, 5))
        self.view.showMainWindow()
        initialObjectIds = self.view.cvsCells.find_all()

        cellSize = self.view.CELL_SIZE
        motionEvent = MagicMock()
        i, j = 0, 4
        motionEvent.x = int(j * cellSize + cellSize // 2)
        motionEvent.y = int(i * cellSize + cellSize // 2)
        self.view.on_CvsCells_HoldingMouseOver(motionEvent)

        currentObjectIds = self.view.cvsCells.find_all()
        objectsDiff = [
            *filter(lambda loc_id: loc_id not in initialObjectIds,
                    currentObjectIds)
        ]
        self.assertTrue(len(objectsDiff) == 1)

        newObjectId = objectsDiff[0]
        newObjectCoords = self.view.cvsCells.coords(newObjectId)
        x0, y0 = j * cellSize, i * cellSize
        x1, y1 = x0 + cellSize, y0 + cellSize
        self.assertEqual(newObjectCoords, [x0, y0, x1, y1])

    def test_outboundsMouseDraw(self):
        """Test mouse draw."""
        self.view.controller.getLifemapSize = MagicMock(return_value=(5, 5))
        self.view.showMainWindow()
        initialObjectIds = self.view.cvsCells.find_all()

        cellSize = self.view.CELL_SIZE
        motionEvent = MagicMock()
        i, j = 5, 5
        motionEvent.x = int(j * cellSize + cellSize // 2)
        motionEvent.y = int(i * cellSize + cellSize // 2)
        self.view.on_CvsCells_HoldingMouseOver(motionEvent)

        currentObjectIds = self.view.cvsCells.find_all()
        objectsDiff = [
            *filter(lambda loc_id: loc_id not in initialObjectIds,
                    currentObjectIds)
        ]
        self.assertTrue(len(objectsDiff) == 0)
Exemple #8
0
def initView(controller):
    """Init view instance."""
    return View(controller)
Exemple #9
0
from modules.view import View
from modules.model import Model

view = View(10)  # create view - 10 is field size
model = Model(view)  # add logic

view.render()
Exemple #10
0
 def setUp(self):
     self.view = View()
Exemple #11
0
class TestView(unittest.TestCase):
    def setUp(self):
        self.view = View()

    def testPrintInfoExpectsArgument(self):
        with self.assertRaises(TypeError):
            self.view.print_line()

    def testPrintInfoPrintsInput(self):
        message = "test message"
        old_stdout = sys.stdout
        sys.stdout = StringIO.StringIO()
        self.view.print_line(message)
        received_stdout = sys.stdout.getvalue().strip()
        sys.stdout = old_stdout
        self.assertEqual(received_stdout, message)

    def testPrintBoldExpectsArgument(self):
        with self.assertRaises(TypeError):
            self.view.print_bold()

    def testPrintBoldPrintsMessageInAnsiBold(self):
        message = "bold message"
        old_stdout = sys.stdout
        sys.stdout = StringIO.StringIO()
        self.view.print_bold(message)
        received_stdout = sys.stdout.getvalue().strip()
        sys.stdout = old_stdout
        bold_message = "\x1b[01m" + message + "\x1b[0m"
        self.assertEqual(received_stdout, bold_message)

    def testPrintErrorExpectsArgument(self):
        with self.assertRaises(TypeError):
            self.view.print_error()

    def testPrintErrorPrintsMessageInAnsiBold(self):
        message = "error message"
        old_stdout = sys.stdout
        sys.stdout = StringIO.StringIO()
        self.view.print_error(message)
        received_stdout = sys.stdout.getvalue()
        sys.stdout = old_stdout
        formatted_message = "\x1b[01m" + message + "\x1b[0m\n"
        self.assertEqual(received_stdout, formatted_message)

    def testPrintHelpPrintsAvailableCommandsToStdout(self):
        old_stdout = sys.stdout
        sys.stdout = StringIO.StringIO()
        self.view.print_help()
        received_stdout = sys.stdout.getvalue()
        sys.stdout = old_stdout
        regexp = re.compile("Available commands:")
        self.assertRegexpMatches(received_stdout, regexp)