1
2
3
4
5 package unify
6
7 import (
8 "strings"
9 "testing"
10 )
11
12 func TestValueFilter(t *testing.T) {
13 read := func(s string) *Value {
14 t.Helper()
15 c, err := Read(strings.NewReader(s), "<test>", ReadOpts{})
16 if err != nil {
17 t.Fatal(err)
18 }
19 return c.val
20 }
21
22 for _, test := range []struct {
23 probe, v string
24 want bool
25 }{
26 {"{a: x}", "{a: x}", true},
27 {"{a: x}", "{a: y}", false},
28 {"{a: x}", `{a: "x|y"}`, true},
29 {"{a: x}", `{a: "y|z"}`, false},
30 {`{a: "y|z"}`, "{a: x}", false},
31
32 {`{a: "w|x"}`, `{a: "y|z"}`, true},
33
34 {"{a: x, b: y}", "{b: z, c: x}", false},
35 {"{a: x}", "{b: y}", true},
36
37 {"{a: _, b: x}", "{a: {c: d}, b: y}", false},
38 {"{a: x, b: y}", "{a: _, b: z}", false},
39
40
41 {"{a: x, b: y}", "{a: {c: d}, b: z}", true},
42 {"{a: $v, b: y}", "{a: x, b: z}", true},
43 {"{a: [x], b: y}", "{a: [x], b: z}", true},
44
45 {"{a: x, b: {c: d}}", "{a: y, b: z}", false},
46
47 {"{a: x}", "x", true},
48 {"{a: x}", "$v", true},
49 } {
50 f := newValueFilter(read(test.probe))
51 if got := f.mayUnify(read(test.v)); got != test.want {
52 t.Errorf("filter %s: mayUnify(%s) = %v, want %v", test.probe, test.v, got, test.want)
53 }
54 }
55 }
56
57
58
59 func TestValueFilterError(t *testing.T) {
60 read := func(s string) Closure {
61 t.Helper()
62 c, err := Read(strings.NewReader(s), "<test>", ReadOpts{})
63 if err != nil {
64 t.Fatal(err)
65 }
66 return c
67 }
68 _, err := Unify(read("!sum [{a: x, b: y}, {a: {c: d}, b: z}]"), read("{a: x, b: y}"))
69 if err == nil || !strings.Contains(err.Error(), "kind mismatch") {
70 t.Errorf("want kind mismatch error, got %v", err)
71 }
72 }
73
View as plain text