Skip to content
This repository has been archived by the owner on May 8, 2022. It is now read-only.

Commit

Permalink
add more test cases for package helper
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxiaohei committed Jan 29, 2017
1 parent 4aa3b44 commit 415f723
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 91 deletions.
19 changes: 19 additions & 0 deletions app/helper/gravatar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package helper

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestGravatar(t *testing.T) {
Convey("Gravatar", t, func() {
url := Gravatar("fuxiaohei@vip.qq.com", 50)
targetURL := "https://www.gravatar.com/avatar/f72f7454ce9d710baa506394f68f4132?size=50"
So(url, ShouldEqual, targetURL)

url2 := Gravatar("fuxiaohei@vip.qq.com", 0)
targetURL = "https://www.gravatar.com/avatar/f72f7454ce9d710baa506394f68f4132?size=80"
So(url2, ShouldEqual, targetURL)
})
}
90 changes: 0 additions & 90 deletions app/helper/helper_test.go

This file was deleted.

7 changes: 6 additions & 1 deletion app/helper/i18n.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package helper

import (
"errors"
"fmt"
"strings"

"github.com/BurntSushi/toml"
"gopkg.in/ini.v1"
)

var (
ErrI18nUnknownExt = errors.New("unknown i18n ext")
)

// I18n object
type I18n struct {
Lang string // language string
Expand Down Expand Up @@ -57,7 +62,7 @@ func NewI18n(lang string, data []byte, ext string) (*I18n, error) {
}
return &I18n{Lang: lang, values: maps}, nil
}
return nil, nil
return nil, ErrI18nUnknownExt
}

// NewI18nEmpty creates new empty i18n object,
Expand Down
19 changes: 19 additions & 0 deletions app/helper/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ about = "About"
source = "Source"`)

i18nIniBytes = []byte(`
"abc" = "123"
"meta.link" = "Link%s"
[meta]
Expand Down Expand Up @@ -81,6 +82,18 @@ func TestI18n(t *testing.T) {
So(i18n.Trim("/en/abc.html"), ShouldEqual, "abc.html")
So(i18n.Trim("/xyz.html"), ShouldEqual, "xyz.html")
})

Convey("Unknown", func() {
i18n, err := NewI18n("zh", i18nIniBytes, ".json")
So(i18n, ShouldBeNil)
So(err, ShouldEqual, ErrI18nUnknownExt)
})

Convey("ErrorData", func() {
i18n, err := NewI18n("en", []byte("abc"), ".toml")
So(i18n, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
}

Expand All @@ -100,5 +113,11 @@ func TestI18nIni(t *testing.T) {

tr = i18n.Trf("meta.link", "xyz")
So(tr, ShouldEqual, "Linkxyz")

Convey("ErrorIniData", func() {
i18n, err := NewI18n("en", []byte("abc"), ".ini")
So(i18n, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
}
20 changes: 20 additions & 0 deletions app/helper/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package helper

import (
"bytes"
"testing"

. "github.com/smartystreets/goconvey/convey"
"gopkg.in/inconshreveable/log15.v2"
)

func TestLog(t *testing.T) {
Convey("Log", t, func() {
var buf bytes.Buffer
l := log15.New()
l.SetHandler(log15.StreamHandler(&buf, LogfmtFormat()))
l.Debug("ABC|%s|%s|%s", "a", "b", "c")

So(buf.String(), ShouldContainSubstring, "ABC|a|b|c")
})
}
19 changes: 19 additions & 0 deletions app/helper/markdown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package helper

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestMarkdown(t *testing.T) {
Convey("Markdown", t, func() {
h1 := []byte("#h1")
So(string(Markdown(h1)), ShouldEqual, `<h1 id="h1">h1</h1>`+"\n")

code := []byte("```go\npackage main\n```")
So(string(Markdown(code)), ShouldEqual, `<pre><code class="language-go">package main
</code></pre>
`)
})
}
18 changes: 18 additions & 0 deletions app/helper/md5_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package helper

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestMd5(t *testing.T) {
Convey("Md5", t, func() {
str := Md5("123456")
So(str, ShouldEqual, "e10adc3949ba59abbe56e057f20f883e")

str, err := Md5File("md5.go")
So(err, ShouldBeNil)
So(str, ShouldEqual, "651e74ed7f68be2b642217a06fda6ec6")
})
}
61 changes: 61 additions & 0 deletions app/helper/pager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package helper

import (
"fmt"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestPager(t *testing.T) {
Convey("Pager", t, func() {
pager := NewPagerCursor(5, 99)

Convey("Page", func() {
page := pager.Page(3)
So(page.Begin, ShouldEqual, 10)
So(page.End, ShouldEqual, 15)

page = pager.Page(20)
So(page.End, ShouldEqual, 99)

page = pager.Page(-1)
So(page, ShouldBeNil)

page = pager.Page(1000)
So(page, ShouldBeNil)
})

Convey("Layout", func() {
page := pager.Page(3)
page.SetLayout("ppp%d")
So(page.URL(), ShouldEqual, "ppp3")

So(page.PrevURL(), ShouldEqual, "ppp2")
So(page.NextURL(), ShouldEqual, "ppp4")

page = pager.Page(1)
So(page.PrevURL(), ShouldEqual, "")

page = pager.Page(20)
So(page.NextURL(), ShouldEqual, "")
})

Convey("SizeCase", func() {
pager := NewPagerCursor(10, 100)
So(pager.pages, ShouldEqual, 10)
pager = NewPagerCursor(10, 98)
So(pager.pages, ShouldEqual, 10)
})

Convey("PagerItems", func() {
pager := NewPagerCursor(10, 100)
page := pager.Page(6)
page.SetLayout("aaa%d")
for i, item := range page.PageItems() {
So(item.Page, ShouldEqual, i+1)
So(item.Link, ShouldEqual, fmt.Sprintf("aaa%d", i+1))
}
})
})
}

0 comments on commit 415f723

Please sign in to comment.