@@ -31,6 +31,8 @@ import (
31
31
"cuelang.org/go/internal/core/convert"
32
32
"cuelang.org/go/internal/core/debug"
33
33
"cuelang.org/go/internal/core/runtime"
34
+
35
+ _ "cuelang.org/go/pkg"
34
36
)
35
37
36
38
func mkBigInt (a int64 ) (v apd.Decimal ) { v .SetInt64 (a ); return }
@@ -265,26 +267,27 @@ func TestX(t *testing.T) {
265
267
266
268
func TestConvertType (t * testing.T ) {
267
269
testCases := []struct {
268
- goTyp interface {}
269
- want string
270
+ goTyp interface {}
271
+ want string
272
+ expectError bool
270
273
}{{
271
- struct {
274
+ goTyp : struct {
272
275
A int `cue:">=0&<100"`
273
276
B * big.Int `cue:">=0"`
274
277
C * big.Int
275
278
D big.Int
276
279
F * big.Float
277
280
}{},
278
281
// TODO: indicate that B is explicitly an int only.
279
- `{
282
+ want : `{
280
283
A: (((int & >=-9223372036854775808) & <=9223372036854775807) & (>=0 & <100))
281
284
B: (int & >=0)
282
285
C?: int
283
286
D: int
284
287
F?: number
285
288
}` ,
286
289
}, {
287
- & struct {
290
+ goTyp : & struct {
288
291
A int16 `cue:">=0&<100"`
289
292
B error `json:"b,"`
290
293
C string
@@ -294,7 +297,7 @@ func TestConvertType(t *testing.T) {
294
297
T time.Time
295
298
G func ()
296
299
}{},
297
- `(*null|{
300
+ want : `(*null|{
298
301
A: (((int & >=-32768) & <=32767) & (>=0 & <100))
299
302
b: null
300
303
C: string
@@ -304,83 +307,89 @@ func TestConvertType(t *testing.T) {
304
307
T: _
305
308
})` ,
306
309
}, {
307
- struct {
310
+ goTyp : struct {
308
311
A int `cue:"<"` // invalid
309
312
}{},
310
- "_|_(invalid tag \" <\" for field \" A\" : expected operand, found 'EOF' (and 1 more errors))" ,
313
+ want : "_|_(invalid tag \" <\" for field \" A\" : expected operand, found 'EOF' (and 1 more errors))" ,
314
+ expectError : true ,
311
315
}, {
312
- struct {
316
+ goTyp : struct {
313
317
A int `json:"-"` // skip
314
318
D * apd.Decimal
315
319
P * * * apd.Decimal
316
320
I interface { Foo () }
317
321
T string `cue:""` // allowed
318
322
h int
319
323
}{},
320
- `{
324
+ want : `{
321
325
D?: number
322
326
P?: (*null|number)
323
327
I?: _
324
328
T: (string & _)
325
329
}` ,
326
330
}, {
327
- struct {
331
+ goTyp : struct {
328
332
A int8 `cue:"C-B"`
329
333
B int8 `cue:"C-A,opt"`
330
334
C int8 `cue:"A+B"`
331
335
}{},
332
336
// TODO: should B be marked as optional?
333
- `{
337
+ want : `{
334
338
A: (((int & >=-128) & <=127) & (〈0;C〉 - 〈0;B〉))
335
339
B?: (((int & >=-128) & <=127) & (〈0;C〉 - 〈0;A〉))
336
340
C: (((int & >=-128) & <=127) & (〈0;A〉 + 〈0;B〉))
337
341
}` ,
338
342
}, {
339
- []string {},
340
- `(*null|[
343
+ goTyp : []string {},
344
+ want : `(*null|[
341
345
...string,
342
346
])` ,
343
347
}, {
344
- [4 ]string {},
345
- `(4 * [
348
+ goTyp : [4 ]string {},
349
+ want : `〈import;list〉.Repeat( [
346
350
string,
347
- ])` ,
351
+ ], 4 )` ,
348
352
}, {
349
- []func (){},
350
- "_|_(unsupported Go type (func()))" ,
353
+ goTyp : []func (){},
354
+ want : "_|_(unsupported Go type (func()))" ,
355
+ expectError : true ,
351
356
}, {
352
- map [string ]struct { A map [string ]uint }{},
353
- `(*null|{
357
+ goTyp : map [string ]struct { A map [string ]uint }{},
358
+ want : `(*null|{
354
359
[string]: {
355
360
A?: (*null|{
356
361
[string]: ((int & >=0) & <=18446744073709551615)
357
362
})
358
363
}
359
364
})` ,
360
365
}, {
361
- map [float32 ]int {},
362
- `_|_(unsupported Go type for map key (float32))` ,
366
+ goTyp : map [float32 ]int {},
367
+ want : `_|_(unsupported Go type for map key (float32))` ,
368
+ expectError : true ,
363
369
}, {
364
- map [int ]map [float32 ]int {},
365
- `_|_(unsupported Go type for map key (float32))` ,
370
+ goTyp : map [int ]map [float32 ]int {},
371
+ want : `_|_(unsupported Go type for map key (float32))` ,
372
+ expectError : true ,
366
373
}, {
367
- map [int ]func (){},
368
- `_|_(unsupported Go type (func()))` ,
374
+ goTyp : map [int ]func (){},
375
+ want : `_|_(unsupported Go type (func()))` ,
376
+ expectError : true ,
369
377
}, {
370
- time .Now , // a function
371
- "_|_(unsupported Go type (func() time.Time))" ,
378
+ goTyp : time .Now , // a function
379
+ want : "_|_(unsupported Go type (func() time.Time))" ,
380
+ expectError : true ,
372
381
}, {
373
- struct {
382
+ goTyp : struct {
374
383
Foobar string `cue:"\"foo,bar\",opt"`
375
384
}{},
376
- `{
385
+ want : `{
377
386
Foobar?: (string & "foo,bar")
378
387
}` ,
379
388
}, {
380
- struct {
389
+ goTyp : struct {
381
390
Foobar string `cue:"\"foo,opt,bar\""`
382
391
}{},
383
- `{
392
+ want : `{
384
393
Foobar: (string & "foo,opt,bar")
385
394
}` ,
386
395
}}
@@ -390,11 +399,22 @@ func TestConvertType(t *testing.T) {
390
399
for _ , tc := range testCases {
391
400
t .Run ("" , func (t * testing.T ) {
392
401
ctx := adt .NewContext (r , & adt.Vertex {})
393
- v , _ := convert .GoTypeToExpr (ctx , tc .goTyp )
402
+ v , err := convert .GoTypeToExpr (ctx , tc .goTyp )
394
403
got := debug .NodeString (ctx , v , nil )
395
404
if got != tc .want {
396
405
t .Errorf ("\n got %q;\n want %q" , got , tc .want )
397
406
}
407
+ if tc .expectError && err == nil {
408
+ t .Errorf ("\n expected an error but didn't get one" )
409
+ } else if ! tc .expectError && err != nil {
410
+ t .Errorf ("\n got unexpected error: %v" , err )
411
+ }
412
+ if err == nil && ! tc .expectError {
413
+ val , _ := ctx .Evaluate (& adt.Environment {}, v )
414
+ if bot , ok := val .(* adt.Bottom ); ok {
415
+ t .Errorf ("\n unexpected error when evaluating result of conversion: %v" , bot )
416
+ }
417
+ }
398
418
})
399
419
}
400
420
}
0 commit comments