Skip to content

Commit

Permalink
entry test
Browse files Browse the repository at this point in the history
  • Loading branch information
ghedamat committed Jan 9, 2014
1 parent aafe015 commit 4fba075
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 8 deletions.
19 changes: 19 additions & 0 deletions i3status/entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package i3status

import (
"encoding/json"
)

type Entry struct {
Name string `json:"name"`
Instance string `json:"instance"`
Button int `json:"button"`
X int `json:"x"`
Y int `json:"y"`
}

func NewEntry(str string) *Entry {
var e Entry
json.Unmarshal([]byte(str), &e)
return &e
}
40 changes: 40 additions & 0 deletions i3status/on_off.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package i3status

import (
"fmt"
"strconv"
"time"
)

type OnOffWidget struct {
BaseWidget
Input chan Entry
}

func NewOnOffWidget(output chan Message, input chan Entry) *OnOffWidget {
instanceCount++
w := OnOffWidget{
BaseWidget{
output,
1000,
instanceCount,
},
input,
}
return &w
}

func (w *OnOffWidget) basicLoop() {
msg := NewMessage()
msg.Name = "Date"
msg.Color = "#ffffff"
msg.Instance = strconv.Itoa(w.Instance)
for {
msg.FullText = fmt.Sprintf("%s", time.Now())
w.Output <- *msg
time.Sleep(w.Refresh * time.Millisecond)
}
}
func (w *OnOffWidget) Start() {
go w.basicLoop()
}
27 changes: 27 additions & 0 deletions test/entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package i3status_test

import (
"github.com/ghedamat/go-i3status/i3status"
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func TestEntry(t *testing.T) {
Convey("Given a json string", t, func() {
str := `{
"name": "ethernet",
"instance": "eth0",
"button": 1,
"x": 1320,
"y": 1400
}`
Convey("When NewEntry is called", func() {
e := i3status.NewEntry(str)
Convey("a new Entry is created", func() {
So(e.Name, ShouldEqual, "ethernet")
So(e.Instance, ShouldEqual, "eth0")
So(e.Button, ShouldEqual, 1)
})
})
})
}
21 changes: 21 additions & 0 deletions test/on_off_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package i3status_test

import (
"github.com/ghedamat/go-i3status/i3status"
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func TestOnOffWidgetConstructor(t *testing.T) {
Convey("Given an input and an output channel", t, func() {
c := make(chan i3status.Message)
i := make(chan i3status.Entry)
Convey("When OnOff is created", func() {
w := i3status.NewOnOffWidget(c, i)
Convey("input and output channel are available", func() {
So(w.Input, ShouldEqual, i)
So(w.Output, ShouldEqual, c)
})
})
})
}
Binary file added test/test.test
Binary file not shown.
8 changes: 0 additions & 8 deletions test/widget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ import (
)

func TestWidgetConstructor(t *testing.T) {
Convey("Given a widget", t, func() {
Convey("When it is created", func() {
Convey("a channel for incoming events is returned", func() {
So("", ShouldEqual, "implement me")
})
})
})

Convey("Given a Message channel", t, func() {
c := make(chan i3status.Message)
Convey("When a widget is created and a channel is passed", func() {
Expand Down

0 comments on commit 4fba075

Please sign in to comment.