github.com/sandwich-go/boost@v1.3.29/plugin/plugin_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  )
     9  
    10  type mockEntry struct{ num int }
    11  
    12  type before interface {
    13  	Name() string
    14  	Before()
    15  	Context() context.Context
    16  }
    17  
    18  type filter interface {
    19  	Name() string
    20  	Filter(*mockEntry) *mockEntry
    21  	Context() context.Context
    22  }
    23  
    24  type mockBefore struct{ ctx context.Context }
    25  
    26  func newMockBefore(ctx context.Context) before { return &mockBefore{ctx: ctx} }
    27  func (b mockBefore) Name() string              { return "Before" }
    28  func (b mockBefore) Context() context.Context  { return b.ctx }
    29  func (b *mockBefore) Before()                  { b.ctx = context.WithValue(b.ctx, b.Name(), struct{}{}) }
    30  
    31  type mockFilter struct {
    32  	ctx    context.Context
    33  	maxNum int
    34  }
    35  
    36  func newMockFilter(ctx context.Context, maxNum int) filter {
    37  	return &mockFilter{ctx: ctx, maxNum: maxNum}
    38  }
    39  func (b mockFilter) Name() string             { return "Filter" }
    40  func (b mockFilter) Context() context.Context { return b.ctx }
    41  func (b *mockFilter) Filter(in *mockEntry) *mockEntry {
    42  	b.ctx = context.WithValue(b.ctx, b.Name(), struct{}{})
    43  	if in.num > b.maxNum {
    44  		return nil
    45  	}
    46  	return in
    47  }
    48  
    49  func TestPlugin(t *testing.T) {
    50  	Convey("Test Plugin", t, func(c C) {
    51  		cc := New(new(before), new(filter))
    52  		So(cc.Add(new(mockEntry)), ShouldNotBeNil)
    53  		So(func() { cc.MustAdd(new(mockEntry)) }, ShouldPanic)
    54  
    55  		maxNum := 2
    56  		ctx := context.Background()
    57  		mockBeforeObj := newMockBefore(ctx)
    58  		mockFilterObj := newMockFilter(ctx, maxNum)
    59  		So(cc.Add(mockBeforeObj), ShouldBeNil)
    60  		cc.MustAdd(mockFilterObj)
    61  
    62  		var es = make([]*mockEntry, 0)
    63  		for i := 0; i <= maxNum; i++ {
    64  			es = append(es, &mockEntry{num: i + 1})
    65  		}
    66  
    67  		cc.Range(func(plugin Plugin) bool {
    68  			if p, ok := plugin.(before); ok {
    69  				p.Before()
    70  			}
    71  			return true
    72  		})
    73  
    74  		cc.Range(func(plugin Plugin) bool {
    75  			if p, ok := plugin.(filter); ok {
    76  				var out = make([]*mockEntry, 0, len(es))
    77  				for _, e := range es {
    78  					if e = p.Filter(e); e != nil {
    79  						out = append(out, e)
    80  					}
    81  				}
    82  				So(len(out)+1, ShouldEqual, len(es))
    83  			}
    84  			return true
    85  		})
    86  
    87  		So(mockBeforeObj.Context().Value(mockBeforeObj.Name()), ShouldNotBeNil)
    88  		So(mockFilterObj.Context().Value(mockFilterObj.Name()), ShouldNotBeNil)
    89  	})
    90  }