Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for event on function with receiver #3

Merged
merged 4 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .travis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: go
go:
- 1.8
install:
- go get github.com/stretchr/testify/assert
script:
- go install;
- go test -v;
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ event and Trigger it from anywhere you want.

### Get The Package
```bash

$ go get -u github.com/sadlil/go-trigger

```

### How to switch to a specific version
`go get` the package. go to the package directory in your $GOPATH/src.
`go get` the package. Go to the package directory in your $GOPATH/src.
Change the tag using git.
`go install` the package.

Expand All @@ -19,7 +17,6 @@ $ go get -u github.com/sadlil/go-trigger
$ cd $GOPATH/src/github.com/sadlil/go-trigger
$ git checkout tags/<tag_name>
$ go install

```
#### Currently [available Tags](https://github.com/sadlil/go-trigger/releases)
- [v0.01](https://github.com/sadlil/go-trigger/releases/tag/v0.01)
Expand All @@ -33,8 +30,8 @@ $ go install

### How To Use
#### 1. Global Events
Import the package into your code. Add the events with `trigger.On` method.
And call that event handler with `trigger.Fire` method. All the event added
Import the package into your code. Add events with `trigger.On` method.
And call that event handler with `trigger.Fire` method. All the events added
like this will be global events. You can call `Fire` from anywhere.

````go
Expand All @@ -57,14 +54,14 @@ func main() {
```


You can define Your events from another package
You can define your events from another package
```go
trigger.On("second-event", packagename.FunctionName)
trigger.Fire("second-event")
```


You can Define events with parameteres and return types.
You can define events with parameteres and return types.
```go
func TestFunc(a, b int) int {
return a + b
Expand All @@ -79,9 +76,9 @@ values, err := trigger.Fire("third-event", 5, 6)
```


You can define your event in one package and trigger it another package. Your event and triggers are global.
Define anywhere, fire anywhere. You can define any function in any package as event u only need to
import the function's specified package where you defien the event. Where You trigger the event You do not
You can define your event in one package and trigger it another package. Your event and trigger are global.
Define anywhere, fire anywhere. You can define any function in any package as event you only need to
import the function's specified package where you define the event. Where you trigger the event, you do not
need to import it there.
```go
//---------------------------------------------
Expand Down Expand Up @@ -143,7 +140,7 @@ implementation of plugable `Trigger` interface.

Create a local trigger instance,

````go
```go
package main

import (
Expand Down Expand Up @@ -215,8 +212,7 @@ EventCount() int


### Under Development Features
1. Add support of Methods on structs events.
2. Multiple event handler for a event.
1. Multiple event handler for a event.

### Licence
Licenced under MIT Licence
Expand Down
31 changes: 31 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,34 @@ func TestNotFunc(t *testing.T) {
assert.NotNil(t, err)
assert.Equal(t, "task is not a function", err.Error())
}

type coder struct {
name string
}

func (c *coder) setName(name string) {
c.name = name
}

func (c *coder) getName() string {
return c.name
}

func TestFuncWithReceiver(t *testing.T) {
c := &coder{}

// add setName event
On("setName", c.setName)
// add getName event
On("getName", c.getName)

// trigger event to set name in "c"
Fire("setName", "aerokite")

// trigger event to get name from "c"
values, err := Fire("getName")
assert.Nil(t, err)
assert.Equal(t, 1, len(values))
assert.EqualValues(t, "aerokite", values[0].String())
assert.EqualValues(t, "aerokite", c.name)
}
3 changes: 2 additions & 1 deletion trigger_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package trigger

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDifferentEventHandlers(t *testing.T) {
Expand Down