def assert_canvas_equal(canvas, name, show_diff=True): test_file = os.path.join(failed_dir, "%s.svg" % name) reference_file = os.path.join(reference_dir, "%s.svg" % name) # Render multiple representations of the canvas for coverage ... html = io.BytesIO() toyplot.html.render(canvas, html) svg = io.BytesIO() toyplot.svg.render(canvas, svg) for module in [ "toyplot.pdf", "toyplot.png", "toyplot.reportlab.pdf", "toyplot.reportlab.png" ]: if module in sys.modules: stream = io.BytesIO() sys.modules[module].render(canvas, stream) # Get rid of any past failures ... if os.path.exists(test_file): os.remove(test_file) # If there's no stored SVG reference for this canvas, create one ... if not os.path.exists(reference_file): with open(reference_file, "wb") as stream: stream.write(svg.getvalue()) raise AssertionError( "Created new reference file %s ... you should verify its contents before re-running the test." % reference_file) # Compare the SVG representation of the canvas to the SVG reference ... svg_dom = xml.fromstring(svg.getvalue()) reference_dom = xml.parse(reference_file).getroot() svg_string = _xml_comparison_string(svg_dom) reference_string = _xml_comparison_string(reference_dom) if svg_string != reference_string: if not os.path.exists(failed_dir): os.mkdir(failed_dir) with open(test_file, "wb") as stream: stream.write(svg.getvalue()) if show_diff: reference = subprocess.Popen( ["xmldiff", test_file, reference_file], stdout=subprocess.PIPE) test = subprocess.Popen(["xmldiff", reference_file, test_file], stdout=subprocess.PIPE) message = "Test output %s doesn't match %s:\n\n*** Test -> Reference ***\n%s\n*** Reference -> Test ***\n%s\n" % ( test_file, reference_file, reference.communicate()[0].decode("utf-8"), test.communicate()[0].decode("utf-8"), ) else: message = "Test output %s doesn't match %s.\n" % (test_file, reference_file) raise AssertionError(message)
def assert_canvas_equal(canvas, name): test_file = os.path.join(failed_dir, "%s.svg" % name) reference_file = os.path.join(reference_dir, "%s.svg" % name) # Render multiple representations of the canvas for coverage ... html = io.BytesIO() toyplot.html.render(canvas, html) svg = io.BytesIO() toyplot.svg.render(canvas, svg) for module in ["toyplot.pdf", "toyplot.png", "toyplot.reportlab.pdf", "toyplot.reportlab.png"]: if module in sys.modules: buffer = io.BytesIO() sys.modules[module].render(canvas, buffer) # Get rid of any past failures ... if os.path.exists(test_file): os.remove(test_file) # If there's no stored SVG reference for this canvas, create one ... if not os.path.exists(reference_file): with open(reference_file, "wb") as file: file.write(svg.getvalue()) raise AssertionError( "Created new reference file %s ... you should verify its contents before re-running the test." % reference_file) # Compare the SVG representation of the canvas to the SVG reference ... svg_dom = xml.fromstring(svg.getvalue()) reference_dom = xml.parse(reference_file).getroot() svg_string = _xml_comparison_string(svg_dom) reference_string = _xml_comparison_string(reference_dom) if svg_string != reference_string: if not os.path.exists(failed_dir): os.mkdir(failed_dir) with open(test_file, "wb") as file: file.write(svg.getvalue()) reference = subprocess.Popen(["xmldiff", reference_file, test_file], stdout=subprocess.PIPE) test = subprocess.Popen(["xmldiff", test_file, reference_file], stdout=subprocess.PIPE) message = "Test output %s doesn't match %s:\n\n*** Reference ***\n%s\n*** Test ***\n%s\n" % ( test_file, reference_file, reference.communicate()[0], test.communicate()[0], ) raise AssertionError(message)
def assert_canvas_matches(canvas, name): # Render every representation of the canvas for coverage ... html = io.BytesIO() toyplot.html.render(canvas, html) svg = io.BytesIO() toyplot.svg.render(canvas, svg) for module in ["toyplot.pdf", "toyplot.png", "toyplot.reportlab.pdf", "toyplot.reportlab.png"]: if module in sys.modules: buffer = io.BytesIO() sys.modules[module].render(canvas, buffer) # Get rid of any past failures ... if os.path.exists("tests/diffs/%s.svg" % name): os.remove("tests/diffs/%s.svg" % name) if os.path.exists("tests/diffs/%s.reference.svg" % name): os.remove("tests/diffs/%s.reference.svg" % name) if os.path.exists("tests/failed/%s.svg" % name): os.remove("tests/failed/%s.svg" % name) # If there's no stored SVG reference for this canvas, create one ... if not os.path.exists("tests/reference/%s.svg" % name): with open("tests/reference/%s.svg" % name, "wb") as file: file.write(svg.getvalue()) raise AssertionError( "Created new reference file tests/reference/%s.svg ... you should verify its contents before re-running the test." % name) # Compare the SVG representation of the canvas to the SVG reference ... svg_dom = xml.fromstring(svg.getvalue()) reference_dom = xml.parse("tests/reference/%s.svg" % name).getroot() svg_string = xml_comparison_string(svg_dom) reference_string = xml_comparison_string(reference_dom) try: if svg_string != reference_string: raise Exception( "\n".join( list( difflib.context_diff( svg_string.split("\n"), reference_string.split("\n"), lineterm="", fromfile="test svg", tofile="reference svg")))) except Exception as e: if not os.path.exists("tests/diffs"): os.mkdir("tests/diffs") with open("tests/diffs/%s.svg" % name, "wb") as file: file.write(svg_string) with open("tests/diffs/%s.reference.svg" % name, "wb") as file: file.write(reference_string) if not os.path.exists("tests/failed"): os.mkdir("tests/failed") with open("tests/failed/%s.svg" % name, "wb") as file: file.write(svg.getvalue()) raise AssertionError( "Test output tests/failed/%s.svg doesn't match tests/reference/%s.svg:\n%s" % (name, name, e))
def assert_canvas_equal(canvas, name): test_file = os.path.join(failed_dir, "%s.svg" % name) reference_file = os.path.join(reference_dir, "%s.svg" % name) # Render multiple representations of the canvas for coverage ... html = io.BytesIO() toyplot.html.render(canvas, html) svg = io.BytesIO() toyplot.svg.render(canvas, svg) if hasattr(toyplot, "eps"): eps = io.BytesIO() toyplot.eps.render(canvas, eps) if hasattr(toyplot, "pdf"): pdf = io.BytesIO() toyplot.pdf.render(canvas, pdf) if hasattr(toyplot, "png"): png = io.BytesIO() toyplot.png.render(canvas, png) # Get rid of any past failures ... # if os.path.exists("tests/diffs/%s.svg" % name): # os.remove("tests/diffs/%s.svg" % name) # if os.path.exists("tests/diffs/%s.reference.svg" % name): # os.remove("tests/diffs/%s.reference.svg" % name) if os.path.exists(test_file): os.remove(test_file) # If there's no stored SVG reference for this canvas, create one ... if not os.path.exists(reference_file): with open(reference_file, "wb") as file: file.write(svg.getvalue()) raise AssertionError( "Created new reference file %s ... you should verify its contents before re-running the test." % reference_file) # Compare the SVG representation of the canvas to the SVG reference ... svg_dom = xml.fromstring(svg.getvalue()) reference_dom = xml.parse(reference_file).getroot() svg_string = _xml_comparison_string(svg_dom) reference_string = _xml_comparison_string(reference_dom) try: if svg_string != reference_string: raise Exception("\n".join( list( difflib.context_diff(svg_string.split("\n"), reference_string.split("\n"), lineterm="", fromfile="test svg", tofile="reference svg")))) except Exception as e: # if not os.path.exists("tests/diffs"): # os.mkdir("tests/diffs") # with open("tests/diffs/%s.svg" % name, "wb") as file: # file.write(svg_string) # with open("tests/diffs/%s.reference.svg" % name, "wb") as file: # file.write(reference_string) if not os.path.exists(failed_dir): os.mkdir(failed_dir) with open(test_file, "wb") as file: file.write(svg.getvalue()) raise AssertionError("Test output %s doesn't match %s:\n%s" % (test_file, reference_file, e))
def assert_canvas_equal(canvas, name, show_diff=True): test_file = os.path.join(failed_dir, "%s.svg" % name) reference_file = os.path.join(reference_dir, "%s.svg" % name) # Render multiple representations of the canvas for coverage. for module in ["toyplot.html", "toyplot.pdf", "toyplot.png", "toyplot.reportlab.pdf", "toyplot.reportlab.png"]: if module in sys.modules: stream = io.BytesIO() sys.modules[module].render(canvas, stream) # Render the canvas to svg for comparison against the reference. svg = io.BytesIO() toyplot.svg.render(canvas, svg) # Get rid of any past failures ... if os.path.exists(test_file): os.remove(test_file) # If there's no stored SVG reference for this canvas, create one. if not os.path.exists(reference_file): with open(reference_file, "wb") as stream: stream.write(svg.getvalue()) raise AssertionError( "Created new reference file %s ... you should verify its contents before re-running the test." % reference_file) # Compare the SVG representation of the canvas to the SVG reference. svg_dom = xml.fromstring(svg.getvalue()) reference_dom = xml.parse(reference_file).getroot() try: assert_dom_equal( svg_dom, reference_dom, exceptions={ "{http://www.w3.org/2000/svg}clipPath": { "id": {"ignore": True}, }, "{http://www.w3.org/2000/svg}g": { "clip-path": {"ignore": True}, "id": {"ignore": True}, "transform": {"type": "transform"}, "style": {"type": "style"}, }, "{http://www.w3.org/2000/svg}linearGradient": { "id": {"ignore": True}, }, "{http://www.w3.org/2000/svg}line": { "x1": {"type": "float"}, "x2": {"type": "float"}, "y1": {"type": "float"}, "y2": {"type": "float"}, "style": {"type": "style"}, }, "{http://www.w3.org/2000/svg}linearGradient": { "id": {"ignore": True}, "x1": {"type": "float"}, "x2": {"type": "float"}, "y1": {"type": "float"}, "y2": {"type": "float"}, }, "{http://www.w3.org/2000/svg}path": { "d": {"type": "path"}, }, "{http://www.w3.org/2000/svg}polygon": { "points": {"type": "points"}, }, "{http://www.w3.org/2000/svg}rect": { "x": {"type": "float"}, "y": {"type": "float"}, "width": {"type": "float"}, "height": {"type": "float"}, "style": {"type": "style"}, }, "{http://www.w3.org/2000/svg}stop": { "offset": {"type": "float"}, }, "{http://www.w3.org/2000/svg}svg": { "id": {"ignore": True}, }, "{http://www.w3.org/2000/svg}text": { "x": {"type": "float"}, "y": {"type": "float"}, }, } ) except AssertionError as e: if not os.path.exists(failed_dir): os.mkdir(failed_dir) with open(test_file, "wb") as stream: stream.write(svg.getvalue()) raise AssertionError("%s\nTest output\n\t%s\ndoesn't match\n\t%s.\n" % (e, test_file, reference_file))
def assert_canvas_equal(canvas, name, show_diff=True): test_file = os.path.join(failed_dir, "%s.svg" % name) reference_file = os.path.join(reference_dir, "%s.svg" % name) # Render multiple representations of the canvas for coverage. for module in [ "toyplot.html", "toyplot.pdf", "toyplot.png", "toyplot.reportlab.pdf", "toyplot.reportlab.png" ]: if module in sys.modules: stream = io.BytesIO() sys.modules[module].render(canvas, stream) # Render the canvas to svg for comparison against the reference. svg = io.BytesIO() toyplot.svg.render(canvas, svg) # Get rid of any past failures ... if os.path.exists(test_file): os.remove(test_file) # If there's no stored SVG reference for this canvas, create one. if not os.path.exists(reference_file): with open(reference_file, "wb") as stream: stream.write(svg.getvalue()) raise AssertionError( "Created new reference file %s ... you should verify its contents before re-running the test." % reference_file) # Compare the SVG representation of the canvas to the SVG reference. svg_dom = xml.fromstring(svg.getvalue()) reference_dom = xml.parse(reference_file).getroot() try: assert_dom_equal(svg_dom, reference_dom, exceptions={ "{http://www.w3.org/2000/svg}clipPath": { "id": { "ignore": True }, }, "{http://www.w3.org/2000/svg}g": { "clip-path": { "ignore": True }, "id": { "ignore": True }, "transform": { "type": "transform" }, "style": { "type": "style" }, }, "{http://www.w3.org/2000/svg}linearGradient": { "id": { "ignore": True }, }, "{http://www.w3.org/2000/svg}line": { "x1": { "type": "float" }, "x2": { "type": "float" }, "y1": { "type": "float" }, "y2": { "type": "float" }, "style": { "type": "style" }, }, "{http://www.w3.org/2000/svg}linearGradient": { "id": { "ignore": True }, "x1": { "type": "float" }, "x2": { "type": "float" }, "y1": { "type": "float" }, "y2": { "type": "float" }, }, "{http://www.w3.org/2000/svg}path": { "d": { "type": "path" }, }, "{http://www.w3.org/2000/svg}polygon": { "points": { "type": "points" }, }, "{http://www.w3.org/2000/svg}rect": { "x": { "type": "float" }, "y": { "type": "float" }, "width": { "type": "float" }, "height": { "type": "float" }, "style": { "type": "style" }, }, "{http://www.w3.org/2000/svg}stop": { "offset": { "type": "float" }, }, "{http://www.w3.org/2000/svg}svg": { "id": { "ignore": True }, }, "{http://www.w3.org/2000/svg}text": { "x": { "type": "float" }, "y": { "type": "float" }, }, }) except AssertionError as e: if not os.path.exists(failed_dir): os.mkdir(failed_dir) with open(test_file, "wb") as stream: stream.write(svg.getvalue()) raise AssertionError("%s\nTest output\n\t%s\ndoesn't match\n\t%s.\n" % (e, test_file, reference_file))