#!/usr/bin/env python3 """What a visitor must be. Run: python3 test_visitors.py Needs python3-svgelements and python3-shapely; no LaTeX.""" import math, sys, tempfile from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from bloom_sheet import read_visitor fails = 0 tmp = Path(tempfile.mkdtemp()) def svg(body): f = tmp / "v.svg" f.write_text(f'{body}') return str(f) def check(name, ok): global fails print(("ok " if ok else "FAIL"), name) fails += not ok def refused(name, body, reason): try: read_visitor(svg(body)) check(f"{name}: refused", False) except SystemExit as e: check(f"{name}: refused for {reason}", reason in str(e)) DISC = '' refused("apart", '', "2 separate") refused("apart lines", '', "2 separate") refused("gradient", '', "gradient") refused("gradient stroke", '', "gradient") refused("text", DISC + 'hi', "text") refused("image", DISC + '', "image") refused("nothing", '', "fill or a stroke") m = read_visitor(svg(DISC)) check("a large bare fill is outlined, not solid", len(m) == 1 and m[0]["outlined"] and m[0]["colour"] == "neonorange") m = read_visitor(svg('')) check("a stroke is an open line, no wash", len(m) == 1 and "pts" in m[0] and not m[0]["closed"] and m[0]["wash"] is None) check("the line keeps its colour", m[0]["colour"] == "neonsky") check("the stroke width scales with the drawing", 0 < m[0]["width"] < 0.1) m = read_visitor(svg('')) check("a fill under a stroke is a wash", m[0]["closed"] and m[0]["wash"] == "neonviolet" and m[0]["colour"] == "neonorange") m = read_visitor(svg('')) check("an all-black icon is seen", m[0]["colour"] == "neonwhite") m = read_visitor(svg(DISC + '')) check("black on colour is a cutout", m[1]["colour"] == "ground" and not m[1]["outlined"]) m = read_visitor(svg(DISC + '')) check("a small bare fill stays solid", not m[1]["outlined"]) m = read_visitor(svg('')) check("a large cutout stays a cutout", m[0]["colour"] == "ground" and not m[0]["outlined"]) m = read_visitor(svg(DISC + '')) check("paint order is kept", [x["colour"] for x in m] == ["neonorange", "ground"]) m = read_visitor(svg('')) check("eyes inside an outline count as touching", len(m) == 3) m = read_visitor(svg('')) check("a hole stays a ring of the solid", len(m) == 1 and len(m[0]["rings"]) == 2) m = read_visitor(svg('')) pts = [p for ring in m[0]["rings"] for p in ring] check("any drawing lands in the unit disc", abs(max(math.hypot(*p) for p in pts) - 1) < 1e-3) top = max(pts, key=lambda p: p[1]) check("y points up", top[1] > 0 and m[0]["rings"][0][0][1] > 0) sys.exit(1 if fails else 0)