def test_ScriptRunner_encoding(self): out = StdOutCollector() ScriptRunner("# -*- coding: utf-8 -*-\nprint(1/2)", stdout=out, stderr=out) self.assertEqual(out.lines(), ["0.5"]) out = StdOutCollector() ScriptRunner(u"# -*- coding: utf-8 -*-\nprint(1/2)", stdout=out, stderr=out) self.assertEqual(out.lines(), ["0.5"])
def test_ScriptRunner_print_function(self): out = StdOutCollector() ScriptRunner("print 'hey!'", stdout=out, stderr=out) if PY3: self.assertEqual(out.lines()[-1], "SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hey!')?") else: self.assertEqual(out.lines(), ["hey!"])
def test_ScriptRunner_oldDivision(self): realGetDefault = drawBot.scriptTools.getDefault def mockedGetDefault(*args): return False drawBot.scriptTools.getDefault = mockedGetDefault try: out = StdOutCollector() ScriptRunner("print(1/2)", stdout=out, stderr=out) if PY3: self.assertEqual(out.lines(), ["0.5"]) else: self.assertEqual(out.lines(), ["0"]) finally: drawBot.scriptTools.getDefault = realGetDefault
def executeScriptPath(self, path): # read content of py file and exec it import __future__ from drawBot.misc import warnings with open(path) as f: source = f.read() compileFlags = __future__.CO_FUTURE_DIVISION code = compile(source, path, "exec", flags=compileFlags, dont_inherit=True) namespace = {"__name__": "__main__", "__file__": path} warnings.resetWarnings() # so we can test DB warnings with StdOutCollector(captureStdErr=True) as output: cwd = os.getcwd() os.chdir(os.path.dirname(path)) try: exec(code, namespace) except: traceback.print_exc() os.chdir(cwd) return output.lines()
def test_instructionStack(self): expected = [ "reset None", "newPage 200 200", "save", "clipPath moveTo 5.0 5.0 lineTo 15.0 5.0 lineTo 15.0 15.0 lineTo 5.0 15.0 closePath", "restore", "image Image Object 10 10 0.5 None", "blendMode saturation", "transform 1 0 0 1 10 10", "drawPath moveTo 10.0 10.0 lineTo 110.0 10.0 lineTo 110.0 110.0 lineTo 10.0 110.0 closePath", "textBox foo bar 82.48291015625 84.0 35.0341796875 26.0 center", "frameDuration 10", "saveImage * {'myExtraAgrument': True}" ] with StdOutCollector() as output: import drawBot drawBot.newDrawing() drawBot.size(200, 200) drawBot.save() path = drawBot.BezierPath() path.rect(5, 5, 10, 10) drawBot.clipPath(path) drawBot.restore() im = drawBot.ImageObject() with im: drawBot.size(20, 20) drawBot.rect(5, 5, 10, 10) drawBot.image(im, (10, 10), alpha=.5) drawBot.blendMode("saturation") drawBot.translate(10, 10) drawBot.rect(10, 10, 100, 100) drawBot.text("foo bar", (100, 100), align="center") drawBot.frameDuration(10) drawBot.saveImage("*", myExtraAgrument=True) drawBot.endDrawing() self.assertEqual(output.lines(), expected)
def test_saveImage_png_ffmpegCodec(self): self.makeTestDrawing() with StdOutCollector(captureStdErr=True) as output: self._saveImageAndReturnSize(".png", ffmpegCodec="mpeg4") self.assertEqual(output.lines(), [ '*** DrawBot warning: Unrecognized saveImage() option found for PNGContext: ffmpegCodec ***' ])
def test_saveImage_mp4_multipage(self): self.makeTestDrawing() with StdOutCollector(captureStdErr=True) as output: self._saveImageAndReturnSize(".mp4", multipage=True) self.assertEqual(output.lines(), [ '*** DrawBot warning: Unrecognized saveImage() option found for MP4Context: multipage ***' ])
def test(self): from drawBot.drawBotDrawingTools import _drawBotDrawingTool code = compile(source, "<%s>" % exampleName, "exec") namespace = {} _drawBotDrawingTool._addToNamespace(namespace) def mockSaveImage(path, **options): fileName = "example_mockSaveImage_" + os.path.basename(path) path = os.path.join(tempTestDataDir, fileName) drawBot.saveImage(path, **options) namespace["saveImage"] = mockSaveImage namespace["image"] = mockImage namespace["imageSize"] = mockImageSize namespace["imagePixelColor"] = mockImagePixelColor namespace["Variable"] = mockVariable namespace["printImage"] = mockPrintImage namespace["installFont"] = mockInstallFont namespace["uninstallFont"] = mockUninstallFont namespace["randint"] = mockRandInt randomSeed(0) drawBot.newDrawing() with StdOutCollector(captureStdErr=True): exec(code, namespace) fileName = "example_%s.png" % exampleName imagePath = os.path.join(tempTestDataDir, fileName) expectedImagePath = os.path.join(testDataDir, fileName) if doSaveImage: drawBot.saveImage(imagePath) self.assertImagesSimilar(imagePath, expectedImagePath)
def test_arbitraryOption(self): self.makeTestAnimation(1) with StdOutCollector(captureStdErr=True) as output: self._saveImageAndReturnSize(".png", someArbitraryOption="foo") self.assertEqual(output.lines(), [ '*** DrawBot warning: Unrecognized saveImage() option found for PNGContext: someArbitraryOption ***' ])
def test_saveImage_multipage_positionalArgument(self): self.makeTestDrawing() with TempFile(suffix=".png") as tmp: with StdOutCollector(captureStdErr=True) as output: drawBot.saveImage(tmp.path, False) self.assertEqual(output.lines(), [ "*** DrawBot warning: 'multipage' should be a keyword argument: use 'saveImage(path, multipage=True)' ***" ])
def test_saveImage_mp4_imageJPEGCompressionFactor(self): self.makeTestDrawing() with StdOutCollector(captureStdErr=True) as output: self._saveImageAndReturnSize(".mp4", imageJPEGCompressionFactor=0.5) self.assertEqual(output.lines(), [ '*** DrawBot warning: Unrecognized saveImage() option found for MP4Context: imageJPEGCompressionFactor ***' ])
def test_runPackages(self): path = os.path.join(os.path.dirname(__file__), "package/empty.drawbot") package = DrawBotPackage(path) with StdOutCollector() as output: succes, _ = package.run() self.assertEqual(output.lines(), ['hello world'])
def test_saveImage_unknownContext(self): self.makeTestDrawing() with self.assertRaises(DrawBotError) as cm: drawBot.saveImage("foo.abcde") self.assertEqual(cm.exception.args[0], "Could not find a supported context for: 'abcde'") with self.assertRaises(DrawBotError) as cm: with StdOutCollector(captureStdErr=True) as output: drawBot.saveImage(["foo.abcde"]) self.assertEqual(output.lines(), ['*** DrawBot warning: saveImage([path, path, ...]) is deprecated, use multiple saveImage statements. ***']) self.assertEqual(cm.exception.args[0], "Could not find a supported context for: 'abcde'")
def test_ScriptRunner_checkSyntaxOnly(self): out = StdOutCollector() ScriptRunner("print(aaaa)", stdout=out, stderr=out, checkSyntaxOnly=True) self.assertEqual(out.lines(), []) out = StdOutCollector() ScriptRunner("print('hello world!')", stdout=out, stderr=out, checkSyntaxOnly=False) self.assertEqual(out.lines(), ['hello world!']) out = StdOutCollector() ScriptRunner("print('hello world!')", stdout=out, stderr=out, checkSyntaxOnly=True) self.assertEqual(out.lines(), []) out = StdOutCollector() ScriptRunner("aaa bbb", stdout=out, stderr=out, checkSyntaxOnly=True) self.assertEqual(out.lines()[-1], 'SyntaxError: invalid syntax')
def test_export_mov(self): self.makeTestAnimation(5) with self.assertRaises(DrawBotError) as cm: with StdOutCollector(captureStdErr=True) as output: self._saveImageAndReturnSize(".mov") if macOSVersion < "10.15": # a warning on lower then 10.15 self.assertEqual(output.lines(), ["*** DrawBot warning: Export to '.mov' is deprecated, use '.mp4' instead. ***"]) else: # a traceback on 10.15 self.assertEqual(cm.exception.args[0], "Export to '.mov' was deprecated and is not supported on this system (10.15 and up). Use .mp4 instead.")
def test(self): import __future__ from drawBot.drawBotDrawingTools import _drawBotDrawingTool compileFlags = __future__.CO_FUTURE_DIVISION code = compile(source, "<%s>" % exampleName, "exec", flags=compileFlags, dont_inherit=True) namespace = {} _drawBotDrawingTool._addToNamespace(namespace) def mockSaveImage(path, **options): fileName = "example_mockSaveImage_" + os.path.basename(path) path = os.path.join(tempTestDataDir, fileName) drawBot.saveImage(path, **options) namespace["saveImage"] = mockSaveImage namespace["image"] = mockImage namespace["imageSize"] = mockImageSize namespace["imagePixelColor"] = mockImagePixelColor namespace["Variable"] = mockVariable namespace["printImage"] = mockPrintImage namespace["installFont"] = mockInstallFont namespace["uninstallFont"] = mockUninstallFont namespace["randint"] = mockRandInt randomSeed(0) drawBot.newDrawing() with StdOutCollector(captureStdErr=True): exec(code, namespace) fileName = "example_%s.png" % exampleName imagePath = os.path.join(tempTestDataDir, fileName) expectedImagePath = os.path.join(testDataDir, fileName) if doSaveImage: drawBot.saveImage(imagePath) if allowFuzzyImageComparison: self.assertImagesSimilar(imagePath, expectedImagePath) else: self.assertFilesEqual(imagePath, expectedImagePath)
def test_formattedString_issue337_part3(self): # Verifying we get the correct line height on an empty string expected = [ 'reset None', 'newPage 1000 1000', 'textBox A 0 -34.0 26.8994140625 104.0 left', 'textBox B 0 -46.0 25.751953125 104.0 left', 'textBox C 0 -58.0 26.9189453125 104.0 left', 'textBox A 10 -34.0 26.8994140625 104.0 left', 'textBox 10 48.0 20.0 104.0 left', 'textBox C 10 -58.0 26.9189453125 104.0 left', 'saveImage * {}' ] with StdOutCollector() as output: drawBot.newDrawing() fs = drawBot.FormattedString("A\nB\nC\n") drawBot.text(fs, (0, 60)) fs = drawBot.FormattedString("A\n\nC\n") drawBot.text(fs, (10, 60)) drawBot.saveImage("*") drawBot.endDrawing() self.assertEqual(output.lines(), expected)
def test_ScriptRunner_division(self): out = StdOutCollector() ScriptRunner("print(1/2)", stdout=out, stderr=out) self.assertEqual(out.lines(), ["0.5"])
def test_ScriptRunner_StdOutCollector(self): out = StdOutCollector() ScriptRunner("print('hey!')", stdout=out, stderr=out) self.assertEqual(out.lines(), ["hey!"])
def test_ScriptRunner_namespace(self): out = StdOutCollector() ScriptRunner("print(aaaa)", stdout=out, stderr=out, namespace=dict(aaaa=123)) self.assertEqual(out.lines(), ["123"])
def test_saveImage_png_multipage(self): self.makeTestDrawing() with StdOutCollector(captureStdErr=True) as output: self._saveImageAndReturnSize(".png", multipage=False) self.assertEqual(output.lines(), [])
def test_saveImage_mp4_imagePNGGamma(self): self.makeTestDrawing() with StdOutCollector(captureStdErr=True) as output: self._saveImageAndReturnSize(".mp4", imagePNGGamma=0.5) self.assertEqual(output.lines(), [])
def test_export_mov(self): self.makeTestAnimation(5) with StdOutCollector(captureStdErr=True) as output: self._saveImageAndReturnSize(".mov") self.assertEqual(output.lines(), ["*** DrawBot warning: export to '.mov' is deprecated, use '.mp4' instead. ***"])
def test_ScriptRunner_fromPath(self): out = StdOutCollector() path = os.path.join(testDataDir, "scriptRunnerTest.py") ScriptRunner(path=path, stdout=out, stderr=out) self.assertEqual(out.lines(), [path, "__main__", u'\xc5benr\xe5'])
def test_ScriptRunner_print_function(self): out = StdOutCollector() ScriptRunner("print 'hey!'", stdout=out, stderr=out) self.assertEqual(out.lines()[-1], "SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hey!')?")
def test_saveImage_mp4_ffmpegCodec(self): self.makeTestDrawing() with StdOutCollector(captureStdErr=True) as output: self._saveImageAndReturnSize(".mp4", ffmpegCodec="mpeg4") self.assertEqual(output.lines(), [])
def test_ScriptRunner_file(self): out = StdOutCollector() path = os.path.join(testDataDir, "scriptRunnerTest.py") # use an actual file, no not confuse coverage testing ScriptRunner("print(__file__)\nprint(__name__)", stdout=out, stderr=out, path=path) self.assertEqual(out.lines(), [path, "__main__"])
def test_saveImage_multipage_keywordArgument(self): self.makeTestDrawing() with TempFile(suffix=".png") as tmp: with StdOutCollector(captureStdErr=True) as output: drawBot.saveImage(tmp.path, multipage=False) self.assertEqual(output.lines(), [])