2009-04-16 17:22:38 +00:00
|
|
|
#!/usr/bin/env python
|
2012-10-16 14:30:31 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-04-16 17:22:38 +00:00
|
|
|
|
|
|
|
from nose.plugins.errorclass import ErrorClass, ErrorClassPlugin
|
2015-01-27 04:15:14 +00:00
|
|
|
from nose.tools import assert_almost_equal
|
2009-04-16 17:22:38 +00:00
|
|
|
|
2015-02-02 18:31:16 +00:00
|
|
|
import os, sys, traceback
|
2012-10-16 15:16:45 +00:00
|
|
|
import mapnik
|
2009-04-12 22:38:46 +00:00
|
|
|
|
2015-01-27 05:58:27 +00:00
|
|
|
HERE = os.path.dirname(__file__)
|
|
|
|
|
2009-04-12 22:38:46 +00:00
|
|
|
def execution_path(filename):
|
|
|
|
return os.path.join(os.path.dirname(sys._getframe(1).f_code.co_filename), filename)
|
2009-04-16 17:22:38 +00:00
|
|
|
|
|
|
|
class Todo(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TodoPlugin(ErrorClassPlugin):
|
|
|
|
name = "todo"
|
|
|
|
|
|
|
|
todo = ErrorClass(Todo, label='TODO', isfailure=False)
|
2010-03-16 17:37:25 +00:00
|
|
|
|
2010-03-18 20:05:08 +00:00
|
|
|
def contains_word(word, bytestring_):
|
|
|
|
"""
|
|
|
|
Checks that a bytestring contains a given word. len(bytestring) should be
|
|
|
|
a multiple of len(word).
|
|
|
|
|
|
|
|
>>> contains_word("abcd", "abcd"*5)
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> contains_word("ab", "ba"*5)
|
|
|
|
False
|
|
|
|
|
|
|
|
>>> contains_word("ab", "ab"*5+"a")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AssertionError: len(bytestring_) not multiple of len(word)
|
|
|
|
"""
|
|
|
|
n = len(word)
|
|
|
|
assert len(bytestring_)%n == 0, "len(bytestring_) not multiple of len(word)"
|
|
|
|
chunks = [bytestring_[i:i+n] for i in xrange(0, len(bytestring_), n)]
|
|
|
|
return word in chunks
|
2012-10-04 23:39:00 +00:00
|
|
|
|
|
|
|
def pixel2channels(pixel):
|
|
|
|
alpha = (pixel >> 24) & 0xff
|
|
|
|
red = pixel & 0xff
|
|
|
|
green = (pixel >> 8) & 0xff
|
|
|
|
blue = (pixel >> 16) & 0xff
|
|
|
|
return red,green,blue,alpha
|
|
|
|
|
|
|
|
def pixel2rgba(pixel):
|
|
|
|
return 'rgba(%s,%s,%s,%s)' % pixel2channels(pixel)
|
|
|
|
|
|
|
|
def get_unique_colors(im):
|
|
|
|
pixels = []
|
|
|
|
for x in range(im.width()):
|
|
|
|
for y in range(im.height()):
|
|
|
|
pixel = im.get_pixel(x,y)
|
|
|
|
if pixel not in pixels:
|
|
|
|
pixels.append(pixel)
|
|
|
|
pixels = sorted(pixels)
|
|
|
|
return map(pixel2rgba,pixels)
|
2012-10-16 14:30:31 +00:00
|
|
|
|
2013-01-18 21:13:01 +00:00
|
|
|
def run_all(iterable):
|
2012-10-16 14:30:31 +00:00
|
|
|
failed = 0
|
2013-01-18 21:13:01 +00:00
|
|
|
for test in iterable:
|
|
|
|
try:
|
|
|
|
test()
|
|
|
|
sys.stderr.write("\x1b[32m✓ \x1b[m" + test.__name__ + "\x1b[m\n")
|
|
|
|
except:
|
|
|
|
exc_type, exc_value, exc_tb = sys.exc_info()
|
|
|
|
failed += 1
|
|
|
|
sys.stderr.write("\x1b[31m✘ \x1b[m" + test.__name__ + "\x1b[m\n")
|
|
|
|
for mline in traceback.format_exception_only(exc_type, exc_value):
|
|
|
|
for line in mline.rstrip().split("\n"):
|
|
|
|
sys.stderr.write(" \x1b[31m" + line + "\x1b[m\n")
|
|
|
|
sys.stderr.write(" Traceback:\n")
|
|
|
|
for mline in traceback.format_tb(exc_tb):
|
|
|
|
for line in mline.rstrip().split("\n"):
|
2013-06-02 20:41:14 +00:00
|
|
|
if not 'utilities.py' in line and not 'trivial.py' in line and not line.strip() == 'test()':
|
|
|
|
sys.stderr.write(" " + line + "\n")
|
2013-01-18 21:13:01 +00:00
|
|
|
sys.stderr.flush()
|
2012-10-16 14:30:31 +00:00
|
|
|
return failed
|
2012-10-16 15:16:45 +00:00
|
|
|
|
|
|
|
def side_by_side_image(left_im, right_im):
|
|
|
|
width = left_im.width() + 1 + right_im.width()
|
|
|
|
height = max(left_im.height(), right_im.height())
|
|
|
|
im = mapnik.Image(width, height)
|
2015-01-27 05:35:35 +00:00
|
|
|
im.composite(left_im,mapnik.CompositeOp.src_over,1.0,0,0)
|
2015-01-27 05:58:27 +00:00
|
|
|
if width > 80:
|
|
|
|
im.composite(mapnik.Image.open(HERE+'/images/expected.png'),mapnik.CompositeOp.difference,1.0,0,0)
|
2015-01-27 05:35:35 +00:00
|
|
|
im.composite(right_im,mapnik.CompositeOp.src_over,1.0,left_im.width() + 1, 0)
|
2015-01-27 05:58:27 +00:00
|
|
|
if width > 80:
|
|
|
|
im.composite(mapnik.Image.open(HERE+'/images/actual.png'),mapnik.CompositeOp.difference,1.0,left_im.width() + 1, 0)
|
2012-10-16 15:16:45 +00:00
|
|
|
return im
|
2015-01-27 04:15:14 +00:00
|
|
|
|
|
|
|
def assert_box2d_almost_equal(a, b, msg=None):
|
|
|
|
msg = msg or ("%r != %r" % (a, b))
|
|
|
|
assert_almost_equal(a.minx, b.minx, msg=msg)
|
|
|
|
assert_almost_equal(a.maxx, b.maxx, msg=msg)
|
|
|
|
assert_almost_equal(a.miny, b.miny, msg=msg)
|
|
|
|
assert_almost_equal(a.maxy, b.maxy, msg=msg)
|