|
| 1 | +// Copyright 2025 CUE Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package adt_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "cuelang.org/go/cue" |
| 21 | + "cuelang.org/go/cue/cuecontext" |
| 22 | +) |
| 23 | + |
| 24 | +func closeAll(v cue.Value) cue.Value { |
| 25 | + path := cue.MakePath(cue.Def("#x")) |
| 26 | + return v.Context().CompileString("#x: _").FillPath(path, v).LookupPath(path) |
| 27 | +} |
| 28 | + |
| 29 | +type editConfig struct { |
| 30 | + Edits []cue.Value `json:"edits,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +// Modifying values can cause a mix of non-finalized nodes as children of |
| 34 | +// a finalized parent. Ensure that this does not cause issues. |
| 35 | +func TestAPIModifyingValues(t *testing.T) { |
| 36 | + ctx := cuecontext.New(cuecontext.CUE_DEBUG("logeval=1")) |
| 37 | + |
| 38 | + v := ctx.CompileString(` |
| 39 | + edits: [{ |
| 40 | + type: "replace" |
| 41 | + data!: "^wrong" |
| 42 | + }] |
| 43 | + `) |
| 44 | + if err := v.Err(); err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + closed := closeAll(ctx.EncodeType(editConfig{})) |
| 48 | + |
| 49 | + v = v.Unify(closed) |
| 50 | + if err := v.Err(); err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + var cfg editConfig |
| 55 | + if err := v.Decode(&cfg); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + w := ctx.CompileString(` |
| 60 | + type!: "fill" | "replace" |
| 61 | + data!: _ |
| 62 | + `) |
| 63 | + |
| 64 | + if err := w.Err(); err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + x := cfg.Edits[0] |
| 68 | + x = x.Unify(w) |
| 69 | + |
| 70 | + if err := x.Err(); err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | +} |
0 commit comments