@@ -240,12 +240,16 @@ func translateRegularContextJSONToEventOutputJSONAndViceVersa(s string) string {
240240 return s
241241}
242242
243- func contextUnmarshalingTests (t * testing.T , unmarshalFn func (* Context , []byte ) error ) {
243+ func contextUnmarshalingTests (
244+ t * testing.T ,
245+ unmarshalSingleContextFn func (* Context , []byte ) error ,
246+ unmarshalArrayFn func (* []Context , []byte ) error ,
247+ ) {
244248 t .Run ("valid data" , func (t * testing.T ) {
245249 for _ , p := range makeAllContextUnmarshalingParams () {
246250 t .Run (p .json , func (t * testing.T ) {
247251 var c Context
248- err := unmarshalFn (& c , []byte (p .json ))
252+ err := unmarshalSingleContextFn (& c , []byte (p .json ))
249253 assert .NoError (t , err )
250254 assert .Equal (t , p .context , c )
251255 })
@@ -256,11 +260,33 @@ func contextUnmarshalingTests(t *testing.T, unmarshalFn func(*Context, []byte) e
256260 for _ , badJSON := range makeContextUnmarshalingErrorInputs () {
257261 t .Run (badJSON , func (t * testing.T ) {
258262 var c Context
259- err := unmarshalFn (& c , []byte (badJSON ))
263+ err := unmarshalSingleContextFn (& c , []byte (badJSON ))
260264 assert .Error (t , err )
261265 })
262266 }
263267 })
268+
269+ if unmarshalArrayFn != nil {
270+ t .Run ("within an array" , func (t * testing.T ) {
271+ // This test shows that in our streaming implementations, the unmarshaler leaves the input
272+ // stream in the proper state at the end of the object, so it will correctly parse a
273+ // subsequent value.
274+ invariantSecondContext := New ("simple" )
275+ invariantSecondContextJSON := `{"kind": "user", "key": "simple"}`
276+ for _ , p := range makeAllContextUnmarshalingParams () {
277+ t .Run (p .json , func (t * testing.T ) {
278+ input := `[ ` + p .json + `, ` + invariantSecondContextJSON + ` ]`
279+ var cs []Context
280+ err := unmarshalArrayFn (& cs , []byte (input ))
281+ assert .NoError (t , err )
282+ if assert .Len (t , cs , 2 ) {
283+ assert .Equal (t , p .context , cs [0 ])
284+ assert .Equal (t , invariantSecondContext , cs [1 ])
285+ }
286+ })
287+ }
288+ })
289+ }
264290}
265291
266292func jsonUnmarshalTestFn (c * Context , data []byte ) error {
@@ -273,12 +299,22 @@ func jsonStreamUnmarshalTestFn(c *Context, data []byte) error {
273299 return r .Error ()
274300}
275301
302+ func jsonStreamUnmarshalArrayTestFn (cs * []Context , data []byte ) error {
303+ r := jreader .NewReader (data )
304+ for arr := r .Array (); arr .Next (); {
305+ var c Context
306+ ContextSerialization .UnmarshalFromJSONReader (& r , & c )
307+ * cs = append (* cs , c )
308+ }
309+ return r .Error ()
310+ }
311+
276312func TestContextJSONUnmarshal (t * testing.T ) {
277- contextUnmarshalingTests (t , jsonUnmarshalTestFn )
313+ contextUnmarshalingTests (t , jsonUnmarshalTestFn , nil )
278314}
279315
280316func TestContextReadFromJSONReader (t * testing.T ) {
281- contextUnmarshalingTests (t , jsonStreamUnmarshalTestFn )
317+ contextUnmarshalingTests (t , jsonStreamUnmarshalTestFn , jsonStreamUnmarshalArrayTestFn )
282318}
283319
284320func TestContextUnmarshalEventOutputFormat (t * testing.T ) {
0 commit comments