-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
javascript_test.go
94 lines (80 loc) · 3.14 KB
/
javascript_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
package biloba_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Javascript", func() {
Describe("RunErr", func() {
Context("when the script succeeds", func() {
It("returns the result as an unencoded Go object", func() {
Ω(b.RunErr(`1+2`)).Should(Equal(3.0))
Ω(b.RunErr(`["a","b","c","d"].map((c, i) => c + (i+1))`)).Should(HaveExactElements("a1", "b2", "c3", "d4"))
Ω(b.RunErr(`var a = {foo:1, bar:true, baz:"ibbl"}; a`)).Should(SatisfyAll(
HaveKeyWithValue("foo", 1.0),
HaveKeyWithValue("bar", BeTrue()),
HaveKeyWithValue("baz", ContainSubstring("ibbl")),
))
})
})
Context("when the script fails", func() {
It("returns an error", func() {
result, err := b.RunErr(`1+`)
Ω(result).Should(BeNil())
Ω(err).Should(MatchError(ContainSubstring("SyntaxError: Unexpected end of input")))
})
})
Context("with an argument", func() {
It("decodes the result into the argument", func() {
var f float64
Ω(b.RunErr(`1+2`, &f)).Error().ShouldNot(HaveOccurred())
Ω(f).Should(Equal(3.0))
var s []string
Ω(b.RunErr(`["a","b","c","d"].map((c, i) => c + (i+1))`, &s)).Error().ShouldNot(HaveOccurred())
Ω(s).Should(Equal([]string{"a1", "b2", "c3", "d4"}))
})
})
})
Describe("Run", func() {
It("runs just like RunErr but fails if an error occurs", func() {
result := b.Run(`1+`)
Ω(result).Should(BeNil())
ExpectFailures(SatisfyAll(
ContainSubstring("Failed to run script:\n1+"),
ContainSubstring("SyntaxError: Unexpected end of input"),
))
})
})
Describe("EvaluateTo", func() {
It("compares the result of the actual script with the passed in matcher", func() {
b.Run("var a = 0")
Eventually(`a += 1`).Should(b.EvaluateTo(5.0))
})
It("fails with a meaningful error if there is no match", func() {
b.Run("var a = 0")
matcher := b.EvaluateTo(5.0)
match, err := matcher.Match(`a += 1`)
Ω(match).Should(BeFalse())
Ω(err).ShouldNot(HaveOccurred())
Ω(matcher.FailureMessage(`a += 1`)).Should(Equal("Return value for script:\na += 1\nFailed with:\nExpected\n <float64>: 1\nto equal\n <float64>: 5"))
})
It("fails with a meaningful error if the script does not compile", func() {
matcher := b.EvaluateTo(1.0)
match, err := matcher.Match(`1+`)
Ω(match).Should(BeFalse())
Ω(err).Should(MatchError("Failed to run script:\n1+\n\nexception \"Uncaught\" (0:2): SyntaxError: Unexpected end of input"))
})
})
Describe("invoking functions", func() {
It("provides a convenient mechanism for invoking functions that handles JSON encoding for you", func() {
b.Run(b.JSFunc("console.log").Invoke(1, []int{2, 3, 4}, "hello", true, nil))
Ω(string(gt.buffer.Contents())).Should(ContainSubstring(`1 - [2, 3, 4] - "hello" - true - <nil>`))
adder := b.JSFunc("(...nums) => nums.reduce((s, n) => s + n, 0)")
var result int
b.Run(adder.Invoke(1, 2, 3, 4, 5, 10), &result)
Ω(result).Should(Equal(25))
Ω(adder.Invoke(1, 2, 3.7, 4, 5)).Should(b.EvaluateTo(15.7))
b.Run("var counter = 17")
Ω(adder.Invoke(1, 2, 3.7, 4, 5, b.JSVar("counter + 3"))).Should(b.EvaluateTo(35.7))
})
})
})