cuelang.org/go@v0.13.0/encoding/jsonschema/debug_test.go (about)

     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 jsonschema_test
    16  
    17  import (
    18  	"path"
    19  	"strings"
    20  	"testing"
    21  
    22  	"cuelang.org/go/cue"
    23  	"cuelang.org/go/cue/cuecontext"
    24  	"cuelang.org/go/cue/errors"
    25  	"cuelang.org/go/cue/format"
    26  	"cuelang.org/go/encoding/json"
    27  	"cuelang.org/go/encoding/jsonschema"
    28  	"cuelang.org/go/encoding/yaml"
    29  	"cuelang.org/go/internal/astinternal"
    30  	"github.com/go-quicktest/qt"
    31  	"golang.org/x/tools/txtar"
    32  )
    33  
    34  func TestX(t *testing.T) {
    35  	t.Skip()
    36  	data := `
    37  -- schema.json --
    38  `
    39  
    40  	a := txtar.Parse([]byte(data))
    41  
    42  	ctx := cuecontext.New()
    43  	var v cue.Value
    44  	var err error
    45  	for _, f := range a.Files {
    46  		switch path.Ext(f.Name) {
    47  		case ".json":
    48  			expr, err := json.Extract(f.Name, f.Data)
    49  			if err != nil {
    50  				t.Fatal(err)
    51  			}
    52  			v = ctx.BuildExpr(expr)
    53  		case ".yaml":
    54  			file, err := yaml.Extract(f.Name, f.Data)
    55  			if err != nil {
    56  				t.Fatal(err)
    57  			}
    58  			v = ctx.BuildFile(file)
    59  		}
    60  	}
    61  
    62  	cfg := &jsonschema.Config{ID: "test"}
    63  	expr, err := jsonschema.Extract(v, cfg)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	t.Fatal(astinternal.DebugStr(expr))
    69  }
    70  
    71  // This is a test for debugging external tests.
    72  func TestExtX(t *testing.T) {
    73  	t.Skip()
    74  
    75  	version := cuecontext.EvalDefault
    76  	// version = cuecontext.EvalExperiment
    77  	ctx := cuecontext.New(cuecontext.EvaluatorVersion(version))
    78  
    79  	const filename = `tests/draft4/optional/ecmascript-regexp.json`
    80  	const jsonSchema = `
    81  		{
    82  			"type": "object",
    83  			"patternProperties": {
    84  				"\\wcole": {}
    85  			},
    86  			"additionalProperties": false
    87  		}
    88  	`
    89  	const testData = `
    90  		{
    91  			"l'école": "pas de vraie vie"
    92  		}
    93  	`
    94  
    95  	jsonAST, err := json.Extract("schema.json", []byte(jsonSchema))
    96  	qt.Assert(t, qt.IsNil(err))
    97  	jsonValue := ctx.BuildExpr(jsonAST)
    98  	qt.Assert(t, qt.IsNil(jsonValue.Err()))
    99  	versStr, _, _ := strings.Cut(strings.TrimPrefix(filename, "tests/"), "/")
   100  	vers, ok := extVersionToVersion[versStr]
   101  	if !ok {
   102  		t.Fatalf("unknown JSON schema version for file %q", filename)
   103  	}
   104  	if vers == jsonschema.VersionUnknown {
   105  		t.Skipf("skipping test for unknown schema version %v", versStr)
   106  	}
   107  	schemaAST, extractErr := jsonschema.Extract(jsonValue, &jsonschema.Config{
   108  		StrictFeatures: true,
   109  		DefaultVersion: vers,
   110  	})
   111  	qt.Assert(t, qt.IsNil(extractErr))
   112  	b, err := format.Node(schemaAST, format.Simplify())
   113  	qt.Assert(t, qt.IsNil(err))
   114  	t.Logf("SCHEMA: %v", string(b))
   115  	schemaValue := ctx.CompileBytes(b, cue.Filename("generated.cue"))
   116  	if err := schemaValue.Err(); err != nil {
   117  		t.Fatalf("cannot compile resulting schema: %v", errors.Details(err, nil))
   118  	}
   119  
   120  	instAST, err := json.Extract("instance.json", []byte(testData))
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  
   125  	qt.Assert(t, qt.IsNil(err), qt.Commentf("test data: %q; details: %v", testData, errors.Details(err, nil)))
   126  
   127  	instValue := ctx.BuildExpr(instAST)
   128  	t.Log("VALUE", instValue)
   129  	qt.Assert(t, qt.IsNil(instValue.Err()))
   130  	err = instValue.Unify(schemaValue).Validate(cue.Concrete(true))
   131  
   132  	t.Error(errors.Details(err, nil))
   133  }