Skip to content

Commit

Permalink
more pick tests, find bugs!
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed May 26, 2020
1 parent 54ac50e commit ed5f06a
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 31 deletions.
56 changes: 28 additions & 28 deletions lib/structural/pick.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ func PickValues(orig, pick cue.Value) (val cue.Value, err error) {

func cuepick_Values(indent, tag string, orig, pick cue.Value) (val ast.Node, err error) {
oKind := orig.Kind()
oLabel, _ := orig.Label()
//oLabel, _ := orig.Label()
pKind := pick.Kind()
pLabel, _ := pick.Label()
//pLabel, _ := pick.Label()

fmt.Printf("%spick: %q %q %q %q %q BEG\n", indent, tag, oLabel, oKind, pLabel, pKind)
// fmt.Printf("%spick: %q %q %q %q %q BEG\n", indent, tag, oLabel, oKind, pLabel, pKind)

if oKind != pKind {
return cuepick_Kinds(indent + " ", tag + "/kinds", orig, pick)
Expand All @@ -221,40 +221,40 @@ func cuepick_Values(indent, tag string, orig, pick cue.Value) (val ast.Node, err
return val, fmt.Errorf("Unknown Cue Type %q in cuepick_Values same type switch", pKind)
}

fmt.Printf("%spick: %s END\n", indent, tag)
// fmt.Printf("%spick: %s END\n", indent, tag)
return val, err
}

func cuepick_Structs(indent, tag string, orig, pick cue.Value) (val ast.Node, err error) {

oKind := orig.Kind()
oLabel, _ := orig.Label()
//oKind := orig.Kind()
//oLabel, _ := orig.Label()
pKind := pick.Kind()
pLabel, _ := pick.Label()
//pLabel, _ := pick.Label()

fmt.Printf("%sstructs: %q %q %q %q %q BEG\n", indent, tag, oLabel, oKind, pLabel, pKind)
// fmt.Printf("%sstructs: %q %q %q %q %q BEG\n", indent, tag, oLabel, oKind, pLabel, pKind)

fields := []ast.Decl{}
oStruct, _ := orig.Struct()
iter := oStruct.Fields()
for iter.Next() {
ol := iter.Label()
ov := iter.Value()
ok := ov.Kind()
// ok := ov.Kind()

// try to look it up
pv := pick.Lookup(ol)
pk := pv.Kind()

// print everything we decide on
fmt.Printf("%s field: %s %s %s %s %s\n", indent, ol, ov, ok, pv, pk)
// fmt.Printf("%s field: %s %s %s %s %s\n", indent, ol, ov, ok, pv, pk)
u := pv.Unify(ov)
fmt.Printf("%s unify: %s %s %s %s\n", indent, ol, pv, ov, u)
fmt.Printf("%s check: %s\n", indent, u.Equals(ov))
// fmt.Printf("%s unify: %s %s %s %s\n", indent, ol, pv, ov, u)
// fmt.Printf("%s check: %s\n", indent, u.Equals(ov))

// If we unified
if u.Equals(ov) {
fmt.Printf("%s equals: %q %s\n", indent, ol, ov)
// fmt.Printf("%s equals: %q %s\n", indent, ol, ov)
syntax := ov.Syntax()
field := &ast.Field {
Label: ast.NewString(ol),
Expand All @@ -265,7 +265,7 @@ func cuepick_Structs(indent, tag string, orig, pick cue.Value) (val ast.Node, er
}

if isBuiltin(pv) {
fmt.Println(indent, " Builtin! ", ol, pk, pv)
// fmt.Println(indent, " Builtin! ", ol, pk, pv)
}

// is it not found?
Expand All @@ -276,7 +276,7 @@ func cuepick_Structs(indent, tag string, orig, pick cue.Value) (val ast.Node, er
switch pk {

case pKind:
fmt.Printf("%s samekind: %q %s\n", indent, ol, pv)
// fmt.Printf("%s samekind: %q %s\n", indent, ol, pv)
nv, err := cuepick_Values(indent + " ", tag + "/pick", ov, pv)
if err != nil {
return val, err
Expand All @@ -290,7 +290,7 @@ func cuepick_Structs(indent, tag string, orig, pick cue.Value) (val ast.Node, er
// fmt.Printf("%s return: %q %s %v\n", indent, ol, nv, err)

default:
fmt.Printf("%s default: %q %s\n", indent, ol, pv)
// fmt.Printf("%s default: %q %s\n", indent, ol, pv)
syntax := pv.Syntax()
field := &ast.Field {
Label: ast.NewString(ol),
Expand All @@ -311,29 +311,29 @@ func cuepick_Structs(indent, tag string, orig, pick cue.Value) (val ast.Node, er
}

func cuepick_Lists(indent, tag string, orig, pick cue.Value) (val ast.Node, err error) {
oKind := orig.Kind()
oLabel, _ := orig.Label()
pKind := pick.Kind()
pLabel, _ := pick.Label()
//oKind := orig.Kind()
//oLabel, _ := orig.Label()
//pKind := pick.Kind()
//pLabel, _ := pick.Label()

fmt.Printf("%slists: %q %q %q %q %q BEG\n", indent, tag, oLabel, oKind, pLabel, pKind)
// fmt.Printf("%slists: %q %q %q %q %q BEG\n", indent, tag, oLabel, oKind, pLabel, pKind)



fmt.Printf("%slists: %s END\n", indent, tag)
// fmt.Printf("%slists: %s END\n", indent, tag)
return val, err
}

func cuepick_Kinds(indent, tag string, orig, pick cue.Value) (val ast.Node, err error) {
oKind := orig.Kind()
oLabel, _ := orig.Label()
//oKind := orig.Kind()
//oLabel, _ := orig.Label()
pKind := pick.Kind()
pLabel, _ := pick.Label()
//pLabel, _ := pick.Label()

fmt.Printf("%skinds: %q %q %q %q %q BEG\n", indent, tag, oLabel, oKind, pLabel, pKind)
// fmt.Printf("%skinds: %q %q %q %q %q BEG\n", indent, tag, oLabel, oKind, pLabel, pKind)

u := pick.Unify(orig)
fmt.Println("Unify", orig, pick, u)
// fmt.Println("Unify", orig, pick, u)
if u.Equals(pick) {
return orig.Syntax(), err
}
Expand All @@ -347,6 +347,6 @@ func cuepick_Kinds(indent, tag string, orig, pick cue.Value) (val ast.Node, err
return val, fmt.Errorf("Unknown Cue Type %q in cuepick_Values same type switch", pKind)
}

fmt.Printf("%skinds: %s END\n", indent, tag)
// fmt.Printf("%skinds: %s END\n", indent, tag)
return val, err
}
2 changes: 1 addition & 1 deletion lib/structural/pick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPickTestSuites(t *testing.T) {

func PickOp(name string, args cue.Value) (val cue.Value, err error) {
aSyn, aErr := cuetils.PrintCueValue(args)
fmt.Println(name, "args:", aSyn)
// fmt.Println(name, "args:", aSyn)
if aErr != nil {
fmt.Println(name, "args:", aSyn)
return val, aErr
Expand Down
28 changes: 28 additions & 0 deletions lib/structural/testdata/000_shared_examples.cue
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
@examples(shared)
#SharedExamples: {

a1: {
a: "a"
}
a2: {
a: "a"
b: "b"
}
a3: {
a: "a"
b: "b"
c: "c"
}
b1: {
a: "a"
}
b2: {
a: "a"
b: 1
}
b3: {
a: "a"
b: 1
c: d: "e"
}



A: {
a: "a"
b: "b"
Expand Down
64 changes: 62 additions & 2 deletions lib/structural/testdata/pick_cases.cue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package testdata

@cases(pick)
#PickCases: {
@group(simple)
simple: {
@group(original)
original: {
t_0001: {
args: {
orig: #SharedExamples.A
Expand All @@ -19,4 +19,64 @@ package testdata
ex: {a: "a"}
}
}

@group(simple)
simple: {
t_0001: {
args: {
orig: #SharedExamples.a1
pick: {a: "a"}
}
ex: { a: "a" }
}
t_0002: {
args: {
orig: #SharedExamples.a1
pick: {a: string}
}
ex: {a: "a"}
}
t_0003: {
args: {
orig: #SharedExamples.a1
pick: {a: =~ "[a-z]+"}
}
ex: {a: "a"}
}
t_0004: {
args: {
orig: #SharedExamples.a1
pick: {a: _}
}
ex: {a: "a"}
}
neg_diffval: {
args: {
orig: #SharedExamples.a1
pick: {a: "b"}
}
ex: {}
}
neg_difftype_1: {
args: {
orig: #SharedExamples.a1
pick: {a: int}
}
ex: {}
}
neg_difftype_1: {
args: {
orig: #SharedExamples.a1
pick: {a: number}
}
ex: {}
}
neg_diffregexp: {
args: {
orig: #SharedExamples.a1
pick: {a: =~ "[A-Z]+"}
}
ex: {}
}
}
}

0 comments on commit ed5f06a

Please sign in to comment.