blob: 4888b8fc1f585e82467cedd1ed4b22e58ccdb911 [file] [log] [blame]
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +01001// Copyright 2018 The CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cue
16
17import (
18 "flag"
19 "fmt"
20 "testing"
21
22 "cuelang.org/go/cue/errors"
23 "cuelang.org/go/cue/parser"
24 "cuelang.org/go/cue/token"
25)
26
27var traceOn = flag.Bool("debug", false, "enable tracing")
28
29func compileFileWithErrors(t *testing.T, body string) (*context, *structLit, errors.List) {
30 t.Helper()
31 ctx, inst, errs := compileInstance(t, body)
32 return ctx, inst.rootValue.evalPartial(ctx).(*structLit), errs
33}
34
35func compileFile(t *testing.T, body string) (*context, *structLit) {
36 t.Helper()
37 ctx, inst, errs := compileInstance(t, body)
38 if errs != nil {
39 t.Fatal(errs)
40 }
41 return ctx, inst.rootValue.evalPartial(ctx).(*structLit)
42}
43
44func compileInstance(t *testing.T, body string) (*context, *Instance, errors.List) {
45 t.Helper()
46
47 fset := token.NewFileSet()
48 x := newIndex(fset).NewInstance(nil)
Marcel van Lohuizen2bf066f2018-12-16 11:43:49 +010049 f, err := parser.ParseFile(fset, "test", body)
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +010050 ctx := x.newContext()
51
52 switch errs := err.(type) {
53 case nil:
54 x.insertFile(f)
55 case errors.List:
56 return ctx, x, errs
57 default:
58 t.Fatal(err)
59 }
60 return ctx, x, nil
61}
62
63func rewriteHelper(t *testing.T, cases []testCase, r rewriteMode) {
64 t.Helper()
65 for _, tc := range cases {
66 t.Run(tc.desc, func(t *testing.T) {
67 t.Helper()
68 ctx, obj := compileFile(t, tc.in)
69 ctx.trace = *traceOn
70 root := testResolve(ctx, obj, r)
71
72 got := debugStr(ctx, root)
73 if v := ctx.processDelayedConstraints(); v != nil {
74 got += fmt.Sprintf("\n%s", debugStr(ctx, v))
75 }
76
77 // Copy the result
78 if got != tc.out {
79 fn := t.Errorf
80 if tc.skip {
81 fn = t.Skipf
82 }
83 fn("output differs:\ngot %s\nwant %s", got, tc.out)
84 }
85 })
86 }
87}
88
89type testCase struct {
90 desc string
91 in string
92 out string
93 skip bool
94}
95
96func TestBasicRewrite(t *testing.T) {
97 testCases := []testCase{{
98 desc: "errors",
99 in: `
100 a: _|_ & _|_
101 b: null & _|_
102 c: b.a == _|_
103 d: _|_ != b.a
104 e: _|_ == _|_
105 `,
106 out: `<0>{a: _|_(from source), b: _|_(from source), c: true, d: false, e: true}`,
107 }, {
108 desc: "arithmetic",
109 in: `
110 sum: -1 + +2 // 1
111 str: "foo" + "bar" // "foobar"
112 div1: 2.0 / 3 * 6 // 4
113 div2: 2 / 3 * 6 // 4
114 rem: 2 % 3 // 2
115 e: 2 + "a" // _|_: unsupported op +(int, string))
116 b: 1 != 4
117 `,
118 out: `<0>{sum: 1, str: "foobar", div1: 4.00000000000000000000000, div2: 4.00000000000000000000000, rem: 2, e: _|_((2 + "a"):unsupported op +(number, string)), b: true}`,
119 }, {
120 desc: "integer-specific arithmetic",
121 in: `
122 q1: 5 quo 2 // 2
123 q2: 5 quo -2 // -2
124 q3: -5 quo 2 // -2
125 q4: -5 quo -2 // 2
126 qe1: 2.0 quo 1
127 qe2: 2 quo 1.0
128
129 r1: 5 rem 2 // 1
130 r2: 5 rem -2 // 1
131 r3: -5 rem 2 // -1
132 r4: -5 rem -2 // -1
133 re1: 2.0 rem 1
134 re2: 2 rem 1.0
135
136 d1: 5 div 2 // 2
137 d2: 5 div -2 // -2
138 d3: -5 div 2 // -3
139 d4: -5 div -2 // 3
140 de1: 2.0 div 1
141 de2: 2 div 1.0
142
143 m1: 5 mod 2 // 1
144 m2: 5 mod -2 // 1
145 m3: -5 mod 2 // 1
146 m4: -5 mod -2 // 1
147 me1: 2.0 mod 1
148 me2: 2 mod 1.0
149
150 // TODO: handle divide by zero
151 `,
152 out: `<0>{q1: 2, q2: -2, q3: -2, q4: 2, ` +
153 `qe1: _|_((2.0 quo 1):unsupported op quo(float, number)), ` +
154 `qe2: _|_((2 quo 1.0):unsupported op quo(number, float)), ` +
155 `r1: 1, r2: 1, r3: -1, r4: -1, re1: ` +
156 `_|_((2.0 rem 1):unsupported op rem(float, number)), ` +
157 `re2: _|_((2 rem 1.0):unsupported op rem(number, float)), ` +
158 `d1: 2, d2: -2, d3: -3, d4: 3, ` +
159 `de1: _|_((2.0 div 1):unsupported op div(float, number)), ` +
160 `de2: _|_((2 div 1.0):unsupported op div(number, float)), ` +
161 `m1: 1, m2: 1, m3: 1, m4: 1, ` +
162 `me1: _|_((2.0 mod 1):unsupported op mod(float, number)), ` +
163 `me2: _|_((2 mod 1.0):unsupported op mod(number, float))}`,
164 }, {
165 desc: "booleans",
166 in: `
167 t: true
168 t: !false
169 f: false
170 f: !t
171 e: true
172 e: !true
173 `,
174 out: "<0>{t: true, f: false, e: _|_(true:failed to unify: true != false)}",
175 }, {
176 desc: "boolean arithmetic",
177 in: `
178 a: true && true
179 b: true || false
180 c: false == true
181 d: false != true
182 e: true & true
183 f: true & false
184 `,
185 out: "<0>{a: true, b: true, c: false, d: true, e: true, f: _|_(true:failed to unify: true != false)}",
186 }, {
187 desc: "basic type",
188 in: `
189 a: 1 & int
190 b: number & 1
191 c: 1.0
192 c: float
193 d: int & float // _|_
194 e: "4" & string
195 f: true
196 f: bool
197 `,
198 out: `<0>{a: 1, b: 1, c: 1.0, d: _|_((int & float):unsupported op &((int)*, (float)*)), e: "4", f: true}`,
199 }, {
200 desc: "escaping",
201
202 in: `
203 a: "foo\nbar",
204 b: a,
205
206 // TODO: mimic http://exploringjs.com/es6/ch_template-literals.html#sec_introduction-template-literals
207 `,
208 out: `<0>{a: "foo\nbar", b: "foo\nbar"}`,
209 // out: `<0>{a: "foo\nbar", b: <0>.a}`,
210 }, {
211 desc: "reference",
212 in: `
213 a: b
214 b: 2
215 d: {
216 d: 3
217 e: d
218 }
219 e: {
220 e: {
221 v: 1
222 }
223 f: {
224 v: e.v
225 }
226 }
227 `,
228 out: "<0>{a: 2, b: 2, d: <1>{d: 3, e: 3}, e: <2>{e: <3>{v: 1}, f: <4>{v: 1}}}",
229 }, {
230 desc: "lists",
231 in: `
232 list: [1,2,3]
233 index: [1,2,3][1]
234 unify: [1,2,3] & [_,2,3]
235 e: [] & 4
236 e2: [3]["d"]
237 e3: [3][-1]
238 e4: [1, 2, ...4..5] & [1, 2, 4, 8]
239 e5: [1, 2, 4, 8] & [1, 2, ...4..5]
240 `,
241 out: `<0>{list: [1,2,3], index: 2, unify: [1,2,3], e: _|_(([] & 4):unsupported op &(list, number)), e2: _|_("d":invalid list index "d" (type string)), e3: _|_(-1:invalid list index -1 (index must be non-negative)), e4: _|_(((4..5) & 8):value 8 not in range (4..5)), e5: _|_(((4..5) & 8):value 8 not in range (4..5))}`,
242 }, {
243 desc: "selecting",
244 in: `
245 obj: {a: 1, b: 2}
246 index: {a: 1, b: 2}["b"]
247 mulidx: {a: 1, b: {a:1, b: 3}}["b"]["b"]
248 e: {a: 1}[4]
249 f: {a: 1}.b
250 g: {a: 1}["b"]
251 h: [3].b
252 `,
253 out: `<0>{obj: <1>{a: 1, b: 2}, index: 2, mulidx: 3, e: _|_(4:invalid struct index 4 (type number)), 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))}`,
254 }, {
255 desc: "obj unify",
256 in: `
257 o1: {a: 1 } & { b: 2} // {a:1,b:2}
258 o2: {a: 1, b:2 } & { b: 2} // {a:1,b:2}
259 o3: {a: 1 } & { a:1, b: 2} // {a:1,b:2}
260 o4: {a: 1 } & { b: 2} // {a:1,b:2}
261 o4: {a: 1, b:2 } & { b: 2}
262 o4: {a: 1 } & { a:1, b: 2}
263 e: 1 // 1 & {a:3}
264 e: {a:3}
265 `,
266 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 &(number, struct))}",
267 }, {
268 desc: "disjunctions",
269 in: `
270 o1: 1 | 2 | 3
271 o2: (1 | 2 | 3) & 1
272 o3: 2 & (1 | 2 | 3)
273 o4: (1 | 2 | 3) & (1 | 2 | 3)
274 o5: (1 | 2 | 3) & (3 | 2 | 1)
275 o6: (1 | 2 | 3) & (3 | 1 | 2)
276 o7: (1 | 2 | 3) & (2 | 3)
277 o8: (1 | 2 | 3) & (3 | 2)
278 o9: (2 | 3) & (1 | 2 | 3)
279 o10: (3 | 2) & (1 | 2 | 3)
280
281 // All errors are treated the same as per the unification model.
282 i1: [1, 2][3] | "c"
283 `,
284 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!), i1: "c"}`,
285 }, {
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100286 desc: "types",
287 in: `
288 i: int
289 j: int & 3
290 s: string
291 t: "s" & string
292 e: int & string
293 e2: 1 & string
294 b: !int
295 p: +true
296 m: -false
297 `,
298 out: `<0>{i: int, j: 3, s: string, t: "s", e: _|_((int & string):unsupported op &((int)*, (string)*)), e2: _|_((1 & string):unsupported op &(number, (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)}`,
299 }, {
300 desc: "comparisson",
301 in: `
302 lss: 1 < 2
303 leq: 1 <= 1.0
304 leq: 2.0 <= 3
305 eql: 1 == 1.0
306 neq: 1.0 == 1
307 gtr: !(2 > 3)
308 geq: 2.0 >= 2
309 seq: "a" + "b" == "ab"
310 err: 2 == "s"
311 `,
312 out: `<0>{lss: true, leq: true, eql: true, neq: true, gtr: true, geq: true, seq: true, err: _|_((2 == "s"):unsupported op ==(number, string))}`,
313 }, {
314 desc: "null",
315 in: `
316 eql: null == null
317 neq: null != null
318 unf: null & null
319
320 // errors
321 eqe1: null == 1
322 eqe2: 1 == null
323 nee1: "s" != null
324 call: null()
325 `,
326 out: `<0>{eql: true, neq: false, unf: null, eqe1: _|_((null == 1):unsupported op ==(null, number)), eqe2: _|_((1 == null):unsupported op ==(number, null)), nee1: _|_(("s" != null):unsupported op !=(string, null)), call: _|_(null:cannot call non-function null (type null))}`,
327 }, {
328 desc: "self-reference cycles",
329 in: `
330 a: b - 100
331 b: a + 100
332
333 c: [c[1], c[0]]
334 `,
335 out: `<0>{a: _|_(cycle detected), b: _|_(cycle detected), c: _|_(cycle detected)}`,
336 // }, {
337 // desc: "resolved self-reference cycles",
338 // in: `
339 // a: b - 100
340 // b: a + 100
341 // b: 200
342
343 // c: [c[1], a] // TODO: should be allowed
344
345 // s1: s2 & {a: 1}
346 // s2: s3 & {b: 2}
347 // s3: s1 & {c: 3}
348 // `,
349 // out: `<0>{a: 100, b: 200, c: _|_(cycle detected)}`,
350 }, {
351 desc: "delayed constraint failure",
352 in: `
353 a: b - 100
354 b: a + 110
355 b: 200
356 `,
357 out: `<0>{a: 100, b: 200}
358_|_(((<1>.a + 110) & 200):constraint violated: _|_((210 & 200):cannot unify numbers 210 and 200))`,
359 // TODO: find a way to mark error in data.
360 }}
361 rewriteHelper(t, testCases, evalPartial)
362}
363
364func TestChooseFirst(t *testing.T) {
365 testCases := []testCase{{
366 desc: "pick first",
367 in: `
368 a: 5 | "a" | true
369 b c: {
370 a: 2
371 } | {
372 a : 3
373 }
374 `,
375 out: "<0>{a: 5, b: <1>{c: <2>{a: 2}}}",
376 }, {
377 desc: "simple disambiguation conflict",
378 in: `
379 a: "a" | "b"
380 b: "b" | "a"
381 c: a & b
382 `,
383 out: `<0>{a: "a", b: "b", c: _|_(("a"! | "b"!):ambiguous disjunction)}`,
384 }, {
385 desc: "disambiguation non-conflict",
386 in: `
387 a: "a" | ("b" | "c")
388 b: ("a" | "b") | "c"
389 c: a & b
390 `,
391 out: `<0>{a: "a", b: "a", c: "a"}`,
392 }}
393 rewriteHelper(t, testCases, evalFull)
394}
395
396func TestResolve(t *testing.T) {
397 testCases := []testCase{{
398 in: `
399 a: b.c.d
400 b c: { d: 3 }
401 c: { c: d.d, }
402 d: { d: 2 }
403 `,
404 out: "<0>{a: 3, b: <1>{c: <2>{d: 3}}, c: <3>{c: 2}, d: <4>{d: 2}}",
405 }, {
406 in: `
407 a: _
408 b: a
409 a: { d: 1, d: _ }
410 b: _
411 `,
412 out: `<0>{a: <1>{d: 1}, b: <2>{d: 1}}`,
413 }, {
414 desc: "JSON",
415 in: `
416 "a": 3
417 b: a
418 o: { "a\nb": 2 } // TODO: use $ for root?
419 c: o["a\nb"]
420 `,
421 out: `<0>{a: 3, b: 3, o: <1>{"a\nb": 2}, c: 2}`,
422 }, {
423 desc: "arithmetic",
424 in: `
425 v1: 1.0T/2.0 //
426 v2: 2.0 == 2
427 i1: 1
428 v5: 2.0 / i1 // TODO: should probably fail
429 e1: 2.0 % 3
430 e2: int & 4.0/2.0
431 `,
432 out: `<0>{v1: 5e+11, v2: true, i1: 1, v5: 2, e1: _|_((2.0 % 3):unsupported op %(float, number)), e2: _|_((int & 2):unsupported op &((int)*, float))}`,
433 // }, {
434 // desc: "null coalescing",
435 // in: `
436 // a: null
437 // b: a.x
438 // c: a["x"]
439 // `,
440 // out: ``,
441 }, {
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100442 desc: "reference across tuples and back",
443 // Tests that it is okay to partially evaluate structs.
444 in: `
445 a: { c: b.e, d: b.f }
446 b: { e: 3, f: a.c }
447 `,
448 out: "<0>{a: <1>{c: 3, d: 3}, b: <2>{e: 3, f: 3}}",
449 }, {
450 desc: "index",
451 in: `
452 a: [2][0]
453 b: {foo:"bar"}["foo"]
454 c: (l|{"3":3})["3"]
455 d: ([]|[1])[0]
456 l: []
457 e1: [2][""]
458 e2: 2[2]
459 e3: [][true]
460 e4: [1,2,3][3]
461 e5: [1,2,3][-1]
462 e6: ([]|{})[1]
463 `,
464 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 number 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)}`,
465 }, {
466 desc: "string index",
467 in: `
468 a0: "abc"[0]
469 a1: "abc"[1]
470 a2: "abc"[2]
471 a3: "abc"[3]
472 a4: "abc"[-1]
473
474 b: "zoëven"[2]
475 `,
476 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: "ë"}`,
477 }, {
478 desc: "disjunctions of lists",
479 in: `
480 l: [ int, int ] | [ string, string ]
481
482 l1: [ "a", "b" ]
483 l2: l & [ "c", "d" ]
484 `,
485 out: `<0>{l: ([int,int] | [string,string]), l1: ["a","b"], l2: ["c","d"]}`,
486 }, {
487 desc: "slice",
488 in: `
489 a: [2][0:0]
490 b: [0][1:1]
491 e1: [][1:1]
492 e2: [0][-1:0]
493 e3: [0][1:0]
494 e4: [0][1:2]
495 e5: 4[1:2]
496 e6: [2]["":]
497 e7: [2][:"9"]
498
499 `,
500 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 number)), e6: _|_("":invalid slice index "" (type string)), e7: _|_("9":invalid slice index "9" (type string))}`,
501 }, {
502 desc: "string slice",
503 in: `
504 a0: ""[0:0]
505 a1: ""[:]
506 a2: ""[0:]
507 a3: ""[:0]
508 b0: "abc"[0:0]
509 b1: "abc"[0:1]
510 b2: "abc"[0:2]
511 b3: "abc"[0:3]
512 b4: "abc"[3:3]
513 b5: "abc"[1:]
514 b6: "abc"[:2]
515
516 // TODO: supported extended graphemes, instead of just runes.
517 u: "Spaß"[3:4]
518 `,
519 out: `<0>{a0: "", a1: "", a2: "", a3: "", b0: "", b1: "a", b2: "ab", b3: "abc", b4: "", b5: "bc", b6: "ab", u: "ß"}`,
520 }, {
521 desc: "list types",
522 in: `
523 l0: 3*[int]
524 l0: [1, 2, 3]
525 l1:(0..5)*[string]
526 l1: ["a", "b"]
527 l2: (0..5)*[{ a: int }]
528 l2: [{a: 1}, {a: 2, b: 3}]
529 l3: (0..10)*[int]
530 l3: [1, 2, 3, ...]
531
532 s1: ((0..6)*[int])[2:3] // TODO: simplify 1*[int] to [int]
533 s2: [0,2,3][1:2]
534
535 i1: ((0..6)*[int])[2]
536 i2: [0,2,3][2]
537
538 t0: [...{a: 8}]
539 t0: [{}]
540
541 e0: (2..5)*[{}]
542 e0: [{}]
543
544 e1: 0.._*[...int]
545 `,
546 out: `<0>{l0: [1,2,3], l1: ["a","b"], l2: [<1>{a: 1},<2>{a: 2, b: 3}], l3: (3..10)*[int]([1,2,3, ...int]), s1: 1*[int], s2: [2], i1: int, i2: 3, t0: [<3>{a: 8}], e0: _|_(((2..5)*[<4>{}] & [<5>{}]):incompatible list lengths: value 1 not in range (2..5)), e1: [, ...int]}`,
547 }, {
548 desc: "list arithmetic",
549 in: `
550 l0: 3*[1, 2, 3]
551 l1: 0*[1, 2, 3]
552 l2: 10*[]
553 l3: (0..2)*[]
554 l4: (0..2)*[int]
555 l5: (0..2)*(int*[int])
556 l6: 3*((3..4)*[int])
557 `,
558 out: `<0>{l0: [1,2,3,1,2,3,1,2,3], l1: [], l2: [], l3: [], l4: (0..2)*[int], l5: (0..2)*[int], l6: (9..12)*[int]}`,
559 }, {
560 desc: "correct error messages",
561 // Tests that it is okay to partially evaluate structs.
562 in: `
563 a: "a" & 1
564 `,
565 out: `<0>{a: _|_(("a" & 1):unsupported op &(string, number))}`,
566 }, {
567 desc: "structs",
568 in: `
569 a: t & { c: 5 } // {c:5,d:15}
570 b: ti & { c: 7 } // {c:7,d:21}
571 t: { c: number, d: c * 3 } // {c:number,d:number*3}
572 ti: t & { c: int }
573 `,
574 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)}}`,
575 }, {
576 desc: "reference to root",
577 in: `
578 a: { b: int }
579 c: a & {
580 b: 100
581 d: a.b + 3 // do not resolve as c != a.
582
583 // TODO(crash)
584 // e: int; e < 100 // where clause can be different.
585 }
586 x: {
587 b: int
588 c: b + 5
589 }
590 y: x & {
591 b: 100
592 // c should resolve to 105
593 }
594 v: {
595 b: int
596 c: v.b + 5 // reference starting from copied node.
597 }
598 w: v & { b: 100 }
599 wp: v & { b: 100 }
600 `,
601 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}}`,
602 }, {
603 desc: "references from template to concrete",
604 in: `
605 res: [t]
606 t <X>: {
607 a: c + b.str
608 b str: string
609 c: "X"
610 }
611 t x: { b str: "DDDD" }
612 `,
613 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"}}}}`,
614 }, {
615 desc: "interpolation",
616 in: `
617 a: "\(4)"
618 b: "one \(a) two \( a + c )"
619 c: "one"
620 d: "\(r)"
621 u: "\(_)"
622 r: _
623 e: "\([])"`,
624 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))}`,
625 }, {
626 desc: "diamond-shaped constraints",
627 in: `
628 S: {
629 A: {
630 a: 1,
631 },
632 B: A & {
633 b: 2,
634 }
635 },
636 T: S & { // S == { A: { a:1 }, B: { a:1, b:2 } }
637 A: {
638 c: 3,
639 },
640 B: { // S.B & A
641 d: 4, // Combines constraints S.A, S.B, T.A, and T.B
642 }
643 }`,
644 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}}}",
645 }, {
646 desc: "field templates",
647 in: `
648 a: {
649 <name>: int
650 k: 1
651 }
652 b: {
653 <x>: { x: 0, y: 1 | int }
654 v: {}
655 w: { x: 0 }
656 }
657 b: { <y>: {} } // TODO: allow different name
658 c: {
659 <Name>: { name: Name, y: 1 }
660 foo: {}
661 bar: _
662 }
663 `,
664 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}}}`,
665 }, {
666 desc: "simple ranges",
667 in: `
668 a: 1..2
669 c: "a".."b"
670 d: (2+3)..(4+5) // 5..9
671
672 s1: 1..1 // 1
673 s2: 1..2..3 // simplify (1..2)..3 to 1..3
674 s3: (1..10)..5 // This is okay!
675 s4: 5..(1..10) // This is okay!
676 s5: (0..(5..6))..(1..10)
677 `,
678 out: `<0>{a: (1..2), c: ("a".."b"), d: (5..9), s1: 1, s2: (1..3), s3: (1..5), s4: (5..10), s5: (0..10)}`,
679 }, {
680 desc: "range unification",
681 in: `
682 // with concrete values
683 a1: 1..5 & 3
684 a2: 1..5 & 1
685 a3: 1..5 & 5
686 a4: 1..5 & 6
687 a5: 1..5 & 0
688
689 a6: 3 & 1..5
690 a7: 1 & 1..5
691 a8: 5 & 1..5
692 a9: 6 & 1..5
693 a10: 0 & 1..5
694
695 // with ranges
696 b1: 1..5 & 1..5
697 b2: 1..5 & 1..1
698 b3: 1..5 & 5..5
699 b4: 1..5 & 2..3
700 b5: 1..5 & 3..9
701 b6: 1..5 & 5..9
702 b7: 1..5 & 6..9
703
704 b8: 1..5 & 1..5
705 b9: 1..1 & 1..5
706 b10: 5..5 & 1..5
707 b11: 2..3 & 1..5
708 b12: 3..9 & 1..5
709 b13: 5..9 & 1..5
710 b14: 6..9 & 1..5
711
712 // ranges with more general types
713 c1: int & 1..5
714 c2: 1..5 & int
715 c3: string & 1..5
716 c4: 1..5 & string
717
718 // other types
719 s1: "d" .. "z" & "e"
720 s2: "d" .. "z" & "ee"
721
722 n1: number & 1..2
723 n2: int & 1.1 .. 1.3
724 n3: 1.0..3.0 & 2
725 n4: 0.0..0.1 & 0.09999
726 n5: 1..5 & 2.5
727 `,
728 out: `<0>{a1: 3, a2: 1, a3: 5, a4: _|_(((1..5) & 6):value 6 not in range (1..5)), a5: _|_(((1..5) & 0):value 0 not in range (1..5)), a6: 3, a7: 1, a8: 5, a9: _|_(((1..5) & 6):value 6 not in range (1..5)), a10: _|_(((1..5) & 0):value 0 not in range (1..5)), b1: (1..5), b2: 1, b3: 5, b4: (2..3), b5: (3..5), b6: 5, b7: _|_(((1..5) & (6..9)):non-overlapping ranges (1..5) and (6..9)), b8: (1..5), b9: 1, b10: 5, b11: (2..3), b12: (3..5), b13: 5, b14: _|_(((6..9) & (1..5)):non-overlapping ranges (6..9) and (1..5)), c1: (1..5), c2: (1..5), c3: _|_((string & (1..5)):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)):unsupported op &((int)*, (float)*)), n3: 2, n4: 0.09999, n5: 2.5}`,
729 }, {
730 desc: "range arithmetic",
731 in: `
732 r0: (1..2) * (4..5)
733 r1: (1..2) * (-1..2)
734 r2: (1.0..2.0) * (-0.5..1.0)
735 r3: (1..2) + (4..5)
736
737 i0: (1..2) * 2
738 i1: (2..3) * -2
739 i2: (1..2) * 2
740 i3: (2..3) * -2
741
742 t0: int * (1..2) // TODO: should be int
743 t1: (1..2) * int
744 t2: (1..2) * (0..int)
745 t3: (1..int) * (0..2)
746 t4: (1..int) * (-1..2)
747 t5: _ * (1..2) // TODO: should be int
748
749 s0: (1..2) - (3..5)
750 s1: (1..2) - 1
751
752 str0: ("ab".."cd") + "ef"
753 str1: ("ab".."cd") + ("ef".."gh")
754 str2: ("ab".."cd") + string
755
756 `,
757 out: `<0>{r0: (4..10), r1: (-2..4), r2: (-1.00..2.00), r3: (5..7), i0: (2..4), i1: (-6..-4), i2: (2..4), i3: (-6..-4), t0: (int * (1..2)), t1: int, t2: (0..int), t3: (0..int), t4: int, t5: _|_((_ * (1..2)):binary operation on non-ground top value), s0: (-4..-1), s1: (0..1), str0: ("abef".."cdef"), str1: ("abef".."cdgh"), str2: ("ab".."cd")}`,
758 }, {
759 desc: "predefined ranges",
760 in: `
761 k1: int8
762 k1: 44
763
764 k2: int64
765 k2: -8_000_000_000
766
767 e1: int16
768 e1: 100_000
769 `,
770 out: `<0>{k1: 44, k2: -8000000000, ` +
771 `e1: _|_(((-32768..32767) & 100000):value 100000 not in range (-32768..32767))}`,
772 // TODO(P3): if two fields are evaluating to the same field, their
773 // values could be bound.
774 // nodes: use a shared node
775 // other: change to where clause
776 // unknown: change to where clause.
777 // in: `
778 // a: b
779 // b: a
780 // a: { d: 1 }
781 // `,
782 // out: `<0>{a: {d:1}, b: {d:1}}`,
783
784 // TODO(P2): circular references in expressions can be resolved when
785 // unified with complete values.
786 // in: `
787 // a: 20
788 // a: b + 10 // 20 & b+10 ==> 20; where a == b+10
789 // b: a - 10 // 10-10 = 10 ==> 20 == 10+10
790 // `
791 // out: `<0>{a_0:20, b_1:10}`
792 }}
793 rewriteHelper(t, testCases, evalPartial)
794}
795
796func TestFullEval(t *testing.T) {
797 testCases := []testCase{{
798 desc: "detect conflicting value",
799 in: `
800 a: 8000.9
801 a: 7080 | int`,
802 out: `<0>{a: _|_(empty disjunction after evaluation)}`,
803 }, {
804 desc: "resolve all disjunctions",
805 in: `
806 service <Name>: {
807 name: Name | string
808 port: 7080 | int
809 }
810 service foo: _
811 service bar: { port: 8000 }
812 service baz: { name: "foobar" }
813 `,
814 out: `<0>{service: <1>{<>: <2>(Name: string)-><3>{name: (<2>.Name | string), port: (7080 | int)}, foo: <4>{name: "foo", port: 7080}, bar: <5>{name: "bar", port: 8000}, baz: <6>{name: "foobar", port: 7080}}}`,
815 }, {
816 desc: "field templates",
817 in: `
818 a: {
819 <name>: int
820 k: 1
821 }
822 b: {
823 <x>: { x: 0, y: 1 | int }
824 v: {}
825 w: { y: 0 }
826 }
827 b: { <y>: {} } // TODO: allow different name
828 c: {
829 <Name>: { name: Name, y: 1 }
830 foo: {}
831 bar: _
832 }
833 `,
834 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}}}`,
835 }, {
836 desc: "field comprehension",
837 in: `
838 a: { "\(k)": v for k, v in b if k < "d" if v > b.a }
839 b: {
840 a: 1
841 b: 2
842 c: 3
843 d: 4
844 }
845 c: {
846 "\(k)": v <-
847 for k, v in b
848 if k < "d"
849 if v > b.a
850 }
851 // TODO: Propagate error:
852 // e: { "\(k)": v for k, v in b if k < "d" if v > b.a }
853 `,
854 out: `<0>{a: <1>{b: 2, c: 3}, b: <2>{a: 1, b: 2, c: 3, d: 4}, c: <3>{b: 2, c: 3}}`,
855 }, {
856 desc: "nested templates in one field",
857 in: `
858 a <A> b <B>: {
859 name: A
860 kind: B
861 }
862 a "A" b "B": _
863 a "C" b "D": _
864 a "E" b "F": { c: "bar" }
865 `,
866 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"}}}, E: <19>{b: <20>{<>: <21>(B: string)-><22>{name: <23>.A, kind: <21>.B}, F: <24>{name: "E", kind: "F", c: "bar"}}}}}`,
867 }, {
868 desc: "template unification within one struct",
869 in: `
870 a: {
871 <A>: { name: A }
872 <A>: { kind: A }
873 }
874 a "A": _
875 a "C": _
876 a "E": { c: "bar" }
877 `,
878 out: `<0>{a: <1>{<>: <2>(A: string)->(<3>{name: <2>.A} & <4>{kind: <2>.A}), ` +
879 `A: <5>{name: "A", kind: "A"}, ` +
880 `C: <6>{name: "C", kind: "C"}, ` +
881 `E: <7>{name: "E", kind: "E", c: "bar"}}}`,
882 }, {
883 desc: "field comprehensions with multiple keys",
884 in: `
885 a "\(x.a)" b "\(x.b)": x for x in [
886 {a: "A", b: "B" },
887 {a: "C", b: "D" },
888 {a: "E", b: "F" },
889 ]
890
891 "\(x.a)" "\(x.b)": x for x in [
892 {a: "A", b: "B" },
893 {a: "C", b: "D" },
894 {a: "E", b: "F" },
895 ]`,
896 out: `<0>{a: <1>{` +
897 `A: <2>{b: <3>{B: <4>{a: "A", b: "B"}}}, ` +
898 `C: <5>{b: <6>{D: <7>{a: "C", b: "D"}}}, ` +
899 `E: <8>{b: <9>{F: <10>{a: "E", b: "F"}}}}, ` +
900 `A: <11>{B: <12>{a: "A", b: "B"}}, ` +
901 `C: <13>{D: <14>{a: "C", b: "D"}}, ` +
902 `E: <15>{F: <16>{a: "E", b: "F"}}}`,
903 }, {
904 desc: "field comprehensions with templates",
905 in: `
906 num: 1
907 a: {
908 <A> <B>: {
909 name: A
910 kind: B
911 } if num < 5
912
913 }
914 a b c d: "bar"
915 `,
916 out: `<0>{num: 1, a: <1>{<>: <2>(A: string)-><3>{<>: <4>(B: string)-><5>{name: <2>.A, kind: <4>.B}, }, ` +
917 `b: <6>{<>: <7>(B: string)-><8>{name: <9>.A, kind: <7>.B}, ` +
918 `c: <10>{name: "b", kind: "c", ` +
919 `d: "bar"}}}}`,
920 }, {
921 desc: "disjunctions of lists",
922 in: `
923 l: [ int, int ] | [ string, string ]
924
925 l1: [ "a", "b" ]
926 l2: l & [ "c", "d" ]
927 `,
928 out: `<0>{l: [int,int], l1: ["a","b"], l2: ["c","d"]}`,
929 }, {
930 desc: "list comprehension",
931 in: `
932 // a: [ k for k: v in b if k < "d" if v > b.a ] // TODO test error using common iso colon
933 a: [ k for k, v in b if k < "d" if v > b.a ]
934 b: {
935 a: 1
936 b: 2
937 c: 3
938 d: 4
939 }
940 c: [ x for _, x in b for _, y in b if x < y ]
941 d: [ x for x, _ in a ]
942 `,
943 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]}`,
944 }, {
945 desc: "struct comprehension with template",
946 in: `
947 result: [ v for _, v in service ]
948
949 service <Name>: {
950 type: "service"
951 name: Name | string
952 port: 7080 | int
953 }
954 service foo: {}
955 service bar: { port: 8000 }
956 service baz: { name: "foobar" }
957 `,
958 out: `<0>{result: [` +
959 `<1>{type: "service", name: "foo", port: 7080},` +
960 `<2>{type: "service", name: "bar", port: 8000},` +
961 `<3>{type: "service", name: "foobar", port: 7080}], ` +
962
963 `service: <4>{` +
964 `<>: <5>(Name: string)-><6>{type: "service", name: (<5>.Name | string), port: (7080 | int)}, ` +
965 `foo: <7>{type: "service", name: "foo", port: 7080}, ` +
966 `bar: <8>{type: "service", name: "bar", port: 8000}, ` +
967 `baz: <9>{type: "service", name: "foobar", port: 7080}}}`,
968 }, {
969 desc: "resolutions in struct comprehension keys",
970 in: `
Marcel van Lohuizen76b92b52018-12-16 10:47:03 +0100971 a: { "\(b + ".")": "a" for _, b in ["c"] }
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100972 `,
973 out: `<0>{a: <1>{c.: "a"}}`,
974 }, {
975 desc: "recursive evaluation within list",
976 in: `
977 l: [a]
978 a: b & { c: "t" }
979 b: {
980 d: c
981 c: string
982 }
983 l1: [a1]
984 a1: b1 & { c: "t" }
985 b1: {
986 d: "s" + c
987 c: string
988 }
989 `,
990 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" + string):unsupported op +(string, (string)*))}}`,
991 }, {
992 desc: "ips",
993 in: `
994 IP: 4*[ 0..255 ]
995
996 Private:
997 [ 192, 168, 0..255, 0..255 ] |
998 [ 10, 0..255, 0..255, 0..255] |
999 [ 172, 16..32, 0..255, 0..255 ]
1000
1001 Inst: Private & [ _, 10, ... ]
1002
1003 MyIP: Inst & [_, _, 10, 10 ]
1004 `,
1005 out: `<0>{IP: 4*[(0..255)], Private: [192,168,(0..255),(0..255)], Inst: [10,10,(0..255),(0..255)], MyIP: [10,10,10,10]}`,
1006 }, {
1007 desc: "complex interaction of groundness",
1008 in: `
1009 res: [ y & { d: "b" } for x in a for y in x ]
1010 res: [ a.b.c & { d: "b" } ]
1011
1012 a b <C>: { d: string, s: "a" + d }
1013 a b c d: string
1014 `,
1015 // TODO(perf): unification should catch shared node.
1016 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" + string):unsupported op +(string, (string)*))}}}}`,
1017 }, {
1018 desc: "complex groundness 2",
1019 in: `
1020 r1: f1 & { y: "c" }
1021
1022 f1: { y: string, res: a.b.c & { d: y } }
1023
1024 a b c: { d: string, s: "a" + d }
1025 a b <C>: { d: string, s: "a" + d }
1026 a b c d: string
1027 `,
1028 out: `<0>{r1: <1>{y: "c", res: <2>{d: "c", s: "ac"}}, f1: <3>{y: string, res: <4>{d: string, s: _|_(("a" + string):unsupported op +(string, (string)*))}}, a: <5>{b: <6>{<>: <7>(C: string)-><8>{d: string, s: ("a" + <8>.d)}, c: <9>{d: string, s: _|_(("a" + string):unsupported op +(string, (string)*))}}}}`,
1029 }, {
1030 desc: "references from template to concrete",
1031 in: `
1032 res: [t]
1033 t <X>: {
1034 a: c + b.str
1035 b str: string
1036 c: "X"
1037 }
1038 t x: { b str: "DDDD" }
1039 `,
1040 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"}}}], ` +
1041 `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"}}}}`,
1042 }}
1043 rewriteHelper(t, testCases, evalFull)
1044}