-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathphantom_test.go
102 lines (89 loc) · 2.34 KB
/
phantom_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
100
101
102
package phantomjs
import (
"testing"
)
func TestStartStop(t *testing.T) {
p, err := Start()
failOnError(err, t)
err = p.Exit()
failOnError(err, t)
}
func TestStartStopWithArgs(t *testing.T) {
p, err := Start("--web-security=no")
failOnError(err, t)
err = p.Exit()
failOnError(err, t)
}
func TestRunACommand(t *testing.T) {
p, err := Start()
defer p.Exit()
failOnError(err, t)
assertFloatResult("function(){ return 2 + 1; }\n", 3, p, t)
}
func TestRunACommandWithoutLineBreak(t *testing.T) {
p, err := Start()
defer p.Exit()
failOnError(err, t)
assertFloatResult("function(){ return 2 + 2; }", 4, p, t)
}
func TestRunAnAsyncCommand(t *testing.T) {
p, err := Start()
failOnError(err, t)
defer p.Exit()
assertFloatResult("function(done){ done(2 + 3) ; }\n", 5, p, t)
p1, err := Start()
failOnError(err, t)
defer p1.Exit()
assertFloatResult("function(done){ setTimeout(function() { done(3 + 3) ; }, 0); }\n", 6, p1, t)
}
func TestRunMultilineCommand(t *testing.T) {
p, err := Start()
failOnError(err, t)
defer p.Exit()
assertFloatResult("function() {\n\t return 3+4;\n}\n", 7, p, t)
}
func TestRunMultipleCommands(t *testing.T) {
p, err := Start()
failOnError(err, t)
defer p.Exit()
assertFloatResult("function() {return 1}", 1, p, t)
assertFloatResult("function() {return 1}", 1, p, t)
assertFloatResult("function() {return 1}", 1, p, t)
}
func TestLoadGlobal(t *testing.T) {
p, err := Start()
failOnError(err, t)
defer p.Exit()
p.Load("function result(result) { return result; }\nvar a = 2")
assertFloatResult("function() {return result(a);}", 2, p, t)
}
func TestMessageSentAfterAnErrorDontCrash(t *testing.T) {
p, err := Start()
failOnError(err, t)
defer p.Exit()
p.Run("function(done) {done(null, 'manual'); done('should not panic');}", nil)
}
func TestDoubleErrorSendDontCrash(t *testing.T) {
p, err := Start()
failOnError(err, t)
defer p.Exit()
p.Run("function(done) {done(null, 'manual'); done(null, 'should not panic');}", nil)
}
func assertFloatResult(jsFunc string, expected float64, p *Phantom, t *testing.T) {
var r interface{}
err := p.Run(jsFunc, &r)
failOnError(err, t)
v, ok := r.(float64)
if !ok {
t.Errorf("Should be an int but is %v", r)
return
}
if v != expected {
t.Errorf("Should be %f but is %f", expected, v)
}
}
func failOnError(err error, t *testing.T) {
if err != nil {
t.Fatal(err)
}
}