cuelang.org/go@v0.10.1/internal/core/export/extract_test.go (about)

     1  // Copyright 2020 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 export_test
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"cuelang.org/go/internal/core/adt"
    22  	"cuelang.org/go/internal/core/compile"
    23  	"cuelang.org/go/internal/core/eval"
    24  	"cuelang.org/go/internal/core/export"
    25  	"cuelang.org/go/internal/cuetdtest"
    26  	"cuelang.org/go/internal/cuetxtar"
    27  )
    28  
    29  func TestExtract(t *testing.T) {
    30  	test := cuetxtar.TxTarTest{
    31  		Root: "./testdata/main",
    32  		Name: "doc",
    33  
    34  		// TODO: use FullMatrix when enough tests pass.
    35  		Matrix: cuetdtest.SmallMatrix,
    36  	}
    37  
    38  	test.Run(t, func(t *cuetxtar.Test) {
    39  		r := t.Runtime()
    40  		a := t.Instance()
    41  
    42  		v, err := compile.Files(nil, r, "", a.Files...)
    43  		if err != nil {
    44  			t.Fatal(err)
    45  		}
    46  
    47  		ctx := eval.NewContext(r, v)
    48  		v.Finalize(ctx)
    49  
    50  		writeDocs(t, r, v, nil)
    51  	})
    52  }
    53  
    54  func writeDocs(t *cuetxtar.Test, r adt.Runtime, v *adt.Vertex, path []string) {
    55  	fmt.Fprintln(t, path)
    56  	for _, c := range export.ExtractDoc(v) {
    57  		fmt.Fprintln(t, "-", c.Text())
    58  	}
    59  
    60  	// Simulate the dereference behavior that is implemented in the CUE api.
    61  	v = v.DerefValue()
    62  	for _, a := range v.Arcs {
    63  		writeDocs(t, r, a, append(path, a.Label.SelectorString(r)))
    64  	}
    65  }