cuelang.org/go@v0.13.0/internal/astinternal/debug_test.go (about)

     1  // Copyright 2024 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  //     http://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 astinternal_test
    16  
    17  import (
    18  	"path"
    19  	"reflect"
    20  	"regexp"
    21  	"strings"
    22  	"testing"
    23  
    24  	"cuelang.org/go/cue/ast"
    25  	"cuelang.org/go/cue/parser"
    26  	"cuelang.org/go/internal/astinternal"
    27  	"cuelang.org/go/internal/cuetxtar"
    28  
    29  	"github.com/go-quicktest/qt"
    30  )
    31  
    32  var ptrPat = regexp.MustCompile(`0x[0-9a-z]+`)
    33  
    34  func TestDebugPrint(t *testing.T) {
    35  	test := cuetxtar.TxTarTest{
    36  		Root: "testdata",
    37  		Name: "debugprint",
    38  	}
    39  
    40  	test.Run(t, func(t *cuetxtar.Test) {
    41  		includePointers := t.HasTag("includePointers")
    42  		for _, file := range t.Archive.Files {
    43  			if strings.HasPrefix(file.Name, "out/") {
    44  				continue
    45  			}
    46  
    47  			f, err := parser.ParseFile(file.Name, file.Data, parser.ParseComments)
    48  			qt.Assert(t, qt.IsNil(err))
    49  
    50  			// The full syntax tree, as printed by default.
    51  			// We enable IncludeNodeRefs because it only adds information
    52  			// that would not otherwise be present.
    53  			// The syntax tree does not contain any maps, so
    54  			// the generated reference names should be deterministic.
    55  			full := astinternal.AppendDebug(nil, f, astinternal.DebugConfig{
    56  				IncludeNodeRefs: true,
    57  				IncludePointers: includePointers,
    58  			})
    59  			if includePointers {
    60  				// Pointer values change between runs. Replace with a constant
    61  				// string so that we can test stable output.
    62  				full = ptrPat.ReplaceAll(full, []byte("XXXX"))
    63  			}
    64  			t.Writer(file.Name).Write(full)
    65  
    66  			// A syntax tree which omits any empty values,
    67  			// and is only interested in showing string fields.
    68  			// We allow ast.Nodes and slices to not stop too early.
    69  			typNode := reflect.TypeFor[ast.Node]()
    70  			strings := astinternal.AppendDebug(nil, f, astinternal.DebugConfig{
    71  				OmitEmpty: true,
    72  				Filter: func(v reflect.Value) bool {
    73  					if v.Type().Implements(typNode) {
    74  						return true
    75  					}
    76  					switch v.Kind() {
    77  					case reflect.Slice, reflect.String:
    78  						return true
    79  					default:
    80  						return false
    81  					}
    82  				},
    83  			})
    84  			t.Writer(path.Join(file.Name, "omitempty-strings")).Write(strings)
    85  		}
    86  	})
    87  }