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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
package otto
import (
"testing"
)
const (
testSourcemapCodeOriginal = "function functionA(argA, argB) {\n functionB(argA, argB);\n}\n\nfunction functionB(argA, argB) {\n functionExternal(argA, argB);\n}"
testSourcemapCodeMangled = "function functionA(argA,argB){functionB(argA,argB)}function functionB(argA,argB){functionExternal(argA,argB)}"
testSourcemapContent = `{"version":3,"sources":["hello.js"],"names":["functionA","argA","argB","functionB","functionExternal"],"mappings":"AAAA,QAASA,WAAUC,KAAMC,MACvBC,UAAUF,KAAMC,MAGlB,QAASC,WAAUF,KAAMC,MACvBE,iBAAiBH,KAAMC"}`
testSourcemapInline = "function functionA(argA,argB){functionB(argA,argB)}function functionB(argA,argB){functionExternal(argA,argB)}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImhlbGxvLmpzIl0sIm5hbWVzIjpbImZ1bmN0aW9uQSIsImFyZ0EiLCJhcmdCIiwiZnVuY3Rpb25CIiwiZnVuY3Rpb25FeHRlcm5hbCJdLCJtYXBwaW5ncyI6IkFBQUEsUUFBU0EsV0FBVUMsS0FBTUMsTUFDdkJDLFVBQVVGLEtBQU1DLE1BR2xCLFFBQVNDLFdBQVVGLEtBQU1DLE1BQ3ZCRSxpQkFBaUJILEtBQU1DIn0="
testSourcemapOriginalStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:6:3)\n at functionA (hello.js:2:3)\n at <anonymous>:1:1\n"
testSourcemapMangledStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:1:82)\n at functionA (hello.js:1:31)\n at <anonymous>:1:1\n"
testSourcemapMappedStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:6:2)\n at functionA (hello.js:2:2)\n at <anonymous>:1:1\n"
)
func TestSourceMapOriginalWithNoSourcemap(t *testing.T) {
tt(t, func() {
vm := New()
s, err := vm.Compile("hello.js", testSourcemapCodeOriginal)
if err != nil {
panic(err)
}
if _, err := vm.Run(s); err != nil {
panic(err)
}
_, err = vm.Run(`functionA()`)
if err == nil {
panic("error should not be nil")
}
is(err.(*Error).String(), testSourcemapOriginalStack)
})
}
func TestSourceMapMangledWithNoSourcemap(t *testing.T) {
tt(t, func() {
vm := New()
s, err := vm.Compile("hello.js", testSourcemapCodeMangled)
if err != nil {
panic(err)
}
if _, err := vm.Run(s); err != nil {
panic(err)
}
_, err = vm.Run(`functionA()`)
if err == nil {
panic("error should not be nil")
}
is(err.(*Error).String(), testSourcemapMangledStack)
})
}
func TestSourceMapMangledWithSourcemap(t *testing.T) {
tt(t, func() {
vm := New()
s, err := vm.CompileWithSourceMap("hello.js", testSourcemapCodeMangled, testSourcemapContent)
if err != nil {
panic(err)
}
if _, err := vm.Run(s); err != nil {
panic(err)
}
_, err = vm.Run(`functionA()`)
if err == nil {
panic("error should not be nil")
}
is(err.(*Error).String(), testSourcemapMappedStack)
})
}
func TestSourceMapMangledWithInlineSourcemap(t *testing.T) {
tt(t, func() {
vm := New()
s, err := vm.CompileWithSourceMap("hello.js", testSourcemapInline, nil)
if err != nil {
panic(err)
}
if _, err := vm.Run(s); err != nil {
panic(err)
}
_, err = vm.Run(`functionA()`)
if err == nil {
panic("error should not be nil")
}
is(err.(*Error).String(), testSourcemapMappedStack)
})
}
func TestSourceMapContextPosition(t *testing.T) {
tt(t, func() {
vm := New()
s, err := vm.CompileWithSourceMap("hello.js", testSourcemapCodeMangled, testSourcemapContent)
if err != nil {
panic(err)
}
if _, err := vm.Run(s); err != nil {
panic(err)
}
vm.Set("functionExternal", func(c FunctionCall) Value {
ctx := c.Otto.Context()
is(ctx.Filename, "hello.js")
is(ctx.Line, 6)
is(ctx.Column, 2)
return UndefinedValue()
})
if _, err := vm.Run(`functionA()`); err != nil {
panic(err)
}
})
}
func TestSourceMapContextStacktrace(t *testing.T) {
tt(t, func() {
vm := New()
s, err := vm.CompileWithSourceMap("hello.js", testSourcemapCodeMangled, testSourcemapContent)
if err != nil {
panic(err)
}
if _, err := vm.Run(s); err != nil {
panic(err)
}
vm.Set("functionExternal", func(c FunctionCall) Value {
ctx := c.Otto.Context()
is(ctx.Stacktrace, []string{
"functionB (hello.js:6:2)",
"functionA (hello.js:2:2)",
"<anonymous>:1:1",
})
return UndefinedValue()
})
if _, err := vm.Run(`functionA()`); err != nil {
panic(err)
}
})
}
|