-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtimespan_test.go
62 lines (49 loc) · 1.1 KB
/
timespan_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"testing"
"time"
)
func TestDescriptions(t *testing.T) {
type TestCase struct {
Seconds int64
Result string
}
cases := []TestCase{{20, "20 seconds ago"}, {13, "13 seconds ago"},
{-43, "43 seconds ago"},
{64, "1 minute ago"},
{300, "5 minutes ago"},
{7000, "1 hour ago"},
{60 * 60 * 2.4, "2 hours ago"},
{60 * 60 * 24, "24 hours ago"},
{60 * 60 * 24 * 3, "3 days ago"},
}
for _, o := range cases {
out := timeDescr(o.Seconds)
if out != o.Result {
t.Errorf("Expected '%s' received '%s' for %d", o.Result, out, o.Seconds)
}
}
}
//
// Test the wrapping method accepts sane values.
//
func TestString(t *testing.T) {
//
// Test "just now".
//
str := fmt.Sprintf("%d", time.Now().Unix())
out := timeRelative(str)
if out != "just now" {
t.Errorf("Invalid time-value - got %s", out)
}
//
// Test again with a negative time.
// (Since "now + 1" will become negative when the test is run.)
//
str = fmt.Sprintf("%d", time.Now().Unix()+1)
out = timeRelative(str)
if out != "1 second ago" {
t.Errorf("Invalid time-value - got %s", out)
}
}