Title: | Contextualizing Tests |
---|---|
Description: | Dissects a package environment or 'covr' coverage object in order to cross reference tested code with the lines that are evaluated, as well as linking those evaluated lines to the documentation that they are described within. Connecting these three pieces of information provides a mechanism of linking tests to documented behaviors. |
Authors: | Doug Kelkhoff [cre, aut] , Szymon Maksymiuk [aut] , Andrew McNeil [aut], F. Hoffmann-La Roche AG [cph, fnd] |
Maintainer: | Doug Kelkhoff <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.1 |
Built: | 2024-12-07 05:25:01 UTC |
Source: | https://github.com/Genentech/covtracer |
Create an S3 list of srcref objects
as_list_of_srcref(x) ## S3 method for class 'environment' as_list_of_srcref(x) ## S3 method for class 'list' as_list_of_srcref(x)
as_list_of_srcref(x) ## S3 method for class 'environment' as_list_of_srcref(x) ## S3 method for class 'list' as_list_of_srcref(x)
x |
A list or single srcref to coerce to a |
A list_of_srcref
class object
Wrap object in test description derivation data
Adds "testthat" style
as_test_desc(x, type = "call") as_testthat_desc(x)
as_test_desc(x, type = "call") as_testthat_desc(x)
x |
A test description string to bind style data to |
type |
A type class to attribute to the test description. Defaults to
|
A test_description
subclass object with additional
style
attribute indicating how the test description was derived.
Coerce a list_of_srcref object to a data.frame
## S3 method for class 'list_of_srcref' as.data.frame( x, ..., use.names = TRUE, expand.srcref = FALSE, row.names = NULL )
## S3 method for class 'list_of_srcref' as.data.frame( x, ..., use.names = TRUE, expand.srcref = FALSE, row.names = NULL )
x |
A |
... |
Additional arguments unused |
use.names |
A |
expand.srcref |
A |
row.names |
|
A data.frame
with one record per srcref
and variables:
Names of the srcref
objects, passed using the names of
x
if use.names = TRUE
srcref
objects if expand.srcrefs = FALSE
The
srcref
file location if it can be determined. If an absolute path
can't be found, only the base file name provided in the srcref
object and
the numeric components of the srcref
objects if expand.srcrefs
= TRUE
pkg <- system.file("examplepkg", package = "covtracer") install.packages( pkg, type = "source", repos = NULL, quiet = TRUE, INSTALL_opts = "--with-keep.source" ) as.data.frame(pkg_srcrefs("examplepkg"))
pkg <- system.file("examplepkg", package = "covtracer") install.packages( pkg, type = "source", repos = NULL, quiet = TRUE, INSTALL_opts = "--with-keep.source" ) as.data.frame(pkg_srcrefs("examplepkg"))
devtools::as.package
Functionally identical to devtools
' as.package
, but without interactive
options for package creation.
as.package(x)
as.package(x)
x |
A package object to coerce |
A package
object
Code inspired by devtools
load_pkg_description
with very minor
edits to further reduce devtools
dependencies.
Check whether the coverage object has expected fields produced when coverage
was captured with option(covr.record_tests = TRUE)
, throwing an error
if it was not.
coverage_check_has_recorded_tests(coverage, warn = TRUE)
coverage_check_has_recorded_tests(coverage, warn = TRUE)
coverage |
a |
warn |
Whether to warn when it is uncertain whether the tests were recorded. It may be uncertain if tests were recorded if there are no tested R code traces. |
Used for side-effects of emitting an error when a coverage object does not contain recorded traces, or a warning when a coverage object appears to have no tests.
Other coverage_tests:
coverage_get_tests()
,
coverage_has_recorded_tests()
Assumes the coverage object was produced while
option(covr.record_tests = TRUE)
.
coverage_get_tests(coverage)
coverage_get_tests(coverage)
coverage |
a |
A list
of tests evaluated when using covr
Other coverage_tests:
coverage_check_has_recorded_tests()
,
coverage_has_recorded_tests()
Test whether the coverage object has expected fields produced when coverage
was captured with option(covr.record_tests = TRUE)
.
coverage_has_recorded_tests(coverage)
coverage_has_recorded_tests(coverage)
coverage |
a |
A logical
value, indicating whether the coverage object has
recorded tests, or NA
when it does not appear to have traced any test
code.
Other coverage_tests:
coverage_check_has_recorded_tests()
,
coverage_get_tests()
Convert an expression, call or symbol to a single-line string
expr_str(ref)
expr_str(ref)
ref |
a |
The given expression, formatted as a string with prefixes for symbols and generics.
srcrefs
over an iterable object, Filtering non-srcref resultsMap srcrefs
over an iterable object, Filtering non-srcref results
flat_map_srcrefs(xs, ns = NULL, breadcrumbs = character())
flat_map_srcrefs(xs, ns = NULL, breadcrumbs = character())
xs |
Any iterable object |
ns |
A |
breadcrumbs |
Recursive methods are expected to propegate a vector of "breadcrumbs" (a character vector of namespace names encountered while traversing the namespace used as a memory of what we've seen already), which is used for short-circuiting recursive environment traversal. |
A list
of srcref
s
Format list_of_srcref as character
## S3 method for class 'list_of_srcref' format(x, ..., full.names = FALSE, full.num = FALSE)
## S3 method for class 'list_of_srcref' format(x, ..., full.names = FALSE, full.num = FALSE)
x |
A |
... |
Additional arguments unused |
full.names |
A |
full.num |
A |
A character
vector of formatted strings
Get namespace exports, filtering methods tables and definitions
get_namespace_object_names(ns)
get_namespace_object_names(ns)
ns |
A namespace object |
The names of exported objects, filtering internal method tables and metadata.
Get the full path to the srcref file
getSrcFilepath(x)
getSrcFilepath(x)
x |
A |
A character
vector of source file paths.
srcref
objectTest whether an object is a srcref
object
is_srcref(x)
is_srcref(x)
x |
Any object |
A logical
indicating whether object is a srcref
References to source code are defined by the source code line and column span of the relevant source code. This function takes data frames containing that information to pair source code in one data frame to source code from another. In this case, source code from the left hand data frame is paired if it is entirely contained within a record of source code in the right hand data frame.
join_on_containing_srcrefs(x, y, by = c(srcref = "srcref"))
join_on_containing_srcrefs(x, y, by = c(srcref = "srcref"))
x |
A |
y |
A |
by |
A named |
A data.frame
of x
joined on y
by spanning srcref
Provided two lists of srcref
objects, find the first srcrefs
in r
that
entirely encapsulate each respective srcref
in l
, returning a list of
indices of srcref
s in r
for each srcref
in l
.
match_containing_srcrefs(l, r)
match_containing_srcrefs(l, r)
l |
A |
r |
A |
A integer
vector of the first index in r
that fully encapsulate
the respective element in l
Build an empty covr-style test trace mapping
new_empty_test_trace_tally()
new_empty_test_trace_tally()
An empty test-trace matrix, as provided by covr
For most objects, this will be identical to the namespace name provided, but reexports will retain their originating package's namespace name. This helper function helps to expose this name to determine which exports are reexports.
obj_namespace_name(x, ns)
obj_namespace_name(x, ns)
x |
A value to find within namespace |
ns |
A package namespace |
A character
string representing a namespace or similar
Test whether the package object collection contains srcref attributes.
package_check_has_keep_source(env)
package_check_has_keep_source(env)
env |
A package namespace environment or iterable collection of package objects |
Used for side effect of throwing an error when a package was not
installed with srcref
s.
Extract all the srcref objects of objects within a package namespace
pkg_srcrefs(x) ## S3 method for class 'environment' pkg_srcrefs(x) ## S3 method for class 'character' pkg_srcrefs(x) ## S3 method for class 'coverage' pkg_srcrefs(x)
pkg_srcrefs(x) ## S3 method for class 'environment' pkg_srcrefs(x) ## S3 method for class 'character' pkg_srcrefs(x) ## S3 method for class 'coverage' pkg_srcrefs(x)
x |
A |
A list_of_srcref
as.data.frame.list_of_srcref
Other srcrefs:
test_srcrefs()
,
trace_srcrefs()
pkg <- system.file("examplepkg", package = "covtracer") install.packages( pkg, type = "source", repos = NULL, quiet = TRUE, INSTALL_opts = "--with-keep.source" ) pkg_srcrefs("examplepkg")
pkg <- system.file("examplepkg", package = "covtracer") install.packages( pkg, type = "source", repos = NULL, quiet = TRUE, INSTALL_opts = "--with-keep.source" ) pkg_srcrefs("examplepkg")
Create a data.frame of package srcref objects
pkg_srcrefs_df(x)
pkg_srcrefs_df(x)
x |
A |
A data.frame
with a record for each source code block with
variables:
A character
Rd alias for the package object
The srcref
of the associated package source code
srcrefs test_trace_mapping
Other srcrefs_df:
test_srcrefs_df()
,
trace_srcrefs_df()
pkg <- system.file("examplepkg", package = "covtracer") install.packages( pkg, type = "source", repos = NULL, quiet = TRUE, INSTALL_opts = "--with-keep.source" ) pkg_srcrefs_df("examplepkg")
pkg <- system.file("examplepkg", package = "covtracer") install.packages( pkg, type = "source", repos = NULL, quiet = TRUE, INSTALL_opts = "--with-keep.source" ) pkg_srcrefs_df("examplepkg")
Provides Rd index info with a few additional columns of information about each exported object. Returns one record per documented object, even if multiple objects alias to the same documentation file.
Rd_df(x)
Rd_df(x)
x |
A package object to coerce |
A data.frame
of documented object information with variables:
A numeric
index of documentation files associated with
documentation objects
A character
filename of the Rd file in the "man" directory
A character
file path of the Rd file in the "man"
directory
character
object names which are aliases for the
documentation in filepath
A logical
indicator of whether the aliased object
is exported from the package namespace
A character
representing the Rd docType field.
package_source_dir <- system.file("examplepkg", package = "covtracer") Rd_df(package_source_dir)
package_source_dir <- system.file("examplepkg", package = "covtracer") Rd_df(package_source_dir)
Parse the expression associated with a srcref
srcref_expr(ref)
srcref_expr(ref)
ref |
a |
A parsed srcref
object
Convert a srcref into a string
srcref_str(ref)
srcref_str(ref)
ref |
a |
A string representing the srcref
srcref
sThis function takes a code collection and returns a list
of related
srcref
objects with list
names that associate the srcref
with a name or
alias that could be used to find documentation. Code collections include
structures such as package namespaces, environments, function definitions,
methods tables or class generators - any object which enapsulates a single or
set of srcref
objects.
srcrefs(x, ...) ## Default S3 method: srcrefs(x, ..., srcref_names = NULL, breadcrumbs = character()) ## S3 method for class 'list' srcrefs(x, ..., srcref_names = NULL, breadcrumbs = character()) ## S3 method for class 'namespace' srcrefs(x, ..., breadcrumbs = character()) ## S3 method for class 'environment' srcrefs(x, ..., breadcrumbs = character()) ## S3 method for class 'R6ClassGenerator' srcrefs(x, ..., srcref_names = NULL, breadcrumbs = character()) ## S3 method for class 'standardGeneric' srcrefs(x, ..., srcref_names = NULL) ## S3 method for class 'nonstandardGenericFunction' srcrefs(x, ..., srcref_names = NULL) ## S3 method for class 'MethodDefinition' srcrefs(x, ..., srcref_names = NULL)
srcrefs(x, ...) ## Default S3 method: srcrefs(x, ..., srcref_names = NULL, breadcrumbs = character()) ## S3 method for class 'list' srcrefs(x, ..., srcref_names = NULL, breadcrumbs = character()) ## S3 method for class 'namespace' srcrefs(x, ..., breadcrumbs = character()) ## S3 method for class 'environment' srcrefs(x, ..., breadcrumbs = character()) ## S3 method for class 'R6ClassGenerator' srcrefs(x, ..., srcref_names = NULL, breadcrumbs = character()) ## S3 method for class 'standardGeneric' srcrefs(x, ..., srcref_names = NULL) ## S3 method for class 'nonstandardGenericFunction' srcrefs(x, ..., srcref_names = NULL) ## S3 method for class 'MethodDefinition' srcrefs(x, ..., srcref_names = NULL)
x |
An object to source srcrefs from |
... |
Additional arguments passed to methods |
srcref_names |
An optional field used to supercede any discovered object names when choosing which names to provide in the returned list. |
breadcrumbs |
Recursive methods are expected to propegate a vector of "breadcrumbs" (a character vector of namespace names encountered while traversing the namespace used as a memory of what we've seen already), which is used for short-circuiting recursive environment traversal. |
For most objects, this is a one-to-one mapping of exported object names to
their srcref
, just like you would get using getNamespace()
. However, for
classes and methods, this can be a one-to-many mapping of related
documentation to the multiple srcref
s that are described there. This is the
case for S3 generics, S4 objects and R6 objects.
Objects without any related srcref
s, such as any datasets or objects
created at package build time will be omitted from the results.
A list
of srcref
objects. Often, has a length of 1, but can be
larger for things like environments, namespaces or generic methods. The
names of the list reflect the name of the Rd name or alias that could be
used to find information related to each srcref
. Elements of the list
will have attribute "namespace"
denoting the source environment namespace
if one can be determined for the srcref object.
# examples use `with` to execute within namespace as function isn't exported ns <- getNamespace("covtracer") # load and extract srcrefs for a package with(ns, srcrefs(getNamespace("covtracer"))) # extract srcrefs for functions with(ns, srcrefs(srcrefs))
# examples use `with` to execute within namespace as function isn't exported ns <- getNamespace("covtracer") # load and extract srcrefs for a package with(ns, srcrefs(getNamespace("covtracer"))) # extract srcrefs for functions with(ns, srcrefs(srcrefs))
In the general case, a simple indicator of the source file and line number is used as a test description. There are some special cases where more descriptive information can be extracted:
test_description(x)
test_description(x)
x |
a unit test call stack or expression. |
testthat
If the test used test_that
,
then the description (desc
parameter) is extracted and evaluated if
need be to produce a descriptive string. Nested calls to
test_that
currently return the outermost test
description, although this behavior is subject to change.
A string that describes the test. If possible, this will be a written description of the test, but will fall back to the test call as a string in cases where no written description can be determined.
test_that
callParse the test description from a test_that
call
test_description_test_that(x, ...)
test_description_test_that(x, ...)
x |
A test_that call object |
... |
Additional arguments unused |
A character
description, parsed from a test_that::test_that
call
describe
callParse the test description from a describe
call
test_description_test_that_describe(x, ...)
test_description_test_that_describe(x, ...)
x |
A |
... |
Additional arguments unused |
A character
description, parsed from a test_that::describe
call
it
callParse the test description from a it
call
test_description_test_that_describe_it(x, ...)
test_description_test_that_describe_it(x, ...)
x |
A |
... |
Additional arguments unused |
A character
description, parsed from a test_that::it
call
Extract test srcref objects
test_srcrefs(x) ## S3 method for class 'coverage' test_srcrefs(x)
test_srcrefs(x) ## S3 method for class 'coverage' test_srcrefs(x)
x |
A |
A list_of_srcref
as.data.frame.list_of_srcref
Other srcrefs:
pkg_srcrefs()
,
trace_srcrefs()
options(covr.record_tests = TRUE) pkg_path <- system.file("examplepkg", package = "covtracer") cov <- covr::package_coverage(pkg_path) test_srcrefs(cov)
options(covr.record_tests = TRUE) pkg_path <- system.file("examplepkg", package = "covtracer") cov <- covr::package_coverage(pkg_path) test_srcrefs(cov)
Extract unit test srcref
s from a
coverage
object. A test name will be
derived from the test source code, preferrably from a written annotation, but
otherwise falling back to using a code snippet. srcrefs
are unique for
each expression executed within a testing suite.
test_srcrefs_df(x)
test_srcrefs_df(x)
x |
A |
A data.frame
of test srcrefs
extracted from a
coverage
object. Contains one record for each srcref
with
variables:
A character
test description. For testthat
tests, the
desc
parameter will be used, otherwise a snippet of code will be
used for the test name
A srcref
object describing the location of the test
A character
indicating the structure of the test. One of
"testthat"
, "call"
or NULL
srcrefs test_trace_mapping
Other srcrefs_df:
pkg_srcrefs_df()
,
trace_srcrefs_df()
options(covr.record_tests = TRUE) pkg_path <- system.file("examplepkg", package = "covtracer") cov <- covr::package_coverage(pkg_path) test_srcrefs_df(cov)
options(covr.record_tests = TRUE) pkg_path <- system.file("examplepkg", package = "covtracer") cov <- covr::package_coverage(pkg_path) test_srcrefs_df(cov)
Intercept unit test coverage reports and process results to link evaluated functions to the unit tests which trigger their evaluation. In doing so, we can then link the associated function documentation of each object to the tests that triggered their evaluation as a way of reusing existing documentation to generate specifications.
test_trace_df(x, ...) ## S3 method for class 'coverage' test_trace_df( x, ..., pkg = as.package(attr(x, "package")$path), aggregate_by = sum )
test_trace_df(x, ...) ## S3 method for class 'coverage' test_trace_df( x, ..., pkg = as.package(attr(x, "package")$path), aggregate_by = sum )
x |
A package object, name, source code path or coverage result to use
as the bases of tracing tests. Coverage results must have been produced
using |
... |
Additional arguments unused |
pkg |
A |
aggregate_by |
|
A data.frame
of tests and corresponding traces
Extract a matrix used to relate test code to the traces that each test evaluates.
test_trace_mapping(x)
test_trace_mapping(x)
x |
A |
A data.frame
with one record for each line of code executed, with
variables:
The index of the test that was executed, reflecting the order in which tests are executed
The call stack depth when the coverage trace was evaluated
The index of the expression evaluated by each test. This can be used to recover an order of trace execution for a given test index
The index of the coverage trace that was evaluated
srcrefs_df srcrefs
options(covr.record_tests = TRUE) pkg_path <- system.file("examplepkg", package = "covtracer") cov <- covr::package_coverage(pkg_path) test_trace_mapping(cov)
options(covr.record_tests = TRUE) pkg_path <- system.file("examplepkg", package = "covtracer") cov <- covr::package_coverage(pkg_path) test_trace_mapping(cov)
Extract srcref objects from coverage object traces
trace_srcrefs(x) ## S3 method for class 'coverage' trace_srcrefs(x)
trace_srcrefs(x) ## S3 method for class 'coverage' trace_srcrefs(x)
x |
( |
A list_of_srcref
as.data.frame.list_of_srcref
Other srcrefs:
pkg_srcrefs()
,
test_srcrefs()
Extract coverage
traces. Traces are the
traced lines of code counted when evaluating code coverage, which are used
for counting expression evaluation. Each traced is a unique expression within
a package's source code.
trace_srcrefs_df(x)
trace_srcrefs_df(x)
x |
A |
A data.frame
, where each record it a trace srcref
with
variables:
A character
identifier. This will use the names of the
elements of a coverage
object,
which are srcref
"keys".
A srcref
object of the trace source code location
srcrefs test_trace_mapping
Other srcrefs_df:
pkg_srcrefs_df()
,
test_srcrefs_df()
options(covr.record_tests = TRUE) pkg_path <- system.file("examplepkg", package = "covtracer") cov <- covr::package_coverage(pkg_path) trace_srcrefs_df(cov)
options(covr.record_tests = TRUE) pkg_path <- system.file("examplepkg", package = "covtracer") cov <- covr::package_coverage(pkg_path) trace_srcrefs_df(cov)
Most relevant data can be traced to an existing srcref. However, some data, such as test traces from coverage objects, are likely cleaned up and their srcfiles deleted, causing a barrage of warnings any time these objects are printed.
with_pseudo_srcref(call, file, lloc)
with_pseudo_srcref(call, file, lloc)
call |
Any code object, most often a |
file |
A filepath to bind as a |
lloc |
A |
A pseudo_srcref
adds in the srcref
data but continues to preserve the
expression content. This allows these expression objects to be pretty-printed
like srcref
s when included as a list_of_srcref
data.frame
column.
A with_pseudo_srcref
object, mimicking the structure of srcref