Skip to content

Commit

Permalink
Implement Destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Jan 12, 2015
1 parent c1d558f commit 0e2ca2a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Table interface {
On(string, interface{}) error
Trigger(string, ...interface{}) error
Off(string, interface{})
Destroy(string) error
}

type table struct {
Expand Down Expand Up @@ -58,4 +59,12 @@ func (t *table) Off(name string, f interface{}) {
e.Off(f)
}

func (t *table) Destroy(name string) error {
if _, ok := t.events[name]; !ok {
return fmt.Errorf("%s event has not been defined yet.", name)
}
delete(t.events, name)
return nil
}

var _ Table = &table{}
24 changes: 24 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,27 @@ func TestTableOff(t *testing.T) {
t.Errorf("i expected 1, but got %d", i)
}
}

func TestTableDestroy(t *testing.T) {
ta := goevent.NewTable()
i := 0
ta.On("foo", func(j int) { i += j })

ta.Trigger("foo", 1)
err := ta.Destroy("foo")
if err != nil {
t.Error(err)
}
err = ta.Trigger("foo", 1)
if err == nil {
t.Errorf("should destroy event. but not destroy.")
}
if i != 1 {
t.Errorf("i expected 1, but got %d", i)
}

err = ta.Destroy("foo")
if err == nil {
t.Errorf("should return error when event has not been defined yet. but got nil")
}
}

0 comments on commit 0e2ca2a

Please sign in to comment.