forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
90 lines (81 loc) · 1.5 KB
/
testing.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
// +build js
package testing
import (
"flag"
"fmt"
"github.com/gopherjs/gopherjs/js"
"os"
"time"
)
func init() {
x := false
if x { // avoid dead code elimination
Main2("", "", nil, nil)
}
}
func Main2(pkgPath string, dir string, names []string, tests []func(*T)) {
flag.Parse()
if len(tests) == 0 {
fmt.Println("testing: warning: no tests to run")
}
d, err := os.Open(dir)
if err != nil {
panic(err)
}
d.Chdir()
ok := true
start := time.Now()
for i := 0; i < len(tests); i++ {
t := &T{
common: common{
start: time.Now(),
},
name: names[i],
}
t.self = t
if *chatty {
fmt.Printf("=== RUN %s\n", t.name)
}
err := runTest.Invoke(tests[i], t)
js.Global.Set("go$jsErr", nil)
if err != nil {
switch {
case !err.Get("go$exit").IsUndefined():
// test failed or skipped
err = nil
case !err.Get("go$notSupported").IsUndefined():
t.log(err.Get("message").Str())
t.skip()
err = nil
default:
t.Fail()
}
}
t.common.duration = time.Now().Sub(t.common.start)
t.report()
if err != nil {
throw.Invoke(err)
}
ok = ok && !t.common.failed
}
duration := time.Now().Sub(start)
status := "ok "
exitCode := 0
if !ok {
status = "FAIL"
exitCode = 1
}
fmt.Printf("%s\t%s\t%.3fs\n", status, pkgPath, duration.Seconds())
os.Exit(exitCode)
}
var runTest = js.Global.Call("eval", `(function(f, t) {
try {
f(t);
return null;
} catch (e) {
return e;
}
})`)
var throw = js.Global.Call("eval", `(function(err) {
throw err;
})`)