Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tcltest and scope

27 views
Skip to first unread message

gamename

unread,
Aug 10, 2006, 4:52:44 PM8/10/06
to
Hi,

I frequently have a situation where I want to test a proc that calls
another proc of mine. How do you "stub out" the procs inside the proc
being tested?

For example:

proc bar{} {
# ... rc gets set somewhere
return $rc
}

proc foo{} {
# lotsa code
set var [bar]
# ...use "var" for further processing...
}

Using tcltest, I want to test "foo". But, I want "bar" to return a
specific value so I can verify different logic paths. The most obvious
way would be something like this:

::tcltest::test foo.1 {
Testing foo
} -setup {
proc bar{} {return 99}
} -body {
foo
} -cleanup {
} -result 99

But, doing it this way will destroy the definition for "bar" and make
it tough to use it in subsequent tests. Put another way, how can I use
the "bar" proc (or the "foo" proc for that matter) in various places,
but not step all over my own code?

How is this problem normally handled?

TIA,
-T

Gerald W. Lester

unread,
Aug 10, 2006, 7:17:23 PM8/10/06
to

Try:

::tcltest::test foo.1 {
Testing foo
} -setup {

rename bar _bar


proc bar {} {return 99}
} -body {
foo
} -cleanup {

rename _bar bar
} -result 99


--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

gamename

unread,
Aug 11, 2006, 12:07:56 PM8/11/06
to
Thanks Gerald! So, doing renames is the only way?

Gerald W. Lester

unread,
Aug 11, 2006, 1:48:34 PM8/11/06
to
gamename wrote:
> Thanks Gerald! So, doing renames is the only way?
>

No, but it is most likely the easiest.

Other ways: load your code to be tested into a safe interp with aliases
defined the way you want them.

Instead of using rename, look and remember the procedure definition then
redefine it at the end.

Do you have a problem with rename?

gamename

unread,
Aug 13, 2006, 4:49:44 PM8/13/06
to
> Do you have a problem with rename?

It just seems inelegant. But if that's the preferred method, then I'll
do that. :)

Thanks Gerald.

-T

0 new messages