瀏覽代碼

add tests for BasePencil class

olinox 9 年之前
父節點
當前提交
3fba7efef5
共有 8 個文件被更改,包括 96 次插入19 次删除
  1. 9 9
      gridviewer/GridViewer.py
  2. 0 0
      gridviewer/GridViewerCell.py
  3. 0 0
      gridviewer/__init__.py
  4. 0 0
      gridviewer/main.py
  5. 0 0
      gridviewer/main.ui
  6. 0 0
      gridviewer/readme.md
  7. 18 10
      pypog/pencil.py
  8. 69 0
      tests/test_pencils.py

+ 9 - 9
tests/gridviewer/GridViewer.py → gridviewer/GridViewer.py

@@ -3,20 +3,22 @@ Created on 26 nov. 2016
 
 
 @author: olinox
 @author: olinox
 '''
 '''
+from PyQt5.QtCore import QPointF, QMimeData
+from PyQt5.QtWidgets import QMainWindow, \
+    QApplication, QGraphicsScene, QGraphicsView, QMessageBox
+import ipdb  # until I find another way to print traceback with pyqt5
+
+from gridviewer.GridViewerCell import GridViewerCell
+from gridviewer.main import Ui_window
+from pypog import geometry
+
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
     import os, sys
     import os, sys
     pypog_path = (os.path.abspath("..\\..\\"))
     pypog_path = (os.path.abspath("..\\..\\"))
     sys.path.append(pypog_path)
     sys.path.append(pypog_path)
     
     
-from PyQt5.QtCore import QPointF, QMimeData
-from PyQt5.QtWidgets import QMainWindow, \
-    QApplication, QGraphicsScene, QGraphicsView, QMessageBox
-import ipdb  # until I find another way to print traceback with pyqt5
 
 
-from pypog import geometry
-from tests.gridviewer.GridViewerCell import GridViewerCell
-from tests.gridviewer.main import Ui_window
 
 
 
 
 class GridViewer(QMainWindow):
 class GridViewer(QMainWindow):
@@ -98,13 +100,11 @@ class GridViewer(QMainWindow):
         for x, y in new_selection:
         for x, y in new_selection:
             self.cells[(x, y)].select()
             self.cells[(x, y)].select()
             
             
-            
     def to_clipboard(self):
     def to_clipboard(self):
         data = QMimeData()
         data = QMimeData()
         data.setText(self.ui.txt_coords.toPlainText())
         data.setText(self.ui.txt_coords.toPlainText())
         app.clipboard().setMimeData(data)
         app.clipboard().setMimeData(data)
 
 
-
     def update_cell_labels(self):
     def update_cell_labels(self):
         for cell in self.cells.values():
         for cell in self.cells.values():
             cell.show_label( bool(self.ui.chk_displayCoords.isChecked()) )
             cell.show_label( bool(self.ui.chk_displayCoords.isChecked()) )

+ 0 - 0
tests/gridviewer/GridViewerCell.py → gridviewer/GridViewerCell.py


+ 0 - 0
tests/gridviewer/__init__.py → gridviewer/__init__.py


+ 0 - 0
tests/gridviewer/main.py → gridviewer/main.py


+ 0 - 0
tests/gridviewer/main.ui → gridviewer/main.ui


+ 0 - 0
tests/gridviewer/readme.md → gridviewer/readme.md


+ 18 - 10
pypog/pencil.py

@@ -14,18 +14,25 @@ Created on 5 dec. 2016
         
         
 @author: olinox
 @author: olinox
 '''
 '''
-from abc import ABCMeta
 
 
-from pypog import geometry
+from pypog import geometry, Grid
+
+class NotStartedException(Exception): 
+    pass
+
+class AlreadyStartedException(Exception): 
+    pass
+
 
 
 class BasePencil(object):
 class BasePencil(object):
     """Base class of all pencils
     """Base class of all pencils
-    This is an abstract class: override it!"""
-    __metaclass__ = ABCMeta
+    This class does not  paint anything: override it!"""
     
     
     def __init__(self, grid):
     def __init__(self, grid):
         
         
         # do we really need the grid ref? cell_shape could be enough?
         # do we really need the grid ref? cell_shape could be enough?
+        if not isinstance(grid, Grid.Grid):
+            raise TypeError("'grid' should be a Grid object (given: {})".format(grid))
         self._grid = grid
         self._grid = grid
         
         
         self._origin = None
         self._origin = None
@@ -97,7 +104,7 @@ class BasePencil(object):
         """start a new painting
         """start a new painting
         (x0, y0) is the origin of the painting, and will not be modified."""
         (x0, y0) is the origin of the painting, and will not be modified."""
         if len(self._selection) > 0:
         if len(self._selection) > 0:
-            raise Exception("the pencil has already been started")
+            raise AlreadyStartedException("the pencil has already been started")
         self._origin = (x0, y0)
         self._origin = (x0, y0)
         self.update(x0, y0)
         self.update(x0, y0)
 
 
