Skip to content

Commit

Permalink
Merge pull request #43 from vinxi/test/config
Browse files Browse the repository at this point in the history
Add missing test for config package and handles sub-packages coverages
  • Loading branch information
h2non committed Jun 5, 2016
2 parents a1ba7f3 + bfbf5de commit ab535f2
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ go:
before_install:
- go get -u github.com/golang/lint/golint
- go get github.com/mattn/goveralls
- go get github.com/modocache/gover
- go get -t ./...

script:
- diff -u <(echo -n) <(gofmt -s -d ./)
- diff -u <(echo -n) <(go vet ./...)
- diff -u <(echo -n) <(golint ./...)
- go test -v -race ./...
# TODO: generate package-specific coverage
- go test -v -race -covermode=atomic -coverprofile=coverage.out

after_success:
- goveralls -coverprofile=coverage.out -service=travis-ci
- go list -f '{{if len .TestGoFiles}}"go test -v -race -covermode=atomic -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"{{end}}' ./... | xargs -L 1 sh -c
- gover
- goveralls -coverprofile=gover.coverprofile -service=travis-ci
96 changes: 96 additions & 0 deletions config/store_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,97 @@
package config

import (
"encoding/json"
"github.com/nbio/st"
"testing"
)

const (
key = "key"
)

func TestGetString(t *testing.T) {
c := Config{}
c.Set(key, "1")
st.Expect(t, "1", c.GetString(key))
}

func TestGetStringForMissingKey(t *testing.T) {
c := Config{}
st.Expect(t, "", c.GetString(key))
}

func TestGetBool(t *testing.T) {
c := Config{}
c.Set(key, true)
st.Expect(t, true, c.GetBool(key))
}

func TestGetBoolForMissingKey(t *testing.T) {
c := Config{}
st.Expect(t, false, c.GetBool(key))
}

func TestGetInt(t *testing.T) {
c := Config{}
c.Set(key, 1)
st.Expect(t, 1, c.GetInt(key))
}

func TestGetIntForMissingKey(t *testing.T) {
c := Config{}
st.Expect(t, 0, c.GetInt(key))
}

func TestGetInt64(t *testing.T) {
c := Config{}
c.Set(key, int64(1))
st.Expect(t, int64(1), c.GetInt64(key))
}

func TestGetInt64ForMissingKey(t *testing.T) {
c := Config{}
st.Expect(t, int64(0), c.GetInt64(key))
}

func TestGetFloat(t *testing.T) {
c := Config{}
c.Set(key, float64(1.1))
st.Expect(t, float64(1.1), c.GetFloat(key))
}

func TestGetFloatForMissingKey(t *testing.T) {
c := Config{}
st.Expect(t, float64(0), c.GetFloat(key))
}

func TestGet(t *testing.T) {
c := Config{}
c.Set(key, 1)
st.Expect(t, 1, c.Get(key))
}

func TestGetForMissingKey(t *testing.T) {
c := Config{}
st.Expect(t, nil, c.Get(key))
}

func TestJSON(t *testing.T) {
c := Config{}
c.Set(key, 1)
c.Set("another_key", "1")
expected := []byte(`{"another_key":"1","key":1}`)
res, err := c.JSON()
st.Expect(t, nil, err)
st.Expect(t, expected, res)
}

func TestJSONError(t *testing.T) {
c := Config{}
c.Set(key, make(chan bool))
res, err := c.JSON()
err, ok := err.(*json.UnsupportedTypeError)
st.Expect(t, true, ok)
st.Expect(t, "json: unsupported type: chan bool", err.Error())
st.Expect(t, 0, len(res))
}

0 comments on commit ab535f2

Please sign in to comment.