# generated from the original tests.
# Henceforth it may be nicer to group tests into separate files.
-- in.cue --
import "encoding/json"

validate: [string]: {
	str:    string | *"{\"a\":10}"
	schema: _
	result: json.Validate(str, schema)
}
validate: t1: schema: {b: string}
validate: t2: schema: {a: <3}

validate: disjunctionRequired: schema: {a!: int} | {b!: int}
validate: disjunctionClosed: schema: close({a: int}) | close({b: int})

// Issue #2395
validate: enforceRequired: {
	str: "{}"
	schema: {x!: int}
}

issue3932: {
	f: json.Validate({name!: string})
	f: json.Marshal({name: "foo"})
}

valid: [string]: {
	string // input
	#result: json.Valid(string)
}
valid: t1: "1"

compact: [string]: X={
	_ // input
	#result: json.Compact(X)
}
compact: t1: "[1, 2]"

indent: [string]: X={
	string // input
	#initial: *"" | string
	#indent:  *"  " | string
	#result: json.Indent(X, #initial, #indent)
}
indent: t1: #"{"a": 1, "b": 2}"#

unmarshal: [string]: X={
	string // input
	#result: json.Unmarshal(X)
}
unmarshal: t1: "1"
unmarshal: {
	trailingValid:   #"{"a": 1}{"b": 2}"#
	trailingInvalid: #"{"a": 1}}invalid json"#
}

marshalStream: [string]: X={
	[...] // input
	#result: json.MarshalStream(X)
}
marshalStream: t1: [{a: 1}, {b: 2}]
marshalStream: t2: [{a: 1}, {b: int | *2}]
marshalStream: t3: [{a: #"\ " & < >"#}, {b: ""}]


marshal: [string]: X={
	_ // input
	#result: json.Marshal(X)
}
marshal: t1: {
	#x: int
	a: #x
}

marshal: t2: {a: #"\ " & < >"#}

htmlEscape: [string]: X={
	string // input
	#result: json.HTMLEscape(X)
}
htmlEscape: t1: marshal.t2.#result
htmlEscape: t2: marshalStream.t3.#result

unmarshalStream: [string]: X={
	string | bytes // input
	#result: json.UnmarshalStream(X)
}
unmarshalStream: {
	t1:    #"{"a": 1}{"b": 2}"#
	t2:    #'{"a": 1}{"b": 2}'#
	empty1: ''
	empty2: ""
	nums1:  '1 2'
	nums2:  "1 2"
}
-- out/json --
Errors:
validate.t2.result: error in call to encoding/json.Validate: invalid value 10 (out of bound <3):
    ./in.cue:6:10
    ./in.cue:9:27
    json.Validate:1:6
unmarshal.trailingInvalid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
    ./in.cue:47:11
unmarshal.trailingValid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
    ./in.cue:47:11

Result:
import "encoding/json"

validate: {
	t1: {
		str: *"{\"a\":10}" | string
		schema: {
			b: string
		}
		result: true
	}
	t2: {
		str: *"{\"a\":10}" | string
		schema: {
			a: <3
		}
		result: _|_ // validate.t2.result: error in call to encoding/json.Validate: validate.t2.result.a: invalid value 10 (out of bound <3)
	}
	disjunctionRequired: {
		str: *"{\"a\":10}" | string
		schema: {
			a!: int
		} | {
			b!: int
		}
		result: true
	}
	disjunctionClosed: {
		str: *"{\"a\":10}" | string
		schema: {
			a: int
		} | {
			b: int
		}
		result: true
	}

	// Issue #2395
	enforceRequired: {
		str: "{}"
		schema: {
			x!: int
		}
		result: json.Validate(str, schema)
	}
}
issue3932: {
	f: "{\"name\":\"foo\"}"
}
valid: {
	t1: {
		"1"
		#result: json.Valid(string)
	}
}
compact: {
	t1: {
		"[1, 2]"
		#result: "[1,2]"
	}
}
indent: {
	t1: {
		"{\"a\": 1, \"b\": 2}"
		#initial: *"" | string
		#indent:  *"  " | string
		#result: """
			{
			  "a": 1,
			  "b": 2
			}
			"""
	}
}
unmarshal: {
	t1: {
		"1"
		#result: 1
	}
	trailingValid: {
		#result: _|_ // unmarshal.trailingValid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON
	}
	trailingInvalid: {
		#result: _|_ // unmarshal.trailingInvalid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON
	}
}
marshalStream: {
	t1: {
		#result: """
			{"a":1}
			{"b":2}

			"""
		[{
			a: 1
		}, {
			b: 2
		}]
	}
	t2: {
		#result: """
			{"a":1}
			{"b":2}

			"""
		[{
			a: 1
		}, {
			b: *2 | int
		}]
	}
	t3: {
		#result: """
			{"a":"\\\\ \\" & < >"}
			{"b":""}

			"""
		[{
			a: "\\ \" & < >"
		}, {
			b: ""
		}]
	}
}
marshal: {
	t1: {
		#result: json.Marshal(X)
		#x:      int
		a:       int
	}
	t2: {
		#result: "{\"a\":\"\\\\ \\\" & < >\"}"
		a:       "\\ \" & < >"
	}
}
htmlEscape: {
	t1: {
		"{\"a\":\"\\\\ \\\" & < >\"}"
		#result: "{\"a\":\"\\\\ \\\" \\u0026 \\u003c \\u003e\"}"
	}
	t2: {
		"""
		{"a":"\\\\ \\" & < >"}
		{"b":""}

		"""
		#result: """
			{"a":"\\\\ \\" \\u0026 \\u003c \\u003e"}
			{"b":""}

			"""
	}
}
unmarshalStream: {
	t1: {
		"{\"a\": 1}{\"b\": 2}"
		#result: [{
			a: 1
		}, {
			b: 2
		}]
	}
	t2: {
		'{"a": 1}{"b": 2}'
		#result: [{
			a: 1
		}, {
			b: 2
		}]
	}
	empty1: {
		''
		#result: []
	}
	empty2: {
		""
		#result: []
	}
	nums1: {
		'1 2'
		#result: [1, 2]
	}
	nums2: {
		"1 2"
		#result: [1, 2]
	}
}
