-- in.cue --
@experiment(explicitopen)

// Basic usage of __closeAll - makes structs recursively closed
a: __closeAll({
	field1: 1
	nested: {
		field2: "hello"
	}
}) @test(eq, {field1: 1, nested: {field2: "hello"}})

// Test that extra fields are not allowed (should error)
b: {
	err: a & {extra: "not allowed"}
	@test(err, at=err.extra, code=eval, contains="field not allowed", pos=[1:12])
}

// Test that nested extra fields are not allowed (should error)
c: {
	err: a & {
		field1: 1
		nested: {
			field2: "hello"
			extra: "not allowed"
		}
	}
	@test(err, at=err.nested.extra, code=eval, contains="field not allowed", pos=[5:4])
}

// Test with non-struct should error
d: {
	err: __closeAll("not a struct") @test(err, code=eval, contains="argument must be a struct or list literal", pos=[0:7])
}

// Test with empty struct
e: __closeAll({}) @test(eq, {})
f: {
	err: e & {extra: "not allowed"}  // should error
	@test(err, at=err.extra, code=eval, contains="field not allowed", pos=[1:12])
}


#A: a: int
g: {
	err: __closeAll(#A) @test(err, code=eval, contains="argument must be a struct or list literal", pos=[0:7])
}
-- old.cue --
// May not be used if explicitopen is not set.
h: {
	err: __closeAll({}) @test(err, code=eval, contains="__closeAll may only be used when explicitopen is enabled", pos=[0:7])
}

-- out/errors.txt --
[eval] b.err.extra: field not allowed:
    ./in.cue:13:12
[eval] c.err.nested.extra: field not allowed:
    ./in.cue:23:4
[eval] d.err: error in call to __closeAll: argument must be a struct or list literal:
    ./in.cue:31:7
[eval] f.err.extra: field not allowed:
    ./in.cue:37:12
[eval] g.err: error in call to __closeAll: argument must be a struct or list literal:
    ./in.cue:44:7
[eval] h.err: error in call to __closeAll: __closeAll may only be used when explicitopen is enabled:
    ./old.cue:3:7
-- out/eval/stats --
Leaks:  0
Freed:  41
Reused: 23
Allocs: 18
Retain: 0

Unifications: 35
Conjuncts:    50
Disjuncts:    0

NumCloseIDs: 8
