diff --git a/pcbdevice/tests/gcode/test_listToGcode.py b/pcbdevice/tests/gcode/test_listToGcode.py index 04571a2..b72d3f8 100644 --- a/pcbdevice/tests/gcode/test_listToGcode.py +++ b/pcbdevice/tests/gcode/test_listToGcode.py @@ -5,31 +5,50 @@ from pcbdevice.models.Coordinates import Coordinate class TestListToGCode(TestCase): + + oneTrace = [Coordinate(1, 2), + Coordinate(1, 5), + Coordinate(2, 5), + Coordinate(2, 8)] + + twoTrace = [Coordinate(1, 2), + Coordinate(1, 5), + Coordinate(5, 5), + Coordinate(5, 2), + Coordinate(1, 2), + Coordinate(-1, -1), + Coordinate(5, 4), + Coordinate(8, 4)] + + threeTrace = [Coordinate(1, 2), + Coordinate(1, 5), + Coordinate(5, 5), + Coordinate(5, 2), + Coordinate(1, 2), + Coordinate(-1, -1), + Coordinate(5, 4), + Coordinate(8, 4), + Coordinate(2, 9), + Coordinate(9, 45), + Coordinate(12, 12), + Coordinate(1, 10)] + def test_listToGCodeMultipleTrace(self): xSize, ySize = 2, 3 - coords = whenSingleTrace() - self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) - - coords = whenTwoTrace() - self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) - - coords = whenThreeTrace() - self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) + self.assertEqual(listToGCode(self.oneTrace, ySize, xSize), getExpected(self.oneTrace, ySize, xSize)) + self.assertEqual(listToGCode(self.twoTrace, ySize, xSize), getExpected(self.twoTrace, ySize, xSize)) + self.assertEqual(listToGCode(self.threeTrace, ySize, xSize), getExpected(self.threeTrace, ySize, xSize)) def test_listToGCodePixelSize(self): xSize, ySize = 1, 4 - coords = whenSingleTrace() - self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) + self.assertEqual(listToGCode(self.oneTrace, ySize, xSize), getExpected(self.oneTrace, ySize, xSize)) xSize, ySize = 4, 2 - coords = whenSingleTrace() - self.assertEqual(listToGCode(coords, ySize, xSize), getExpected(coords, ySize, xSize)) + self.assertEqual(listToGCode(self.oneTrace, ySize, xSize), getExpected(self.oneTrace, ySize, xSize)) xSize, ySize = 8, -1 - coords = whenSingleTrace() - self.assertRaises(RuntimeError, lambda: listToGCode(coords, ySize, xSize)) - + self.assertRaises(RuntimeError, lambda: listToGCode(self.oneTrace, ySize, xSize)) def getExpected(coords, ySize, xSize): header = ['G28', 'G90\n'] @@ -48,37 +67,4 @@ def getExpected(coords, ySize, xSize): else: content.append('G0 Z0') - return header + content + footer - - -def whenSingleTrace(): - return [Coordinate(1, 2), - Coordinate(1, 5), - Coordinate(2, 5), - Coordinate(2, 8)] - - -def whenTwoTrace(): - return [Coordinate(1, 2), - Coordinate(1, 5), - Coordinate(5, 5), - Coordinate(5, 2), - Coordinate(1, 2), - Coordinate(-1, -1), - Coordinate(5, 4), - Coordinate(8, 4)] - - -def whenThreeTrace(): - return [Coordinate(1, 2), - Coordinate(1, 5), - Coordinate(5, 5), - Coordinate(5, 2), - Coordinate(1, 2), - Coordinate(-1, -1), - Coordinate(5, 4), - Coordinate(8, 4), - Coordinate(2, 9), - Coordinate(9, 45), - Coordinate(12, 12), - Coordinate(1, 10)] \ No newline at end of file + return header + content + footer \ No newline at end of file diff --git a/pcbdevice/tests/resources/expected/text1.txt b/pcbdevice/tests/resources/expected/text1.txt new file mode 100644 index 0000000..8e78941 --- /dev/null +++ b/pcbdevice/tests/resources/expected/text1.txt @@ -0,0 +1,5 @@ +This +is +a + +test diff --git a/pcbdevice/tests/resources/expected/text2.txt b/pcbdevice/tests/resources/expected/text2.txt new file mode 100644 index 0000000..c7cf44c --- /dev/null +++ b/pcbdevice/tests/resources/expected/text2.txt @@ -0,0 +1,5 @@ +G28 +G90 +G0 Z3 + +G0 X15 Y45 diff --git a/pcbdevice/tests/utils/test_fileUtils.py b/pcbdevice/tests/utils/test_fileUtils.py index 2ecffa6..f87f846 100644 --- a/pcbdevice/tests/utils/test_fileUtils.py +++ b/pcbdevice/tests/utils/test_fileUtils.py @@ -2,6 +2,7 @@ from unittest import TestCase from pcbdevice.utils import TestUtils from pcbdevice.utils.FileUtils import FileUtils +from pcbdevice.utils.TestUtils import readStringFile resources = './pcbdevice/tests/resources/' @@ -9,18 +10,35 @@ class TestFileUtils(TestCase): def test_pbmToMatrix(self): actual, h, w = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm') expected = TestUtils.readIntFile(resources + 'formatted/test1.csv') - assert actual == expected + self.assertEqual(actual, expected) def test_saveMatrixToFile(self): actual, h, w = FileUtils.pbmToMatrix(resources + 'raw/test1.pbm') FileUtils.saveMatrixToFile(actual, resources + 'output/test1.csv') expected = TestUtils.readIntFile(resources + 'output/test1.csv') - assert actual == expected + self.assertEqual(actual, expected) + + def test_saveStringListToFile(self): + stringList = ['This', + 'is', + 'a\n', + 'test'] + + FileUtils.saveStringListToFile(stringList, resources + 'output/text1.txt') + self.assertEqual(readStringFile(resources + 'output/text1.txt'), readStringFile(resources+'expected/text1.txt')) + + stringList = ['G28', + 'G90', + 'G0 Z3\n', + 'G0 X15 Y45'] + + FileUtils.saveStringListToFile(stringList, resources + 'output/text2.txt') + self.assertEqual(readStringFile(resources + 'output/text2.txt'), readStringFile(resources + 'expected/text2.txt')) def test_getPixelSize(self): - assert 10, 10 == FileUtils.getPixelSize(10, 10, 100, 100) - assert 1, 1 == FileUtils.getPixelSize(100, 100, 100, 100) - assert 10, 10 == FileUtils.getPixelSize(10, 10, 10, 10, unit = 'cm') - assert 10, 10 == FileUtils.getPixelSize(10, 10, 1, 1, unit = 'm') - assert 254, 254 == FileUtils.getPixelSize(10, 10, 10, 10, unit = 'in') - assert 10, 5 == FileUtils.getPixelSize(10, 10, 10, 20) \ No newline at end of file + self.assertEqual((10, 10), FileUtils.getPixelSize(10, 10, 100, 100)) + self.assertEqual((1, 1), FileUtils.getPixelSize(100, 100, 100, 100)) + self.assertEqual((10, 10), FileUtils.getPixelSize(10, 10, 10, 10, unit = 'cm')) + self.assertEqual((10, 10), FileUtils.getPixelSize(10, 10, 1, 1, unit = 'm')) + self.assertEqual((25.4, 25.4), FileUtils.getPixelSize(10, 10, 10, 10, unit = 'in')) + self.assertEqual((1, 2), FileUtils.getPixelSize(10, 10, 10, 20)) \ No newline at end of file diff --git a/pcbdevice/utils/FileUtils.py b/pcbdevice/utils/FileUtils.py index 08caf56..3014aa9 100644 --- a/pcbdevice/utils/FileUtils.py +++ b/pcbdevice/utils/FileUtils.py @@ -29,6 +29,13 @@ class FileUtils: f.write('\n') f.close() + @staticmethod + def saveStringListToFile(stringList, filePath): + with open(filePath, 'w') as f: + for line in stringList: + f.write('%s\n' % line) + f.close() + @staticmethod def getPixelSize(matHeight, matWidth, pcbHeight, pcbWidth, unit = 'mm'): if unit == 'mm': diff --git a/pcbdevice/utils/TestUtils.py b/pcbdevice/utils/TestUtils.py index f5904f4..384e50d 100644 --- a/pcbdevice/utils/TestUtils.py +++ b/pcbdevice/utils/TestUtils.py @@ -11,4 +11,11 @@ def readIntFile(filePath): completeFile.append(tempArray) - return completeFile \ No newline at end of file + return completeFile + +def readStringFile(filePath): + file = open(filePath, 'r') + lines = file.readlines() + file.close() + + return lines \ No newline at end of file