@@ -107,7 +114,7 @@ class BasePencil(object):
         if not all(isinstance(var, int) for var in (x, y)):
         if not all(isinstance(var, int) for var in (x, y)):
             raise TypeError("x, y has to be an integers (given: {})".format((x, y)))
             raise TypeError("x, y has to be an integers (given: {})".format((x, y)))
         if self._origin == None:
         if self._origin == None:
-            raise Exception("Pencil has to be started before any update: use 'start' method")
+            raise NotStartedException("Pencil has to be started before any update: use 'start' method")
         self._position = (x, y)
         self._position = (x, y)
         self._update()
         self._update()
         
         
@@ -126,8 +133,9 @@ class LinePencil(BasePencil):
         line = set( geometry.line2d(self._grid.cell_shape, x0, y0, x, y) )
         line = set( geometry.line2d(self._grid.cell_shape, x0, y0, x, y) )
         
         
         # apply size with geometry.zone
         # apply size with geometry.zone
-        for x, y in line:
-            result |= set( geometry.zone(self._grid.cell_shape, x, y, self.size) )
+        if self._grid.size >= 1: 
+            for x, y in line:
+                result |= set( geometry.zone(self._grid.cell_shape, x, y, self.size - 1) )
         
         
         self._added = result - self._selection
         self._added = result - self._selection
         self._removed = self._selection - result
         self._removed = self._selection - result
@@ -205,7 +213,7 @@ class PaintPotPencil(BasePencil):
         self._selection = current_selection
         self._selection = current_selection
         
         
         
         
-def RectanglePencil(BasePencil):
+class RectanglePencil(BasePencil):
     """ RectanglePencil draw a plain rectangle with origin being the 
     """ RectanglePencil draw a plain rectangle with origin being the 
     top left corner, and position the bottom right corner"""
     top left corner, and position the bottom right corner"""
     def __init__(self, *args):
     def __init__(self, *args):
@@ -221,7 +229,7 @@ def RectanglePencil(BasePencil):
         self._removed = self._selection - new_selection
         self._removed = self._selection - new_selection
         self._selection = new_selection
         self._selection = new_selection
         
         
-def HollowRectanglePencil(BasePencil):
+class HollowRectanglePencil(BasePencil):
     """ HollowRectanglePencil draw an hollow rectangle with origin being the 
     """ HollowRectanglePencil draw an hollow rectangle with origin being the 
     top left corner, and position the bottom right corner"""
     top left corner, and position the bottom right corner"""
     def __init__(self, *args):
     def __init__(self, *args):

+ 69 - 0
tests/test_pencils.py

@@ -0,0 +1,69 @@
+'''
+Created on 12 dec. 2016
+
+@author: olinox
+'''
+import unittest
+
+from pypog import geometry, Grid, pencil
+
+class Test(unittest.TestCase):
+
+    def test_base_pencil(self):
+        for cell_shape in (geometry.HEX, geometry.SQUARE):
+        
+            self.assertRaises(TypeError, pencil.BasePencil, "invalid arg")
+            
+            grid = Grid.Grid(cell_shape, 30, 30)
+            my_pencil = pencil.BasePencil(grid)
+            
+            # default origin and position
+            self.assertEqual(my_pencil.origin, None)
+            self.assertEqual(my_pencil.position, None)
+            self.assertRaises(AttributeError, my_pencil.origin, (1,1))
+            self.assertRaises(AttributeError, my_pencil.position, (1,1))
+            
+            # size
+            self.assertRaises(TypeError, setattr, my_pencil, "size", "a")
+            self.assertRaises(ValueError, setattr, my_pencil, "size", -1)
+            self.assertEqual(my_pencil.size, 1)
+            
+            # selection, added, removed
+            self.assertEqual(my_pencil.selection, [])
+            self.assertEqual(my_pencil.added, [])
+            self.assertEqual(my_pencil.removed, [])
+            
+            # pencil methods
+            self.assertRaises(TypeError, my_pencil.start, "a")
+            self.assertRaises(pencil.NotStartedException, my_pencil.update, 1,1)
+            self.assertRaises(NotImplementedError, my_pencil._update)
+            
+            try:
+                my_pencil.start(0,0)
+            except NotImplementedError:
+                pass
+            self.assertRaises(TypeError, my_pencil.update, "a")
+            self.assertEqual(my_pencil.origin, (0,0))
+            
+    def test_line_pencil(self):
+        pass
+    
+    def test_free_pencil(self):
+        pass
+    
+    def test_pot_pencil(self):
+        pass
+    
+    def test_rect_pencil(self):
+        pass
+    
+    def test_hrect_pencil(self):
+        pass
+    
+    def test_boundary_pencil(self):
+        pass
+    
+    
+
+if __name__ == "__main__":
+    unittest.main()