@@ -2078,3 +2078,88 @@ func TestIndexerList(t *testing.T) {
20782078 })
20792079 }
20802080}
2081+
2082+ func TestUnionList (t * testing.T ) {
2083+ tests := []struct {
2084+ name string
2085+ cql string
2086+ wantModel model.IExpression
2087+ wantResult result.Value
2088+ }{
2089+ {
2090+ name : "Union on two equal lists" ,
2091+ cql : "{'a', 'b'} union {'a', 'b'}" ,
2092+ wantModel : & model.Union {
2093+ BinaryExpression : & model.BinaryExpression {
2094+ Expression : model .ResultType (& types.List {ElementType : types .String }),
2095+ Operands : []model.IExpression {
2096+ model .NewList ([]string {"a" , "b" }, types .String ),
2097+ model .NewList ([]string {"a" , "b" }, types .String ),
2098+ },
2099+ },
2100+ },
2101+ wantResult : newOrFatal (t , result.List {Value : []result.Value {newOrFatal (t , "a" ), newOrFatal (t , "b" )}, StaticType : & types.List {ElementType : types .String }}),
2102+ },
2103+ {
2104+ name : "Union where right is a superset of left" ,
2105+ cql : "{1} union {1, 2}" ,
2106+ wantResult : newOrFatal (t , result.List {Value : []result.Value {newOrFatal (t , int32 (1 )), newOrFatal (t , int32 (2 ))}, StaticType : & types.List {ElementType : types .Integer }}),
2107+ },
2108+ {
2109+ name : "Union where left is a superset of right" ,
2110+ cql : "{1, 2} union {1}" ,
2111+ wantResult : newOrFatal (t , result.List {Value : []result.Value {newOrFatal (t , int32 (1 )), newOrFatal (t , int32 (2 ))}, StaticType : & types.List {ElementType : types .Integer }}),
2112+ },
2113+ {
2114+ name : "Union with both empty lists" ,
2115+ cql : "{} union {}" ,
2116+ wantResult : newOrFatal (t , result.List {Value : []result.Value {}, StaticType : & types.List {ElementType : types .Any }}),
2117+ },
2118+ {
2119+ name : "Union with right null" ,
2120+ cql : "{1, 2} union null" ,
2121+ wantResult : newOrFatal (t , result.List {Value : []result.Value {newOrFatal (t , int32 (1 )), newOrFatal (t , int32 (2 ))}, StaticType : & types.List {ElementType : types .Integer }}),
2122+ },
2123+ {
2124+ name : "Union with left null" ,
2125+ cql : "null union {1, 2}" ,
2126+ wantResult : newOrFatal (t , result.List {Value : []result.Value {newOrFatal (t , int32 (1 )), newOrFatal (t , int32 (2 ))}, StaticType : & types.List {ElementType : types .Integer }}),
2127+ },
2128+ {
2129+ name : "Union with both null" ,
2130+ cql : "null as List<Integer> union null as List<Integer>" ,
2131+ wantResult : newOrFatal (t , result.List {Value : []result.Value {}, StaticType : & types.List {ElementType : types .Any }}),
2132+ },
2133+ {
2134+ name : "Union with symbolic operator" ,
2135+ cql : "{1, 2} | {1}" ,
2136+ wantResult : newOrFatal (t , result.List {Value : []result.Value {newOrFatal (t , int32 (1 )), newOrFatal (t , int32 (2 ))}, StaticType : & types.List {ElementType : types .Integer }}),
2137+ },
2138+ {
2139+ name : "Union functional form" ,
2140+ cql : "Union({1, 2}, {1})" ,
2141+ wantResult : newOrFatal (t , result.List {Value : []result.Value {newOrFatal (t , int32 (1 )), newOrFatal (t , int32 (2 ))}, StaticType : & types.List {ElementType : types .Integer }}),
2142+ },
2143+ }
2144+ for _ , tc := range tests {
2145+ t .Run (tc .name , func (t * testing.T ) {
2146+ p := newFHIRParser (t )
2147+ parsedLibs , err := p .Libraries (context .Background (), wrapInLib (t , tc .cql ), parser.Config {})
2148+ if err != nil {
2149+ t .Fatalf ("Parse returned unexpected error: %v" , err )
2150+ }
2151+ if diff := cmp .Diff (tc .wantModel , getTESTRESULTModel (t , parsedLibs )); tc .wantModel != nil && diff != "" {
2152+ t .Errorf ("Parse diff (-want +got):\n %s" , diff )
2153+ }
2154+
2155+ results , err := interpreter .Eval (context .Background (), parsedLibs , defaultInterpreterConfig (t , p ))
2156+ if err != nil {
2157+ t .Fatalf ("Eval returned unexpected error: %v" , err )
2158+ }
2159+ if diff := cmp .Diff (tc .wantResult , getTESTRESULT (t , results ), protocmp .Transform ()); diff != "" {
2160+ t .Errorf ("Eval diff (-want +got)\n %v" , diff )
2161+ }
2162+
2163+ })
2164+ }
2165+ }
0 commit comments