@@ -1848,6 +1848,88 @@ func TestIntervalContains(t *testing.T) {
18481848 }
18491849}
18501850
1851+ func TestIntervalWidth (t * testing.T ) {
1852+ tests := []struct {
1853+ name string
1854+ cql string
1855+ wantModel model.IExpression
1856+ wantResult result.Value
1857+ }{
1858+ {
1859+ name : "width of inclusive interval" ,
1860+ cql : "width of Interval[1, 4]" ,
1861+ wantModel : & model.Width {
1862+ UnaryExpression : & model.UnaryExpression {
1863+ Operand : & model.Interval {
1864+ Low : model .NewLiteral ("1" , types .Integer ),
1865+ High : model .NewLiteral ("4" , types .Integer ),
1866+ LowInclusive : true ,
1867+ HighInclusive : true ,
1868+ Expression : model .ResultType (& types.Interval {PointType : types .Integer }),
1869+ },
1870+ Expression : model .ResultType (types .Integer ),
1871+ },
1872+ },
1873+ wantResult : newOrFatal (t , 3 ),
1874+ },
1875+ {
1876+ name : "width of interval non-inclusive bounds" ,
1877+ cql : "width of Interval(1, 4)" ,
1878+ wantResult : newOrFatal (t , 1 ),
1879+ },
1880+ {
1881+ name : "width of interval with null bounds" ,
1882+ cql : "width of Interval(null, 4)" ,
1883+ wantResult : newOrFatal (t , nil ),
1884+ },
1885+ {
1886+ name : "width of interval with null bounds" ,
1887+ cql : "width of Interval(1, null)" ,
1888+ wantResult : newOrFatal (t , nil ),
1889+ },
1890+ {
1891+ name : "width of interval with null bounds" ,
1892+ cql : "width of Interval(null as Integer, null as Integer)" ,
1893+ wantResult : newOrFatal (t , nil ),
1894+ },
1895+ {
1896+ name : "width of interval with decimal" ,
1897+ cql : "Round(width of Interval[1.0, 4.0], 1)" ,
1898+ wantResult : newOrFatal (t , 3.0 ),
1899+ },
1900+ {
1901+ name : "width of interval with long" ,
1902+ cql : "width of Interval[1L, 4L]" ,
1903+ wantResult : newOrFatal (t , int64 (3 )),
1904+ },
1905+ {
1906+ name : "width of interval with quantity" ,
1907+ cql : "width of Interval[1'cm', 4'cm']" ,
1908+ wantResult : newOrFatal (t , result.Quantity {Value : 3 , Unit : "cm" }),
1909+ },
1910+ }
1911+ for _ , tc := range tests {
1912+ t .Run (tc .name , func (t * testing.T ) {
1913+ p := newFHIRParser (t )
1914+ parsedLibs , err := p .Libraries (context .Background (), wrapInLib (t , tc .cql ), parser.Config {})
1915+ if err != nil {
1916+ t .Fatalf ("Parse returned unexpected error: %v" , err )
1917+ }
1918+ if diff := cmp .Diff (tc .wantModel , getTESTRESULTModel (t , parsedLibs )); tc .wantModel != nil && diff != "" {
1919+ t .Errorf ("Parse diff (-want +got):\n %s" , diff )
1920+ }
1921+
1922+ results , err := interpreter .Eval (context .Background (), parsedLibs , defaultInterpreterConfig (t , p ))
1923+ if err != nil {
1924+ t .Fatalf ("Eval returned unexpected error: %v" , err )
1925+ }
1926+ if diff := cmp .Diff (tc .wantResult , getTESTRESULT (t , results ), protocmp .Transform ()); diff != "" {
1927+ t .Errorf ("Eval diff (-want +got)\n %v" , diff )
1928+ }
1929+ })
1930+ }
1931+ }
1932+
18511933func TestComparison_Error (t * testing.T ) {
18521934 tests := []struct {
18531935 name string
0 commit comments