77
88 "cuelang.org/go/cue/cuecontext"
99 "cuelang.org/go/cue/errors"
10+
11+ "github.com/stretchr/testify/assert"
12+ "github.com/stretchr/testify/require"
1013)
1114
1215var linstr = `name: "single"
@@ -17,6 +20,13 @@ schemas: [{
1720 anint: int64 | *42
1821 abool: bool
1922 }
23+ examples: {
24+ simple: {
25+ astring: "some string"
26+ anint: 42
27+ abool: true
28+ }
29+ }
2030}]
2131`
2232
@@ -76,6 +86,33 @@ func TestBindType(t *testing.T) {
7686 }
7787}
7888
89+ func TestSchema_Examples (t * testing.T ) {
90+ lin := testLin ()
91+
92+ sch := lin .First ()
93+ examples := sch .Examples ()
94+ require .NotNil (t , examples )
95+
96+ // There must be a "simple" example based on
97+ // the definition above (beginning of file).
98+ require .NotEmpty (t , examples )
99+ require .NotNil (t , examples ["simple" ])
100+
101+ tt := & TestType {}
102+ ts , err := BindType [* TestType ](sch , tt )
103+ require .NoError (t , err )
104+
105+ tinst , err := ts .ValidateTyped (examples ["simple" ].Underlying ())
106+ require .NoError (t , err )
107+
108+ val , err := tinst .Value ()
109+ require .NoError (t , err )
110+
111+ assert .Equal (t , "some string" , * val .Astring )
112+ assert .Equal (t , int64 (42 ), val .Anint )
113+ assert .Equal (t , true , val .Abool )
114+ }
115+
79116// scratch test, preserved only as a simpler sandbox for future playing with pointers, generics, reflect
80117func testPointerNewVar (t * testing.T ) {
81118 type Foo struct {
0 commit comments