cuelang.org/go@v0.13.0/cue/testdata/eval/issue3688.txtar (about)

     1  # This test is important in locking in the behaviour of evalv3,
     2  # which fixed some closedness bugs present in the older evalv2.
     3  
     4  -- x.cue --
     5  package x
     6  
     7  #TypeA: string | {foo: int}
     8  
     9  #fun1: {
    10  	_in: #TypeA
    11  	out: [
    12  		// either `in` is a string
    13  		if (_in & string) != _|_ {
    14  			_in
    15  		},
    16  		// or `in` is has field `foo`
    17  		if _in.foo != _|_ {
    18  			"foo"
    19  		},
    20  		"impossible", // in & #TypeA != _|_ !!
    21  	][0]
    22  }
    23  
    24  examples: [string]: {
    25  	in: _
    26  	res: (#fun1 & {_in: in, _}).out
    27  }
    28  
    29  examples: eg1: in: "test"
    30  examples: eg2: in: {
    31  	foo: 5
    32  }
    33  examples: eg3: in: {
    34  	foo: 5
    35  	bar: "test"
    36  }
    37  -- out/compile --
    38  --- x.cue
    39  {
    40    #TypeA: (string|{
    41      foo: int
    42    })
    43    #fun1: {
    44      _in: 〈1;#TypeA〉
    45      out: [
    46        if ((〈1;_in〉 & string) != _|_(explicit error (_|_ literal) in source)) {
    47          〈2;_in〉
    48        },
    49        if (〈1;_in〉.foo != _|_(explicit error (_|_ literal) in source)) {
    50          "foo"
    51        },
    52        "impossible",
    53      ][0]
    54    }
    55    examples: {
    56      [string]: {
    57        in: _
    58        res: (〈2;#fun1〉 & {
    59          _in: 〈1;in〉
    60          _
    61        }).out
    62      }
    63    }
    64    examples: {
    65      eg1: {
    66        in: "test"
    67      }
    68    }
    69    examples: {
    70      eg2: {
    71        in: {
    72          foo: 5
    73        }
    74      }
    75    }
    76    examples: {
    77      eg3: {
    78        in: {
    79          foo: 5
    80          bar: "test"
    81        }
    82      }
    83    }
    84  }
    85  -- out/eval/stats --
    86  Leaks:  15
    87  Freed:  105
    88  Reused: 99
    89  Allocs: 21
    90  Retain: 37
    91  
    92  Unifications: 82
    93  Conjuncts:    246
    94  Disjuncts:    139
    95  -- out/eval --
    96  (struct){
    97    #TypeA: ((string|struct)){ |((string){ string }, (#struct){
    98        foo: (int){ int }
    99      }) }
   100    #fun1: (#struct){
   101      _in(:x): ((string|struct)){ |((string){ string }, (#struct){
   102          foo: (int){ int }
   103        }) }
   104      out: (string){ "impossible" }
   105    }
   106    examples: (struct){
   107      eg1: (struct){
   108        in: (string){ "test" }
   109        res: (string){ "test" }
   110      }
   111      eg2: (struct){
   112        in: (struct){
   113          foo: (int){ 5 }
   114        }
   115        res: (string){ "foo" }
   116      }
   117      eg3: (struct){
   118        in: (struct){
   119          foo: (int){ 5 }
   120          bar: (string){ "test" }
   121        }
   122        res: (string){ "impossible" }
   123      }
   124    }
   125  }