Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Chaplin committed May 20, 2010
1 parent 4a36f79 commit 2404914
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 190 deletions.
6 changes: 1 addition & 5 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@ $ python setup.py install

Testing
-------
The tests use py.test from pylib
http://codespeak.net/py/dist/

$ cd test
$ py.test
See test/README
66 changes: 33 additions & 33 deletions examples/cairo_snippets/snippets_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import sys

import cairo
if not cairo.HAS_PNG_FUNCTIONS:
raise SystemExit ('cairo was not compiled with PNG support')
if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')

from snippets import snip_list, snippet_normalize

Expand All @@ -17,36 +17,36 @@


def do_snippet (snippet):
if verbose_mode:
print 'processing %s' % snippet,

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context (surface)

cr.save()
try:
execfile ('snippets/%s.py' % snippet, globals(), locals())
except:
# exc_type, exc_value = sys.exc_info()[:2]
# print >> sys.stderr, exc_type, exc_value
raise
else:
cr.restore()
surface.write_to_png ('snippets/%s.png' % snippet)

if verbose_mode:
print
if verbose_mode:
print 'processing %s' % snippet,

surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context (surface)

cr.save()
try:
execfile ('snippets/%s.py' % snippet, globals(), locals())
except:
# exc_type, exc_value = sys.exc_info()[:2]
# print >> sys.stderr, exc_type, exc_value
raise
else:
cr.restore()
surface.write_to_png ('snippets/%s.png' % snippet)

if verbose_mode:
print

if __name__ == '__main__':
verbose_mode = True
if len(sys.argv) > 1 and sys.argv[1] == '-s':
verbose_mode = False
del sys.argv[1]

if len(sys.argv) > 1: # do specified snippets
snippet_list = sys.argv[1:]
else: # do all snippets
snippet_list = snip_list

for s in snippet_list:
do_snippet (s)
verbose_mode = True
if len(sys.argv) > 1 and sys.argv[1] == '-s':
verbose_mode = False
del sys.argv[1]

if len(sys.argv) > 1: # do specified snippets
snippet_list = sys.argv[1:]
else: # do all snippets
snippet_list = snip_list

for s in snippet_list:
do_snippet (s)
5 changes: 3 additions & 2 deletions test/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
EXTRA_DIST = \
examples_test.py
README \
api_test.py \
examples_test.py \
isurface_create_for_data1.py \
isurface_create_for_data2.py \
isurface_create_from_png.py \
Expand All @@ -8,4 +10,3 @@ EXTRA_DIST = \
pygame-test2.py \
surface_create_for_stream.py \
surface_write_to_png.py

13 changes: 13 additions & 0 deletions test/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pycairo tests
-------------

The main test files are the '*_test.py' files.
They use py.test from pylib.
http://codespeak.net/py/dist/

$ cd test
$ py.test

The other files are tests that were used to test/develop specific
functions. They usually require you run the test and then visually examine the
output.
84 changes: 60 additions & 24 deletions test/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,77 @@


def test_context():
pass
if cairo.HAS_IMAGE_SURFACE:
f, w, h = cairo.FORMAT_ARGB32, 100, 100
s = cairo.ImageSurface(f, w, h)
ctx = cairo.Context(s)
ctx.set_source_rgb(1.0, 1.0, 1.0)
ctx.set_operator(cairo.OPERATOR_SOURCE)
ctx.paint()


def test_matrix():
pass
m = cairo.Matrix()
m.rotate(10)
m.scale(1.5, 2.5)
m.translate(10, 20)


def test_path():
pass
# AttributeError: 'module' object has no attribute 'Path'
test.raises(AttributeError, "p = cairo.Path()")
# see examples/warpedtext.py


def test_pattern():
# TypeError: The Pattern type cannot be instantiated
test.raises(TypeError, "p = cairo.Pattern()")

r,g,b,a = 0.1, 0.2, 0.3, 0.4
p = cairo.SolidPattern(r,g,b,a)
assert p.get_rgba() == (r,g,b,a)

# SurfacePattern

# TypeError: The Gradient type cannot be instantiated
test.raises(TypeError, "p = cairo.Gradient()")

x0,y0,x1,y1 = 0.0, 0.0, 0.0, 1.0
p = cairo.LinearGradient(x0,y0,x1,y1)
assert p.get_linear_points() == (x0,y0,x1,y1)
p.add_color_stop_rgba(1, 0, 0, 0, 1)
p.add_color_stop_rgba(0, 1, 1, 1, 1)

def test_patterns():
pass
cx0, cy0, radius0, cx1, cy1, radius1 = 1.0, 1.0, 1.0, 2.0, 2.0, 1.0
p = cairo.RadialGradient(cx0, cy0, radius0, cx1, cy1, radius1)
assert p.get_radial_circles() == (cx0, cy0, radius0, cx1, cy1, radius1)
p.add_color_stop_rgba(0, 1, 1, 1, 1)
p.add_color_stop_rgba(1, 0, 0, 0, 1)


def test_surfaces():
# The Surface type cannot be instantiated
test.raises(TypeError, "s = cairo.Surface()")
def test_surface():
# TypeError: The Surface type cannot be instantiated
test.raises(TypeError, "s = cairo.Surface()")

