# -*- coding:utf-8;mode:python;mode:font-lock -*- import sys import os import tempfile import filecmp import spiffy from spiffy.component import * # # We're going to define a ComponentState class, which we'll # use in some tests. This depends on MappedAttributeMixIn # working, so we'll test that right away. # class ComponentState (spiffy.MappedAttributeMixIn): def __init__(self): self._title = None def foo (self): return "foo" def true1 (self): return True def true2 (self): return "True" def false1(self): return False def false2(self): return "False" def title(self): return self._title def set_title(self, value): self._title = value state = ComponentState() # Make sure attribute mapping works assert state["foo"] == "foo" # Shouldn't be able to set key backed by a method without a set method try: state["foo"] = "bar" except IndexError: pass else: raise AssertionError("Shouldn't be able to set foo") # But we should be able to if there is a set method state["title"] = "Super Spiffy Test" assert state["title"] == "Super Spiffy Test" # Shouldn't be able to delete key backed by a method try: del(state["foo"]) except IndexError: pass else: raise AssertionError("Shouldn't be able to delete foo") # # Run the test. # The output should match the saved output. # name = sys.argv[1] testdir = os.path.join(os.path.dirname(__file__), name) print "Running test %s..." % name, tmpfile, tmpfilename = tempfile.mkstemp(suffix=".html") os.close(tmpfile) tmpfile = file(tmpfilename, "w") try: try: spiffy.component.set_output(tmpfile) state["templates_dir"] = testdir spiffy = spiffy.IncludeComponent(state=state, parent=None) spiffy.start(name="test") spiffy.close() finally: tmpfile.close() if filecmp.cmp(tmpfilename, os.path.join(testdir, "out.html")): print "OK" else: print "failed:" tmpfile = file(tmpfilename) try: sys.stdout.write(tmpfile.read()) finally: tmpfile.close() finally: os.remove(tmpfilename)