45 lines
1.9 KiB
Go
45 lines
1.9 KiB
Go
package argschema
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidate(t *testing.T) {
|
|
schema := json.RawMessage(`{"type":"object","required":["path"],"properties":{"path":{"type":"string"},"limit":{"type":"integer"},"ratio":{"type":"number"},"all":{"type":"boolean"},"any":{}}}`)
|
|
tests := []struct {
|
|
name string
|
|
schema json.RawMessage
|
|
args string
|
|
want string
|
|
}{
|
|
{"valid", schema, `{"path":"a.txt","limit":3,"ratio":0.5,"all":true,"any":[1]}`, ""},
|
|
{"no-schema-accepts-object", nil, `{"x":1}`, ""},
|
|
{"no-properties-accepts-unknown", json.RawMessage(`{"type":"object"}`), `{"x":1}`, ""},
|
|
{"invalid-schema", json.RawMessage(`[`), `{}`, "invalid tool schema"},
|
|
{"array-arguments", schema, `[1]`, "arguments must be a JSON object"},
|
|
{"broken-arguments", schema, `{"path":`, "arguments must be a JSON object"},
|
|
{"non-object-schema", json.RawMessage(`{"type":"array"}`), `{}`, "unsupported schema type array"},
|
|
{"missing-required", schema, `{}`, "missing required argument path"},
|
|
{"null-required", schema, `{"path":null}`, "missing required argument path"},
|
|
{"blank-required", schema, `{"path":" "}`, "missing required argument path"},
|
|
{"wrong-string", schema, `{"path":7}`, "argument path must be string"},
|
|
{"fractional-integer", schema, `{"path":"a","limit":1.5}`, "argument limit must be integer"},
|
|
{"wrong-number", schema, `{"path":"a","ratio":"x"}`, "argument ratio must be number"},
|
|
{"wrong-boolean", schema, `{"path":"a","all":1}`, "argument all must be boolean"},
|
|
{"unknown-key", schema, `{"path":"a","extra":1}`, "unknown argument extra"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := Validate(tc.schema, json.RawMessage(tc.args))
|
|
got := ""
|
|
if err != nil {
|
|
got = err.Error()
|
|
}
|
|
if got != tc.want {
|
|
t.Fatalf("Validate(%s, %s) = %q, want %q", tc.schema, tc.args, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|