entry_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package registry_test
  2. import (
  3. "flow"
  4. "flow/internal/assert"
  5. "flow/registry"
  6. "testing"
  7. )
  8. func TestFlowFunc(t *testing.T) {
  9. a := assert.A(t)
  10. r := registry.New()
  11. f := flow.New()
  12. f.SetRegistry(r)
  13. r.Add("flowfunc", func(paramFlow *flow.Flow) int {
  14. a.Eq(paramFlow, f, "Flow should be equal")
  15. return 0
  16. })
  17. op, err := f.Op("flowfunc")
  18. a.Eq(err, nil, "should not error fetching operator")
  19. op.Process()
  20. }
  21. func TestNewEntryInvalid(t *testing.T) {
  22. a := assert.A(t)
  23. r := registry.New()
  24. _, err := registry.NewEntry(r, "string")
  25. a.Eq(err, registry.ErrNotAFunc, "entry is not a function")
  26. }
  27. func TestNewEntryValid(t *testing.T) {
  28. a := assert.A(t)
  29. r := registry.New()
  30. _, err := registry.NewEntry(r, func(a int) int { return 0 })
  31. a.Eq(err, nil, "fetching an entry")
  32. }
  33. func TestDescription(t *testing.T) {
  34. a := assert.A(t)
  35. r := registry.New()
  36. e, err := registry.NewEntry(r, func(a int) int { return 0 })
  37. a.Eq(err, nil, "should not fail creating new entry")
  38. e.Describer().Tags("a", "b")
  39. a.Eq(len(e.Description.Tags), 2, "should have 2 categories")
  40. a.Eq(len(e.Description.Inputs), 1, "Should have 2 input description")
  41. e2, err := registry.NewEntry(r, func(a, b int) int { return 0 })
  42. a.Eq(err, nil, "should not fail creating new entry")
  43. a.Eq(len(e2.Description.Inputs), 2, "Should have 2 input description")
  44. e.Describer().Inputs("input")
  45. a.Eq(e.Description.Inputs[0].Name, "input", "should have the input description")
  46. e.Describer().Inputs("input", "2", "3")
  47. a.Eq(len(e.Description.Inputs), 1, "should have only one input")
  48. e.Describer().Output("output name")
  49. a.Eq(e.Description.Output.Name, "output name", "output description should be the same")
  50. e.Describer().Extra("test", 123)
  51. a.Eq(e.Description.Extra["test"], 123, "extra text should be as expected")
  52. e.Describer().Description("test")
  53. }
  54. func TestEntryBatch(t *testing.T) {
  55. a := assert.A(t)
  56. r := registry.New()
  57. d := registry.Describer(
  58. r.Add(func() int { return 0 }),
  59. r.Add(func() int { return 0 }),
  60. r.Add(func() int { return 0 }),
  61. ).Tags("test").Extra("name", 1)
  62. a.Eq(d.Err, nil, "should not error registering funcs")
  63. a.Eq(len(d.Entries()), 3, "should have 3 items")
  64. for _, e := range d.Entries() {
  65. a.Eq(e.Description.Tags[0], "test", "It should be of category test")
  66. a.Eq(e.Description.Extra["name"], 1, "It should contain extra")
  67. }
  68. }