Add tests. Fix bugs found by tests.

Add tests for callOnEnter(), callOnExit(), callOnResult(), and
callOnException().