Posted to tcl by apn at Tue Oct 08 06:28:49 GMT 2024view raw

  1. # Extremely simplistic to illustrate. Does not deal with namespaces, will not handle
  2. # mocking commands used within the redefinition (lindex, puts etc.)
  3.  
  4. proc mock {cmd} {
  5. rename $cmd ${cmd}_original
  6. proc $cmd args {
  7. set me [lindex [info level 0] 0]
  8. incr ::counts($me)
  9. puts Trace:[info level 0]
  10. tailcall {*}${me}_original {*}$args
  11. }
  12. }
  13.  
  14. % mock format
  15. % format %s%d foo 42
  16. Trace:format %s%d foo 42
  17. foo42
  18. % format %s%d foo 43
  19. Trace:format %s%d foo 43
  20. foo43
  21. % set counts(format)
  22. 2
  23. % format %s%d foo 44
  24. Trace:format %s%d foo 44
  25. foo44
  26. % set counts(format)
  27. 3
  28.  
  29.