Fix register callbacks in some cases

This commit is contained in:
Jinzhu 2014-08-27 16:10:33 +08:00
parent 922395c965
commit 0ed805099e
2 changed files with 36 additions and 29 deletions

View File

@ -88,7 +88,7 @@ func getRIndex(strs []string, str string) int {
} }
func sortProcessors(cps []*callback_processor) []*func(scope *Scope) { func sortProcessors(cps []*callback_processor) []*func(scope *Scope) {
var sortCallbackProcessor func(c *callback_processor, force bool) var sortCallbackProcessor func(c *callback_processor)
var names, sortedNames = []string{}, []string{} var names, sortedNames = []string{}, []string{}
for _, cp := range cps { for _, cp := range cps {
@ -104,7 +104,7 @@ func sortProcessors(cps []*callback_processor) []*func(scope *Scope) {
names = append(names, cp.name) names = append(names, cp.name)
} }
sortCallbackProcessor = func(c *callback_processor, force bool) { sortCallbackProcessor = func(c *callback_processor) {
if getRIndex(sortedNames, c.name) > -1 { if getRIndex(sortedNames, c.name) > -1 {
return return
} }
@ -114,7 +114,7 @@ func sortProcessors(cps []*callback_processor) []*func(scope *Scope) {
sortedNames = append(sortedNames[:index], append([]string{c.name}, sortedNames[index:]...)...) sortedNames = append(sortedNames[:index], append([]string{c.name}, sortedNames[index:]...)...)
} else if index := getRIndex(names, c.before); index > -1 { } else if index := getRIndex(names, c.before); index > -1 {
sortedNames = append(sortedNames, c.name) sortedNames = append(sortedNames, c.name)
sortCallbackProcessor(cps[index], true) sortCallbackProcessor(cps[index])
} else { } else {
sortedNames = append(sortedNames, c.name) sortedNames = append(sortedNames, c.name)
} }
@ -128,19 +128,19 @@ func sortProcessors(cps []*callback_processor) []*func(scope *Scope) {
if len(cp.before) == 0 { if len(cp.before) == 0 {
cp.before = c.name cp.before = c.name
} }
sortCallbackProcessor(cp, true) sortCallbackProcessor(cp)
} else { } else {
sortedNames = append(sortedNames, c.name) sortedNames = append(sortedNames, c.name)
} }
} }
if getRIndex(sortedNames, c.name) == -1 && force { if getRIndex(sortedNames, c.name) == -1 {
sortedNames = append(sortedNames, c.name) sortedNames = append(sortedNames, c.name)
} }
} }
for _, cp := range cps { for _, cp := range cps {
sortCallbackProcessor(cp, false) sortCallbackProcessor(cp)
} }
var funcs = []*func(scope *Scope){} var funcs = []*func(scope *Scope){}

View File

@ -36,42 +36,49 @@ func TestRegisterCallback(t *testing.T) {
} }
} }
func TestRegisterCallbackWithBasicOrder(t *testing.T) { func TestRegisterCallbackWithOrder(t *testing.T) {
var callback = &callback{processors: []*callback_processor{}} var callback1 = &callback{processors: []*callback_processor{}}
callback1.Create().Register("before_create1", before_create1)
callback1.Create().Register("create", create)
callback1.Create().Register("after_create1", after_create1)
callback1.Create().Before("after_create1").Register("after_create2", after_create2)
if !equalFuncs(callback1.creates, []string{"before_create1", "create", "after_create2", "after_create1"}) {
t.Errorf("register callback with order")
}
callback.Update().Register("create", create) var callback2 = &callback{processors: []*callback_processor{}}
callback.Update().Before("create").Register("before_create1", before_create1)
callback.Update().After("after_create2").Register("after_create1", after_create1)
callback.Update().Before("before_create1").Register("before_create2", before_create2)
callback.Update().Register("after_create2", after_create2)
if !equalFuncs(callback.updates, []string{"before_create2", "before_create1", "create", "after_create2", "after_create1"}) { callback2.Update().Register("create", create)
callback2.Update().Before("create").Register("before_create1", before_create1)
callback2.Update().After("after_create2").Register("after_create1", after_create1)
callback2.Update().Before("before_create1").Register("before_create2", before_create2)
callback2.Update().Register("after_create2", after_create2)
if !equalFuncs(callback2.updates, []string{"before_create2", "before_create1", "create", "after_create2", "after_create1"}) {
t.Errorf("register callback with order") t.Errorf("register callback with order")
} }
} }
func TestRegisterCallbackWithComplexOrder1(t *testing.T) { func TestRegisterCallbackWithComplexOrder(t *testing.T) {
var callback = &callback{processors: []*callback_processor{}} var callback1 = &callback{processors: []*callback_processor{}}
callback.Query().Before("after_create1").After("before_create1").Register("create", create) callback1.Query().Before("after_create1").After("before_create1").Register("create", create)
callback.Query().Register("before_create1", before_create1) callback1.Query().Register("before_create1", before_create1)
callback.Query().Register("after_create1", after_create1) callback1.Query().Register("after_create1", after_create1)
if !equalFuncs(callback.queries, []string{"before_create1", "create", "after_create1"}) { if !equalFuncs(callback1.queries, []string{"before_create1", "create", "after_create1"}) {
t.Errorf("register callback with order") t.Errorf("register callback with order")
} }
}
func TestRegisterCallbackWithComplexOrder2(t *testing.T) { var callback2 = &callback{processors: []*callback_processor{}}
var callback = &callback{processors: []*callback_processor{}}
callback.Delete().Before("after_create1").After("before_create1").Register("create", create) callback2.Delete().Before("after_create1").After("before_create1").Register("create", create)
callback.Delete().Before("create").Register("before_create1", before_create1) callback2.Delete().Before("create").Register("before_create1", before_create1)
callback.Delete().After("before_create1").Register("before_create2", before_create2) callback2.Delete().After("before_create1").Register("before_create2", before_create2)
callback.Delete().Register("after_create1", after_create1) callback2.Delete().Register("after_create1", after_create1)
callback.Delete().After("after_create1").Register("after_create2", after_create2) callback2.Delete().After("after_create1").Register("after_create2", after_create2)
if !equalFuncs(callback.deletes, []string{"before_create1", "before_create2", "create", "after_create1", "after_create2"}) { if !equalFuncs(callback2.deletes, []string{"before_create1", "before_create2", "create", "after_create1", "after_create2"}) {
t.Errorf("register callback with order") t.Errorf("register callback with order")
} }
} }