-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathparse_test.go
57 lines (50 loc) · 1.7 KB
/
parse_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
package main
import (
"io/ioutil"
"path"
"testing"
"time"
"github.com/prometheus/common/promlog"
)
func init() { // so we don't panic
allowedLevel := promlog.AllowedLevel{}
allowedLevel.Set("debug")
allowedFormat := promlog.AllowedFormat{}
allowedFormat.Set("logfmt")
promlogConfig := promlog.Config{
Level: &allowedLevel,
Format: &allowedFormat,
}
logger = promlog.New(&promlogConfig)
}
func TestParsing(t *testing.T) {
cases := []struct {
filename string
date time.Time
}{
{filename: "google.cn", date: time.Date(2019, 3, 17, 12, 48, 36, 0, time.UTC)},
{filename: "google.jp", date: time.Date(2021, 5, 31, 0, 0, 0, 0, time.UTC)},
{filename: "google.com", date: time.Date(2020, 9, 14, 4, 0, 0, 0, time.UTC)},
{filename: "ietf.org", date: time.Date(2020, 3, 12, 5, 0, 0, 0, time.UTC)},
{filename: "unisportstore.fi", date: time.Date(2019, 3, 20, 17, 13, 49, 0, time.UTC)},
{filename: "yolo.im", date: time.Date(2022, 06, 05, 00, 59, 59, 0, time.UTC)},
// This is a .com WHOIS response that has a different format.
{filename: "com", date: time.Date(2018, 7, 3, 19, 6, 9, 0, time.UTC)},
{filename: "io", date: time.Date(2018, 12, 21, 17, 35, 22, 0, time.UTC)},
}
for i := range cases {
w := path.Join("testdata", cases[i].filename)
ans, err := ioutil.ReadFile(w)
if err != nil {
t.Errorf("problem on %s: %v", cases[i].filename, err)
}
parsedDate, err := parse(cases[i].filename, ans)
if parsedDate == 0 || err != nil {
t.Errorf("%s got %.2f (error=%v) ", cases[i].filename, parsedDate, err)
}
answerDate := float64(cases[i].date.Unix())
if answerDate != parsedDate {
t.Errorf("cases[%d]: parsedDate=%.0f answerDate=%.0f", i, parsedDate, answerDate)
}
}
}