提交 4c1dd1b9 编写于 作者: T tariqibrahim

Adding unit tests for util methods

上级 b15d8de5
......@@ -14,6 +14,8 @@ import (
"github.com/github/hub/ui"
)
var timeNow = time.Now
func Check(err error) {
if err != nil {
ui.Errorln(err)
......@@ -113,7 +115,7 @@ func (c *Color) Brightness() float32 {
}
func TimeAgo(t time.Time) string {
duration := time.Since(t)
duration := timeNow().Sub(t)
minutes := duration.Minutes()
hours := duration.Hours()
days := hours / 24
......
......@@ -3,6 +3,7 @@ package utils
import (
"github.com/bmizerany/assert"
"testing"
"time"
)
func TestSearchBrowserLauncher(t *testing.T) {
......@@ -16,3 +17,62 @@ func TestSearchBrowserLauncher(t *testing.T) {
func TestConcatPaths(t *testing.T) {
assert.Equal(t, "foo/bar/baz", ConcatPaths("foo", "bar", "baz"))
}
func TestTimeAgo(t *testing.T) {
timeNow = func() time.Time {
return time.Date(2018, 10, 28, 14, 34, 58, 651387237, time.UTC)
}
now := timeNow()
secAgo := now.Add(-1 * time.Second)
actual := TimeAgo(secAgo)
assert.Equal(t, "now", actual)
minAgo := now.Add(-1 * time.Minute)
actual = TimeAgo(minAgo)
assert.Equal(t, "1 minute ago", actual)
minsAgo := now.Add(-5 * time.Minute)
actual = TimeAgo(minsAgo)
assert.Equal(t, "5 minutes ago", actual)
hourAgo := now.Add(-1 * time.Hour)
actual = TimeAgo(hourAgo)
assert.Equal(t, "1 hour ago", actual)
hoursAgo := now.Add(-3 * time.Hour)
actual = TimeAgo(hoursAgo)
assert.Equal(t, "3 hours ago", actual)
dayAgo := now.Add(-1 * 24 * time.Hour)
actual = TimeAgo(dayAgo)
assert.Equal(t, "1 day ago", actual)
daysAgo := now.Add(-5 * 24 * time.Hour)
actual = TimeAgo(daysAgo)
assert.Equal(t, "5 days ago", actual)
monthAgo := now.Add(-1 * 24 * 31 * time.Hour)
actual = TimeAgo(monthAgo)
assert.Equal(t, "1 month ago", actual)
monthsAgo := now.Add(-2 * 24 * 31 * time.Hour)
actual = TimeAgo(monthsAgo)
assert.Equal(t, "2 months ago", actual)
yearAgo := now.Add(-1 * 24 * 31 * 12 * time.Hour)
actual = TimeAgo(yearAgo)
assert.Equal(t, "1 year ago", actual)
yearsAgo := now.Add(-2 * 24 * 31 * 12 * time.Hour)
actual = TimeAgo(yearsAgo)
assert.Equal(t, "2 years ago", actual)
}
func TestColorBrightness(t *testing.T) {
c, err := NewColor("880000")
assert.Equal(t, nil, err)
actual := c.Brightness()
assert.Equal(t, float32(0.15946665406227112), actual)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册