-
Notifications
You must be signed in to change notification settings - Fork 1
/
reference_test.go
99 lines (82 loc) · 2.2 KB
/
reference_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//go:build reference
package strftime_test
import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"
"testing"
"time"
"github.com/ncruces/go-strftime"
)
func TestFormat_ruby(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
exe, err := exec.LookPath("ruby")
if err != nil {
t.Skip(err)
}
ref := reference.Format(time.RFC3339Nano)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
ruby := func(format string) func(t *testing.T) {
return func(t *testing.T) {
script := fmt.Sprintf("print(DateTime.parse(%q).strftime(%q))", ref, format)
cmd := exec.CommandContext(ctx, exe, "-e", "require 'date'", "-e", script)
t.Parallel()
want, err := cmd.CombinedOutput()
if err != nil {
t.Error(err)
}
if got := strftime.Format(format, reference); got != string(want) {
t.Errorf("Format(%q) = %q, ruby wants %q", format, got, string(want))
}
}
}
for _, test := range timeTests {
t.Run("", ruby(test.format))
}
}
func TestFormat_osascript(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
exe, err := exec.LookPath("osascript")
if err != nil {
t.Skip(err)
}
zone, _ := reference.Zone()
unix := float64(reference.UnixNano()) / 1e9
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
osascript := func(pattern, format string) func(t *testing.T) {
return func(t *testing.T) {
script := fmt.Sprintf(`
ObjC.import('Cocoa')
var fmt = $.NSDateFormatter.alloc.init
fmt.dateFormat = %q
fmt.timeZone = $.NSTimeZone.timeZoneWithName(%q)
fmt.locale = $.NSLocale.localeWithLocaleIdentifier("en_US_POSIX");
fmt.stringFromDate($.NSDate.dateWithTimeIntervalSince1970(%g))
`, pattern, zone, unix)
cmd := exec.CommandContext(ctx, exe, "-l", "JavaScript")
cmd.Stdin = strings.NewReader(script)
t.Parallel()
want, err := cmd.CombinedOutput()
if err != nil {
t.Error(err)
}
want = bytes.TrimSuffix(want, []byte("\n"))
if got := strftime.Format(format, reference); got != string(want) {
t.Errorf("Format(%q) = %q, osascript wants %q", format, got, string(want))
}
}
}
for _, test := range timeTests {
if test.uts35 != "" {
t.Run("", osascript(test.uts35, test.format))
}
}
}