if cairo.HAS_IMAGE_SURFACE:
f, w, h = cairo.FORMAT_ARGB32, 100, 100
s = cairo.ImageSurface(f, w, h)
assert s.get_format() == f
assert s.get_width() == w
assert s.get_height() == h
if cairo.HAS_IMAGE_SURFACE:
f, w, h = cairo.FORMAT_ARGB32, 100, 100
s = cairo.ImageSurface(f, w, h)
assert s.get_format() == f
assert s.get_width() == w
assert s.get_height() == h

if cairo.HAS_PDF_SURFACE:
f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
s = cairo.PDFSurface(f, w, h)
if cairo.HAS_PDF_SURFACE:
f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
s = cairo.PDFSurface(f, w, h)

if cairo.HAS_PS_SURFACE:
f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
s = cairo.PSSurface(f, w, h)
if cairo.HAS_PS_SURFACE:
f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
s = cairo.PSSurface(f, w, h)

if cairo.HAS_SVG_SURFACE:
f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
s = cairo.SVGSurface(f, w, h)
if cairo.HAS_SVG_SURFACE:
f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100
s = cairo.SVGSurface(f, w, h)


def test_text():
pass
pass
34 changes: 19 additions & 15 deletions test/examples_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@
import os.path
import subprocess

import cairo
#import py.test as test


def test_examples():
'''run non-gui example scripts and check they exit successfully.
'''
os.chdir(os.path.join(os.path.dirname(__file__), '..', 'examples'))
for f in (x for x in os.listdir('.') if x.endswith('.py')):
retcode = subprocess.call('python %s' % f, shell=True)
assert retcode == 0, 'Error: {0} retcode == {1}'.format(f, retcode)


def test_snippets_png():
'''run all snippets in png mode and check they exit successfully.
'''
os.chdir(os.path.join(os.path.dirname(__file__), '..', 'examples',
'cairo_snippets'))
retcode = subprocess.call('python snippets_png.py -s', shell=True)
def test_snippets():
'''Run all snippets in png,pdf,ps,svg mode and check they exit successfully.
This will create *.{pdf,png,ps,svg} output files in
examples/cairo_snippets/snippets/
'''
def doSnippets(name):
retcode = subprocess.call('python %s -s' % name, shell=True)
assert retcode == 0, 'Error: retcode == {0}'.format(retcode)

os.chdir(os.path.join(os.path.dirname(__file__), '..', 'examples',
'cairo_snippets'))
if cairo.HAS_PDF_SURFACE:
doSnippets('snippets_pdf.py')
if cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS:
doSnippets('snippets_png.py')
if cairo.HAS_PS_SURFACE:
doSnippets('snippets_ps.py')
if cairo.HAS_SVG_SURFACE:
doSnippets('snippets_svg.py')
25 changes: 15 additions & 10 deletions test/isurface_create_for_data1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
"""

import array
import tempfile

import cairo

dir_ = "/tmp/"
if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')

h, fileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
width, height = 255, 255
data = array.array('B', [0] * width * height * 4)

for y in range(height):
for x in range(width):
offset = (x + (y * width)) * 4
alpha = y
for x in range(width):
offset = (x + (y * width)) * 4
alpha = y

# cairo.FORMAT_ARGB32 uses pre-multiplied alpha
data[offset+0] = int(x * alpha/255.0) # B
data[offset+1] = int(y * alpha/255.0) # G
data[offset+2] = 0 # R
data[offset+3] = alpha # A
# cairo.FORMAT_ARGB32 uses pre-multiplied alpha
data[offset+0] = int(x * alpha/255.0) # B
data[offset+1] = int(y * alpha/255.0) # G
data[offset+2] = 0 # R
data[offset+3] = alpha # A

surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32,
width, height)
ctx = cairo.Context(surface)
surface.write_to_png(dir_ + 'for_data1.png')
surface.write_to_png(fileName)
print "see %s output file" % fileName
24 changes: 14 additions & 10 deletions test/isurface_create_for_data2.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
#!/usr/bin/env python
"""test cairo.ImageSurface.create_for_data() with a numpy array
"""
import tempfile

import cairo

import numpy

dir_ = "/tmp/"
if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS):
raise SystemExit ('cairo was not compiled with ImageSurface and PNG support')

h, fileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png')
width, height = 255, 255
data = numpy.ndarray (shape=(height,width,4), dtype=numpy.uint8)

for x in range(width):
for y in range(height):
alpha = y
for y in range(height):
alpha = y

# cairo.FORMAT_ARGB32 uses pre-multiplied alpha
data[y][x][0] = int(x * alpha/255.0)
data[y][x][1] = int(y * alpha/255.0)
data[y][x][2] = 0
data[y][x][3] = alpha
# cairo.FORMAT_ARGB32 uses pre-multiplied alpha
data[y][x][0] = int(x * alpha/255.0) # B
data[y][x][1] = int(y * alpha/255.0) # G
data[y][x][2] = 0 # R
data[y][x][3] = alpha # A

surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32,
width, height)
ctx = cairo.Context(surface)
surface.write_to_png(dir_ + 'for_data2.png')
surface.write_to_png(fileName)
print "see %s output file" % fileName
Loading

0 comments on commit 2404914

Please sign in to comment.