# HG changeset patch # User Sylvain Thénault # Date 1270031167 -7200 # Node ID 04c84959cd854c307628311ddb8a9fb3dc10df09 # Parent 0d01acee1f0ab7006adb4d9bace2bf49e39bf63c [testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager diff -r 0d01acee1f0a -r 04c84959cd85 devtools/testlib.py --- a/devtools/testlib.py Wed Mar 31 12:21:39 2010 +0200 +++ b/devtools/testlib.py Wed Mar 31 12:26:07 2010 +0200 @@ -12,6 +12,7 @@ import re from urllib import unquote from math import log +from contextlib import contextmanager import simplejson @@ -354,6 +355,15 @@ def entity(self, rql, args=None, eidkey=None, req=None): return self.execute(rql, args, eidkey, req=req).get_entity(0, 0) + @contextmanager + def temporary_appobjects(self, *appobjects): + self.vreg._loadedmods.setdefault(self.__module__, {}) + for obj in appobjects: + self.vreg.register(obj) + yield + for obj in appobjects: + self.vreg.unregister(obj) + # vregistry inspection utilities ########################################### def pviews(self, req, rset):