Skip to content

Commit

Permalink
fixed partial vars bug and split tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jduraniglesias committed Jun 5, 2024
1 parent fbf5c77 commit 093efd7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
75 changes: 75 additions & 0 deletions cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,81 @@ func TestResidualAstAttributeQualifiers(t *testing.T) {
}
}

func TestPartialVarsEnv(t *testing.T) {
env := testEnv(t,
Variable("x", IntType),
Variable("y", IntType),
)

// Use env to make sure internals are all initialized.
ast, iss := env.Compile("x == y")

if iss.Err() != nil {
t.Fatalf("env.Compile() failed: %v", iss.Err())
}
prg, err := env.Program(ast, EvalOptions(OptPartialEval))
if err != nil {
t.Fatalf("env.Program() failed: %v", err)
}

act, err := env.PartialVars(map[string]any{"x": 1, "y": 1})

if err != nil {
t.Fatalf("env.PartialVars failed: %v", err)
}
val, _, err := prg.Eval(act)
if err != nil {
t.Fatalf("Eval failed: %v", err)
}

if val != types.True {
t.Fatalf("want: %v, got: %v", types.True, val)
}
}

func TestPartialVarsExtendedEnv(t *testing.T) {
env := testEnv(t,
Variable("x", IntType),
Variable("y", IntType),
)

//To:DO
//then make pr to clone, repl add cmd to enable partial eval and add support for command and then add another pr

// Now test that a sub environment is correctly copied.
env2, err := env.Extend(Variable("z", IntType))
if err != nil {
t.Fatalf("env.Extend failed: %v", err)
}

ast, iss := env2.Compile("x == y && y == z")

if iss.Err() != nil {
t.Fatalf("env.Compile() failed: %v", iss.Err())
}
prg, err := env2.Program(ast, EvalOptions(OptPartialEval))
if err != nil {
t.Fatalf("env.Program() failed: %v", err)
}

act, err := env2.PartialVars(map[string]any{"z": 1, "y": 1})

if err != nil {
t.Fatalf("env.PartialVars failed: %v", err)
}
val, _, err := prg.Eval(act)
if err != nil {
t.Fatalf("Eval failed: %v", err)
}
if !types.IsUnknown(val) {
t.Fatalf("Wanted unknown, got %v", val)
}

if !reflect.DeepEqual(val, types.NewUnknown(1, types.NewAttributeTrail("x"))) {
t.Fatalf("Wanted Unknown(x (1)), got: %v", val)
}
}

func TestResidualAstModified(t *testing.T) {
env := testEnv(t,
Variable("x", MapType(StringType, IntType)),
Expand Down
9 changes: 3 additions & 6 deletions cel/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,14 @@ func (e *Env) Extend(opts ...EnvOption) (*Env, error) {
copy(chkOptsCopy, e.chkOpts)

// Copy the declarations if needed.
varsCopy := []*decls.VariableDecl{}
if chk != nil {
// If the type-checker has already been instantiated, then the e.declarations have been
// validated within the chk instance.
chkOptsCopy = append(chkOptsCopy, checker.ValidatedDeclarations(chk))
} else {
// If the type-checker has not been instantiated, ensure the unvalidated declarations are
// provided to the extended Env instance.
varsCopy = make([]*decls.VariableDecl, len(e.variables))
copy(varsCopy, e.variables)
}
//
varsCopy := make([]*decls.VariableDecl, len(e.variables))
copy(varsCopy, e.variables)

// Copy macros and program options
macsCopy := make([]parser.Macro, len(e.macros))
Expand Down

0 comments on commit 093efd7

Please sign in to comment.