github.com/goki/ki@v1.1.11/ki/signal_test.go (about)

     1  // Copyright (c) 2018, The GoKi Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ki
     6  
     7  import (
     8  	"fmt"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/goki/ki/kit"
    13  )
    14  
    15  type TestNode struct {
    16  	Node
    17  	sig1 Signal
    18  	sig2 Signal
    19  }
    20  
    21  var KiTTestNode = kit.Types.AddType(&TestNode{}, nil)
    22  
    23  func TestSignalConnect(t *testing.T) {
    24  	parent := TestNode{}
    25  	parent.InitName(&parent, "par1")
    26  	typ := reflect.TypeOf(parent)
    27  	child1 := parent.AddNewChild(typ, "child1")
    28  	// child2 := parent.AddNewChild(nil, "child2")
    29  
    30  	// note: now that signal is a map, cannot test reliably due to ordering
    31  	res := make([]string, 0, 10)
    32  	parent.sig1.Connect(child1, func(receiver, sender Ki, sig int64, data interface{}) {
    33  		res = append(res, fmt.Sprintf("recv: %v, sender: %v sig: %v data: %v",
    34  			receiver.Name(), sender.Name(), NodeSignals(sig), data))
    35  	})
    36  	// parent.sig1.Connect(child2, func(receiver, sender Ki, sig int64, data interface{}) {
    37  	// 	res = append(res, fmt.Sprintf("recv: %v, sender: %v sig: %v data: %v",
    38  	// 		receiver.Name(), sender.Name(), NodeSignals(sig), data))
    39  	// })
    40  
    41  	parent.sig1.Emit(&parent, int64(NodeSignalNil), 1234)
    42  
    43  	// fmt.Printf("res: %v\n", res)
    44  	trg := []string{"recv: child1, sender: par1 sig: NodeSignalNil data: 1234"}
    45  	if !reflect.DeepEqual(res, trg) {
    46  		t.Errorf("Add child sigs error -- results: %v != target: %v\n", res, trg)
    47  	}
    48  	res = res[:0]
    49  
    50  	// time.Sleep(time.Second * 2)
    51  }
    52  
    53  func TestSignalNameToInt(t *testing.T) {
    54  	for i := NodeSignalNil; i < NodeSignalsN; i++ {
    55  		st := NodeSignals(i)
    56  		str := st.String()
    57  		stc := NodeSignalNil
    58  		err := stc.FromString(str)
    59  		if err != nil {
    60  			t.Error(err)
    61  		}
    62  		stnm := stc.String()
    63  		if stnm != str {
    64  			t.Errorf("could not convert from signal type name %v -- got: %v -- maybe need to run go generate?", str, stnm)
    65  		}
    66  	}
    67  
    68  	str := "NodeSignalUpdated"
    69  	stc := NodeSignalNil
    70  	stc.FromString(str)
    71  	if stc.String() != str {
    72  		t.Errorf("could not convert from signal type name %v -- got: %v -- maybe need to run go generate?", str, stc.String())
    73  	}
    74  }