go-sdk/internal/jsonschema/util_test.go
Jonathan Amsterdam c99ff4897b jsonschema: validate some keywords
This CL implements validation for the "type", "enum" and "const"
keywords.

Validation in its full generality requires that references in the schema
be resolved. We put that off for now; it isn't needed for most simple
keywords.

The JSON Schema folks provide an extensive test suite for validation at
https://github.com/json-schema-org/JSON-Schema-Test-Suite. We use it for
our tests, copying the necessary files into testdata. The files aren't
perfectly independent; for example, one test in enum.json requires the
"properties" keyword, which we haven't done yet. We will excise those
tests via calls to t.Skip as they arise.

Change-Id: I568b5c87802a39aecbc58f342931308fc8fb211c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/667875
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
2025-04-25 08:34:02 -07:00

73 lines
1.5 KiB
Go

// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package jsonschema
import (
"encoding/json"
"reflect"
"testing"
)
func TestEqual(t *testing.T) {
for _, tt := range []struct {
x1, x2 any
want bool
}{
{0, 1, false},
{1, 1.0, true},
{nil, 0, false},
{"0", 0, false},
{2.5, 2.5, true},
{[]int{1, 2}, []float64{1.0, 2.0}, true},
{[]int(nil), []int{}, false},
{[]map[string]any(nil), []map[string]any{}, false},
{
map[string]any{"a": 1, "b": 2.0},
map[string]any{"a": 1.0, "b": 2},
true,
},
} {
check := func(x1, x2 any, want bool) {
t.Helper()
if got := Equal(x1, x2); got != want {
t.Errorf("jsonEqual(%#v, %#v) = %t, want %t", x1, x2, got, want)
}
}
check(tt.x1, tt.x1, true)
check(tt.x2, tt.x2, true)
check(tt.x1, tt.x2, tt.want)
check(tt.x2, tt.x1, tt.want)
}
}
func TestJSONType(t *testing.T) {
for _, tt := range []struct {
val string
want string
}{
{`null`, "null"},
{`0`, "integer"},
{`0.0`, "integer"},
{`1e2`, "integer"},
{`0.1`, "number"},
{`""`, "string"},
{`true`, "boolean"},
{`[]`, "array"},
{`{}`, "object"},
} {
var val any
if err := json.Unmarshal([]byte(tt.val), &val); err != nil {
t.Fatal(err)
}
got, ok := jsonType(reflect.ValueOf(val))
if !ok {
t.Fatalf("jsonType failed on %q", tt.val)
}
if got != tt.want {
t.Errorf("%s: got %q, want %q", tt.val, got, tt.want)
}
}
}