| 1 |
package interp_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"context" |
| 7 |
"fmt" |
| 8 |
"go/build" |
| 9 |
"go/parser" |
| 10 |
"io" |
| 11 |
"log" |
| 12 |
"net/http" |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"reflect" |
| 16 |
"runtime" |
| 17 |
"strconv" |
| 18 |
"strings" |
| 19 |
"sync" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/traefik/yaegi/interp" |
| 24 |
"github.com/traefik/yaegi/stdlib" |
| 25 |
) |
| 26 |
|
| 27 |
func init() { log.SetFlags(log.Lshortfile) } |
| 28 |
|
| 29 |
// testCase represents an interpreter test case. |
| 30 |
// Care must be taken when defining multiple test cases within the same interpreter |
| 31 |
// context, as all declarations occur in the global scope and are therefore |
| 32 |
// shared between multiple test cases. |
| 33 |
// Hint: use different variables or package names in testcases to keep them uncoupled. |
| 34 |
type testCase struct { |
| 35 |
desc, src, res, err string |
| 36 |
skip string // if not empty, skip this test case (used in case of known error) |
| 37 |
pre func() // functions to execute prior eval src, or nil |
| 38 |
} |
| 39 |
|
| 40 |
func TestEvalArithmetic(t *testing.T) { |
| 41 |
i := interp.New(interp.Options{}) |
| 42 |
runTests(t, i, []testCase{ |
| 43 |
{desc: "add_II", src: "2 + 3", res: "5"}, |
| 44 |
{desc: "add_FI", src: "2.3 + 3", res: "5.3"}, |
| 45 |
{desc: "add_IF", src: "2 + 3.3", res: "5.3"}, |
| 46 |
{desc: "add_SS", src: `"foo" + "bar"`, res: "foobar"}, |
| 47 |
{desc: "add_SI", src: `"foo" + 1`, err: "1:28: invalid operation: mismatched types untyped string and untyped int"}, |
| 48 |
{desc: "sub_SS", src: `"foo" - "bar"`, err: "1:28: invalid operation: operator - not defined on untyped string"}, |
| 49 |
{desc: "sub_II", src: "7 - 3", res: "4"}, |
| 50 |
{desc: "sub_FI", src: "7.2 - 3", res: "4.2"}, |
| 51 |
{desc: "sub_IF", src: "7 - 3.2", res: "3.8"}, |
| 52 |
{desc: "mul_II", src: "2 * 3", res: "6"}, |
| 53 |
{desc: "mul_FI", src: "2.2 * 3", res: "6.6"}, |
| 54 |
{desc: "mul_IF", src: "3 * 2.2", res: "6.6"}, |
| 55 |
{desc: "quo_Z", src: "3 / 0", err: "1:28: invalid operation: division by zero"}, |
| 56 |
{desc: "rem_FI", src: "8.2 % 4", err: "1:28: invalid operation: operator % not defined on untyped float"}, |
| 57 |
{desc: "rem_Z", src: "8 % 0", err: "1:28: invalid operation: division by zero"}, |
| 58 |
{desc: "shl_II", src: "1 << 8", res: "256"}, |
| 59 |
{desc: "shl_IN", src: "1 << -1", err: "1:28: invalid operation: shift count type untyped int, must be integer"}, |
| 60 |
{desc: "shl_IF", src: "1 << 1.0", res: "2"}, |
| 61 |
{desc: "shl_IF1", src: "1 << 1.1", err: "1:28: invalid operation: shift count type untyped float, must be integer"}, |
| 62 |
{desc: "shl_IF2", src: "1.0 << 1", res: "2"}, |
| 63 |
{desc: "shr_II", src: "1 >> 8", res: "0"}, |
| 64 |
{desc: "shr_IN", src: "1 >> -1", err: "1:28: invalid operation: shift count type untyped int, must be integer"}, |
| 65 |
{desc: "shr_IF", src: "1 >> 1.0", res: "0"}, |
| 66 |
{desc: "shr_IF1", src: "1 >> 1.1", err: "1:28: invalid operation: shift count type untyped float, must be integer"}, |
| 67 |
{desc: "neg_I", src: "-2", res: "-2"}, |
| 68 |
{desc: "pos_I", src: "+2", res: "2"}, |
| 69 |
{desc: "bitnot_I", src: "^2", res: "-3"}, |
| 70 |
{desc: "bitnot_F", src: "^0.2", err: "1:28: invalid operation: operator ^ not defined on untyped float"}, |
| 71 |
{desc: "not_B", src: "!false", res: "true"}, |
| 72 |
{desc: "not_I", src: "!0", err: "1:28: invalid operation: operator ! not defined on untyped int"}, |
| 73 |
}) |
| 74 |
} |
| 75 |
|
| 76 |
func TestEvalShift(t *testing.T) { |
| 77 |
i := interp.New(interp.Options{}) |
| 78 |
runTests(t, i, []testCase{ |
| 79 |
{src: "a, b, m := uint32(1), uint32(2), uint32(0); m = a + (1 << b)", res: "5"}, |
| 80 |
{src: "c := uint(1); d := uint64(+(-(1 << c)))", res: "18446744073709551614"}, |
| 81 |
{src: "e, f := uint32(0), uint32(0); f = 1 << -(e * 2)", res: "1"}, |
| 82 |
{src: "p := uint(0xdead); byte((1 << (p & 7)) - 1)", res: "31"}, |
| 83 |
{pre: func() { eval(t, i, "const k uint = 1 << 17") }, src: "int(k)", res: "131072"}, |
| 84 |
}) |
| 85 |
} |
| 86 |
|
| 87 |
func TestOpVarConst(t *testing.T) { |
| 88 |
i := interp.New(interp.Options{}) |
| 89 |
runTests(t, i, []testCase{ |
| 90 |
{pre: func() { eval(t, i, "const a uint = 8 + 2") }, src: "a", res: "10"}, |
| 91 |
{src: "b := uint(5); a+b", res: "15"}, |
| 92 |
{src: "b := uint(5); b+a", res: "15"}, |
| 93 |
{src: "b := uint(5); b>a", res: "false"}, |
| 94 |
{src: "const maxlen = cap(aa); var aa = []int{1,2}", err: "1:20: constant definition loop"}, |
| 95 |
}) |
| 96 |
} |
| 97 |
|
| 98 |
func TestEvalStar(t *testing.T) { |
| 99 |
i := interp.New(interp.Options{}) |
| 100 |
runTests(t, i, []testCase{ |
| 101 |
{src: `a := &struct{A int}{1}; b := *a`, res: "{1}"}, |
| 102 |
{src: `a := struct{A int}{1}; b := *a`, err: "1:57: invalid operation: cannot indirect \"a\""}, |
| 103 |
}) |
| 104 |
} |
| 105 |
|
| 106 |
func TestEvalAssign(t *testing.T) { |
| 107 |
i := interp.New(interp.Options{}) |
| 108 |
if err := i.Use(interp.Exports{ |
| 109 |
"testpkg/testpkg": { |
| 110 |
"val": reflect.ValueOf(int64(11)), |
| 111 |
}, |
| 112 |
}); err != nil { |
| 113 |
t.Fatal(err) |
| 114 |
} |
| 115 |
|
| 116 |
_, e := i.Eval(`import "testpkg"`) |
| 117 |
if e != nil { |
| 118 |
t.Fatal(e) |
| 119 |
} |
| 120 |
|
| 121 |
runTests(t, i, []testCase{ |
| 122 |
{src: `a := "Hello"; a += " world"`, res: "Hello world"}, |
| 123 |
{src: `b := "Hello"; b += 1`, err: "1:42: invalid operation: mismatched types string and untyped int"}, |
| 124 |
{src: `c := "Hello"; c -= " world"`, err: "1:42: invalid operation: operator -= not defined on string"}, |
| 125 |
{src: "e := 64.4; e %= 64", err: "1:39: invalid operation: operator %= not defined on float64"}, |
| 126 |
{src: "f := int64(3.2)", err: "1:39: cannot convert expression of type untyped float to type int64"}, |
| 127 |
{src: "g := 1; g <<= 8", res: "256"}, |
| 128 |
{src: "h := 1; h >>= 8", res: "0"}, |
| 129 |
{src: "i := 1; j := &i; (*j) = 2", res: "2"}, |
| 130 |
{src: "i64 := testpkg.val; i64 == 11", res: "true"}, |
| 131 |
{pre: func() { eval(t, i, "k := 1") }, src: `k := "Hello world"`, res: "Hello world"}, // allow reassignment in subsequent evaluations |
| 132 |
{src: "_ = _", err: "1:28: cannot use _ as value"}, |
| 133 |
{src: "j := true || _", err: "1:33: cannot use _ as value"}, |
| 134 |
{src: "j := true && _", err: "1:33: cannot use _ as value"}, |
| 135 |
{src: "j := interface{}(int(1)); j.(_)", err: "1:54: cannot use _ as value"}, |
| 136 |
{src: "ff := func() (a, b, c int) {return 1, 2, 3}; x, y, x := ff()", err: "1:73: x repeated on left side of :="}, |
| 137 |
{src: "xx := 1; xx, _ := 2, 3", err: "1:37: no new variables on left side of :="}, |
| 138 |
{src: "1 = 2", err: "1:28: cannot assign to 1 (untyped int constant)"}, |
| 139 |
}) |
| 140 |
} |
| 141 |
|
| 142 |
func TestEvalBuiltin(t *testing.T) { |
| 143 |
i := interp.New(interp.Options{}) |
| 144 |
runTests(t, i, []testCase{ |
| 145 |
{src: `a := []int{}; a = append(a, 1); a`, res: "[1]"}, |
| 146 |
{src: `b := []int{1}; b = append(a, 2, 3); b`, res: "[1 2 3]"}, |
| 147 |
{src: `c := []int{1}; d := []int{2, 3}; c = append(c, d...); c`, res: "[1 2 3]"}, |
| 148 |
{src: `string(append([]byte("hello "), "world"...))`, res: "hello world"}, |
| 149 |
{src: `e := "world"; string(append([]byte("hello "), e...))`, res: "hello world"}, |
| 150 |
{src: `b := []int{1}; b = append(1, 2, 3); b`, err: "1:54: first argument to append must be slice; have untyped int"}, |
| 151 |
{src: `a1 := []int{0,1,2}; append(a1)`, res: "[0 1 2]"}, |
| 152 |
{src: `append(nil)`, err: "first argument to append must be slice; have nil"}, |
| 153 |
{src: `g := len(a)`, res: "1"}, |
| 154 |
{src: `g := len("test")`, res: "4"}, |
| 155 |
{src: `g := len(map[string]string{"a": "b"})`, res: "1"}, |
| 156 |
{src: `n := len()`, err: "not enough arguments in call to len"}, |
| 157 |
{src: `n := len([]int, 0)`, err: "too many arguments for len"}, |
| 158 |
{src: `g := cap("test")`, err: "1:37: invalid argument for cap"}, |
| 159 |
{src: `g := cap(map[string]string{"a": "b"})`, err: "1:37: invalid argument for cap"}, |
| 160 |
{src: `h := make(chan int, 1); close(h); len(h)`, res: "0"}, |
| 161 |
{src: `close(a)`, err: "1:34: invalid operation: non-chan type []int"}, |
| 162 |
{src: `h := make(chan int, 1); var i <-chan int = h; close(i)`, err: "1:80: invalid operation: cannot close receive-only channel"}, |
| 163 |
{src: `j := make([]int, 2)`, res: "[0 0]"}, |
| 164 |
{src: `j := make([]int, 2, 3)`, res: "[0 0]"}, |
| 165 |
{src: `j := make(int)`, err: "1:38: cannot make int; type must be slice, map, or channel"}, |
| 166 |
{src: `j := make([]int)`, err: "1:33: not enough arguments in call to make"}, |
| 167 |
{src: `j := make([]int, 0, 1, 2)`, err: "1:33: too many arguments for make"}, |
| 168 |
{src: `j := make([]int, 2, 1)`, err: "1:33: len larger than cap in make"}, |
| 169 |
{src: `j := make([]int, "test")`, err: "1:45: cannot convert \"test\" to int"}, |
| 170 |
{src: `k := []int{3, 4}; copy(k, []int{1,2}); k`, res: "[1 2]"}, |
| 171 |
{src: `f := []byte("Hello"); copy(f, "world"); string(f)`, res: "world"}, |
| 172 |
{src: `copy(g, g)`, err: "1:28: copy expects slice arguments"}, |
| 173 |
{src: `copy(a, "world")`, err: "1:28: arguments to copy have different element types []int and untyped string"}, |
| 174 |
{src: `l := map[string]int{"a": 1, "b": 2}; delete(l, "a"); l`, res: "map[b:2]"}, |
| 175 |
{src: `delete(a, 1)`, err: "1:35: first argument to delete must be map; have []int"}, |
| 176 |
{src: `l := map[string]int{"a": 1, "b": 2}; delete(l, 1)`, err: "1:75: cannot use untyped int as type string in delete"}, |
| 177 |
{src: `a := []int{1,2}; println(a...)`, err: "invalid use of ... with builtin println"}, |
| 178 |
{src: `m := complex(3, 2); real(m)`, res: "3"}, |
| 179 |
{src: `m := complex(3, 2); imag(m)`, res: "2"}, |
| 180 |
{src: `m := complex("test", 2)`, err: "1:33: invalid types string and int"}, |
| 181 |
{src: `imag("test")`, err: "1:33: cannot convert untyped string to untyped complex"}, |
| 182 |
{src: `imag(a)`, err: "1:33: invalid argument type []int for imag"}, |
| 183 |
{src: `real(a)`, err: "1:33: invalid argument type []int for real"}, |
| 184 |
{src: `t := map[int]int{}; t[123]++; t`, res: "map[123:1]"}, |
| 185 |
{src: `t := map[int]int{}; t[123]--; t`, res: "map[123:-1]"}, |
| 186 |
{src: `t := map[int]int{}; t[123] += 1; t`, res: "map[123:1]"}, |
| 187 |
{src: `t := map[int]int{}; t[123] -= 1; t`, res: "map[123:-1]"}, |
| 188 |
{src: `println("hello", _)`, err: "1:28: cannot use _ as value"}, |
| 189 |
{src: `f := func() complex64 { return complex(0, 0) }()`, res: "(0+0i)"}, |
| 190 |
{src: `f := func() float32 { return real(complex(2, 1)) }()`, res: "2"}, |
| 191 |
{src: `f := func() int8 { return imag(complex(2, 1)) }()`, res: "1"}, |
| 192 |
}) |
| 193 |
} |
| 194 |
|
| 195 |
func TestEvalDecl(t *testing.T) { |
| 196 |
i := interp.New(interp.Options{}) |
| 197 |
runTests(t, i, []testCase{ |
| 198 |
{pre: func() { eval(t, i, "var i int = 2") }, src: "i", res: "2"}, |
| 199 |
{pre: func() { eval(t, i, "var j, k int = 2, 3") }, src: "j", res: "2"}, |
| 200 |
{pre: func() { eval(t, i, "var l, m int = 2, 3") }, src: "k", res: "3"}, |
| 201 |
{pre: func() { eval(t, i, "func f() int {return 4}") }, src: "f()", res: "4"}, |
| 202 |
{pre: func() { eval(t, i, `package foo; var I = 2`) }, src: "foo.I", res: "2"}, |
| 203 |
{pre: func() { eval(t, i, `package foo; func F() int {return 5}`) }, src: "foo.F()", res: "5"}, |
| 204 |
}) |
| 205 |
} |
| 206 |
|
| 207 |
func TestEvalDeclWithExpr(t *testing.T) { |
| 208 |
i := interp.New(interp.Options{}) |
| 209 |
runTests(t, i, []testCase{ |
| 210 |
{src: `a1 := ""; var a2 int; a2 = 2`, res: "2"}, |
| 211 |
{src: `b1 := ""; const b2 = 2; b2`, res: "2"}, |
| 212 |
{src: `c1 := ""; var c2, c3 [8]byte; c3[3]`, res: "0"}, |
| 213 |
}) |
| 214 |
} |
| 215 |
|
| 216 |
func TestEvalTypeSpec(t *testing.T) { |
| 217 |
i := interp.New(interp.Options{}) |
| 218 |
runTests(t, i, []testCase{ |
| 219 |
{src: `type _ struct{}`, err: "1:19: cannot use _ as value"}, |
| 220 |
{src: `a := struct{a, _ int}{32, 0}`, res: "{32 0}"}, |
| 221 |
{src: "type A int; type A = string", err: "1:31: A redeclared in this block"}, |
| 222 |
{src: "type B int; type B string", err: "1:31: B redeclared in this block"}, |
| 223 |
}) |
| 224 |
} |
| 225 |
|
| 226 |
func TestEvalFunc(t *testing.T) { |
| 227 |
i := interp.New(interp.Options{}) |
| 228 |
runTests(t, i, []testCase{ |
| 229 |
{src: `(func () string {return "ok"})()`, res: "ok"}, |
| 230 |
{src: `(func () (res string) {res = "ok"; return})()`, res: "ok"}, |
| 231 |
{src: `(func () int {f := func() (a, b int) {a, b = 3, 4; return}; x, y := f(); return x+y})()`, res: "7"}, |
| 232 |
{src: `(func () int {f := func() (a int, b, c int) {a, b, c = 3, 4, 5; return}; x, y, z := f(); return x+y+z})()`, res: "12"}, |
| 233 |
{src: `(func () int {f := func() (a, b, c int) {a, b, c = 3, 4, 5; return}; x, y, z := f(); return x+y+z})()`, res: "12"}, |
| 234 |
{src: `func f() int { return _ }`, err: "1:29: cannot use _ as value"}, |
| 235 |
{src: `(func (x int) {})(_)`, err: "1:28: cannot use _ as value"}, |
| 236 |
}) |
| 237 |
} |
| 238 |
|
| 239 |
func TestEvalImport(t *testing.T) { |
| 240 |
i := interp.New(interp.Options{}) |
| 241 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 242 |
t.Fatal(err) |
| 243 |
} |
| 244 |
runTests(t, i, []testCase{ |
| 245 |
{pre: func() { eval(t, i, `import "time"`) }, src: "2 * time.Second", res: "2s"}, |
| 246 |
}) |
| 247 |
} |
| 248 |
|
| 249 |
func TestEvalStdout(t *testing.T) { |
| 250 |
var out, err bytes.Buffer |
| 251 |
i := interp.New(interp.Options{Stdout: &out, Stderr: &err}) |
| 252 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 253 |
t.Fatal(err) |
| 254 |
} |
| 255 |
_, e := i.Eval(`import "fmt"; func main() { fmt.Println("hello") }`) |
| 256 |
if e != nil { |
| 257 |
t.Fatal(e) |
| 258 |
} |
| 259 |
wanted := "hello\n" |
| 260 |
if res := out.String(); res != wanted { |
| 261 |
t.Fatalf("got %v, want %v", res, wanted) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func TestEvalNil(t *testing.T) { |
| 266 |
i := interp.New(interp.Options{}) |
| 267 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 268 |
t.Fatal(err) |
| 269 |
} |
| 270 |
runTests(t, i, []testCase{ |
| 271 |
{desc: "assign nil", src: "a := nil", err: "1:33: use of untyped nil"}, |
| 272 |
{desc: "return nil", pre: func() { eval(t, i, "func getNil() error {return nil}") }, src: "getNil()", res: "<nil>"}, |
| 273 |
{ |
| 274 |
desc: "return func which return error", |
| 275 |
pre: func() { |
| 276 |
eval(t, i, ` |
| 277 |
package bar |
| 278 |
|
| 279 |
func New() func(string) error { |
| 280 |
return func(v string) error { |
| 281 |
return nil |
| 282 |
} |
| 283 |
} |
| 284 |
`) |
| 285 |
v := eval(t, i, `bar.New()`) |
| 286 |
fn, ok := v.Interface().(func(string) error) |
| 287 |
if !ok { |
| 288 |
t.Fatal("conversion failed") |
| 289 |
} |
| 290 |
if res := fn("hello"); res != nil { |
| 291 |
t.Fatalf("got %v, want nil", res) |
| 292 |
} |
| 293 |
}, |
| 294 |
}, |
| 295 |
{ |
| 296 |
desc: "return nil pointer", |
| 297 |
pre: func() { |
| 298 |
eval(t, i, ` |
| 299 |
import "fmt" |
| 300 |
|
| 301 |
type Foo struct{} |
| 302 |
|
| 303 |
func Hello() *Foo { |
| 304 |
fmt.Println("Hello") |
| 305 |
return nil |
| 306 |
} |
| 307 |
`) |
| 308 |
}, |
| 309 |
src: "Hello()", |
| 310 |
res: "<nil>", |
| 311 |
}, |
| 312 |
{ |
| 313 |
desc: "return nil func", |
| 314 |
pre: func() { |
| 315 |
eval(t, i, `func Bar() func() { return nil }`) |
| 316 |
}, |
| 317 |
src: "Bar()", |
| 318 |
res: "<nil>", |
| 319 |
}, |
| 320 |
}) |
| 321 |
} |
| 322 |
|
| 323 |
func TestEvalStruct0(t *testing.T) { |
| 324 |
i := interp.New(interp.Options{}) |
| 325 |
runTests(t, i, []testCase{ |
| 326 |
{ |
| 327 |
desc: "func field in struct", |
| 328 |
pre: func() { |
| 329 |
eval(t, i, ` |
| 330 |
type Fromage struct { |
| 331 |
Name string |
| 332 |
Call func(string) string |
| 333 |
} |
| 334 |
|
| 335 |
func f() string { |
| 336 |
a := Fromage{} |
| 337 |
a.Name = "test" |
| 338 |
a.Call = func(s string) string { return s } |
| 339 |
|
| 340 |
return a.Call(a.Name) |
| 341 |
} |
| 342 |
`) |
| 343 |
}, |
| 344 |
src: "f()", |
| 345 |
res: "test", |
| 346 |
}, |
| 347 |
{ |
| 348 |
desc: "literal func field in struct", |
| 349 |
pre: func() { |
| 350 |
eval(t, i, ` |
| 351 |
type Fromage2 struct { |
| 352 |
Name string |
| 353 |
Call func(string) string |
| 354 |
} |
| 355 |
|
| 356 |
func f2() string { |
| 357 |
a := Fromage2{ |
| 358 |
"test", |
| 359 |
func(s string) string { return s }, |
| 360 |
} |
| 361 |
return a.Call(a.Name) |
| 362 |
} |
| 363 |
`) |
| 364 |
}, |
| 365 |
src: "f2()", |
| 366 |
res: "test", |
| 367 |
}, |
| 368 |
}) |
| 369 |
} |
| 370 |
|
| 371 |
func TestEvalStruct1(t *testing.T) { |
| 372 |
i := interp.New(interp.Options{}) |
| 373 |
eval(t, i, ` |
| 374 |
type Fromage struct { |
| 375 |
Name string |
| 376 |
Call func(string) string |
| 377 |
} |
| 378 |
|
| 379 |
func f() string { |
| 380 |
a := Fromage{ |
| 381 |
"test", |
| 382 |
func(s string) string { return s }, |
| 383 |
} |
| 384 |
|
| 385 |
return a.Call(a.Name) |
| 386 |
} |
| 387 |
`) |
| 388 |
|
| 389 |
v := eval(t, i, `f()`) |
| 390 |
if v.Interface().(string) != "test" { |
| 391 |
t.Fatalf("got %v, want test", v) |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
func TestEvalComposite0(t *testing.T) { |
| 396 |
i := interp.New(interp.Options{}) |
| 397 |
eval(t, i, ` |
| 398 |
type T struct { |
| 399 |
a, b, c, d, e, f, g, h, i, j, k, l, m, n string |
| 400 |
o map[string]int |
| 401 |
p []string |
| 402 |
} |
| 403 |
|
| 404 |
var a = T{ |
| 405 |
o: map[string]int{"truc": 1, "machin": 2}, |
| 406 |
p: []string{"hello", "world"}, |
| 407 |
} |
| 408 |
`) |
| 409 |
v := eval(t, i, `a.p[1]`) |
| 410 |
if v.Interface().(string) != "world" { |
| 411 |
t.Fatalf("got %v, want word", v) |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
func TestEvalCompositeBin0(t *testing.T) { |
| 416 |
i := interp.New(interp.Options{}) |
| 417 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 418 |
t.Fatal(err) |
| 419 |
} |
| 420 |
eval(t, i, ` |
| 421 |
import ( |
| 422 |
"fmt" |
| 423 |
"net/http" |
| 424 |
"time" |
| 425 |
) |
| 426 |
|
| 427 |
func Foo() { |
| 428 |
http.DefaultClient = &http.Client{Timeout: 2 * time.Second} |
| 429 |
} |
| 430 |
`) |
| 431 |
http.DefaultClient = &http.Client{} |
| 432 |
eval(t, i, `Foo()`) |
| 433 |
if http.DefaultClient.Timeout != 2*time.Second { |
| 434 |
t.Fatalf("got %v, want 2s", http.DefaultClient.Timeout) |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
func TestEvalComparison(t *testing.T) { |
| 439 |
i := interp.New(interp.Options{}) |
| 440 |
runTests(t, i, []testCase{ |
| 441 |
{src: `2 > 1`, res: "true"}, |
| 442 |
{src: `1.2 > 1.1`, res: "true"}, |
| 443 |
{src: `"hhh" > "ggg"`, res: "true"}, |
| 444 |
{src: `a, b, c := 1, 1, false; if a == b { c = true }; c`, res: "true"}, |
| 445 |
{src: `a, b, c := 1, 2, false; if a != b { c = true }; c`, res: "true"}, |
| 446 |
{ |
| 447 |
desc: "mismatched types equality", |
| 448 |
src: ` |
| 449 |
type Foo string |
| 450 |
type Bar string |
| 451 |
|
| 452 |
var a = Foo("test") |
| 453 |
var b = Bar("test") |
| 454 |
var c = a == b |
| 455 |
`, |
| 456 |
err: "7:13: invalid operation: mismatched types main.Foo and main.Bar", |
| 457 |
}, |
| 458 |
{ |
| 459 |
desc: "mismatched types less than", |
| 460 |
src: ` |
| 461 |
type Foo string |
| 462 |
type Bar string |
| 463 |
|
| 464 |
var a = Foo("test") |
| 465 |
var b = Bar("test") |
| 466 |
var c = a < b |
| 467 |
`, |
| 468 |
err: "7:13: invalid operation: mismatched types main.Foo and main.Bar", |
| 469 |
}, |
| 470 |
{src: `1 > _`, err: "1:28: cannot use _ as value"}, |
| 471 |
{src: `(_) > 1`, err: "1:28: cannot use _ as value"}, |
| 472 |
{src: `v := interface{}(2); v == 2`, res: "true"}, |
| 473 |
{src: `v := interface{}(2); v > 1`, err: "1:49: invalid operation: operator > not defined on interface{}"}, |
| 474 |
{src: `v := interface{}(int64(2)); v == 2`, res: "false"}, |
| 475 |
{src: `v := interface{}(int64(2)); v != 2`, res: "true"}, |
| 476 |
{src: `v := interface{}(2.3); v == 2.3`, res: "true"}, |
| 477 |
{src: `v := interface{}(float32(2.3)); v != 2.3`, res: "true"}, |
| 478 |
{src: `v := interface{}("hello"); v == "hello"`, res: "true"}, |
| 479 |
{src: `v := interface{}("hello"); v < "hellp"`, err: "1:55: invalid operation: operator < not defined on interface{}"}, |
| 480 |
}) |
| 481 |
} |
| 482 |
|
| 483 |
func TestEvalCompositeArray(t *testing.T) { |
| 484 |
i := interp.New(interp.Options{}) |
| 485 |
eval(t, i, `const l = 10`) |
| 486 |
runTests(t, i, []testCase{ |
| 487 |
{src: "a := []int{1, 2, 7: 20, 30}", res: "[1 2 0 0 0 0 0 20 30]"}, |
| 488 |
{src: `a := []int{1, 1.2}`, err: "1:42: 6/5 truncated to int"}, |
| 489 |
{src: `a := []int{0:1, 0:1}`, err: "1:46: duplicate index 0 in array or slice literal"}, |
| 490 |
{src: `a := []int{1.1:1, 1.2:"test"}`, err: "1:39: index untyped float must be integer constant"}, |
| 491 |
{src: `a := [2]int{1, 1.2}`, err: "1:43: 6/5 truncated to int"}, |
| 492 |
{src: `a := [1]int{1, 2}`, err: "1:43: index 1 is out of bounds (>= 1)"}, |
| 493 |
{src: `b := [l]int{1, 2}`, res: "[1 2 0 0 0 0 0 0 0 0]"}, |
| 494 |
{src: `i := 10; a := [i]int{1, 2}`, err: "1:43: non-constant array bound \"i\""}, |
| 495 |
{src: `c := [...]float64{1, 3: 3.4, 5}`, res: "[1 0 0 3.4 5]"}, |
| 496 |
}) |
| 497 |
} |
| 498 |
|
| 499 |
func TestEvalCompositeMap(t *testing.T) { |
| 500 |
i := interp.New(interp.Options{}) |
| 501 |
runTests(t, i, []testCase{ |
| 502 |
{src: `a := map[string]int{"one":1, "two":2}`, res: "map[one:1 two:2]"}, |
| 503 |
{src: `a := map[string]int{1:1, 2:2}`, err: "1:48: cannot convert 1 to string"}, |
| 504 |
{src: `a := map[string]int{"one":1, "two":2.2}`, err: "1:63: 11/5 truncated to int"}, |
| 505 |
{src: `a := map[string]int{1, "two":2}`, err: "1:48: missing key in map literal"}, |
| 506 |
{src: `a := map[string]int{"one":1, "one":2}`, err: "1:57: duplicate key one in map literal"}, |
| 507 |
}) |
| 508 |
} |
| 509 |
|
| 510 |
func TestEvalCompositeStruct(t *testing.T) { |
| 511 |
i := interp.New(interp.Options{}) |
| 512 |
runTests(t, i, []testCase{ |
| 513 |
{src: `a := struct{A,B,C int}{}`, res: "{0 0 0}"}, |
| 514 |
{src: `a := struct{A,B,C int}{1,2,3}`, res: "{1 2 3}"}, |
| 515 |
{src: `a := struct{A,B,C int}{1,2.2,3}`, err: "1:53: 11/5 truncated to int"}, |
| 516 |
{src: `a := struct{A,B,C int}{1,2}`, err: "1:53: too few values in struct literal"}, |
| 517 |
{src: `a := struct{A,B,C int}{1,2,3,4}`, err: "1:57: too many values in struct literal"}, |
| 518 |
{src: `a := struct{A,B,C int}{1,B:2,3}`, err: "1:53: mixture of field:value and value elements in struct literal"}, |
| 519 |
{src: `a := struct{A,B,C int}{A:1,B:2,C:3}`, res: "{1 2 3}"}, |
| 520 |
{src: `a := struct{A,B,C int}{B:2}`, res: "{0 2 0}"}, |
| 521 |
{src: `a := struct{A,B,C int}{A:1,D:2,C:3}`, err: "1:55: unknown field D in struct literal"}, |
| 522 |
{src: `a := struct{A,B,C int}{A:1,A:2,C:3}`, err: "1:55: duplicate field name A in struct literal"}, |
| 523 |
{src: `a := struct{A,B,C int}{A:1,B:2.2,C:3}`, err: "1:57: 11/5 truncated to int"}, |
| 524 |
{src: `a := struct{A,B,C int}{A:1,2,C:3}`, err: "1:55: mixture of field:value and value elements in struct literal"}, |
| 525 |
{src: `a := struct{A,B,C int}{1,2,_}`, err: "1:33: cannot use _ as value"}, |
| 526 |
{src: `a := struct{A,B,C int}{B: _}`, err: "1:51: cannot use _ as value"}, |
| 527 |
}) |
| 528 |
} |
| 529 |
|
| 530 |
func TestEvalSliceExpression(t *testing.T) { |
| 531 |
i := interp.New(interp.Options{}) |
| 532 |
runTests(t, i, []testCase{ |
| 533 |
{src: `a := []int{0,1,2}[1:3]`, res: "[1 2]"}, |
| 534 |
{src: `a := []int{0,1,2}[:3]`, res: "[0 1 2]"}, |
| 535 |
{src: `a := []int{0,1,2}[:]`, res: "[0 1 2]"}, |
| 536 |
{src: `a := []int{0,1,2,3}[1:3:4]`, res: "[1 2]"}, |
| 537 |
{src: `a := []int{0,1,2,3}[:3:4]`, res: "[0 1 2]"}, |
| 538 |
{src: `ar := [3]int{0,1,2}; a := ar[1:3]`, res: "[1 2]"}, |
| 539 |
{src: `a := (&[3]int{0,1,2})[1:3]`, res: "[1 2]"}, |
| 540 |
{src: `a := (&[3]int{0,1,2})[1:3]`, res: "[1 2]"}, |
| 541 |
{src: `s := "hello"[1:3]`, res: "el"}, |
| 542 |
{src: `str := "hello"; s := str[1:3]`, res: "el"}, |
| 543 |
{src: `a := int(1)[0:1]`, err: "1:33: cannot slice type int"}, |
| 544 |
{src: `a := (&[]int{0,1,2,3})[1:3]`, err: "1:33: cannot slice type *[]int"}, |
| 545 |
{src: `a := "hello"[1:3:4]`, err: "1:45: invalid operation: 3-index slice of string"}, |
| 546 |
{src: `ar := [3]int{0,1,2}; a := ar[:4]`, err: "1:58: index int is out of bounds"}, |
| 547 |
{src: `a := []int{0,1,2,3}[1::4]`, err: "index required in 3-index slice"}, |
| 548 |
{src: `a := []int{0,1,2,3}[1:3:]`, err: "index required in 3-index slice"}, |
| 549 |
{src: `a := []int{0,1,2}[3:1]`, err: "invalid index values, must be low <= high <= max"}, |
| 550 |
{pre: func() { eval(t, i, `type Str = string; var r Str = "truc"`) }, src: `r[1]`, res: "114"}, |
| 551 |
{src: `_[12]`, err: "1:28: cannot use _ as value"}, |
| 552 |
{src: `b := []int{0,1,2}[_:4]`, err: "1:33: cannot use _ as value"}, |
| 553 |
}) |
| 554 |
} |
| 555 |
|
| 556 |
func TestEvalConversion(t *testing.T) { |
| 557 |
i := interp.New(interp.Options{}) |
| 558 |
runTests(t, i, []testCase{ |
| 559 |
{src: `a := uint64(1)`, res: "1"}, |
| 560 |
{src: `i := 1.1; a := uint64(i)`, res: "1"}, |
| 561 |
{src: `b := string(49)`, res: "1"}, |
| 562 |
{src: `c := uint64(1.1)`, err: "1:40: cannot convert expression of type untyped float to type uint64"}, |
| 563 |
{src: `int(_)`, err: "1:28: cannot use _ as value"}, |
| 564 |
}) |
| 565 |
} |
| 566 |
|
| 567 |
func TestEvalUnary(t *testing.T) { |
| 568 |
i := interp.New(interp.Options{}) |
| 569 |
runTests(t, i, []testCase{ |
| 570 |
{src: "a := -1", res: "-1"}, |
| 571 |
{src: "b := +1", res: "1", skip: "BUG"}, |
| 572 |
{src: "c := !false", res: "true"}, |
| 573 |
{src: "_ = 2; _++", err: "1:35: cannot use _ as value"}, |
| 574 |
{src: "_ = false; !_ == true", err: "1:39: cannot use _ as value"}, |
| 575 |
{src: "!((((_))))", err: "1:28: cannot use _ as value"}, |
| 576 |
}) |
| 577 |
} |
| 578 |
|
| 579 |
func TestEvalMethod(t *testing.T) { |
| 580 |
i := interp.New(interp.Options{}) |
| 581 |
eval(t, i, ` |
| 582 |
type Root struct { |
| 583 |
Name string |
| 584 |
} |
| 585 |
|
| 586 |
type One struct { |
| 587 |
Root |
| 588 |
} |
| 589 |
|
| 590 |
type Hi interface { |
| 591 |
Hello() string |
| 592 |
} |
| 593 |
|
| 594 |
type Hey interface { |
| 595 |
Hello() string |
| 596 |
} |
| 597 |
|
| 598 |
func (r *Root) Hello() string { return "Hello " + r.Name } |
| 599 |
|
| 600 |
var r = Root{"R"} |
| 601 |
var o = One{r} |
| 602 |
// TODO(mpl): restore empty interfaces when type assertions work (again) on them. |
| 603 |
// var root interface{} = &Root{Name: "test1"} |
| 604 |
// var one interface{} = &One{Root{Name: "test2"}} |
| 605 |
var root Hey = &Root{Name: "test1"} |
| 606 |
var one Hey = &One{Root{Name: "test2"}} |
| 607 |
`) |
| 608 |
runTests(t, i, []testCase{ |
| 609 |
{src: "r.Hello()", res: "Hello R"}, |
| 610 |
{src: "(&r).Hello()", res: "Hello R"}, |
| 611 |
{src: "o.Hello()", res: "Hello R"}, |
| 612 |
{src: "(&o).Hello()", res: "Hello R"}, |
| 613 |
{src: "root.(Hi).Hello()", res: "Hello test1"}, |
| 614 |
{src: "one.(Hi).Hello()", res: "Hello test2"}, |
| 615 |
}) |
| 616 |
} |
| 617 |
|
| 618 |
func TestEvalChan(t *testing.T) { |
| 619 |
i := interp.New(interp.Options{}) |
| 620 |
runTests(t, i, []testCase{ |
| 621 |
{ |
| 622 |
src: `(func () string { |
| 623 |
messages := make(chan string) |
| 624 |
go func() { messages <- "ping" }() |
| 625 |
msg := <-messages |
| 626 |
return msg |
| 627 |
})()`, res: "ping", |
| 628 |
}, |
| 629 |
{ |
| 630 |
src: `(func () bool { |
| 631 |
messages := make(chan string) |
| 632 |
go func() { messages <- "ping" }() |
| 633 |
msg, ok := <-messages |
| 634 |
return ok && msg == "ping" |
| 635 |
})()`, res: "true", |
| 636 |
}, |
| 637 |
{ |
| 638 |
src: `(func () bool { |
| 639 |
messages := make(chan string) |
| 640 |
go func() { messages <- "ping" }() |
| 641 |
var msg string |
| 642 |
var ok bool |
| 643 |
msg, ok = <-messages |
| 644 |
return ok && msg == "ping" |
| 645 |
})()`, res: "true", |
| 646 |
}, |
| 647 |
{src: `a :=5; a <- 4`, err: "cannot send to non-channel int"}, |
| 648 |
{src: `a :=5; b := <-a`, err: "cannot receive from non-channel int"}, |
| 649 |
}) |
| 650 |
} |
| 651 |
|
| 652 |
func TestEvalFunctionCallWithFunctionParam(t *testing.T) { |
| 653 |
i := interp.New(interp.Options{}) |
| 654 |
eval(t, i, ` |
| 655 |
func Bar(s string, fn func(string)string) string { return fn(s) } |
| 656 |
`) |
| 657 |
|
| 658 |
v := eval(t, i, "Bar") |
| 659 |
bar := v.Interface().(func(string, func(string) string) string) |
| 660 |
|
| 661 |
got := bar("hello ", func(s string) string { |
| 662 |
return s + "world!" |
| 663 |
}) |
| 664 |
|
| 665 |
want := "hello world!" |
| 666 |
if got != want { |
| 667 |
t.Errorf("unexpected result of function eval: got %q, want %q", got, want) |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
func TestEvalCall(t *testing.T) { |
| 672 |
i := interp.New(interp.Options{}) |
| 673 |
runTests(t, i, []testCase{ |
| 674 |
{src: ` test := func(a int, b float64) int { return a } |
| 675 |
a := test(1, 2.3)`, res: "1"}, |
| 676 |
{src: ` test := func(a int, b float64) int { return a } |
| 677 |
a := test(1)`, err: "2:10: not enough arguments in call to test"}, |
| 678 |
{src: ` test := func(a int, b float64) int { return a } |
| 679 |
s := "test" |
| 680 |
a := test(1, s)`, err: "3:18: cannot use type string as type float64"}, |
| 681 |
{src: ` test := func(a ...int) int { return 1 } |
| 682 |
a := test([]int{1}...)`, res: "1"}, |
| 683 |
{src: ` test := func(a ...int) int { return 1 } |
| 684 |
a := test()`, res: "1"}, |
| 685 |
{src: ` test := func(a ...int) int { return 1 } |
| 686 |
blah := func() []int { return []int{1,1} } |
| 687 |
a := test(blah()...)`, res: "1"}, |
| 688 |
{src: ` test := func(a ...int) int { return 1 } |
| 689 |
a := test([]string{"1"}...)`, err: "2:15: cannot use []string as type []int"}, |
| 690 |
{src: ` test := func(a ...int) int { return 1 } |
| 691 |
i := 1 |
| 692 |
a := test(i...)`, err: "3:15: cannot use int as type []int"}, |
| 693 |
{src: ` test := func(a int) int { return a } |
| 694 |
a := test([]int{1}...)`, err: "2:10: invalid use of ..., corresponding parameter is non-variadic"}, |
| 695 |
{src: ` test := func(a ...int) int { return 1 } |
| 696 |
blah := func() (int, int) { return 1, 1 } |
| 697 |
a := test(blah()...)`, err: "3:15: cannot use ... with 2-valued func() (int,int)"}, |
| 698 |
{src: ` test := func(a, b int) int { return a } |
| 699 |
blah := func() (int, int) { return 1, 1 } |
| 700 |
a := test(blah())`, res: "1"}, |
| 701 |
{src: ` test := func(a, b int) int { return a } |
| 702 |
blah := func() int { return 1 } |
| 703 |
a := test(blah(), blah())`, res: "1"}, |
| 704 |
{src: ` test := func(a, b, c, d int) int { return a } |
| 705 |
blah := func() (int, int) { return 1, 1 } |
| 706 |
a := test(blah(), blah())`, err: "3:15: cannot use func() (int,int) as type int"}, |
| 707 |
{src: ` test := func(a, b int) int { return a } |
| 708 |
blah := func() (int, float64) { return 1, 1.1 } |
| 709 |
a := test(blah())`, err: "3:15: cannot use func() (int,float64) as type (int,int)"}, |
| 710 |
{src: "func f()", err: "missing function body"}, |
| 711 |
}) |
| 712 |
} |
| 713 |
|
| 714 |
func TestEvalBinCall(t *testing.T) { |
| 715 |
i := interp.New(interp.Options{}) |
| 716 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 717 |
t.Fatal(err) |
| 718 |
} |
| 719 |
if _, err := i.Eval(`import "fmt"`); err != nil { |
| 720 |
t.Fatal(err) |
| 721 |
} |
| 722 |
runTests(t, i, []testCase{ |
| 723 |
{src: `a := fmt.Sprint(1, 2.3)`, res: "1 2.3"}, |
| 724 |
{src: `a := fmt.Sprintf()`, err: "1:33: not enough arguments in call to fmt.Sprintf"}, |
| 725 |
{src: `i := 1 |
| 726 |
a := fmt.Sprintf(i)`, err: "2:24: cannot use type int as type string"}, |
| 727 |
{src: `a := fmt.Sprint()`, res: ""}, |
| 728 |
}) |
| 729 |
} |
| 730 |
|
| 731 |
func TestEvalReflect(t *testing.T) { |
| 732 |
i := interp.New(interp.Options{}) |
| 733 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 734 |
t.Fatal(err) |
| 735 |
} |
| 736 |
if _, err := i.Eval(` |
| 737 |
import ( |
| 738 |
"net/url" |
| 739 |
"reflect" |
| 740 |
) |
| 741 |
|
| 742 |
type Encoder interface { |
| 743 |
EncodeValues(key string, v *url.Values) error |
| 744 |
} |
| 745 |
`); err != nil { |
| 746 |
t.Fatal(err) |
| 747 |
} |
| 748 |
|
| 749 |
runTests(t, i, []testCase{ |
| 750 |
{src: "reflect.TypeOf(new(Encoder)).Elem()", res: "interp.valueInterface"}, |
| 751 |
}) |
| 752 |
} |
| 753 |
|
| 754 |
func TestEvalMissingSymbol(t *testing.T) { |
| 755 |
defer func() { |
| 756 |
r := recover() |
| 757 |
if r != nil { |
| 758 |
t.Errorf("unexpected panic: %v", r) |
| 759 |
} |
| 760 |
}() |
| 761 |
|
| 762 |
type S2 struct{} |
| 763 |
type S1 struct { |
| 764 |
F S2 |
| 765 |
} |
| 766 |
i := interp.New(interp.Options{}) |
| 767 |
if err := i.Use(interp.Exports{"p/p": map[string]reflect.Value{ |
| 768 |
"S1": reflect.Zero(reflect.TypeOf(&S1{})), |
| 769 |
}}); err != nil { |
| 770 |
t.Fatal(err) |
| 771 |
} |
| 772 |
_, err := i.Eval(`import "p"`) |
| 773 |
if err != nil { |
| 774 |
t.Fatalf("failed to import package: %v", err) |
| 775 |
} |
| 776 |
_, err = i.Eval(`p.S1{F: p.S2{}}`) |
| 777 |
if err == nil { |
| 778 |
t.Error("unexpected nil error for expression with undefined type") |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
func TestEvalWithContext(t *testing.T) { |
| 783 |
tests := []testCase{ |
| 784 |
{ |
| 785 |
desc: "for {}", |
| 786 |
src: `(func() { |
| 787 |
for {} |
| 788 |
})()`, |
| 789 |
}, |
| 790 |
{ |
| 791 |
desc: "select {}", |
| 792 |
src: `(func() { |
| 793 |
select {} |
| 794 |
})()`, |
| 795 |
}, |
| 796 |
{ |
| 797 |
desc: "blocked chan send", |
| 798 |
src: `(func() { |
| 799 |
c := make(chan int) |
| 800 |
c <- 1 |
| 801 |
})()`, |
| 802 |
}, |
| 803 |
{ |
| 804 |
desc: "blocked chan recv", |
| 805 |
src: `(func() { |
| 806 |
c := make(chan int) |
| 807 |
<-c |
| 808 |
})()`, |
| 809 |
}, |
| 810 |
{ |
| 811 |
desc: "blocked chan recv2", |
| 812 |
src: `(func() { |
| 813 |
c := make(chan int) |
| 814 |
_, _ = <-c |
| 815 |
})()`, |
| 816 |
}, |
| 817 |
{ |
| 818 |
desc: "blocked range chan", |
| 819 |
src: `(func() { |
| 820 |
c := make(chan int) |
| 821 |
for range c {} |
| 822 |
})()`, |
| 823 |
}, |
| 824 |
{ |
| 825 |
desc: "double lock", |
| 826 |
src: `(func() { |
| 827 |
var mu sync.Mutex |
| 828 |
mu.Lock() |
| 829 |
mu.Lock() |
| 830 |
})()`, |
| 831 |
}, |
| 832 |
} |
| 833 |
|
| 834 |
for _, test := range tests { |
| 835 |
done := make(chan struct{}) |
| 836 |
src := test.src |
| 837 |
go func() { |
| 838 |
defer close(done) |
| 839 |
i := interp.New(interp.Options{}) |
| 840 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 841 |
t.Error(err) |
| 842 |
} |
| 843 |
_, err := i.Eval(`import "sync"`) |
| 844 |
if err != nil { |
| 845 |
t.Errorf(`failed to import "sync": %v`, err) |
| 846 |
return |
| 847 |
} |
| 848 |
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 849 |
defer cancel() |
| 850 |
_, err = i.EvalWithContext(ctx, src) |
| 851 |
switch err { |
| 852 |
case context.DeadlineExceeded: |
| 853 |
// Successful cancellation. |
| 854 |
|
| 855 |
// Check we can still execute an expression. |
| 856 |
v, err := i.EvalWithContext(context.Background(), "1+1\n") |
| 857 |
if err != nil { |
| 858 |
t.Errorf("failed to evaluate expression after cancellation: %v", err) |
| 859 |
} |
| 860 |
got := v.Interface() |
| 861 |
if got != 2 { |
| 862 |
t.Errorf("unexpected result of eval(1+1): got %v, want 2", got) |
| 863 |
} |
| 864 |
case nil: |
| 865 |
t.Errorf("unexpected success evaluating expression %q", test.desc) |
| 866 |
default: |
| 867 |
t.Errorf("failed to evaluate expression %q: %v", test.desc, err) |
| 868 |
} |
| 869 |
}() |
| 870 |
select { |
| 871 |
case <-time.After(time.Second): |
| 872 |
t.Errorf("timeout failed to terminate execution of %q", test.desc) |
| 873 |
case <-done: |
| 874 |
} |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
func runTests(t *testing.T, i *interp.Interpreter, tests []testCase) { |
| 879 |
t.Helper() |
| 880 |
|
| 881 |
for _, test := range tests { |
| 882 |
t.Run(test.desc, func(t *testing.T) { |
| 883 |
if test.skip != "" { |
| 884 |
t.Skip(test.skip) |
| 885 |
} |
| 886 |
if test.pre != nil { |
| 887 |
test.pre() |
| 888 |
} |
| 889 |
if test.src != "" { |
| 890 |
assertEval(t, i, test.src, test.err, test.res) |
| 891 |
} |
| 892 |
}) |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
func eval(t *testing.T, i *interp.Interpreter, src string) reflect.Value { |
| 897 |
t.Helper() |
| 898 |
res, err := i.Eval(src) |
| 899 |
if err != nil { |
| 900 |
t.Logf("Error: %v", err) |
| 901 |
if e, ok := err.(interp.Panic); ok { |
| 902 |
t.Log(string(e.Stack)) |
| 903 |
} |
| 904 |
t.FailNow() |
| 905 |
} |
| 906 |
return res |
| 907 |
} |
| 908 |
|
| 909 |
func assertEval(t *testing.T, i *interp.Interpreter, src, expectedError, expectedRes string) { |
| 910 |
t.Helper() |
| 911 |
|
| 912 |
res, err := i.Eval(src) |
| 913 |
|
| 914 |
if expectedError != "" { |
| 915 |
if err == nil || !strings.Contains(err.Error(), expectedError) { |
| 916 |
t.Fatalf("got %v, want %s", err, expectedError) |
| 917 |
} |
| 918 |
return |
| 919 |
} |
| 920 |
|
| 921 |
if err != nil { |
| 922 |
t.Logf("got an error: %v", err) |
| 923 |
if e, ok := err.(interp.Panic); ok { |
| 924 |
t.Log(string(e.Stack)) |
| 925 |
} |
| 926 |
t.FailNow() |
| 927 |
} |
| 928 |
|
| 929 |
if fmt.Sprintf("%v", res) != expectedRes { |
| 930 |
t.Fatalf("got %v, want %s", res, expectedRes) |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
func TestMultiEval(t *testing.T) { |
| 935 |
t.Skip("fail in CI only ?") |
| 936 |
// catch stdout |
| 937 |
backupStdout := os.Stdout |
| 938 |
defer func() { |
| 939 |
os.Stdout = backupStdout |
| 940 |
}() |
| 941 |
r, w, _ := os.Pipe() |
| 942 |
os.Stdout = w |
| 943 |
|
| 944 |
i := interp.New(interp.Options{}) |
| 945 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 946 |
t.Fatal(err) |
| 947 |
} |
| 948 |
|
| 949 |
f, err := os.Open(filepath.Join("testdata", "multi", "731")) |
| 950 |
if err != nil { |
| 951 |
t.Fatal(err) |
| 952 |
} |
| 953 |
names, err := f.Readdirnames(-1) |
| 954 |
if err != nil { |
| 955 |
t.Fatal(err) |
| 956 |
} |
| 957 |
for _, v := range names { |
| 958 |
if _, err := i.EvalPath(filepath.Join(f.Name(), v)); err != nil { |
| 959 |
t.Fatal(err) |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
// read stdout |
| 964 |
if err = w.Close(); err != nil { |
| 965 |
t.Fatal(err) |
| 966 |
} |
| 967 |
outInterp, err := io.ReadAll(r) |
| 968 |
if err != nil { |
| 969 |
t.Fatal(err) |
| 970 |
} |
| 971 |
|
| 972 |
// restore Stdout |
| 973 |
os.Stdout = backupStdout |
| 974 |
|
| 975 |
want := "A\nB\n" |
| 976 |
got := string(outInterp) |
| 977 |
if got != want { |
| 978 |
t.Fatalf("unexpected output: got %v, wanted %v", got, want) |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
func TestMultiEvalNoName(t *testing.T) { |
| 983 |
t.Skip("fail in CI only ?") |
| 984 |
i := interp.New(interp.Options{}) |
| 985 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 986 |
t.Fatal(err) |
| 987 |
} |
| 988 |
|
| 989 |
f, err := os.Open(filepath.Join("testdata", "multi", "731")) |
| 990 |
if err != nil { |
| 991 |
t.Fatal(err) |
| 992 |
} |
| 993 |
names, err := f.Readdirnames(-1) |
| 994 |
if err != nil { |
| 995 |
t.Fatal(err) |
| 996 |
} |
| 997 |
for k, v := range names { |
| 998 |
data, err := os.ReadFile(filepath.Join(f.Name(), v)) |
| 999 |
if err != nil { |
| 1000 |
t.Fatal(err) |
| 1001 |
} |
| 1002 |
_, err = i.Eval(string(data)) |
| 1003 |
if k == 1 { |
| 1004 |
expectedErr := fmt.Errorf("3:8: fmt/%s redeclared in this block", interp.DefaultSourceName) |
| 1005 |
if err == nil || err.Error() != expectedErr.Error() { |
| 1006 |
t.Fatalf("unexpected result; wanted error %v, got %v", expectedErr, err) |
| 1007 |
} |
| 1008 |
return |
| 1009 |
} |
| 1010 |
if err != nil { |
| 1011 |
t.Fatal(err) |
| 1012 |
} |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
const goMinorVersionTest = 16 |
| 1017 |
|
| 1018 |
func TestHasIOFS(t *testing.T) { |
| 1019 |
code := ` |
| 1020 |
// +build go1.18 |
| 1021 |
|
| 1022 |
package main |
| 1023 |
|
| 1024 |
import ( |
| 1025 |
"errors" |
| 1026 |
"io/fs" |
| 1027 |
) |
| 1028 |
|
| 1029 |
func main() { |
| 1030 |
pe := fs.PathError{} |
| 1031 |
pe.Op = "nothing" |
| 1032 |
pe.Path = "/nowhere" |
| 1033 |
pe.Err = errors.New("an error") |
| 1034 |
println(pe.Error()) |
| 1035 |
} |
| 1036 |
|
| 1037 |
// Output: |
| 1038 |
// nothing /nowhere: an error |
| 1039 |
` |
| 1040 |
|
| 1041 |
var buf bytes.Buffer |
| 1042 |
i := interp.New(interp.Options{Stdout: &buf}) |
| 1043 |
if err := i.Use(interp.Symbols); err != nil { |
| 1044 |
t.Fatal(err) |
| 1045 |
} |
| 1046 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 1047 |
t.Fatal(err) |
| 1048 |
} |
| 1049 |
|
| 1050 |
if _, err := i.Eval(code); err != nil { |
| 1051 |
t.Fatal(err) |
| 1052 |
} |
| 1053 |
|
| 1054 |
var expectedOutput string |
| 1055 |
var minor int |
| 1056 |
var err error |
| 1057 |
version := runtime.Version() |
| 1058 |
version = strings.Replace(version, "beta", ".", 1) |
| 1059 |
version = strings.Replace(version, "rc", ".", 1) |
| 1060 |
fields := strings.Fields(version) |
| 1061 |
// Go stable |
| 1062 |
if len(fields) == 1 { |
| 1063 |
v := strings.Split(version, ".") |
| 1064 |
if len(v) < 2 { |
| 1065 |
t.Fatalf("unexpected: %v", version) |
| 1066 |
} |
| 1067 |
minor, err = strconv.Atoi(v[1]) |
| 1068 |
if err != nil { |
| 1069 |
t.Fatal(err) |
| 1070 |
} |
| 1071 |
} else { |
| 1072 |
// Go devel |
| 1073 |
if fields[0] != "devel" { |
| 1074 |
t.Fatalf("unexpected: %v", fields[0]) |
| 1075 |
} |
| 1076 |
parts := strings.Split(fields[1], "-") |
| 1077 |
if len(parts) != 2 { |
| 1078 |
t.Fatalf("unexpected: %v", fields[1]) |
| 1079 |
} |
| 1080 |
minor, err = strconv.Atoi(strings.TrimPrefix(parts[0], "go1.")) |
| 1081 |
if err != nil { |
| 1082 |
t.Fatal(err) |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
if minor >= goMinorVersionTest { |
| 1087 |
expectedOutput = "nothing /nowhere: an error\n" |
| 1088 |
} |
| 1089 |
|
| 1090 |
output := buf.String() |
| 1091 |
if buf.String() != expectedOutput { |
| 1092 |
t.Fatalf("got: %v, wanted: %v", output, expectedOutput) |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
func TestImportPathIsKey(t *testing.T) { |
| 1097 |
// FIXME(marc): support of stdlib generic packages like "cmp", "maps", "slices" has changed |
| 1098 |
// the scope layout by introducing new source packages when stdlib is used. |
| 1099 |
// The logic of the following test doesn't apply anymore. |
| 1100 |
t.Skip("This test needs to be reworked.") |
| 1101 |
// No need to check the results of Eval, as TestFile already does it. |
| 1102 |
i := interp.New(interp.Options{GoPath: filepath.FromSlash("../_test/testdata/redeclaration-global7")}) |
| 1103 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 1104 |
t.Fatal(err) |
| 1105 |
} |
| 1106 |
|
| 1107 |
filePath := filepath.Join("..", "_test", "ipp_as_key.go") |
| 1108 |
if _, err := i.EvalPath(filePath); err != nil { |
| 1109 |
t.Fatal(err) |
| 1110 |
} |
| 1111 |
|
| 1112 |
wantScopes := map[string][]string{ |
| 1113 |
"main": { |
| 1114 |
"titi/ipp_as_key.go", |
| 1115 |
"tutu/ipp_as_key.go", |
| 1116 |
"main", |
| 1117 |
}, |
| 1118 |
"guthib.com/toto": { |
| 1119 |
"quux/titi.go", |
| 1120 |
"Quux", |
| 1121 |
}, |
| 1122 |
"guthib.com/bar": { |
| 1123 |
"Quux", |
| 1124 |
}, |
| 1125 |
"guthib.com/tata": { |
| 1126 |
"quux/tutu.go", |
| 1127 |
"Quux", |
| 1128 |
}, |
| 1129 |
"guthib.com/baz": { |
| 1130 |
"Quux", |
| 1131 |
}, |
| 1132 |
} |
| 1133 |
wantPackages := map[string]string{ |
| 1134 |
"guthib.com/baz": "quux", |
| 1135 |
"guthib.com/tata": "tutu", |
| 1136 |
"main": "main", |
| 1137 |
"guthib.com/bar": "quux", |
| 1138 |
"guthib.com/toto": "titi", |
| 1139 |
} |
| 1140 |
|
| 1141 |
scopes := i.Scopes() |
| 1142 |
if len(scopes) != len(wantScopes) { |
| 1143 |
t.Fatalf("want %d, got %d", len(wantScopes), len(scopes)) |
| 1144 |
} |
| 1145 |
for k, v := range scopes { |
| 1146 |
wantSym := wantScopes[k] |
| 1147 |
if len(v) != len(wantSym) { |
| 1148 |
t.Fatalf("want %d, got %d", len(wantSym), len(v)) |
| 1149 |
} |
| 1150 |
for _, sym := range wantSym { |
| 1151 |
if _, ok := v[sym]; !ok { |
| 1152 |
t.Fatalf("symbol %s not found in scope %s", sym, k) |
| 1153 |
} |
| 1154 |
} |
| 1155 |
} |
| 1156 |
|
| 1157 |
packages := i.Packages() |
| 1158 |
for k, v := range wantPackages { |
| 1159 |
pkg := packages[k] |
| 1160 |
if pkg != v { |
| 1161 |
t.Fatalf("for import path %s, want %s, got %s", k, v, pkg) |
| 1162 |
} |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
// The code in hello1.go and hello2.go spawns a "long-running" goroutine, which |
| 1167 |
// means each call to EvalPath actually terminates before the evaled code is done |
| 1168 |
// running. So this test demonstrates: |
| 1169 |
// 1) That two sequential calls to EvalPath don't see their "compilation phases" |
| 1170 |
// collide (no data race on the fields of the interpreter), which is somewhat |
| 1171 |
// obvious since the calls (and hence the "compilation phases") are sequential too. |
| 1172 |
// 2) That two concurrent goroutine runs spawned by the same interpreter do not |
| 1173 |
// collide either. |
| 1174 |
func TestConcurrentEvals(t *testing.T) { |
| 1175 |
if testing.Short() { |
| 1176 |
return |
| 1177 |
} |
| 1178 |
pin, pout := io.Pipe() |
| 1179 |
defer func() { |
| 1180 |
_ = pin.Close() |
| 1181 |
_ = pout.Close() |
| 1182 |
}() |
| 1183 |
interpr := interp.New(interp.Options{Stdout: pout}) |
| 1184 |
if err := interpr.Use(stdlib.Symbols); err != nil { |
| 1185 |
t.Fatal(err) |
| 1186 |
} |
| 1187 |
|
| 1188 |
if _, err := interpr.EvalPath("testdata/concurrent/hello1.go"); err != nil { |
| 1189 |
t.Fatal(err) |
| 1190 |
} |
| 1191 |
if _, err := interpr.EvalPath("testdata/concurrent/hello2.go"); err != nil { |
| 1192 |
t.Fatal(err) |
| 1193 |
} |
| 1194 |
|
| 1195 |
c := make(chan error) |
| 1196 |
go func() { |
| 1197 |
hello1, hello2 := false, false |
| 1198 |
sc := bufio.NewScanner(pin) |
| 1199 |
for sc.Scan() { |
| 1200 |
l := sc.Text() |
| 1201 |
switch l { |
| 1202 |
case "hello world1": |
| 1203 |
hello1 = true |
| 1204 |
case "hello world2": |
| 1205 |
hello2 = true |
| 1206 |
case "hello world1hello world2", "hello world2hello world1": |
| 1207 |
hello1 = true |
| 1208 |
hello2 = true |
| 1209 |
default: |
| 1210 |
c <- fmt.Errorf("unexpected output: %v", l) |
| 1211 |
return |
| 1212 |
} |
| 1213 |
if hello1 && hello2 { |
| 1214 |
break |
| 1215 |
} |
| 1216 |
} |
| 1217 |
c <- nil |
| 1218 |
}() |
| 1219 |
|
| 1220 |
timeout := time.NewTimer(5 * time.Second) |
| 1221 |
select { |
| 1222 |
case <-timeout.C: |
| 1223 |
t.Fatal("timeout") |
| 1224 |
case err := <-c: |
| 1225 |
if err != nil { |
| 1226 |
t.Fatal(err) |
| 1227 |
} |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
// TestConcurrentEvals2 shows that even though EvalWithContext calls Eval in a |
| 1232 |
// goroutine, it indeed waits for Eval to terminate, and that therefore the code |
| 1233 |
// called by EvalWithContext is sequential. And that there is no data race for the |
| 1234 |
// interp package global vars or the interpreter fields in this case. |
| 1235 |
func TestConcurrentEvals2(t *testing.T) { |
| 1236 |
if testing.Short() { |
| 1237 |
return |
| 1238 |
} |
| 1239 |
pin, pout := io.Pipe() |
| 1240 |
defer func() { |
| 1241 |
_ = pin.Close() |
| 1242 |
_ = pout.Close() |
| 1243 |
}() |
| 1244 |
interpr := interp.New(interp.Options{Stdout: pout}) |
| 1245 |
if err := interpr.Use(stdlib.Symbols); err != nil { |
| 1246 |
t.Fatal(err) |
| 1247 |
} |
| 1248 |
|
| 1249 |
done := make(chan error) |
| 1250 |
go func() { |
| 1251 |
hello1 := false |
| 1252 |
sc := bufio.NewScanner(pin) |
| 1253 |
for sc.Scan() { |
| 1254 |
l := sc.Text() |
| 1255 |
if hello1 { |
| 1256 |
if l == "hello world2" { |
| 1257 |
break |
| 1258 |
} |
| 1259 |
done <- fmt.Errorf("unexpected output: %v", l) |
| 1260 |
return |
| 1261 |
} |
| 1262 |
if l == "hello world1" { |
| 1263 |
hello1 = true |
| 1264 |
} else { |
| 1265 |
done <- fmt.Errorf("unexpected output: %v", l) |
| 1266 |
return |
| 1267 |
} |
| 1268 |
} |
| 1269 |
done <- nil |
| 1270 |
}() |
| 1271 |
|
| 1272 |
ctx := context.Background() |
| 1273 |
if _, err := interpr.EvalWithContext(ctx, `import "time"`); err != nil { |
| 1274 |
t.Fatal(err) |
| 1275 |
} |
| 1276 |
if _, err := interpr.EvalWithContext(ctx, `time.Sleep(time.Second); println("hello world1")`); err != nil { |
| 1277 |
t.Fatal(err) |
| 1278 |
} |
| 1279 |
if _, err := interpr.EvalWithContext(ctx, `time.Sleep(time.Second); println("hello world2")`); err != nil { |
| 1280 |
t.Fatal(err) |
| 1281 |
} |
| 1282 |
|
| 1283 |
timeout := time.NewTimer(5 * time.Second) |
| 1284 |
select { |
| 1285 |
case <-timeout.C: |
| 1286 |
t.Fatal("timeout") |
| 1287 |
case err := <-done: |
| 1288 |
if err != nil { |
| 1289 |
t.Fatal(err) |
| 1290 |
} |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
// TestConcurrentEvals3 makes sure that we don't regress into data races at the package level, i.e from: |
| 1295 |
// - global vars, which should obviously not be mutated. |
| 1296 |
// - when calling Interpreter.Use, the symbols given as argument should be |
| 1297 |
// copied when being inserted into interp.binPkg, and not directly used as-is. |
| 1298 |
func TestConcurrentEvals3(t *testing.T) { |
| 1299 |
if testing.Short() { |
| 1300 |
return |
| 1301 |
} |
| 1302 |
allDone := make(chan bool) |
| 1303 |
runREPL := func() { |
| 1304 |
done := make(chan error) |
| 1305 |
pinin, poutin := io.Pipe() |
| 1306 |
pinout, poutout := io.Pipe() |
| 1307 |
i := interp.New(interp.Options{Stdin: pinin, Stdout: poutout}) |
| 1308 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 1309 |
t.Fatal(err) |
| 1310 |
} |
| 1311 |
|
| 1312 |
go func() { |
| 1313 |
_, _ = i.REPL() |
| 1314 |
}() |
| 1315 |
|
| 1316 |
input := []string{ |
| 1317 |
`hello one`, |
| 1318 |
`hello two`, |
| 1319 |
`hello three`, |
| 1320 |
} |
| 1321 |
|
| 1322 |
go func() { |
| 1323 |
sc := bufio.NewScanner(pinout) |
| 1324 |
k := 0 |
| 1325 |
for sc.Scan() { |
| 1326 |
l := sc.Text() |
| 1327 |
if l != input[k] { |
| 1328 |
done <- fmt.Errorf("unexpected output, want %q, got %q", input[k], l) |
| 1329 |
return |
| 1330 |
} |
| 1331 |
k++ |
| 1332 |
if k > 2 { |
| 1333 |
break |
| 1334 |
} |
| 1335 |
} |
| 1336 |
done <- nil |
| 1337 |
}() |
| 1338 |
|
| 1339 |
for _, v := range input { |
| 1340 |
in := strings.NewReader(fmt.Sprintf("println(%q)\n", v)) |
| 1341 |
if _, err := io.Copy(poutin, in); err != nil { |
| 1342 |
t.Fatal(err) |
| 1343 |
} |
| 1344 |
time.Sleep(time.Second) |
| 1345 |
} |
| 1346 |
|
| 1347 |
if err := <-done; err != nil { |
| 1348 |
t.Fatal(err) |
| 1349 |
} |
| 1350 |
_ = pinin.Close() |
| 1351 |
_ = poutin.Close() |
| 1352 |
_ = pinout.Close() |
| 1353 |
_ = poutout.Close() |
| 1354 |
allDone <- true |
| 1355 |
} |
| 1356 |
|
| 1357 |
for i := 0; i < 2; i++ { |
| 1358 |
go func() { |
| 1359 |
runREPL() |
| 1360 |
}() |
| 1361 |
} |
| 1362 |
|
| 1363 |
timeout := time.NewTimer(10 * time.Second) |
| 1364 |
for i := 0; i < 2; i++ { |
| 1365 |
select { |
| 1366 |
case <-allDone: |
| 1367 |
case <-timeout.C: |
| 1368 |
t.Fatal("timeout") |
| 1369 |
} |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
func TestConcurrentComposite1(t *testing.T) { |
| 1374 |
testConcurrentComposite(t, "./testdata/concurrent/composite/composite_lit.go") |
| 1375 |
} |
| 1376 |
|
| 1377 |
func TestConcurrentComposite2(t *testing.T) { |
| 1378 |
testConcurrentComposite(t, "./testdata/concurrent/composite/composite_sparse.go") |
| 1379 |
} |
| 1380 |
|
| 1381 |
func testConcurrentComposite(t *testing.T, filePath string) { |
| 1382 |
t.Helper() |
| 1383 |
|
| 1384 |
if testing.Short() { |
| 1385 |
return |
| 1386 |
} |
| 1387 |
pin, pout := io.Pipe() |
| 1388 |
i := interp.New(interp.Options{Stdout: pout}) |
| 1389 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 1390 |
t.Fatal(err) |
| 1391 |
} |
| 1392 |
|
| 1393 |
errc := make(chan error) |
| 1394 |
var output string |
| 1395 |
go func() { |
| 1396 |
sc := bufio.NewScanner(pin) |
| 1397 |
k := 0 |
| 1398 |
for sc.Scan() { |
| 1399 |
output += sc.Text() |
| 1400 |
k++ |
| 1401 |
if k > 1 { |
| 1402 |
break |
| 1403 |
} |
| 1404 |
} |
| 1405 |
errc <- nil |
| 1406 |
}() |
| 1407 |
|
| 1408 |
if _, err := i.EvalPath(filePath); err != nil { |
| 1409 |
t.Fatal(err) |
| 1410 |
} |
| 1411 |
|
| 1412 |
_ = pin.Close() |
| 1413 |
_ = pout.Close() |
| 1414 |
|
| 1415 |
if err := <-errc; err != nil { |
| 1416 |
t.Fatal(err) |
| 1417 |
} |
| 1418 |
|
| 1419 |
expected := "{hello}{hello}" |
| 1420 |
if output != expected { |
| 1421 |
t.Fatalf("unexpected output, want %q, got %q", expected, output) |
| 1422 |
} |
| 1423 |
} |
| 1424 |
|
| 1425 |
func TestEvalREPL(t *testing.T) { |
| 1426 |
if testing.Short() { |
| 1427 |
return |
| 1428 |
} |
| 1429 |
type testCase struct { |
| 1430 |
desc string |
| 1431 |
src []string |
| 1432 |
errorLine int |
| 1433 |
} |
| 1434 |
tests := []testCase{ |
| 1435 |
{ |
| 1436 |
desc: "no error", |
| 1437 |
src: []string{ |
| 1438 |
`func main() {`, |
| 1439 |
`println("foo")`, |
| 1440 |
`}`, |
| 1441 |
}, |
| 1442 |
errorLine: -1, |
| 1443 |
}, |
| 1444 |
|
| 1445 |
{ |
| 1446 |
desc: "no parsing error, but block error", |
| 1447 |
src: []string{ |
| 1448 |
`func main() {`, |
| 1449 |
`println(foo)`, |
| 1450 |
`}`, |
| 1451 |
}, |
| 1452 |
errorLine: 2, |
| 1453 |
}, |
| 1454 |
{ |
| 1455 |
desc: "parsing error", |
| 1456 |
src: []string{ |
| 1457 |
`func main() {`, |
| 1458 |
`println(/foo)`, |
| 1459 |
`}`, |
| 1460 |
}, |
| 1461 |
errorLine: 1, |
| 1462 |
}, |
| 1463 |
{ |
| 1464 |
desc: "multi-line string literal", |
| 1465 |
src: []string{ |
| 1466 |
"var a = `hello", |
| 1467 |
"there, how", |
| 1468 |
"are you?`", |
| 1469 |
}, |
| 1470 |
errorLine: -1, |
| 1471 |
}, |
| 1472 |
|
| 1473 |
{ |
| 1474 |
desc: "multi-line comma operand", |
| 1475 |
src: []string{ |
| 1476 |
`println(2,`, |
| 1477 |
`3)`, |
| 1478 |
}, |
| 1479 |
errorLine: -1, |
| 1480 |
}, |
| 1481 |
{ |
| 1482 |
desc: "multi-line arithmetic operand", |
| 1483 |
src: []string{ |
| 1484 |
`println(2. /`, |
| 1485 |
`3.)`, |
| 1486 |
}, |
| 1487 |
errorLine: -1, |
| 1488 |
}, |
| 1489 |
{ |
| 1490 |
desc: "anonymous func call with no assignment", |
| 1491 |
src: []string{ |
| 1492 |
`func() { println(3) }()`, |
| 1493 |
}, |
| 1494 |
errorLine: -1, |
| 1495 |
}, |
| 1496 |
{ |
| 1497 |
// to make sure that special handling of the above anonymous, does not break this general case. |
| 1498 |
desc: "just func", |
| 1499 |
src: []string{ |
| 1500 |
`func foo() { println(3) }`, |
| 1501 |
}, |
| 1502 |
errorLine: -1, |
| 1503 |
}, |
| 1504 |
{ |
| 1505 |
// to make sure that special handling of the above anonymous, does not break this general case. |
| 1506 |
desc: "just method", |
| 1507 |
src: []string{ |
| 1508 |
`type bar string`, |
| 1509 |
`func (b bar) foo() { println(3) }`, |
| 1510 |
}, |
| 1511 |
errorLine: -1, |
| 1512 |
}, |
| 1513 |
{ |
| 1514 |
desc: "define a label", |
| 1515 |
src: []string{ |
| 1516 |
`a:`, |
| 1517 |
}, |
| 1518 |
errorLine: -1, |
| 1519 |
}, |
| 1520 |
} |
| 1521 |
|
| 1522 |
runREPL := func(t *testing.T, test testCase) { |
| 1523 |
// TODO(mpl): use a pipe for the output as well, just as in TestConcurrentEvals5 |
| 1524 |
var stdout bytes.Buffer |
| 1525 |
safeStdout := &safeBuffer{buf: &stdout} |
| 1526 |
var stderr bytes.Buffer |
| 1527 |
safeStderr := &safeBuffer{buf: &stderr} |
| 1528 |
pin, pout := io.Pipe() |
| 1529 |
i := interp.New(interp.Options{Stdin: pin, Stdout: safeStdout, Stderr: safeStderr}) |
| 1530 |
defer func() { |
| 1531 |
// Closing the pipe also takes care of making i.REPL terminate, |
| 1532 |
// hence freeing its goroutine. |
| 1533 |
_ = pin.Close() |
| 1534 |
_ = pout.Close() |
| 1535 |
}() |
| 1536 |
|
| 1537 |
go func() { |
| 1538 |
_, _ = i.REPL() |
| 1539 |
}() |
| 1540 |
for k, v := range test.src { |
| 1541 |
if _, err := pout.Write([]byte(v + "\n")); err != nil { |
| 1542 |
t.Error(err) |
| 1543 |
} |
| 1544 |
Sleep(100 * time.Millisecond) |
| 1545 |
|
| 1546 |
errMsg := safeStderr.String() |
| 1547 |
if k == test.errorLine { |
| 1548 |
if errMsg == "" { |
| 1549 |
t.Fatalf("test %q: statement %q should have produced an error", test.desc, v) |
| 1550 |
} |
| 1551 |
break |
| 1552 |
} |
| 1553 |
if errMsg != "" { |
| 1554 |
t.Fatalf("test %q: unexpected error: %v", test.desc, errMsg) |
| 1555 |
} |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
for _, test := range tests { |
| 1560 |
runREPL(t, test) |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
type safeBuffer struct { |
| 1565 |
mu sync.RWMutex |
| 1566 |
buf *bytes.Buffer |
| 1567 |
} |
| 1568 |
|
| 1569 |
func (sb *safeBuffer) Read(p []byte) (int, error) { |
| 1570 |
return sb.buf.Read(p) |
| 1571 |
} |
| 1572 |
|
| 1573 |
func (sb *safeBuffer) String() string { |
| 1574 |
sb.mu.RLock() |
| 1575 |
defer sb.mu.RUnlock() |
| 1576 |
return sb.buf.String() |
| 1577 |
} |
| 1578 |
|
| 1579 |
func (sb *safeBuffer) Write(p []byte) (int, error) { |
| 1580 |
sb.mu.Lock() |
| 1581 |
defer sb.mu.Unlock() |
| 1582 |
return sb.buf.Write(p) |
| 1583 |
} |
| 1584 |
|
| 1585 |
const ( |
| 1586 |
// CITimeoutMultiplier is the multiplier for all timeouts in the CI. |
| 1587 |
CITimeoutMultiplier = 3 |
| 1588 |
) |
| 1589 |
|
| 1590 |
// Sleep pauses the current goroutine for at least the duration d. |
| 1591 |
func Sleep(d time.Duration) { |
| 1592 |
d = applyCIMultiplier(d) |
| 1593 |
time.Sleep(d) |
| 1594 |
} |
| 1595 |
|
| 1596 |
func applyCIMultiplier(timeout time.Duration) time.Duration { |
| 1597 |
ci := os.Getenv("CI") |
| 1598 |
if ci == "" { |
| 1599 |
return timeout |
| 1600 |
} |
| 1601 |
b, err := strconv.ParseBool(ci) |
| 1602 |
if err != nil || !b { |
| 1603 |
return timeout |
| 1604 |
} |
| 1605 |
return time.Duration(float64(timeout) * CITimeoutMultiplier) |
| 1606 |
} |
| 1607 |
|
| 1608 |
func TestREPLCommands(t *testing.T) { |
| 1609 |
if testing.Short() { |
| 1610 |
return |
| 1611 |
} |
| 1612 |
t.Setenv("YAEGI_PROMPT", "1") // To force prompts over non-tty streams |
| 1613 |
|
| 1614 |
allDone := make(chan bool) |
| 1615 |
runREPL := func() { |
| 1616 |
done := make(chan error) |
| 1617 |
pinin, poutin := io.Pipe() |
| 1618 |
pinout, poutout := io.Pipe() |
| 1619 |
i := interp.New(interp.Options{Stdin: pinin, Stdout: poutout}) |
| 1620 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 1621 |
t.Fatal(err) |
| 1622 |
} |
| 1623 |
|
| 1624 |
go func() { |
| 1625 |
_, _ = i.REPL() |
| 1626 |
}() |
| 1627 |
|
| 1628 |
defer func() { |
| 1629 |
_ = pinin.Close() |
| 1630 |
_ = poutin.Close() |
| 1631 |
_ = pinout.Close() |
| 1632 |
_ = poutout.Close() |
| 1633 |
allDone <- true |
| 1634 |
}() |
| 1635 |
|
| 1636 |
input := []string{ |
| 1637 |
`1/1`, |
| 1638 |
`7/3`, |
| 1639 |
`16/5`, |
| 1640 |
`3./2`, // float |
| 1641 |
`reflect.TypeOf(math_rand.Int)`, |
| 1642 |
`reflect.TypeOf(crypto_rand.Int)`, |
| 1643 |
} |
| 1644 |
output := []string{ |
| 1645 |
`1`, |
| 1646 |
`2`, |
| 1647 |
`3`, |
| 1648 |
`1.5`, |
| 1649 |
`func() int`, |
| 1650 |
`func(io.Reader, *big.Int) (*big.Int, error)`, |
| 1651 |
} |
| 1652 |
|
| 1653 |
go func() { |
| 1654 |
sc := bufio.NewScanner(pinout) |
| 1655 |
k := 0 |
| 1656 |
for sc.Scan() { |
| 1657 |
l := sc.Text() |
| 1658 |
if l != "> : "+output[k] { |
| 1659 |
done <- fmt.Errorf("unexpected output, want %q, got %q", output[k], l) |
| 1660 |
return |
| 1661 |
} |
| 1662 |
k++ |
| 1663 |
if k > 3 { |
| 1664 |
break |
| 1665 |
} |
| 1666 |
} |
| 1667 |
done <- nil |
| 1668 |
}() |
| 1669 |
|
| 1670 |
for _, v := range input { |
| 1671 |
in := strings.NewReader(v + "\n") |
| 1672 |
if _, err := io.Copy(poutin, in); err != nil { |
| 1673 |
t.Fatal(err) |
| 1674 |
} |
| 1675 |
select { |
| 1676 |
case err := <-done: |
| 1677 |
if err != nil { |
| 1678 |
t.Fatal(err) |
| 1679 |
} |
| 1680 |
return |
| 1681 |
default: |
| 1682 |
time.Sleep(time.Second) |
| 1683 |
} |
| 1684 |
} |
| 1685 |
|
| 1686 |
if err := <-done; err != nil { |
| 1687 |
t.Fatal(err) |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
go func() { |
| 1692 |
runREPL() |
| 1693 |
}() |
| 1694 |
|
| 1695 |
timeout := time.NewTimer(10 * time.Second) |
| 1696 |
select { |
| 1697 |
case <-allDone: |
| 1698 |
case <-timeout.C: |
| 1699 |
t.Fatal("timeout") |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
func TestStdio(t *testing.T) { |
| 1704 |
i := interp.New(interp.Options{}) |
| 1705 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 1706 |
t.Fatal(err) |
| 1707 |
} |
| 1708 |
i.ImportUsed() |
| 1709 |
if _, err := i.Eval(`var x = os.Stdout`); err != nil { |
| 1710 |
t.Fatal(err) |
| 1711 |
} |
| 1712 |
v, _ := i.Eval(`x`) |
| 1713 |
if _, ok := v.Interface().(*os.File); !ok { |
| 1714 |
t.Fatalf("%v not *os.file", v.Interface()) |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
func TestNoGoFiles(t *testing.T) { |
| 1719 |
i := interp.New(interp.Options{GoPath: build.Default.GOPATH}) |
| 1720 |
_, err := i.Eval(`import "github.com/traefik/yaegi/_test/p3"`) |
| 1721 |
if strings.Contains(err.Error(), "no Go files in") { |
| 1722 |
return |
| 1723 |
} |
| 1724 |
|
| 1725 |
t.Fatalf("failed to detect no Go files: %v", err) |
| 1726 |
} |
| 1727 |
|
| 1728 |
func TestIssue1142(t *testing.T) { |
| 1729 |
i := interp.New(interp.Options{}) |
| 1730 |
runTests(t, i, []testCase{ |
| 1731 |
{src: "a := 1; // foo bar", res: "1"}, |
| 1732 |
}) |
| 1733 |
} |
| 1734 |
|
| 1735 |
type Issue1149Array [3]float32 |
| 1736 |
|
| 1737 |
func (v Issue1149Array) Foo() string { return "foo" } |
| 1738 |
func (v *Issue1149Array) Bar() string { return "foo" } |
| 1739 |
|
| 1740 |
func TestIssue1149(t *testing.T) { |
| 1741 |
i := interp.New(interp.Options{}) |
| 1742 |
if err := i.Use(interp.Exports{ |
| 1743 |
"pkg/pkg": map[string]reflect.Value{ |
| 1744 |
"Type": reflect.ValueOf((*Issue1149Array)(nil)), |
| 1745 |
}, |
| 1746 |
}); err != nil { |
| 1747 |
t.Fatal(err) |
| 1748 |
} |
| 1749 |
i.ImportUsed() |
| 1750 |
|
| 1751 |
_, err := i.Eval(` |
| 1752 |
type Type = pkg.Type |
| 1753 |
`) |
| 1754 |
if err != nil { |
| 1755 |
t.Fatal(err) |
| 1756 |
} |
| 1757 |
|
| 1758 |
runTests(t, i, []testCase{ |
| 1759 |
{src: "Type{1, 2, 3}.Foo()", res: "foo"}, |
| 1760 |
{src: "Type{1, 2, 3}.Bar()", res: "foo"}, |
| 1761 |
}) |
| 1762 |
} |
| 1763 |
|
| 1764 |
func TestIssue1150(t *testing.T) { |
| 1765 |
i := interp.New(interp.Options{}) |
| 1766 |
_, err := i.Eval(` |
| 1767 |
type ArrayT [3]float32 |
| 1768 |
type SliceT []float32 |
| 1769 |
type StructT struct { A, B, C float32 } |
| 1770 |
type StructT2 struct { A, B, C float32 } |
| 1771 |
type FooerT interface { Foo() string } |
| 1772 |
|
| 1773 |
func (v ArrayT) Foo() string { return "foo" } |
| 1774 |
func (v SliceT) Foo() string { return "foo" } |
| 1775 |
func (v StructT) Foo() string { return "foo" } |
| 1776 |
func (v *StructT2) Foo() string { return "foo" } |
| 1777 |
|
| 1778 |
type Array = ArrayT |
| 1779 |
type Slice = SliceT |
| 1780 |
type Struct = StructT |
| 1781 |
type Struct2 = StructT2 |
| 1782 |
type Fooer = FooerT |
| 1783 |
`) |
| 1784 |
if err != nil { |
| 1785 |
t.Fatal(err) |
| 1786 |
} |
| 1787 |
|
| 1788 |
runTests(t, i, []testCase{ |
| 1789 |
{desc: "array", src: "Array{1, 2, 3}.Foo()", res: "foo"}, |
| 1790 |
{desc: "slice", src: "Slice{1, 2, 3}.Foo()", res: "foo"}, |
| 1791 |
{desc: "struct", src: "Struct{1, 2, 3}.Foo()", res: "foo"}, |
| 1792 |
{desc: "*struct", src: "Struct2{1, 2, 3}.Foo()", res: "foo"}, |
| 1793 |
{desc: "interface", src: "v := Fooer(Array{1, 2, 3}); v.Foo()", res: "foo"}, |
| 1794 |
}) |
| 1795 |
} |
| 1796 |
|
| 1797 |
func TestIssue1151(t *testing.T) { |
| 1798 |
type pkgStruct struct{ X int } |
| 1799 |
type pkgArray [1]int |
| 1800 |
|
| 1801 |
i := interp.New(interp.Options{}) |
| 1802 |
if err := i.Use(interp.Exports{ |
| 1803 |
"pkg/pkg": map[string]reflect.Value{ |
| 1804 |
"Struct": reflect.ValueOf((*pkgStruct)(nil)), |
| 1805 |
"Array": reflect.ValueOf((*pkgArray)(nil)), |
| 1806 |
}, |
| 1807 |
}); err != nil { |
| 1808 |
t.Fatal(err) |
| 1809 |
} |
| 1810 |
i.ImportUsed() |
| 1811 |
|
| 1812 |
runTests(t, i, []testCase{ |
| 1813 |
{src: "x := pkg.Struct{1}", res: "{1}"}, |
| 1814 |
{src: "x := pkg.Array{1}", res: "[1]"}, |
| 1815 |
}) |
| 1816 |
} |
| 1817 |
|
| 1818 |
func TestPassArgs(t *testing.T) { |
| 1819 |
i := interp.New(interp.Options{Args: []string{"arg0", "arg1"}}) |
| 1820 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 1821 |
t.Fatal(err) |
| 1822 |
} |
| 1823 |
i.ImportUsed() |
| 1824 |
runTests(t, i, []testCase{ |
| 1825 |
{src: "os.Args", res: "[arg0 arg1]"}, |
| 1826 |
}) |
| 1827 |
} |
| 1828 |
|
| 1829 |
func TestRestrictedEnv(t *testing.T) { |
| 1830 |
i := interp.New(interp.Options{Env: []string{"foo=bar"}}) |
| 1831 |
if err := i.Use(stdlib.Symbols); err != nil { |
| 1832 |
t.Fatal(err) |
| 1833 |
} |
| 1834 |
i.ImportUsed() |
| 1835 |
runTests(t, i, []testCase{ |
| 1836 |
{src: `os.Getenv("foo")`, res: "bar"}, |
| 1837 |
{src: `s, ok := os.LookupEnv("foo"); s`, res: "bar"}, |
| 1838 |
{src: `s, ok := os.LookupEnv("foo"); ok`, res: "true"}, |
| 1839 |
{src: `s, ok := os.LookupEnv("PATH"); s`, res: ""}, |
| 1840 |
{src: `s, ok := os.LookupEnv("PATH"); ok`, res: "false"}, |
| 1841 |
{src: `os.Setenv("foo", "baz"); os.Environ()`, res: "[foo=baz]"}, |
| 1842 |
{src: `os.ExpandEnv("foo is ${foo}")`, res: "foo is baz"}, |
| 1843 |
{src: `os.Unsetenv("foo"); os.Environ()`, res: "[]"}, |
| 1844 |
{src: `os.Setenv("foo", "baz"); os.Environ()`, res: "[foo=baz]"}, |
| 1845 |
{src: `os.Clearenv(); os.Environ()`, res: "[]"}, |
| 1846 |
{src: `os.Setenv("foo", "baz"); os.Environ()`, res: "[foo=baz]"}, |
| 1847 |
}) |
| 1848 |
if s, ok := os.LookupEnv("foo"); ok { |
| 1849 |
t.Fatal("expected \"\", got " + s) |
| 1850 |
} |
| 1851 |
} |
| 1852 |
|
| 1853 |
func TestIssue1388(t *testing.T) { |
| 1854 |
i := interp.New(interp.Options{Env: []string{"foo=bar"}}) |
| 1855 |
err := i.Use(stdlib.Symbols) |
| 1856 |
if err != nil { |
| 1857 |
t.Fatal(err) |
| 1858 |
} |
| 1859 |
|
| 1860 |
_, err = i.Eval(`x := errors.New("")`) |
| 1861 |
if err == nil { |
| 1862 |
t.Fatal("Expected an error") |
| 1863 |
} |
| 1864 |
|
| 1865 |
_, err = i.Eval(`import "errors"`) |
| 1866 |
if err != nil { |
| 1867 |
t.Fatal(err) |
| 1868 |
} |
| 1869 |
|
| 1870 |
_, err = i.Eval(`x := errors.New("")`) |
| 1871 |
if err != nil { |
| 1872 |
t.Fatal(err) |
| 1873 |
} |
| 1874 |
} |
| 1875 |
|
| 1876 |
func TestIssue1383(t *testing.T) { |
| 1877 |
const src = ` |
| 1878 |
package main |
| 1879 |
|
| 1880 |
func main() { |
| 1881 |
fmt.Println("Hello") |
| 1882 |
} |
| 1883 |
` |
| 1884 |
|
| 1885 |
i := interp.New(interp.Options{}) |
| 1886 |
err := i.Use(stdlib.Symbols) |
| 1887 |
if err != nil { |
| 1888 |
t.Fatal(err) |
| 1889 |
} |
| 1890 |
_, err = i.Eval(`import "fmt"`) |
| 1891 |
if err != nil { |
| 1892 |
t.Fatal(err) |
| 1893 |
} |
| 1894 |
|
| 1895 |
ast, err := parser.ParseFile(i.FileSet(), "_.go", src, parser.DeclarationErrors) |
| 1896 |
if err != nil { |
| 1897 |
t.Fatal(err) |
| 1898 |
} |
| 1899 |
prog, err := i.CompileAST(ast) |
| 1900 |
if err != nil { |
| 1901 |
t.Fatal(err) |
| 1902 |
} |
| 1903 |
_, err = i.Execute(prog) |
| 1904 |
if err != nil { |
| 1905 |
t.Fatal(err) |
| 1906 |
} |
| 1907 |
} |
| 1908 |
|
| 1909 |
func TestIssue1623(t *testing.T) { |
| 1910 |
var f float64 |
| 1911 |
var j int |
| 1912 |
var s string = "foo" |
| 1913 |
|
| 1914 |
i := interp.New(interp.Options{}) |
| 1915 |
if err := i.Use(interp.Exports{ |
| 1916 |
"pkg/pkg": map[string]reflect.Value{ |
| 1917 |
"F": reflect.ValueOf(&f).Elem(), |
| 1918 |
"J": reflect.ValueOf(&j).Elem(), |
| 1919 |
"S": reflect.ValueOf(&s).Elem(), |
| 1920 |
}, |
| 1921 |
}); err != nil { |
| 1922 |
t.Fatal(err) |
| 1923 |
} |
| 1924 |
i.ImportUsed() |
| 1925 |
|
| 1926 |
runTests(t, i, []testCase{ |
| 1927 |
{desc: "pkg.F = 2.0", src: "pkg.F = 2.0; pkg.F", res: "2"}, |
| 1928 |
{desc: "pkg.J = 3", src: "pkg.J = 3; pkg.J", res: "3"}, |
| 1929 |
{desc: `pkg.S = "bar"`, src: `pkg.S = "bar"; pkg.S`, res: "bar"}, |
| 1930 |
}) |
| 1931 |
} |