blob: 193a6efed136c50efcdec5289743525557367891 [file] [log] [blame]
// Copyright 2018 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cue
import (
"flag"
"testing"
)
var traceOn = flag.Bool("debug", false, "enable tracing")
func compileFileWithErrors(t *testing.T, body string) (*context, *structLit, error) {
t.Helper()
ctx, inst, err := compileInstance(t, body)
return ctx, inst.rootValue.evalPartial(ctx).(*structLit), err
}
func compileFile(t *testing.T, body string) (*context, *structLit) {
t.Helper()
ctx, inst, errs := compileInstance(t, body)
if errs != nil {
t.Fatal(errs)
}
return ctx, inst.rootValue.evalPartial(ctx).(*structLit)
}
func compileInstance(t *testing.T, body string) (*context, *Instance, error) {
t.Helper()
var r Runtime
inst, err := r.Parse("test", body)
if err != nil {
x := newIndex().NewInstance(nil)
ctx := x.newContext()
return ctx, x, err
}
return r.index().newContext(), inst, nil
}
func rewriteHelper(t *testing.T, cases []testCase, r rewriteMode) {
t.Helper()
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
t.Helper()
ctx, obj := compileFile(t, tc.in)
ctx.trace = *traceOn
root := testResolve(ctx, obj, r)
got := debugStr(ctx, root)
// Copy the result
if got != tc.out {
fn := t.Errorf
if tc.skip {
fn = t.Skipf
}
fn("output differs:\ngot %s\nwant %s", got, tc.out)
}
})
}
}
type testCase struct {
desc string
in string
out string
skip bool
}
func TestBasicRewrite(t *testing.T) {
testCases := []testCase{{
desc: "errors",
in: `
a: _|_ & _|_
b: null & _|_
c: b.a == _|_
d: _|_ != b.a
e: _|_ == _|_
`,
out: `<0>{a: _|_(from source), b: _|_(from source), c: true, d: false, e: true}`,
}, {
desc: "regexp",
in: `
c1: "a" =~ "a"
c2: "foo" =~ "[a-z]{3}"
c3: "foo" =~ "[a-z]{4}"
c4: "foo" !~ "[a-z]{4}"
b1: =~ "a"
b1: "a"
b2: =~ "[a-z]{3}"
b2: "foo"
b3: =~ "[a-z]{4}"
b3: "foo"
b4: !~ "[a-z]{4}"
b4: "foo"
s1: != "b" & =~"c" // =~"c"
s2: != "b" & =~"[a-z]" // != "b" & =~"[a-z]"
e1: "foo" =~ 1
e2: "foo" !~ true
e3: != "a" & <5
`,
out: `<0>{c1: true, ` +
`c2: true, ` +
`c3: false, ` +
`c4: true, ` +
`b1: "a", ` +
`b2: "foo", ` +
`b3: _|_((=~"[a-z]{4}" & "foo"):"foo" does not match =~"[a-z]{4}"), ` +
`b4: "foo", ` +
`s1: =~"c", ` +
`s2: (!="b" & =~"[a-z]"), ` +
`e1: _|_(("foo" =~ 1):unsupported op =~(string, int)), ` +
`e2: _|_(("foo" !~ true):unsupported op !~(string, bool)), ` +
`e3: _|_((!="a" & <5):unsupported op &(string, number))}`,
}, {
desc: "arithmetic",
in: `
i1: 1 & int
i2: 2 & int
sum: -1 + +2 // 1
div1: 2.0 / 3 * 6 // 4
div2: 2 / 3 * 6 // 4
rem: 2 % 3 // 2
b: 1 != 4
v1: 1.0T/2.0
v2: 2.0 == 2
v3: 2.0/3.0
v4: 2.0%3.0
v5: i1 div i2
e0: 2 + "a"
// these are now all alloweed
// e1: 2.0 / i1
// e2: i1 / 2.0
// e3: 3.0 % i2
// e4: i1 % 2.0
e5: 1.0 div 2
e6: 2 rem 2.0
e7: 2 quo 2.0
e8: 1.0 mod 1
`,
out: `<0>{i1: 1, i2: 2, ` +
`sum: 1, ` +
`div1: 4.00000000000000000000000, ` +
`div2: 4.00000000000000000000000, ` +
`rem: 2, ` +
`b: true, ` +
`v1: 5e+11, ` +
`v2: true, ` +
`v3: 0.666666666666666666666667, ` +
`v4: 2.0, ` +
`v5: 0, ` +
`e0: _|_((2 + "a"):unsupported op +(int, string)), ` +
// `e1: _|_((2.0 / 1):unsupported op /(float, int)), ` +
// `e2: _|_((1 / 2.0):unsupported op /(int, float)), ` +
// `e3: _|_((3.0 % 2):unsupported op %(float, int)), ` +
// `e4: _|_((1 % 2.0):unsupported op %(int, float)), ` +
`e5: _|_((1.0 div 2):unsupported op div(float, int)), ` +
`e6: _|_((2 rem 2.0):unsupported op rem(int, float)), ` +
`e7: _|_((2 quo 2.0):unsupported op quo(int, float)), ` +
`e8: _|_((1.0 mod 1):unsupported op mod(float, int))}`,
}, {
desc: "integer-specific arithmetic",
in: `
q1: 5 quo 2 // 2
q2: 5 quo -2 // -2
q3: -5 quo 2 // -2
q4: -5 quo -2 // 2
qe1: 2.0 quo 1
qe2: 2 quo 1.0
r1: 5 rem 2 // 1
r2: 5 rem -2 // 1
r3: -5 rem 2 // -1
r4: -5 rem -2 // -1
re1: 2.0 rem 1
re2: 2 rem 1.0
d1: 5 div 2 // 2
d2: 5 div -2 // -2
d3: -5 div 2 // -3
d4: -5 div -2 // 3
de1: 2.0 div 1
de2: 2 div 1.0
m1: 5 mod 2 // 1
m2: 5 mod -2 // 1
m3: -5 mod 2 // 1
m4: -5 mod -2 // 1
me1: 2.0 mod 1
me2: 2 mod 1.0
// TODO: handle divide by zero
`,
out: `<0>{q1: 2, q2: -2, q3: -2, q4: 2, ` +
`qe1: _|_((2.0 quo 1):unsupported op quo(float, int)), ` +
`qe2: _|_((2 quo 1.0):unsupported op quo(int, float)), ` +
`r1: 1, r2: 1, r3: -1, r4: -1, re1: ` +
`_|_((2.0 rem 1):unsupported op rem(float, int)), ` +
`re2: _|_((2 rem 1.0):unsupported op rem(int, float)), ` +
`d1: 2, d2: -2, d3: -3, d4: 3, ` +
`de1: _|_((2.0 div 1):unsupported op div(float, int)), ` +
`de2: _|_((2 div 1.0):unsupported op div(int, float)), ` +
`m1: 1, m2: 1, m3: 1, m4: 1, ` +
`me1: _|_((2.0 mod 1):unsupported op mod(float, int)), ` +
`me2: _|_((2 mod 1.0):unsupported op mod(int, float))}`,
}, {
desc: "booleans",
in: `
t: true
t: !false
f: false
f: !t
e: true
e: !true
`,
out: "<0>{t: true, f: false, e: _|_(true:conflicting values: true != false)}",
}, {
desc: "boolean arithmetic",
in: `
a: true && true
b: true || false
c: false == true
d: false != true
e: true & true
f: true & false
`,
out: "<0>{a: true, b: true, c: false, d: true, e: true, f: _|_(true:conflicting values: true != false)}",
}, {
desc: "basic type",
in: `
a: 1 & int
b: number & 1
c: 1.0
c: float
d: int & float // _|_
e: "4" & string
f: true
f: bool
`,
out: `<0>{a: 1, b: 1, c: 1.0, d: _|_((int & float):unsupported op &(int, float)), e: "4", f: true}`,
}, {
desc: "strings and bytes",
in: `
s0: "foo" + "bar"
s1: 3 * "abc"
s2: "abc" * 2
b0: 'foo' + 'bar'
b1: 3 * 'abc'
b2: 'abc' * 2
// TODO: consider the semantics of this and perhaps allow this.
e0: "a" + ''
e1: 'b' + "c"
`,
out: `<0>{` +
`s0: "foobar", ` +
`s1: "abcabcabc", ` +
`s2: "abcabc", ` +
`b0: 'foobar', ` +
`b1: 'abcabcabc', ` +
`b2: 'abcabc', ` +
`e0: _|_(("a" + ''):unsupported op +(string, bytes)), ` +
`e1: _|_(('b' + "c"):unsupported op +(bytes, string))` +
`}`,
}, {
desc: "escaping",
in: `
a: "foo\nbar",
b: a,
// TODO: mimic http://exploringjs.com/es6/ch_template-literals.html#sec_introduction-template-literals
`,
out: `<0>{a: "foo\nbar", b: "foo\nbar"}`,
// out: `<0>{a: "foo\nbar", b: <0>.a}`,
}, {
desc: "reference",
in: `
a: b
b: 2
d: {
d: 3
e: d
}
e: {
e: {
v: 1
}
f: {
v: e.v
}
}
`,
out: "<0>{a: 2, b: 2, d: <1>{d: 3, e: 3}, e: <2>{e: <3>{v: 1}, f: <4>{v: 1}}}",
}, {
desc: "lists",
in: `
list: [1,2,3]
index: [1,2,3][1]
unify: [1,2,3] & [_,2,3]
e: [] & 4
e2: [3]["d"]
e3: [3][-1]
e4: [1, 2, ...>=4 & <=5] & [1, 2, 4, 8]
e5: [1, 2, 4, 8] & [1, 2, ...>=4 & <=5]
`,
out: `<0>{list: [1,2,3], index: 2, unify: [1,2,3], e: _|_(([] & 4):unsupported op &(list, int)), e2: _|_("d":invalid list index "d" (type string)), e3: _|_(-1:invalid list index -1 (index must be non-negative)), e4: [1,2,4,_|_((<=5 & 8):8 not within bound <=5)], e5: [1,2,4,_|_((<=5 & 8):8 not within bound <=5)]}`,
}, {
desc: "list arithmetic",
in: `
list: [1,2,3]
mul0: list*0
mul1: list*1
mul2: 2*list
list1: [1]
mul1_0: list1*0
mul1_1: 1*list1
mul1_2: list1*2
e: list*-1
`,
out: `<0>{list: [1,2,3], ` +
`mul0: [], ` +
`mul1: [1,2,3], ` +
`mul2: [1,2,3,1,2,3], ` +
`list1: [1], ` +
`mul1_0: [], ` +
`mul1_1: [1], ` +
`mul1_2: [1,1], ` +
`e: _|_((<1>.list * -1):negative number -1 multiplies list)}`,
}, {
desc: "selecting",
in: `
obj: {a: 1, b: 2}
index: {a: 1, b: 2}["b"]
mulidx: {a: 1, b: {a:1, b: 3}}["b"]["b"]
e: {a: 1}[4]
f: {a: 1}.b
g: {a: 1}["b"]
h: [3].b
`,
out: `<0>{obj: <1>{a: 1, b: 2}, index: 2, mulidx: 3, e: _|_(4:invalid struct index 4 (type int)), f: _|_(<2>{a: 1}.b:undefined field "b"), g: _|_(<3>{a: 1}["b"]:undefined field "b"), h: _|_([3]:invalid operation: [3].b (type list does not support selection))}`,
}, {
desc: "obj unify",
in: `
o1: {a: 1 } & { b: 2} // {a:1,b:2}
o2: {a: 1, b:2 } & { b: 2} // {a:1,b:2}
o3: {a: 1 } & { a:1, b: 2} // {a:1,b:2}
o4: {a: 1 } & { b: 2} // {a:1,b:2}
o4: {a: 1, b:2 } & { b: 2}
o4: {a: 1 } & { a:1, b: 2}
e: 1 // 1 & {a:3}
e: {a:3}
`,
out: "<0>{o1: <1>{a: 1, b: 2}, o2: <2>{a: 1, b: 2}, o3: <3>{a: 1, b: 2}, o4: <4>{a: 1, b: 2}, e: _|_((1 & <5>{a: 3}):unsupported op &(int, struct))}",
}, {
desc: "disjunctions",
in: `
o1: 1 | 2 | 3
o2: (1 | 2 | 3) & 1
o3: 2 & (1 | *2 | 3)
o4: (1 | *2 | 3) & (1 | 2 | *3)
o5: (1 | *2 | 3) & (3 | *2 | 1)
o6: (1 | 2 | 3) & (3 | 1 | 2)
o7: (1 | 2 | 3) & (2 | 3)
o8: (1 | 2 | 3) & (3 | 2)
o9: (2 | 3) & (1 | 2 | 3)
o10: (3 | 2) & (1 | *2 | 3)
m1: (*1 | (*2 | 3)) & (>=2 & <=3)
m2: (*1 | (*2 | 3)) & (2 | 3)
m3: (*1 | *(*2 | 3)) & (2 | 3)
m4: (2 | 3) & (*2 | 3)
m5: (*2 | 3) & (2 | 3)
// (*2 | 3) & (2 | 3)
// (2 | 3) & (*2 | 3)
// 2&(*2 | 3) | 3&(*2 | 3)
// (*1 | (*2 | 3)) & (2 | 3)
// *1& (2 | 3) | (*2 | 3)&(2 | 3)
// *2&(2 | 3) | 3&(2 | 3)
// (2 | 3)&(*1 | (*2 | 3))
// 2&(*1 | (*2 | 3)) | 3&(*1 | (*2 | 3))
// *1&2 | (*2 | 3)&2 | *1&3 | (*2 | 3)&3
// (*2 | 3)&2 | (*2 | 3)&3
// *2 | 3
// All errors are treated the same as per the unification model.
i1: [1, 2][3] | "c"
`,
out: `<0>{o1: (1 | 2 | 3), o2: 1, o3: 2, o4: (1 | 2 | 3 | *_|_), o5: (1 | *2 | 3), o6: (1 | 2 | 3), o7: (2 | 3), o8: (2 | 3), o9: (2 | 3), o10: (3 | *2), m1: (*2 | 3), m2: (*2 | 3), m3: (*2 | 3), m4: (*2 | 3), m5: (*2 | 3), i1: "c"}`,
}, {
desc: "types",
in: `
i: int
j: int & 3
s: string
t: "s" & string
e: int & string
e2: 1 & string
b: !int
p: +true
m: -false
`,
out: `<0>{i: int, j: 3, s: string, t: "s", e: _|_((int & string):unsupported op &(int, string)), e2: _|_((1 & string):unsupported op &(int, string)), b: _|_(!int:unary '!' requires bool value, found int), p: _|_(+true:unary '+' requires numeric value, found bool), m: _|_(-false:unary '-' requires numeric value, found bool)}`,
}, {
desc: "comparison",
in: `
lss: 1 < 2
leq: 1 <= 1.0
leq: 2.0 <= 3
eql: 1 == 1.0
neq: 1.0 == 1
gtr: !(2 > 3)
geq: 2.0 >= 2
seq: "a" + "b" == "ab"
err: 2 == "s"
`,
out: `<0>{lss: true, leq: true, eql: true, neq: true, gtr: true, geq: true, seq: true, err: _|_((2 == "s"):unsupported op ==(int, string))}`,
}, {
desc: "null",
in: `
eql: null == null
neq: null != null
unf: null & null
// errors
eq1: null == 1
eq2: 1 == null
ne1: "s" != null
call: null()
`,
out: `<0>{eql: true, neq: false, unf: null, eq1: false, eq2: false, ne1: true, call: _|_(null:cannot call non-function null (type null))}`,
}, {
desc: "self-reference cycles",
in: `
a: b - 100
b: a + 100
c: [c[1], c[0]]
`,
out: `<0>{a: _|_((<1>.b - 100):cycle detected), ` +
`b: _|_((<1>.a + 100):cycle detected), ` +
`c: [_|_(<1>.c[1]:cycle detected),_|_(<1>.c[0]:cycle detected)]}`,
}, {
desc: "resolved self-reference cycles",
in: `
a: b - 100
b: a + 100
b: 200
c: [c[1], a]
s1: s2 & {a: 1}
s2: s3 & {b: 2}
s3: s1 & {c: 3}
`,
out: `<0>{a: 100, b: 200, c: [100,100], s1: <1>{a: 1, b: 2, c: 3}, s2: <2>{a: 1, b: 2, c: 3}, s3: <3>{a: 1, b: 2, c: 3}}`,
}, {
desc: "resolved self-reference cycles: Issue 19",
in: `
// CUE knows how to resolve the following:
x: y + 100
y: x - 100
x: 200
z1: z2 + 1
z2: z3 + 2
z3: z1 - 3
z3: 8
// TODO: extensive tests with disjunctions.
`,
out: `<0>{x: 200, y: 100, z1: 11, z2: 10, z3: 8}`,
}, {
desc: "delayed constraint failure",
in: `
a: b - 100
b: a + 110
b: 200
x: 100
x: x + 1
`,
out: `<0>{` +
`a: _|_((210 & 200):conflicting values: 210 != 200), ` +
`b: _|_((210 & 200):conflicting values: 210 != 200), ` +
`x: _|_((100 & 101):conflicting values: 100 != 101)}`,
// TODO: find a way to mark error in data.
}}
rewriteHelper(t, testCases, evalPartial)
}
func TestChooseFirst(t *testing.T) {
testCases := []testCase{{
desc: "pick first",
in: `
a: *5 | "a" | true
b c: *{
a: 2
} | {
a : 3
}
`,
out: "<0>{a: 5, b: <1>{c: <2>{a: 2}}}",
}, {
desc: "simple disambiguation conflict",
in: `
a: *"a" | "b"
b: *"b" | "a"
c: a & b
`,
out: `<0>{a: "a", b: "b", c: _|_(("a" | "b" | *_|_):more than one element remaining ("a" and "b"))}`,
}, {
desc: "associativity of defaults",
in: `
a: *"a" | ("b" | "c")
b: (*"a" | "b") | "c"
c: *"a" | (*"b" | "c")
x: a & b
y: b & c
`,
out: `<0>{a: "a", b: "a", c: _|_((*"a" | *"b" | "c"):more than one default remaining ("a" and "b")), x: "a", y: _|_((*"a" | *"b" | "c"):more than one default remaining ("a" and "b"))}`,
}}
rewriteHelper(t, testCases, evalFull)
}
func TestResolve(t *testing.T) {
testCases := []testCase{{
in: `
a: b.c.d
b c: { d: 3 }
c: { c: d.d, }
d: { d: 2 }
`,
out: "<0>{a: 3, b: <1>{c: <2>{d: 3}}, c: <3>{c: 2}, d: <4>{d: 2}}",
}, {
in: `
a: _
b: a
a: { d: 1, d: _ }
b: _
`,
out: `<0>{a: <1>{d: 1}, b: <2>{d: 1}}`,
}, {
desc: "JSON",
in: `
"a": 3
b: a
o: { "a\nb": 2 } // TODO: use $ for root?
c: o["a\nb"]
`,
out: `<0>{a: 3, b: 3, o: <1>{"a\nb": 2}, c: 2}`,
}, {
desc: "arithmetic",
in: `
v1: 1.0T/2.0
v2: 2.0 == 2
n1: 1
v5: 2.0 / n1
e1: 2.0 % (3&int)
e2: int & 4.0/2.0
`,
out: `<0>{v1: 5e+11, v2: true, n1: 1, v5: 2, e1: 2.0, e2: _|_((int & 2):unsupported op &(int, float))}`,
}, {
desc: "inequality",
in: `
a: 1 != 2
b: 1 != null
c: true == null
d: null != {}
e: null == []
f: 0 == 0.0 // types are unified first TODO: make this consistent
`,
out: `<0>{a: true, b: true, c: false, d: true, e: false, f: true}`,
}, {
desc: "attributes",
in: `
a: { foo: 1 @foo() @baz(1) }
b: { foo: 1 @bar() @foo() }
c: a & b
e: a & { foo: 1 @foo(other) }
`,
out: `<0>{a: <1>{foo: 1 @baz(1) @foo()}, ` +
`b: <2>{foo: 1 @bar() @foo()}, ` +
`c: <3>{foo: 1 @bar() @baz(1) @foo()}, ` +
`e: _|_((<4>.a & <5>{foo: 1 @foo(other)}):conflicting attributes for key "foo")}`,
}, {
desc: "optional fields",
in: `
a: { foo?: string }
b: { foo: "foo" }
c: a & b
d: a & { "foo"?: "bar" }
g1: 1
"g\(1)"?: 1
"g\(2)"?: 2
`,
out: `<0>{a: <1>{foo?: string}, ` +
`b: <2>{foo: "foo"}, ` +
`c: <3>{foo: "foo"}, ` +
`d: <4>{foo?: "bar"}, ` +
`g1: 1, ` +
`g2?: 2}`,
}, {
desc: "bounds",
in: `
i1: >1 & 5
i2: (>=0 & <=10) & 5
i3: !=null & []
i4: !=2 & !=4
s1: >=0 & <=10 & !=1 // no simplification
s2: >=0 & <=10 & !=11 // >=0 & <=10
s3: >5 & !=5 // >5
s4: <10 & !=10 // <10
s5: !=2 & !=2
// TODO: could change inequality
s6: !=2 & >=2
s7: >=2 & !=2
s8: !=5 & >5
s10: >=0 & <=10 & <12 & >1 // >1 & <=10
s11: >0 & >=0 & <=12 & <12 // >0 & <12
s20: >=10 & <=10 // 10
s22: >5 & <=6 // no simplification
s22a: >5 & (<=6 & int) // 6
s22b: (int & >5) & <=6 // 6
s22c: >=5 & (<6 & int) // 5
s22d: (int & >=5) & <6 // 5
s22e: (>=5 & <6) & int // 5
s22f: int & (>=5 & <6) // 5
s23: >0 & <2 // no simplification
s23a: (>0 & <2) & int // int & 1
s23b: int & (>0 & <2) // int & 1
s23c: (int & >0) & <2 // int & 1
s23d: >0 & (int & <2) // int & 1
s23e: >0.0 & <2.0 // no simplification
s30: >0 & int
e1: null & !=null
e2: !=null & null
e3: >1 & 1
e4: <0 & 0
e5: >1 & <0
e6: >11 & <11
e7: >=11 & <11
e8: >11 & <=11
e9: >"a" & <1
`,
out: `<0>{i1: 5, i2: 5, i3: [], i4: (!=2 & !=4), ` +
`s1: (>=0 & <=10 & !=1), ` +
`s2: (>=0 & <=10), ` +
`s3: >5, ` +
`s4: <10, ` +
`s5: !=2, ` +
`s6: (!=2 & >=2), ` +
`s7: (>=2 & !=2), ` +
`s8: >5, ` +
`s10: (<=10 & >1), ` +
`s11: (>0 & <12), ` +
`s20: 10, ` +
`s22: (>5 & <=6), ` +
`s22a: 6, ` +
`s22b: 6, ` +
`s22c: 5, ` +
`s22d: 5, ` +
`s22e: 5, ` +
`s22f: 5, ` +
`s23: (>0 & <2), ` +
`s23a: 1, ` +
`s23b: 1, ` +
`s23c: 1, ` +
`s23d: 1, ` +
`s23e: (>0.0 & <2.0), ` +
`s30: int & >0, ` +
`e1: _|_((!=null & null):null excluded by !=null), ` +
`e2: _|_((!=null & null):null excluded by !=null), ` +
`e3: _|_((>1 & 1):1 not within bound >1), ` +
`e4: _|_((<0 & 0):0 not within bound <0), ` +
`e5: _|_(incompatible bounds >1 and <0), ` +
`e6: _|_(incompatible bounds >11 and <11), ` +
`e7: _|_(incompatible bounds >=11 and <11), ` +
`e8: _|_(incompatible bounds >11 and <=11), ` +
`e9: _|_((>"a" & <1):unsupported op &(string, number))}`,
}, {
desc: "custom validators",
in: `
import "strings"
a: strings.ContainsAny("ab")
a: "after"
b: strings.ContainsAny("c")
b: "dog"
`,
out: `<0>{` +
`a: "after", ` +
`b: _|_(builtin:ContainsAny ("c"):value "dog" not in ContainsAny("c"))` +
`}`,
}, {
desc: "null coalescing",
in: `
a: null
b: a.x | "b"
c: a["x"] | "c"
`,
out: `<1>{a: null, b: "b", c: "c"}`,
}, {
desc: "reference across tuples and back",
// Tests that it is okay to partially evaluate structs.
in: `
a: { c: b.e, d: b.f }
b: { e: 3, f: a.c }
`,
out: "<0>{a: <1>{c: 3, d: 3}, b: <2>{e: 3, f: 3}}",
}, {
desc: "index",
in: `
a: [2][0]
b: {foo:"bar"}["foo"]
c: (*l|{"3":3})["3"]
d: (*[]|[1])[0]
l: []
e1: [2][""]
e2: 2[2]
e3: [][true]
e4: [1,2,3][3]
e5: [1,2,3][-1]
e6: (*[]|{})[1]
`,
out: `<0>{a: 2, b: "bar", c: _|_("3":invalid list index "3" (type string)), l: [], d: _|_([]:index 0 out of bounds), e1: _|_("":invalid list index "" (type string)), e2: _|_(2:invalid operation: 2[2] (type int does not support indexing)), e3: _|_(true:invalid list index true (type bool)), e4: _|_([1,2,3]:index 3 out of bounds), e5: _|_(-1:invalid list index -1 (index must be non-negative)), e6: _|_([]:index 1 out of bounds)}`,
}, {
desc: "string index",
in: `
a0: "abc"[0]
a1: "abc"[1]
a2: "abc"[2]
a3: "abc"[3]
a4: "abc"[-1]
b: "zoëven"[2]
`,
out: `<0>{a0: "a", a1: "b", a2: "c", a3: _|_("abc":index 3 out of bounds), a4: _|_(-1:invalid string index -1 (index must be non-negative)), b: "ë"}`,
}, {
desc: "disjunctions of lists",
in: `
l: [ int, int ] | [ string, string ]
l1: [ "a", "b" ]
l2: l & [ "c", "d" ]
`,
out: `<0>{l: ([int,int] | [string,string]), l1: ["a","b"], l2: ["c","d"]}`,
}, {
desc: "slice",
in: `
a: [2][0:0]
b: [0][1:1]
e1: [][1:1]
e2: [0][-1:0]
e3: [0][1:0]
e4: [0][1:2]
e5: 4[1:2]
e6: [2]["":]
e7: [2][:"9"]
`,
out: `<0>{a: [], b: [], e1: _|_(1:slice bounds out of range), e2: _|_([0]:negative slice index), e3: _|_([0]:invalid slice index: 1 > 0), e4: _|_(2:slice bounds out of range), e5: _|_(4:cannot slice 4 (type int)), e6: _|_("":invalid slice index "" (type string)), e7: _|_("9":invalid slice index "9" (type string))}`,
}, {
desc: "string slice",
in: `
a0: ""[0:0]
a1: ""[:]
a2: ""[0:]
a3: ""[:0]
b0: "abc"[0:0]
b1: "abc"[0:1]
b2: "abc"[0:2]
b3: "abc"[0:3]
b4: "abc"[3:3]
b5: "abc"[1:]
b6: "abc"[:2]
// TODO: supported extended graphemes, instead of just runes.
u: "Spaß"[3:4]
`,
out: `<0>{a0: "", a1: "", a2: "", a3: "", b0: "", b1: "a", b2: "ab", b3: "abc", b4: "", b5: "bc", b6: "ab", u: "ß"}`,
}, {
desc: "list types",
in: `
l0: 3*[int]
l0: [1, 2, 3]
l2: [...{ a: int }]
l2: [{a: 1}, {a: 2, b: 3}]
// TODO: work out a decent way to specify length ranges of lists.
// l3: <=10*[int]
// l3: [1, 2, 3, ...]
s1: (6*[int])[2:3]
s2: [0,2,3][1:2]
i1: (6*[int])[2]
i2: [0,2,3][2]
t0: [...{a: 8}]
t0: [{}]
t1: [...]
t1: [...int]
e0: 2*[{}]
e0: [{}]
e1: [...int]
e1: [...float]
`,
out: `<0>{` +
`l0: [1,2,3], ` +
`l2: [<1>{a: 1},<2>{a: 2, b: 3}], ` +
`s1: [int], ` +
`s2: [2], ` +
`i1: int, ` +
`i2: 3, ` +
`t0: [<3>{a: 8}], ` +
`t1: [, ...int], ` +
`e0: _|_(([<4>{},<4>{}] & [<5>{}]):incompatible list lengths: conflicting values: 2 != 1), ` +
`e1: _|_(([, ...int] & [, ...float]):incompatible list types: unsupported op &(int, float): )` +
`}`,
}, {
// TODO: consider removing list arithmetic altogether. It is no longer
// needed to indicate the allowed capacity of a list and that didn't
// work anyway.
desc: "list arithmetic",
in: `
l0: 3*[1, 2, 3]
l1: 0*[1, 2, 3]
l2: 10*[]
l3: <=2*[]
l4: <=2*[int]
l5: <=2*(int*[int])
l6: 3*[...int]
l7: 3*[1, ...int]
l8: 3*[1, 2, ...int]
s0: [] + []
s1: [1] + []
s2: [] + [2]
s3: [1] + [2]
s4: [1,2] + []
s5: [] + [1,2]
s6: [1] + [1,2]
s7: [1,2] + [1]
s8: [1,2] + [1,2]
s9: [] + [...]
s10: [1] + [...]
s11: [] + [2, ...]
s12: [1] + [2, ...]
s13: [1,2] + [...]
s14: [] + [1,2, ...]
s15: [1] + [1,2, ...]
s16: [1,2] + [1, ...]
s17: [1,2] + [1,2, ...]
s18: [...] + []
s19: [1, ...] + []
s20: [...] + [2]
s21: [1, ...] + [2]
s22: [1,2, ...] + []
s23: [...] + [1,2]
s24: [1, ...] + [1,2]
s25: [1,2, ...] + [1]
s26: [1,2, ...] + [1,2]
s27: [...] + [...]
s28: [1, ...] + [...]
s29: [...] + [2, ...]
s30: [1, ...] + [2, ...]
s31: [1,2, ...] + [...]
s32: [...] + [1,2, ...]
s33: [1, ...] + [1,2, ...]
s34: [1,2, ...] + [1, ...]
s35: [1,2, ...] + [1,2, ...]
`,
out: `<0>{l0: [1,2,3,1,2,3,1,2,3], ` +
`l1: [], ` +
`l2: [], ` +
`l3: (<=2 * []), ` +
`l4: (<=2 * [int]), ` +
`l5: (<=2 * (int * [int])), ` +
`l6: [], ` +
`l7: [1,1,1], ` +
`l8: [1,2,1,2,1,2], ` +
`s0: [], ` +
`s1: [1], ` +
`s2: [2], ` +
`s3: [1,2], ` +
`s4: [1,2], ` +
`s5: [1,2], ` +
`s6: [1,1,2], ` +
`s7: [1,2,1], ` +
`s8: [1,2,1,2], ` +
`s9: [], ` +
`s10: [1], ` +
`s11: [2], ` +
`s12: [1,2], ` +
`s13: [1,2], ` +
`s14: [1,2], ` +
`s15: [1,1,2], ` +
`s16: [1,2,1], ` +
`s17: [1,2,1,2], ` +
`s18: [], ` +
`s19: [1], ` +
`s20: [2], ` +
`s21: [1,2], ` +
`s22: [1,2], ` +
`s23: [1,2], ` +
`s24: [1,1,2], ` +
`s25: [1,2,1], ` +
`s26: [1,2,1,2], ` +
`s27: [], ` +
`s28: [1], ` +
`s29: [2], ` +
`s30: [1,2], ` +
`s31: [1,2], ` +
`s32: [1,2], ` +
`s33: [1,1,2], ` +
`s34: [1,2,1], ` +
`s35: [1,2,1,2]` +
`}`,
}, {
desc: "list equality",
in: `
eq0: [] == []
eq1: [...] == []
eq2: [] == [...]
eq3: [...] == [...]
eq4: [1] == [1]
eq5: [1, ...] == [1]
eq6: [1] == [1, ...]
eq7: [1, ...] == [1, ...]
eq8: [1, 2] == [1, 2]
eq9: [1, 2, ...] == [1, 2]
eq10: [1, 2] == [1, 2, ...]
eq11: [1, 2, ...] == [1, 2, ...]
ne0: [] != []
ne1: [...] != []
ne2: [] != [...]
ne3: [...] != [...]
ne4: [1] != [1]
ne5: [1, ...] != [1]
ne6: [1] != [1, ...]
ne7: [1, ...] != [1, ...]
ne8: [1, 2] != [1, 2]
ne9: [1, 2, ...] != [1, 2]
ne10: [1, 2] != [1, 2, ...]
ne11: [1, 2, ...] != [1, 2, ...]
feq0: [] == [1]
feq1: [...] == [1]
feq2: [] == [1, ...]
feq3: [...] == [1, ...]
feq4: [1] == []
feq5: [1, ...] == []
feq6: [1] == [...]
feq7: [1, ...] == [...]
feq8: [1, 2] == [1]
feq9: [1, ...] == [1, 2]
feq10: [1, 2] == [1, ...]
feq11: [1, ...] == [1, 2, ...]
fne0: [] != [1]
fne1: [...] != [1]
fne2: [] != [1, ...]
fne3: [1, ...] != [1, ...]
fne4: [1] != []
fne5: [1, ...] != []
fne6: [1] != [...]
fne7: [1, ...] != [...]
fne8: [1, 2] != [1]
fne9: [1, ...] != [1, 2]
fne10: [1, 2] != [1, ...]
fne11: [1, ...] != [1, 2, ...]
`,
out: `<0>{` +
`eq0: true, eq1: true, eq2: true, eq3: true, eq4: true, eq5: true, eq6: true, eq7: true, eq8: true, eq9: true, eq10: true, eq11: true, ` +
`ne0: true, ne1: true, ne2: true, ne3: true, ne4: false, ne5: false, ne6: false, ne7: false, ne8: false, ne9: false, ne10: false, ne11: false, ` +
`feq0: false, feq1: false, feq2: false, feq3: false, feq4: false, feq5: false, feq6: false, feq7: false, feq8: false, feq9: false, feq10: false, feq11: false, ` +
`fne0: false, fne1: false, fne2: false, fne3: false, fne4: false, fne5: false, fne6: false, fne7: false, fne8: false, fne9: false, fne10: false, fne11: false}`,
}, {
desc: "list unification",
in: `
a: { l: ["foo", v], v: l[1] }
b: a & { l: [_, "bar"] }
`,
out: `<0>{` +
`a: <1>{l: ["foo",_|_(<2>.v:cycle detected)], ` +
`v: _|_(<2>.l[1]:cycle detected)}, ` +
`b: <3>{l: ["foo","bar"], v: "bar"}}`,
}, {
desc: "correct error messages",
// Tests that it is okay to partially evaluate structs.
in: `
a: "a" & 1
`,
out: `<0>{a: _|_(("a" & 1):unsupported op &(string, int))}`,
}, {
desc: "structs",
in: `
a: t & { c: 5 } // {c:5,d:15}
b: ti & { c: 7 } // {c:7,d:21}
t: { c: number, d: c * 3 } // {c:number,d:number*3}
ti: t & { c: int }
`,
out: `<0>{a: <1>{c: 5, d: 15}, t: <2>{c: number, d: (<3>.c * 3)}, b: <4>{c: 7, d: 21}, ti: <5>{c: int, d: (<6>.c * 3)}}`,
}, {
desc: "reference to root",
in: `
a: { b: int }
c: a & {
b: 100
d: a.b + 3 // do not resolve as c != a.
}
x: {
b: int
c: b + 5
}
y: x & {
b: 100
// c should resolve to 105
}
v: {
b: int
c: v.b + 5 // reference starting from copied node.
}
w: v & { b: 100 }
wp: v & { b: 100 }
`,
out: `<0>{a: <1>{b: int}, c: <2>{b: 100, d: (<3>.a.b + 3)}, x: <4>{b: int, c: (<5>.b + 5)}, y: <6>{b: 100, c: 105}, v: <7>{b: int, c: (<8>.b + 5)}, w: <9>{b: 100, c: 105}, wp: <10>{b: 100, c: 105}}`,
}, {
desc: "references from template to concrete",
in: `
res: [t]
t <X>: {
a: c + b.str
b str: string
c: "X"
}
t x: { b str: "DDDD" }
`,
out: `<0>{res: [<1>{<>: <2>(X: string)-><3>{a: (<3>.c + <3>.b.str), c: "X", b: <4>{str: string}}, x: <5>{a: "XDDDD", c: "X", b: <6>{str: "DDDD"}}}], t: <7>{<>: <2>(X: string)-><3>{a: (<3>.c + <3>.b.str), c: "X", b: <4>{str: string}}, x: <8>{a: "XDDDD", c: "X", b: <9>{str: "DDDD"}}}}`,
}, {
desc: "interpolation",
in: `
a: "\(4)"
b: "one \(a) two \( a + c )"
c: "one"
d: "\(r)"
u: "\(_)"
r: _
e: "\([])"`,
out: `<0>{a: "4", b: "one 4 two 4one", c: "one", d: ""+<1>.r+"", r: _, u: ""+_+"", e: _|_([]:expression in interpolation must evaluate to a number kind or string (found list))}`,
}, {
desc: "multiline interpolation",
in: `
a1: """
before
\(4)
after
"""
a2: """
before
\(4)
"""
a3: """
\(4)
after
"""
a4: """
\(4)
"""
m1: """
before
\(
4)
after
"""
m2: """
before
\(
4)
"""
m3: """
\(
4)
after
"""
m4: """
\(
4)
"""
`,
out: `<0>{` +
`a1: "before\n4\nafter", a2: "before\n4\n", a3: "\n4\nafter", a4: "\n4\n", ` +
`m1: "before\n4\nafter", m2: "before\n4\n", m3: "\n4\nafter", m4: "\n4\n"` +
`}`,
}, {
desc: "diamond-shaped constraints",
in: `
S: {
A: {
a: 1,
},
B: A & {
b: 2,
}
},
T: S & { // S == { A: { a:1 }, B: { a:1, b:2 } }
A: {
c: 3,
},
B: { // S.B & A
d: 4, // Combines constraints S.A, S.B, T.A, and T.B
}
}`,
out: "<0>{S: <1>{A: <2>{a: 1}, B: <3>{a: 1, b: 2}}, T: <4>{A: <5>{a: 1, c: 3}, B: <6>{a: 1, b: 2, c: 3, d: 4}}}",
}, {
desc: "field templates",
in: `
a: {
<name>: int
k: 1
}
b: {
<x>: { x: 0, y: *1 | int }
v: {}
w: { x: 0 }
}
b: { <y>: {} } // TODO: allow different name
c: {
<Name>: { name: Name, y: 1 }
foo: {}
bar: _
}
`,
out: `<0>{a: <1>{<>: <2>(name: string)->int, k: 1}, b: <3>{<>: <4>(x: string)->(<5>{x: 0, y: (*1 | int)} & <6>{}), v: <7>{x: 0, y: (*1 | int)}, w: <8>{x: 0, y: (*1 | int)}}, c: <9>{<>: <10>(Name: string)-><11>{name: <10>.Name, y: 1}, foo: <12>{name: "foo", y: 1}, bar: <13>{name: "bar", y: 1}}}`,
}, {
desc: "range unification",
in: `
// with concrete values
a1: >=1 & <=5 & 3
a2: >=1 & <=5 & 1
a3: >=1 & <=5 & 5
a4: >=1 & <=5 & 6
a5: >=1 & <=5 & 0
a6: 3 & >=1 & <=5
a7: 1 & >=1 & <=5
a8: 5 & >=1 & <=5
a9: 6 & >=1 & <=5
a10: 0 & >=1 & <=5
// with ranges
b1: >=1 & <=5 & >=1 & <=5
b2: >=1 & <=5 & >=1 & <=1
b3: >=1 & <=5 & >=5 & <=5
b4: >=1 & <=5 & >=2 & <=3
b5: >=1 & <=5 & >=3 & <=9
b6: >=1 & <=5 & >=5 & <=9
b7: >=1 & <=5 & >=6 & <=9
b8: >=1 & <=5 & >=1 & <=5
b9: >=1 & <=1 & >=1 & <=5
b10: >=5 & <=5 & >=1 & <=5
b11: >=2 & <=3 & >=1 & <=5
b12: >=3 & <=9 & >=1 & <=5
b13: >=5 & <=9 & >=1 & <=5
b14: >=6 & <=9 & >=1 & <=5
// ranges with more general types
c1: int & >=1 & <=5
c2: >=1 & <=5 & int
c3: string & >=1 & <=5
c4: >=1 & <=5 & string
// other types
s1: >="d" & <="z" & "e"
s2: >="d" & <="z" & "ee"
n1: number & >=1 & <=2
n2: int & >=1.1 & <=1.3
n3: >=1.0 & <=3.0 & 2
n4: >=0.0 & <=0.1 & 0.09999
n5: >=1 & <=5 & 2.5
`,
out: `<0>{` +
`a1: 3, ` +
`a2: 1, ` +
`a3: 5, ` +
`a4: _|_((<=5 & 6):6 not within bound <=5), ` +
`a5: _|_((>=1 & 0):0 not within bound >=1), ` +
`a6: 3, ` +
`a7: 1, ` +
`a8: 5, ` +
// TODO: improve error
`a9: _|_((<=5 & 6):6 not within bound <=5), ` +
`a10: _|_((>=1 & 0):0 not within bound >=1), ` +
`b1: (>=1 & <=5), ` +
`b2: 1, ` +
`b3: 5, ` +
`b4: (>=2 & <=3), ` +
`b5: (>=3 & <=5), ` +
`b6: 5, ` +
`b7: _|_(incompatible bounds >=6 and <=5), ` +
`b8: (>=1 & <=5), ` +
`b9: 1, ` +
`b10: 5, ` +
`b11: (>=2 & <=3), ` +
`b12: (>=3 & <=5), ` +
`b13: 5, ` +
`b14: _|_(incompatible bounds >=6 and <=5), ` +
`c1: (int & >=1 & <=5), ` +
`c2: (<=5 & int & >=1), ` +
`c3: _|_((string & >=1):unsupported op &(string, number)), ` +
`c4: _|_(((>=1 & <=5) & string):unsupported op &(number, string)), ` +
`s1: "e", ` +
`s2: "ee", ` +
`n1: (>=1 & <=2), ` +
`n2: (int & >=1.1 & <=1.3), ` +
`n3: 2, ` +
`n4: 0.09999, ` +
`n5: 2.5}`,
}, {
desc: "predefined ranges",
in: `
k1: int8
k1: 44
k2: int64
k2: -8_000_000_000
e1: int16
e1: 100_000
`,
out: `<0>{k1: 44, k2: -8000000000, ` +
`e1: _|_((int & <=32767 & 100000):100000 not within bound int & <=32767)}`,
}, {
desc: "field comprehensions",
in: `
obj foo a: "bar"
obj <Name>: {
a: *"dummy" | string
sub as: a if true
}
`,
out: `<0>{obj: <1>{<>: <2>(Name: string)-><3>{a: (*"dummy" | string) if true yield ("sub"): <4>{as: <3>.a}}, ` +
`foo: <5>{a: "bar", sub: <6>{as: "bar"}}}}`,
}, {
desc: "builtins",
in: `
a1: {
a: and([b, c])
b: =~"oo"
c: =~"fo"
}
a2: a1 & { a: "foo" }
a3: a1 & { a: "bar" }
o1: {
a: or([b, c])
b: string
c: "bar"
}
o2: o1 & { a: "foo" }
o3: o1 & { a: "foo", b: "baz" }
`,
out: `<0>{` +
`a1: <1>{a: (=~"oo" & =~"fo"), b: =~"oo", c: =~"fo"}, ` +
`a2: <2>{a: "foo", b: =~"oo", c: =~"fo"}, ` +
`a3: <3>{a: _|_((=~"oo" & "bar"):"bar" does not match =~"oo"), b: =~"oo", c: =~"fo"}, ` +
`o1: <4>{a: string, b: string, c: "bar"}, ` +
`o2: <5>{a: "foo", b: string, c: "bar"}, ` +
`o3: <6>{a: _|_((builtin:or ([<7>.b,<7>.c]) & "foo"):empty disjunction: conflicting values: baz != foo), b: "baz", c: "bar"}}`,
}, {
desc: "self-reference cycles conflicts with strings",
in: `
a: {
x: y+"?"
y: x+"!"
}
a x: "hey"
`,
out: `<0>{a: <1>{x: _|_(("hey!?" & "hey"):conflicting values: hey!? != hey), y: "hey!"}}`,
}, {
desc: "resolved self-reference cycles with disjunctions",
in: `
a: b&{x:1} | {y:1} // {x:1,y:3,z:2} | {y:1}
b: {x:2} | c&{z:2} // {x:2} | {x:1,y:3,z:2}
c: a&{y:3} | {z:3} // {x:1,y:3,z:2} | {z:3}
`,
out: `<0>{a: (<1>{x: 1, y: 3, z: 2} | <2>{y: 1}), b: (<3>{x: 2} | <4>{x: 1, y: 3, z: 2}), c: (<5>{x: 1, y: 3, z: 2} | <6>{z: 3})}`,
}, {
// We take a very conservative stance on delaying arithmetic
// expressions within disjunctions. It should remain resolvable, though,
// once the user specifies one.
desc: "resolved self-reference cycles with disjunction",
in: `
// The second disjunct in xa1 is not resolvable and can be
// eliminated:
// xa4 & 9
// (xa2 + 2) & 9
// ((xa3 + 2) + 2) & 9
// (((6 & xa1-2) + 2) + 2) & 9
// ((6 + 2) + 2) & 9 // 6 == xa1-2
// 10 & 9 => _|_
// The remaining values resolve.
xa1: (xa2 & 8) | (xa4 & 9)
xa2: xa3 + 2
xa3: 6 & xa1-2
xa4: xa2 + 2
// The second disjunct in xb4 can be eliminated as both disjuncts
// of xb3 result in an incompatible sum when substituted.
xb1: (xb2 & 8) | (xb4 & 9)
xb2: xb3 + 2
xb3: (6 & (xb1-2)) | (xb4 & 9)
xb4: xb2 + 2
// Another variant with more disjunctions. xc1 remains with two
// possibilities. Technically, only the first value is valid.
// However, to fully determine that, all options of the remaining
// disjunction will have to be evaluated algebraically, which is
// not done.
xc1: xc2 & 8 | xc4 & 9 | xc5 & 9
xc2: xc3 + 2
xc3: 6 & xc1-2
xc4: xc2 + 1
xc5: xc2 + 2
// The above is resolved by setting xd1 explicitly.
xd1: xd2 & 8 | xd4 & 9 | xd5 & 9
xd2: xd3 + 2
xd3: 6 & xd1-2
xd4: xd2 + 1
xd5: xd2 + 2
xd1: 8
// The above is resolved by setting xd1 explicitly to the wrong
// value, resulting in an error.
xe1: xe2 & 8 | xe4 & 9 | xe5 & 9
xe2: xe3 + 2
xe3: 6 & xe1-2
xe4: xe2 + 1
xe5: xe2 + 2
xe1: 9
// Only one solution.
xf1: xf2 & 8 | xf4 & 9
xf2: xf3 + 2
xf3: 6 & xf1-2 | xf4 & 9
xf4: xf2 + 2
z1: z2 + 1 | z3 + 5
z2: z3 + 2
z3: z1 - 3
z3: 8
`,
out: `<0>{` +
`xa1: 8, ` +
`xa2: 8, ` +
`xa4: 10, ` +
`xa3: 6, ` +
`xb1: 8, ` +
`xb2: 8, ` +
`xb4: 10, ` +
`xb3: 6, ` +
`xc1: ((<1>.xc2 & 8) | (<1>.xc4 & 9) | (<1>.xc5 & 9)), ` +
`xc2: (<1>.xc3 + 2), ` +
`xc4: (<1>.xc2 + 1), ` +
`xc5: (<1>.xc2 + 2), ` +
`xc3: (6 & (<1>.xc1 - 2)), ` +
`xd1: 8, ` +
`xd2: 8, ` +
`xd4: 9, ` +
`xd5: 10, ` +
`xd3: 6, ` +
`xe1: _|_((6 & 7):conflicting values: 6 != 7), ` +
`xe2: _|_((6 & 7):conflicting values: 6 != 7), ` +
`xe4: _|_((6 & 7):conflicting values: 6 != 7), ` +
`xe5: _|_((6 & 7):conflicting values: 6 != 7), ` +
`xe3: _|_((6 & 7):conflicting values: 6 != 7), ` +
`xf1: 8, ` +
`xf2: 8, ` +
`xf4: 10, ` +
`xf3: 6, ` +
`z1: ((<1>.z2 + 1) | (<1>.z3 + 5)), ` +
`z2: (<1>.z3 + 2), ` +
`z3: ((<1>.z1 - 3) & 8)}`,
}, {
// Defaults should not alter the result of the above disjunctions.
// The results may differ, but errors and resolution should be roughly
// the same.
desc: "resolved self-reference cycles with disjunction with defaults",
in: `
// The disjunction in xa could be resolved, but as disjunctions
// are not resolved for expression, it remains unresolved.
xa1: (xa2 & 8) | *(xa4 & 9)
xa2: xa3 + 2
xa3: 6 & xa1-2
xa4: xa2 + 2
// As xb3 is a disjunction, xb2 cannot be resolved and evaluating
// the cycle completely is broken. However, it is not an error
// as the user might still resolve the disjunction.
xb1: *(xb2 & 8) | (xb4 & 9)
xb2: xb3 + 2
xb3: *(6 & (xb1-2)) | (xb4 & 9)
xb4: xb2 + 2
// Another variant with more disjunctions. xc1 remains with two
// possibilities. Technically, only the first value is valid.
// However, to fully determine that, all options of the remaining
// disjunction will have to be evaluated algebraically, which is
// not done.
xc1: *(xc2 & 8) | (xc4 & 9) | (xc5 & 9)
xc2: xc3 + 2
xc3: 6 & xc1-2
xc4: xc2 + 1
xc5: xc2 + 2
// The above is resolved by setting xd1 explicitly.
xd1: *(xd2 & 8) | xd4 & 9 | xd5 & 9
xd2: xd3 + 2
xd3: 6 & xd1-2
xd4: xd2 + 1
xd5: xd2 + 2
// The above is resolved by setting xd1 explicitly to the wrong
// value, resulting in an error.
xe1: *(xe2 & 8) | xe4 & 9 | xe5 & 9
xe2: xe3 + 2
xe3: 6 & xe1-2
xe4: xe2 + 1
xe5: xe2 + 2
xe1: 9
z1: *(z2 + 1) | z3 + 5
z2: z3 + 2
z3: z1 - 3
z3: 8
`,
out: `<0>{` +
`xa1: 8, ` +
`xa2: 8, ` +
`xa4: 10, ` +
`xa3: 6, ` +
`xb1: 8, ` +
`xb2: 8, ` +
`xb4: 10, ` +
`xb3: 6, ` +
`xc1: (*8 | 9), ` + // not resolved because we use evalPartial
`xc2: 8, ` +
`xc4: 9, ` +
`xc5: 10, ` +
`xc3: 6, ` +
`xd1: (*8 | 9), ` + // TODO: eliminate 9?
`xd2: 8, ` +
`xd4: 9, ` +
`xd5: 10, ` +
`xd3: 6, ` +
`xe1: _|_((6 & 7):conflicting values: 6 != 7), ` +
`xe2: _|_((6 & 7):conflicting values: 6 != 7), ` +
`xe4: _|_((6 & 7):conflicting values: 6 != 7), ` +
`xe5: _|_((6 & 7):conflicting values: 6 != 7), ` +
`xe3: _|_((6 & 7):conflicting values: 6 != 7), ` +
`z1: (*11 | 13), ` + // 13 is eliminated with evalFull
`z2: 10, ` +
`z3: 8}`,
}}
rewriteHelper(t, testCases, evalPartial)
}
func TestFullEval(t *testing.T) {
testCases := []testCase{{
desc: "detect conflicting value",
in: `
a: 8000.9
a: 7080 | int`,
out: `<0>{a: _|_((8000.9 & int):unsupported op &(float, int))}`,
}, {
desc: "resolve all disjunctions",
in: `
service <Name>: {
name: string | *Name
port: int | *7080
}
service foo: _
service bar: { port: 8000 }
service baz: { name: "foobar" }
`,
out: `<0>{service: <1>{<>: <2>(Name: string)-><3>{name: (string | *<2>.Name), port: (int | *7080)}, foo: <4>{name: "foo", port: 7080}, bar: <5>{name: "bar", port: 8000}, baz: <6>{name: "foobar", port: 7080}}}`,
}, {
desc: "field templates",
in: `
a: {
<name>: int
k: 1
}
b: {
<x>: { x: 0, y: *1 | int }
v: {}
w: { y: 0 }
}
b: { <y>: {} } // TODO: allow different name
c: {
<Name>: { name: Name, y: 1 }
foo: {}
bar: _
}
`,
out: `<0>{a: <1>{<>: <2>(name: string)->int, k: 1}, b: <3>{<>: <4>(x: string)->(<5>{x: 0, y: (*1 | int)} & <6>{}), v: <7>{x: 0, y: 1}, w: <8>{x: 0, y: 0}}, c: <9>{<>: <10>(Name: string)-><11>{name: <10>.Name, y: 1}, foo: <12>{name: "foo", y: 1}, bar: <13>{name: "bar", y: 1}}}`,
}, {
desc: "field comprehension",
in: `
a: { "\(k)": v for k, v in b if k < "d" if v > b.a }
b: {
a: 1
b: 2
c: 3
d: 4
}
c: {
"\(k)": v <-
for k, v in b
if k < "d"
if v > b.a
}
`,
out: `<0>{a: <1>{b: 2, c: 3}, b: <2>{a: 1, b: 2, c: 3, d: 4}, c: <3>{b: 2, c: 3}}`,
}, {
desc: "conditional field",
in: `
a: "foo" if b
b: true
c: {
a: 3
a: 3 if a > 1
}
`,
out: `<0>{b: true, c: <1>{a: _|_(3:field a both generated by and referred to by field comprehension in same struct)}, a: "foo"}`,
}, {
desc: "referencing field in field comprehension",
in: `
a: { b c: 4 }
a: {
b d: 5
"\(k)": v for k, v in b
}
`,
out: `<0>{a: <1>{b: <2>{c: 4, d: 5}, c: 4, d: 5}}`,
}, {
desc: "different labels for templates",
in: `
a <X>: { name: X }
a <Name>: { name: Name }
a foo: {}
`,
out: `<0>{a: <1>{<>: <2>(X: string)->(<3>{name: <2>.X} & <4>{name: <2>.X}), foo: <5>{name: "foo"}}}`,
}, {
// TODO: rename EE and FF to E and F to check correct ordering.
desc: "nested templates in one field",
in: `
a <A> b <B>: {
name: A
kind: B
}
a "A" b "B": _
a "C" b "D": _
a "EE" b "FF": { c: "bar" }
`,
out: `<0>{a: <1>{<>: <2>(A: string)-><3>{b: <4>{<>: <5>(B: string)-><6>{name: <2>.A, kind: <5>.B}, }}, ` +
`A: <7>{b: <8>{<>: <9>(B: string)-><10>{name: <11>.A, kind: <9>.B}, ` +
`B: <12>{name: "A", kind: "B"}}}, ` +
`C: <13>{b: <14>{<>: <15>(B: string)-><16>{name: <17>.A, kind: <15>.B}, ` +
`D: <18>{name: "C", kind: "D"}}}, ` +
`EE: <19>{b: <20>{<>: <21>(B: string)-><22>{name: <23>.A, kind: <21>.B}, ` +
`FF: <24>{name: "EE", kind: "FF", c: "bar"}}}}}`,
}, {
desc: "template unification within one struct",
in: `
a: {
<A>: { name: A }
<A>: { kind: A }
}
a "A": _
a "C": _
a "E": { c: "bar" }
`,
out: `<0>{a: <1>{<>: <2>(A: string)->(<3>{name: <2>.A} & <4>{kind: <2>.A}), ` +
`E: <5>{name: "E", kind: "E", c: "bar"}, ` +
`A: <6>{name: "A", kind: "A"}, ` +
`C: <7>{name: "C", kind: "C"}}}`,
}, {
desc: "field comprehensions with multiple keys",
in: `
a "\(x.a)" b "\(x.b)": x for x in [
{a: "A", b: "B" },
{a: "C", b: "D" },
{a: "E", b: "F" },
]
"\(x.a)" "\(x.b)": x for x in [
{a: "A", b: "B" },
{a: "C", b: "D" },
{a: "E", b: "F" },
]`,
out: `<0>{E: <1>{F: <2>{a: "E", b: "F"}}, ` +
`a: <3>{` +
`E: <4>{b: <5>{F: <6>{a: "E", b: "F"}}}, ` +
`A: <7>{b: <8>{B: <9>{a: "A", b: "B"}}}, ` +
`C: <10>{b: <11>{D: <12>{a: "C", b: "D"}}}}, ` +
`A: <13>{B: <14>{a: "A", b: "B"}}, ` +
`C: <15>{D: <16>{a: "C", b: "D"}}}`,
// TODO: this order would be desirable.
// out: `<0>{a: <1>{` +
// `A: <2>{b: <3>{B: <4>{a: "A", b: "B"}}}, ` +
// `C: <5>{b: <6>{D: <7>{a: "C", b: "D"}}}, ` +
// `E: <8>{b: <9>{F: <10>{a: "E", b: "F"}}}}, ` +
// `A: <11>{B: <12>{a: "A", b: "B"}}, ` +
// `C: <13>{D: <14>{a: "C", b: "D"}}, ` +
// `E: <15>{F: <16>{a: "E", b: "F"}}}`,
}, {
desc: "field comprehensions with templates",
in: `
num: 1
a: {
<A> <B>: {
name: A
kind: B
} if num < 5
}
a b c d: "bar"
`,
out: `<0>{num: 1, a: <1>{<>: <2>(A: string)-><3>{<>: <4>(B: string)-><5>{name: <2>.A, kind: <4>.B}, }, ` +
`b: <6>{<>: <7>(B: string)-><8>{name: <9>.A, kind: <7>.B}, ` +
`c: <10>{name: "b", kind: "c", ` +
`d: "bar"}}}}`,
}, {
desc: "disjunctions of lists",
in: `
l: *[ int, int ] | [ string, string ]
l1: [ "a", "b" ]
l2: l & [ "c", "d" ]
`,
out: `<0>{l: [int,int], l1: ["a","b"], l2: ["c","d"]}`,
}, {
desc: "normalization",
in: `
a: string | string
b: *1 | *int
c: *1.0 | *float
`,
out: `<0>{a: string, b: int, c: float}`,
}, {
desc: "default disambiguation and elimination",
in: `
a: *1 | int
b: *3 | int
c: a & b
d: b & a
e: *1 | *1
`,
out: `<0>{a: 1, b: 3, c: int, d: int, e: 1}`,
}, {
desc: "list comprehension",
in: `
// a: [ k for k: v in b if k < "d" if v > b.a ] // TODO test error using common iso colon
a: [ k for k, v in b if k < "d" if v > b.a ]
b: {
a: 1
b: 2
c: 3
d: 4
}
c: [ x for _, x in b for _, y in b if x < y ]
d: [ x for x, _ in a ]
`,
out: `<0>{a: ["b","c"], b: <1>{a: 1, b: 2, c: 3, d: 4}, c: [1,1,1,2,2,3], d: [0,1]}`,
}, {
desc: "struct comprehension with template",
in: `
result: [ v for _, v in service ]
service <Name>: {
name: *Name | string
type: "service"
port: *7080 | int
}
service foo: {}
service bar: { port: 8000 }
service baz: { name: "foobar" }
`,
out: `<0>{result: [` +
`<1>{name: "foo", type: "service", port: 7080},` +
`<2>{name: "bar", type: "service", port: 8000},` +
`<3>{name: "foobar", type: "service", port: 7080}], ` +
`service: <4>{` +
`<>: <5>(Name: string)-><6>{name: (*<5>.Name | string), type: "service", port: (*7080 | int)}, ` +
`foo: <7>{name: "foo", type: "service", port: 7080}, ` +
`bar: <8>{name: "bar", type: "service", port: 8000}, ` +
`baz: <9>{name: "foobar", type: "service", port: 7080}}}`,
}, {
desc: "resolutions in struct comprehension keys",
in: `
a: { "\(b + ".")": "a" for _, b in ["c"] }
`,
out: `<0>{a: <1>{c.: "a"}}`,
}, {
desc: "recursive evaluation within list",
in: `
l: [a]
a: b & { c: "t" }
b: {
d: c
c: string
}
l1: [a1]
a1: b1 & { c: "t" }
b1: {
d: "s" + c
c: string
}
`,
out: `<0>{` +
`l: [<1>{c: "t", d: "t"}], ` +
`a: <2>{c: "t", d: "t"}, ` +
`b: <3>{c: string, d: string}, ` +
`l1: [<4>{c: "t", d: "st"}], ` +
`a1: <5>{c: "t", d: "st"}, ` +
`b1: <6>{c: string, d: ("s" + <7>.c)}}`,
}, {
desc: "ips",
in: `
IP: 4*[ uint8 ]
Private:
*[ 192, 168, uint8, uint8 ] |
[ 10, uint8, uint8, uint8] |
[ 172, >=16 & <=32, uint8, uint8 ]
Inst: Private & [ _, 10, ... ]
MyIP: Inst & [_, _, 10, 10 ]
`,
out: `<0>{` +
`IP: [(int & >=0 & int & <=255),(int & >=0 & int & <=255),(int & >=0 & int & <=255),(int & >=0 & int & <=255)], ` +
`Private: [192,168,(int & >=0 & int & <=255),(int & >=0 & int & <=255)], ` +
`Inst: [10,10,(int & >=0 & int & <=255),(int & >=0 & int & <=255)], ` +
`MyIP: [10,10,10,10]` +
`}`,
}, {
desc: "complex interaction of groundness",
in: `
res: [ y & { d: "b" } for x in a for y in x ]
res: [ a.b.c & { d: "b" } ]
a b <C>: { d: string, s: "a" + d }
a b c d: string
`,
// TODO(perf): unification should catch shared node.
out: `<0>{res: [<1>{d: "b", s: "ab"}], ` +
`a: <2>{b: <3>{<>: <4>(C: string)-><5>{d: string, s: ("a" + <5>.d)}, c: <6>{d: string, s: ("a" + <7>.d)}}}}`,
}, {
desc: "complex groundness 2",
in: `
r1: f1 & { y: "c" }
f1: { y: string, res: a.b.c & { d: y } }
a b c: { d: string, s: "a" + d }
a b <C>: { d: string, s: "a" + d }
a b c d: string
`,
out: `<0>{r1: <1>{y: "c", res: <2>{d: "c", s: "ac"}}, f1: <3>{y: string, res: <4>{d: string, s: (("a" + <5>.d) & ("a" + <5>.d))}}, a: <6>{b: <7>{<>: <8>(C: string)-><9>{d: string, s: ("a" + <9>.d)}, c: <10>{d: string, s: (("a" + <11>.d) & ("a" + <11>.d))}}}}`,
}, {
desc: "references from template to concrete",
in: `
res: [t]
t <X>: {
a: c + b.str
b str: string
c: "X"
}
t x: { b str: "DDDD" }
`,
out: `<0>{res: [<1>{<>: <2>(X: string)-><3>{a: (<3>.c + <3>.b.str), c: "X", b: <4>{str: string}}, x: <5>{a: "XDDDD", c: "X", b: <6>{str: "DDDD"}}}], ` +
`t: <7>{<>: <2>(X: string)-><3>{a: (<3>.c + <3>.b.str), c: "X", b: <4>{str: string}}, x: <8>{a: "XDDDD", c: "X", b: <9>{str: "DDDD"}}}}`,
}, {
// TODO: A nice property for CUE to have would be that evaluation time
// is proportional to the number of output nodes (note that this is
// not the same as saying that the running time is O(n)).
// We should probably disallow shenanigans like the one below. But until
// this is allowed, it should at least be correct. At least we are not
// making reentrant coding easy.
desc: "reentrance",
in: `
// This indirection is needed to avoid binding references to fib
// within fib to the instantiated version.
fibRec: {nn: int, out: (fib & {n: nn}).out}
fib: {
n: int
out: (fibRec & {nn: n - 2}).out + (fibRec & {nn: n - 1}).out if n >= 2
out: n if n < 2
}
fib2: (fib & {n: 2}).out
fib7: (fib & {n: 7}).out
fib12: (fib & {n: 12}).out
`,
out: `<0>{fibRec: <1>{nn: int, out: _|_((<2>.fib & <3>{n: <4>.nn}).out:undefined field "out")}, fib: <5>{n: int}, ` +
`fib2: 1, ` +
`fib7: 13, ` +
`fib12: 144}`,
}, {
desc: "Issue #23",
in: `
x: {a:1}|{a:2}
y: x & {a:3}
`,
out: `<3>{x: _|_((<0>{a: 1} | <1>{a: 2}):more than one element remaining (<0>{a: 1} and <1>{a: 2})), y: _|_((<4>.x & <5>{a: 3}):empty disjunction: <2>{a: (1 & 3)})}`,
}, {
desc: "cannot resolve references that would be ambiguous",
in: `
a1: *0 | 1
a1: a3 - a2
a2: *0 | 1
a2: a3 - a1
a3: 1
b1: (*0 | 1) & b2
b2: (0 | *1) & b1
c1: (*{a:1} | {b:1}) & c2
c2: (*{a:2} | {b:2}) & c1
`,
out: `<4>{` +
`a1: _|_(((*0 | 1) & (<5>.a3 - <5>.a2)):cycle detected), ` +
`a3: 1, ` +
`a2: _|_(((*0 | 1) & (<5>.a3 - <5>.a1)):cycle detected), ` +
`b1: _|_((0 | 1 | *_|_):more than one element remaining (0 and 1)), ` +
`b2: _|_((0 | 1 | *_|_):more than one element remaining (0 and 1)), ` +
`c1: _|_((<0>{a: 1, b: 2} | <1>{a: 2, b: 1} | *_|_):more than one element remaining (<0>{a: 1, b: 2} and <1>{a: 2, b: 1})), ` +
`c2: _|_((<2>{a: 2, b: 1} | <3>{a: 1, b: 2} | *_|_):more than one element remaining (<2>{a: 2, b: 1} and <3>{a: 1, b: 2}))}`,
}}
rewriteHelper(t, testCases, evalFull)
}
func TestX(t *testing.T) {
t.Skip()
// Don't remove. For debugging.
testCases := []testCase{{
in: `
`,
}}
rewriteHelper(t, testCases, evalFull)
}