registry_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package registry_test
  2. import (
  3. "flow/internal/assert"
  4. "flow/registry"
  5. "testing"
  6. "github.com/gohxs/prettylog"
  7. )
  8. func init() {
  9. prettylog.Global()
  10. }
  11. func TestRegistry(t *testing.T) {
  12. a := assert.A(t)
  13. r := registry.New()
  14. r.Add("vecadd", dummy1)
  15. e, err := r.Entry("vecadd")
  16. a.Eq(err, nil, "fetching entry")
  17. d := e.Description
  18. a.Eq(len(d.Inputs), 2, "should have 2 outputs")
  19. a.Eq(d.Output.Type, "[]float32", "output type")
  20. t.Log(d)
  21. }
  22. func TestEntry(t *testing.T) {
  23. a := assert.A(t)
  24. r := registry.New()
  25. e, err := r.Entry("bogus")
  26. a.NotEq(err, nil, "should get an error")
  27. a.Eq(e, nil, "entry should be nil")
  28. }
  29. func TestRegisterDuplicate(t *testing.T) {
  30. a := assert.A(t)
  31. r := registry.New()
  32. r.Add("func", func(a, b int) int { return 0 })
  33. d := r.Add("func", func(b int) int { return 0 })
  34. a.Eq(d.Err, nil, "should allow duplicate")
  35. }
  36. func TestRegisterOutput(t *testing.T) {
  37. a := assert.A(t)
  38. r := registry.New()
  39. d := r.Add("func", func(a int) {})
  40. a.Eq(d.Err, nil, "should not give output error")
  41. }
  42. func TestRegisterInvalidInput(t *testing.T) {
  43. a := assert.A(t)
  44. r := registry.New()
  45. d := r.Add("func", func() int { return 0 })
  46. a.Eq(d.Err, nil, "should register a func without params")
  47. }
  48. func TestRegistryGet(t *testing.T) {
  49. a := assert.A(t)
  50. r := registry.New()
  51. d := r.Add("func", func() int { return 0 })
  52. a.Eq(d.Err, nil, "should register func")
  53. fn, err := r.Get("func")
  54. a.Eq(err, nil, "should fetch a function")
  55. a.NotEq(fn, nil, "fn should not be nil")
  56. }
  57. func TestRegistryGetEmpty(t *testing.T) {
  58. a := assert.A(t)
  59. r := registry.New()
  60. fn, err := r.Get("notfoundfunc")
  61. a.Eq(err, registry.ErrNotFound, "should fail fetching a unregistered func")
  62. a.Eq(fn, nil, "should return a <nil> func")
  63. }
  64. func TestRegistryGetConstructor(t *testing.T) {
  65. a := assert.A(t)
  66. r := registry.New()
  67. d := r.Add("func", func() func() int {
  68. return func() int {
  69. return 0
  70. }
  71. })
  72. a.Eq(d.Err, nil, "should register the constructor func")
  73. ifn, err := r.Get("func")
  74. a.Eq(err, nil, "get should not error")
  75. fn, ok := ifn.(func() int)
  76. a.Eq(ok, true, "function should be the constructor type")
  77. ret := fn()
  78. a.Eq(ret, 0, "function should return 0")
  79. }
  80. func TestRegistryGetConstructorParam(t *testing.T) {
  81. a := assert.A(t)
  82. r := registry.New()
  83. d := r.Add("func2", func(a, b int) func() int {
  84. return func() int {
  85. return a + b
  86. }
  87. })
  88. a.Eq(d.Err, nil)
  89. ifn, err := r.Get("func2", 1, 1)
  90. a.Eq(err, nil, "should not fail passing params to constructor")
  91. a.NotEq(ifn, nil, "should return a function")
  92. fn, ok := ifn.(func() int)
  93. a.Eq(ok, true, "function should match the type")
  94. ret := fn()
  95. a.Eq(ret, 2, "function should execute")
  96. }
  97. func TestDescriptions(t *testing.T) {
  98. a := assert.A(t)
  99. r := registry.New()
  100. r.Add("vecadd", dummy1)
  101. r.Add("vecstr", dummy2)
  102. d, err := r.Descriptions()
  103. a.Eq(err, nil, "should fetch Descriptions")
  104. a.Eq(len(d), 2, "should contain 2 descriptions")
  105. t.Log(d)
  106. }
  107. func TestClone(t *testing.T) {
  108. a := assert.A(t)
  109. r := registry.New()
  110. r.Add("vecadd", dummy1)
  111. desc, err := r.Descriptions()
  112. a.Eq(err, nil, "should not error fetching description")
  113. a.Eq(len(desc), 1, "should contain 1 descriptions")
  114. r2 := r.Clone()
  115. r2.Add("vecmul", dummy2)
  116. a.Eq(len(desc), 1, "should contain 1 descriptions")
  117. d2, err := r2.Descriptions()
  118. a.Eq(err, nil, "should not error fetching descriptions")
  119. a.Eq(len(d2), 2, "should contain 2 descriptions")
  120. _, ok := d2["vecmul"]
  121. a.Eq(ok, true, "should be equal")
  122. }
  123. func TestNotAFunc(t *testing.T) {
  124. a := assert.A(t)
  125. r := registry.New()
  126. d := r.Add("test", []string{})
  127. a.Eq(d.Err, registry.ErrNotAFunc, "should give a func error")
  128. d = r.Add("test")
  129. a.Eq(d.Err, registry.ErrNotAFunc, "should give a func error")
  130. d = r.Add([]string{})
  131. a.Eq(d.Err, registry.ErrNotAFunc, "should give a func error")
  132. }
  133. func TestRegisterErr(t *testing.T) {
  134. r := registry.New()
  135. d := r.Add("name", "notfunc")
  136. assert.Eq(t, d.Err, registry.ErrNotAFunc, "should give a func error")
  137. }
  138. /*func TestAddEntry(t *testing.T) {
  139. a := assert.A(t)
  140. r := registry.New()
  141. g, err := r.Register("vecadd", dummy1)
  142. //a.Eq(err, ErrNotAFunc, "should error giving a func")
  143. a.Eq(len(g), 0, "should contain 1 entry")
  144. r.Add(dummy2)
  145. d, err := r.Descriptions()
  146. a.Eq(err, nil, "should not error fetching descriptions")
  147. a.Eq(len(d), 2, "should contain 2 descriptions")
  148. }*/
  149. func dummy1([]float32, []float32) []float32 {
  150. return []float32{1, 3, 3, 7}
  151. }
  152. func dummy2([]float32) string {
  153. return ""
  154. }