api-coverage.texi 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2010, 2013 Free Software Foundation, Inc.
  4. @c See the file guile.texi for copying conditions.
  5. @node Code Coverage
  6. @section Code Coverage Reports
  7. @cindex code coverage
  8. @cindex coverage
  9. When writing a test suite for a program or library, it is desirable to know what
  10. part of the code is @dfn{covered} by the test suite. The @code{(system vm
  11. coverage)} module provides tools to gather code coverage data and to present
  12. them, as detailed below.
  13. @deffn {Scheme Procedure} with-code-coverage thunk
  14. Run @var{thunk}, a zero-argument procedure, while instrumenting Guile's
  15. virtual machine to collect code coverage data. Return code coverage
  16. data and the values returned by @var{thunk}.
  17. @end deffn
  18. @deffn {Scheme Procedure} coverage-data? obj
  19. Return @code{#t} if @var{obj} is a @dfn{coverage data} object as returned by
  20. @code{with-code-coverage}.
  21. @end deffn
  22. @deffn {Scheme Procedure} coverage-data->lcov data port #:key modules
  23. Traverse code coverage information @var{data}, as obtained with
  24. @code{with-code-coverage}, and write coverage information to port in the
  25. @code{.info} format used by @url{http://ltp.sourceforge.net/coverage/lcov.php,
  26. LCOV}. The report will include all of @var{modules} (or, by default, all the
  27. currently loaded modules) even if their code was not executed.
  28. The generated data can be fed to LCOV's @command{genhtml} command to produce an
  29. HTML report, which aids coverage data visualization.
  30. @end deffn
  31. Here's an example use:
  32. @example
  33. (use-modules (system vm coverage)
  34. (system vm vm))
  35. (call-with-values (lambda ()
  36. (with-code-coverage
  37. (lambda ()
  38. (do-something-tricky))))
  39. (lambda (data result)
  40. (let ((port (open-output-file "lcov.info")))
  41. (coverage-data->lcov data port)
  42. (close port))))
  43. @end example
  44. In addition, the module provides low-level procedures that would make it
  45. possible to write other user interfaces to the coverage data.
  46. @deffn {Scheme Procedures} instrumented-source-files data
  47. Return the list of ``instrumented'' source files, i.e., source files whose
  48. code was loaded at the time @var{data} was collected.
  49. @end deffn
  50. @deffn {Scheme Procedures} line-execution-counts data file
  51. Return a list of line number/execution count pairs for @var{file}, or
  52. @code{#f} if @var{file} is not among the files covered by @var{data}. This
  53. includes lines with zero count.
  54. @end deffn
  55. @deffn {Scheme Procedures} instrumented/executed-lines data file
  56. Return the number of instrumented and the number of executed source lines
  57. in @var{file} according to @var{data}.
  58. @end deffn
  59. @deffn {Scheme Procedures} procedure-execution-count data proc
  60. Return the number of times @var{proc}'s code was executed, according to
  61. @var{data}, or @code{#f} if @var{proc} was not executed. When @var{proc}
  62. is a closure, the number of times its code was executed is returned, not
  63. the number of times this code associated with this particular closure was
  64. executed.
  65. @end deffn