On Thu, 2024-09-05 at 18:14 -0700, 'Tristan Swadell' via CEL Go
Discussion Forum wrote:
> Hi Dan,
>
> Is part of the issue that the InterpretableAttribute intercepts some
> portion of the coverage graph? In general, you should be able to rely
> on a similar mechanism to the decorator used for doing state
> tracking:
>
https://pkg.go.dev/github.com/google/cel...@v0.21.0/cel#EvalOption
>
> -Tristan
Thanks. That would work, but it's significantly heavier than I'd like
since it keeps all of the states as well, which I don't need. If I
could define the implementation of the interpreter.EvalState, then it
would be fine, but this is essentially the same issue that I had with
the previous approaches that I tried because it happens internally;
there are interfaces that should be able to be used, but the details of
the interfaces make it impossible for me to satisfy them because the
required states are not made available, only being constructed
internally.
Essentially what I'd like to do is this
type coverage struct {
node interpreter.Interpretable
all map[int64]bool
cov map[int64]bool
}
func (c coverage) ProgramOption() cel.ProgramOption {
return cel.CustomDecorator(func(i interpreter.Interpretable)
(interpreter.Interpretable, error) {
c.all[i.ID()] = true
return coverage{node: i, cov: c.cov}, nil
})
}
func (c coverage) ID() int64 {
return
c.node.ID()
}
func (c coverage) Eval(a interpreter.Activation) ref.Val {
c.cov[
c.node.ID()] = true
return c.node.Eval(a)
}
which I can then use like so
c := coverage{all: make(map[int64]bool), cov: make(map[int64]bool)}
prg, err := env.Program(ast, c.ProgramOption())
and examine the two maps after to get a coverage percentage and
potentially be able to render the source in a meaningful way to guide
testing. But it fails with
failed program instantiation: failed program instantiation: invalid
attribute decoration: {0xc00040cb40 map[] map[]}(main.coverage)
Dan