Skip to content

Commit 99d50f0

Browse files
committed
tests: Add failing test for internalization/externalization of time.Time/Date.
This is a test for issue #279. Proper testing for this issue prevents "testing" package from being imported. So, implement it as a low level single file .go test and invoke it from backend. In the time_inexternalization.go, you can use these statements for manual testing: var _ = time.Now() // Force time.Time not to be DCEed. var _ = time.Unix(0, 0) // Force time.Unix (and time.Time implicitly) not to be DCEed. Adding both of those statements (or just time.Unix one, since it also includes time.Time indirectly) would cause the test to pass. But a true fix will be in the compiler itself, in a way that will avoid the panics/incorrect behavior even when time.Time and/or time.Unix are DCEed, or time package is not imported at all.
1 parent 8ce9990 commit 99d50f0

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

tests/lowlevel_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// +build !js
2+
3+
package tests_test
4+
5+
import (
6+
"bytes"
7+
"io/ioutil"
8+
"os/exec"
9+
"path/filepath"
10+
"testing"
11+
)
12+
13+
// Test for internalization/externalization of time.Time/Date when time package is imported
14+
// but time.Time is unused, causing it to be DCEed (or time package not imported at all).
15+
//
16+
// See https://github.com/gopherjs/gopherjs/issues/279.
17+
func TestTimeInternalizationExternalization(t *testing.T) {
18+
got, err := exec.Command("gopherjs", "run", filepath.Join("testdata", "time_inexternalization.go")).CombinedOutput()
19+
if err != nil {
20+
t.Fatalf("%v:\n%s", err, got)
21+
}
22+
23+
want, err := ioutil.ReadFile(filepath.Join("testdata", "time_inexternalization.out"))
24+
if err != nil {
25+
t.Fatalf("error reading .out file: %v", err)
26+
}
27+
28+
if !bytes.Equal(got, want) {
29+
t.Fatalf("got != want:\ngot:\n%s\nwant:\n%s", got, want)
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"time"
5+
6+
"github.com/gopherjs/gopherjs/js"
7+
)
8+
9+
var _ = time.Sleep // Force "time" package to be imported but let time.Time and time.Unix be DCEed since they're not used.
10+
11+
func main() {
12+
// Excercise externalization of Go struct (with its special handling of time.Time).
13+
js.Global.Get("console").Call("log", struct{ S string }{"externalization ok"})
14+
15+
// Excercise internalization of JavaScript Date object (with its special handling of time.Time).
16+
date := js.Global.Get("Date").New("2015-08-29T20:56:00.869Z").Interface()
17+
js.Global.Set("myDate", date)
18+
js.Global.Get("console").Call("log", js.Global.Get("myDate").Call("toUTCString"))
19+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ S: 'externalization ok' }
2+
Sat, 29 Aug 2015 20:56:00 GMT

0 commit comments

Comments
 (0)