Skip to content

Commit

Permalink
tests build, generated type info using gtigen
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Sep 13, 2023
1 parent c35c023 commit ee77807
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 93 deletions.
16 changes: 12 additions & 4 deletions ki.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ type Ki interface {
// given name -- Try version returns error on failure.
ParentByNameTry(name string) (Ki, error)

// ParentByType finds parent recursively up hierarchy, by type, and
// returns nil if not found. If embeds is true, then it looks for any
// type that embeds the given type at any level of anonymous embedding.
ParentByType(t *gti.Type, embeds bool) Ki

// ParentByTypeTry finds parent recursively up hierarchy, by type, and
// returns error if not found. If embeds is true, then it looks for any
// type that embeds the given type at any level of anonymous embedding.
ParentByTypeTry(t *gti.Type, embeds bool) (Ki, error)

//////////////////////////////////////////////////////////////////////////
// Children

Expand Down Expand Up @@ -162,15 +172,13 @@ type Ki interface {
// [ki.StartMiddle] to start in the middle (good default).
ChildByNameTry(name string, startIdx int) (Ki, error)

// todo: gti

// ChildByType returns first element that has given type, nil if not found.
// If embeds is true, then it looks for any type that embeds the given type
// at any level of anonymous embedding.
// startIdx arg allows for optimized bidirectional find if you have
// an idea where it might be -- can be key speedup for large lists -- pass
// [ki.StartMiddle] to start in the middle (good default).
// ChildByType(t *gti.Type, embeds bool, startIdx int) Ki
ChildByType(t *gti.Type, embeds bool, startIdx int) Ki

// ChildByTypeTry returns first element that has given name -- Try version
// returns error message if not found.
Expand All @@ -179,7 +187,7 @@ type Ki interface {
// startIdx arg allows for optimized bidirectional find if you have
// an idea where it might be -- can be key speedup for large lists -- pass
// [ki.StartMiddle] to start in the middle (good default).
// ChildByTypeTry(t *gti.Type, embeds bool, startIdx int) (Ki, error)
ChildByTypeTry(t *gti.Type, embeds bool, startIdx int) (Ki, error)

//////////////////////////////////////////////////////////////////////////
// Paths
Expand Down
31 changes: 10 additions & 21 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,28 +219,23 @@ func (n *Node) ParentByNameTry(name string) (Ki, error) {
return nil, fmt.Errorf("ki %v: Parent name: %v not found", n.Nm, name)
}

/*
todo: these are not available generically, but are used in several
places in gi -- need to write a Gi-specific version for different
types using the embed logic.
// ParentByType finds parent recursively up hierarchy, by type, and
// returns nil if not found. If embeds is true, then it looks for any
// type that embeds the given type at any level of anonymous embedding.
func (n *Node) ParentByType(t *gti.Type, embeds bool) Ki {
if IsRoot(n) {
return nil
}
if embeds {
if TypeEmbeds(n.Par, t) {
return n.Par
}
} else {
if n.Par.Type() == t {
return n.Par
}
// todo: gti
// if embeds {
// if TypeEmbeds(n.Par, t) {
// return n.Par
// }
// } else {
if n.Par.Type() == t {
return n.Par
}
// }
return n.Par.ParentByType(t, embeds)
}

Expand All @@ -254,7 +249,6 @@ func (n *Node) ParentByTypeTry(t *gti.Type, embeds bool) (Ki, error) {
}
return nil, fmt.Errorf("ki %v: Parent of type: %v not found", n.Nm, t)
}
*/

//////////////////////////////////////////////////////////////////////////
// Children
Expand Down Expand Up @@ -322,10 +316,6 @@ func (n *Node) ChildByNameTry(name string, startIdx int) (Ki, error) {
return n.Kids[idx], nil
}

/*
todo: gti
// ChildByType returns first element that has given type, nil if not found.
// If embeds is true, then it looks for any type that embeds the given type
// at any level of anonymous embedding.
Expand All @@ -346,11 +336,10 @@ func (n *Node) ChildByType(t *gti.Type, embeds bool, startIdx int) Ki {
func (n *Node) ChildByTypeTry(t *gti.Type, embeds bool, startIdx int) (Ki, error) {
idx, ok := n.Kids.IndexByType(t, embeds, startIdx)
if !ok {
return nil, fmt.Errorf("ki %v: child of type: %t not found", n.Nm, t)
return nil, fmt.Errorf("ki %v: child of type: %s not found", n.Nm, t.Name)
}
return n.Kids[idx], nil
}
*/

//////////////////////////////////////////////////////////////////////////
// Paths
Expand Down
64 changes: 32 additions & 32 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package ki_test

import (
"bytes"
"fmt"
"io/ioutil"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -675,7 +673,7 @@ func TestProps(t *testing.T) {
if !ok {
t.Errorf("TestProps error -- floatprop inherited not found\n")
}
spropf, ok := sprop(float64)
spropf, ok := sprop.(float64)
if !ok || spropf != 42.0 {
t.Errorf("TestProps error -- floatprop inherited %v != %v\n", spropf, 42.0)
}
Expand Down Expand Up @@ -898,28 +896,30 @@ func TestClone(t *testing.T) {
parent.NewChild(typ, "child1")
child2.NewChild(typ, "subchild1")

var buf bytes.Buffer
err := parent.WriteJSON(&buf, true)
if err != nil {
t.Error(err)
// } else {
// fmt.Printf("json output:\n%v\n", string(buf.Bytes()))
}
b := buf.Bytes()
/*
var buf bytes.Buffer
err := parent.WriteJSON(&buf, true)
if err != nil {
t.Error(err)
// } else {
// fmt.Printf("json output:\n%v\n", string(buf.Bytes()))
}
b := buf.Bytes()
tstload := parent.Clone()
var buf2 bytes.Buffer
err = tstload.WriteJSON(&buf2, true)
if err != nil {
t.Error(err)
}
tstb := buf2.Bytes()
// fmt.Printf("test loaded json output: %v\n", string(tstb))
if !bytes.Equal(tstb, b) {
t.Error("original and unmarshal'd json rep are not equivalent")
ioutil.WriteFile("/tmp/jsonout1", b, 0644)
ioutil.WriteFile("/tmp/jsonout2", tstb, 0644)
}
tstload := parent.Clone()
var buf2 bytes.Buffer
err = tstload.WriteJSON(&buf2, true)
if err != nil {
t.Error(err)
}
tstb := buf2.Bytes()
// fmt.Printf("test loaded json output: %v\n", string(tstb))
if !bytes.Equal(tstb, b) {
t.Error("original and unmarshal'd json rep are not equivalent")
ioutil.WriteFile("/tmp/jsonout1", b, 0644)
ioutil.WriteFile("/tmp/jsonout2", tstb, 0644)
}
*/
}

// BuildGuiTreeSlow builds a tree that is typical of GUI structures where there are
Expand Down Expand Up @@ -973,21 +973,21 @@ var NParts = 5

func BenchmarkBuildGuiTree_NodeEmbed(b *testing.B) {
for n := 0; n < b.N; n++ {
wt := BuildGuiTree(NWidgets, NParts, TypeNodeEmbed)
wt := BuildGuiTree(NWidgets, NParts, testdata.NodeEmbedType)
TestGUITree_NodeEmbed = wt
}
}

func BenchmarkBuildGuiTree_NodeField(b *testing.B) {
for n := 0; n < b.N; n++ {
wt := BuildGuiTree(NWidgets, NParts, TypeNodeField)
wt := BuildGuiTree(NWidgets, NParts, testdata.NodeFieldType)
TestGUITree_NodeField = wt
}
}

func BenchmarkBuildGuiTree_NodeField2(b *testing.B) {
for n := 0; n < b.N; n++ {
wt := BuildGuiTree(NWidgets, NParts, TypeNodeField2)
wt := BuildGuiTree(NWidgets, NParts, testdata.NodeField2Type)
TestGUITree_NodeField2 = wt
}
}
Expand All @@ -996,7 +996,7 @@ func BenchmarkBuildGuiTreeSlow_NodeEmbed(b *testing.B) {
// prof.Reset()
// prof.Profiling = true
for n := 0; n < b.N; n++ {
wt := BuildGuiTreeSlow(NWidgets, NParts, TypeNodeEmbed)
wt := BuildGuiTreeSlow(NWidgets, NParts, testdata.NodeEmbedType)
TestGUITree_NodeEmbed = wt
}
// prof.Report(time.Millisecond)
Expand All @@ -1008,7 +1008,7 @@ func BenchmarkFuncDownMeFirst_NodeEmbed(b *testing.B) {
nnodes := 0
for n := 0; n < b.N; n++ {
wt.FuncDownMeFirst(0, nil, func(k Ki, level int, d any) bool {
k.ClearFlag(int(Updating))
k.SetFlag(false, Updating)
nnodes++
return Continue
})
Expand All @@ -1022,7 +1022,7 @@ func BenchmarkFuncDownMeFirst_NodeField(b *testing.B) {
nnodes := 0
for n := 0; n < b.N; n++ {
wt.FuncDownMeFirst(0, nil, func(k Ki, level int, d any) bool {
k.ClearFlag(int(Updating))
k.SetFlag(false, Updating)
nnodes++
return Continue
})
Expand All @@ -1036,7 +1036,7 @@ func BenchmarkFuncDownMeFirst_NodeField2(b *testing.B) {
nnodes := 0
for n := 0; n < b.N; n++ {
wt.FuncDownMeFirst(0, nil, func(k Ki, level int, d any) bool {
k.ClearFlag(int(Updating))
k.SetFlag(false, Updating)
nnodes++
return Continue
})
Expand All @@ -1047,7 +1047,7 @@ func BenchmarkFuncDownMeFirst_NodeField2(b *testing.B) {

func BenchmarkNewOfType(b *testing.B) {
for n := 0; n < b.N; n++ {
n := NewOfType(TypeNode)
n := NewOfType(NodeType)
n.InitName(n, "")
}
}
Expand Down
9 changes: 6 additions & 3 deletions signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import (
"fmt"
"reflect"
"testing"

. "goki.dev/ki/v2"
"goki.dev/ki/v2/testdata"
)

func TestSignalConnect(t *testing.T) {
parent := TestNode{}
parent := testdata.TestNode{}
parent.InitName(&parent, "par1")
typ := parent.Type()
child1 := parent.NewChild(typ, "child1")
// child2 := parent.NewChild(nil, "child2")

// note: now that signal is a map, cannot test reliably due to ordering
res := make([]string, 0, 10)
parent.sig1.Connect(child1, func(receiver, sender Ki, sig int64, data any) {
parent.Sig1.Connect(child1, func(receiver, sender Ki, sig int64, data any) {
res = append(res, fmt.Sprintf("recv: %v, sender: %v sig: %v data: %v",
receiver.Name(), sender.Name(), NodeSignals(sig), data))
})
Expand All @@ -28,7 +31,7 @@ func TestSignalConnect(t *testing.T) {
// receiver.Name(), sender.Name(), NodeSignals(sig), data))
// })

parent.sig1.Emit(&parent, int64(NodeSignalNil), 1234)
parent.Sig1.Emit(&parent, int64(NodeSignalNil), 1234)

// fmt.Printf("res: %v\n", res)
trg := []string{"recv: child1, sender: par1 sig: NodeSignalNil data: 1234"}
Expand Down
61 changes: 30 additions & 31 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package ki

import (
"fmt"

"goki.dev/gti"
)

// Slice is just a slice of ki elements: []Ki, providing methods for accessing
Expand Down Expand Up @@ -156,16 +158,15 @@ func (sl *Slice) IndexByName(name string, startIdx int) (int, bool) {
return sl.IndexByFunc(startIdx, func(ch Ki) bool { return ch.Name() == name })
}

// todo: gti

// // IndexByType returns index of element that either is that type or embeds
// // that type, false if not found. See IndexOf for info on startIdx.
// func (sl *Slice) IndexByType(t *gti.Type, embeds bool, startIdx int) (int, bool) {
// if embeds {
// return sl.IndexByFunc(startIdx, func(ch Ki) bool { return TypeEmbeds(ch, t) })
// }
// return sl.IndexByFunc(startIdx, func(ch Ki) bool { return Type(ch) == t })
// }
// IndexByType returns index of element that either is that type or embeds
// that type, false if not found. See IndexOf for info on startIdx.
func (sl *Slice) IndexByType(t *gti.Type, embeds bool, startIdx int) (int, bool) {
// todo: gti
// if embeds {
// return sl.IndexByFunc(startIdx, func(ch Ki) bool { return TypeEmbeds(ch, t) })
// }
return sl.IndexByFunc(startIdx, func(ch Ki) bool { return ch.Type() == t })
}

// ElemByName returns first element that has given name, nil if not found.
// See IndexOf for info on startIdx.
Expand All @@ -187,27 +188,25 @@ func (sl *Slice) ElemByNameTry(name string, startIdx int) (Ki, error) {
return (*sl)[idx], nil
}

// todo: gti

// // ElemByType returns index of element that either is that type or embeds
// // that type, nil if not found. See IndexOf for info on startIdx.
// func (sl *Slice) ElemByType(t *gti.Type, embeds bool, startIdx int) Ki {
// idx, ok := sl.IndexByType(t, embeds, startIdx)
// if !ok {
// return nil
// }
// return (*sl)[idx]
// }
//
// // ElemByTypeTry returns index of element that either is that type or embeds
// // that type, error if not found. See IndexOf for info on startIdx.
// func (sl *Slice) ElemByTypeTry(t *gti.Type, embeds bool, startIdx int) (Ki, error) {
// idx, ok := sl.IndexByType(t, embeds, startIdx)
// if !ok {
// return nil, fmt.Errorf("ki.Slice: element of type: %v not found", t)
// }
// return (*sl)[idx], nil
// }
// ElemByType returns index of element that either is that type or embeds
// that type, nil if not found. See IndexOf for info on startIdx.
func (sl *Slice) ElemByType(t *gti.Type, embeds bool, startIdx int) Ki {
idx, ok := sl.IndexByType(t, embeds, startIdx)
if !ok {
return nil
}
return (*sl)[idx]
}

// ElemByTypeTry returns index of element that either is that type or embeds
// that type, error if not found. See IndexOf for info on startIdx.
func (sl *Slice) ElemByTypeTry(t *gti.Type, embeds bool, startIdx int) (Ki, error) {
idx, ok := sl.IndexByType(t, embeds, startIdx)
if !ok {
return nil, fmt.Errorf("ki.Slice: element of type: %v not found", t)
}
return (*sl)[idx], nil
}

// SliceInsert item at index -- does not do any parent updating etc -- use Ki/Node
// method unless you know what you are doing.
Expand Down
Loading

0 comments on commit ee77807

Please sign in to comment.