github.com/traefik/yaegi@v0.15.1/example/closure/closure_test.go (about)

     1  package clos1
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/traefik/yaegi/interp"
     7  	"github.com/traefik/yaegi/stdlib"
     8  )
     9  
    10  func TestFunctionCall(t *testing.T) {
    11  	i := interp.New(interp.Options{GoPath: "./_pkg"})
    12  	if err := i.Use(stdlib.Symbols); err != nil {
    13  		t.Fatal(err)
    14  	}
    15  
    16  	_, err := i.Eval(`import "foo/bar"`)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	fnv, err := i.Eval(`bar.NewSample()`)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	fn, ok := fnv.Interface().(func(string, string) func(string) string)
    27  	if !ok {
    28  		t.Fatal("conversion failed")
    29  	}
    30  
    31  	fn2 := fn("hello", "world")
    32  	val := fn2("truc")
    33  
    34  	expected := "herev1helloworldtruc"
    35  	if val != expected {
    36  		t.Errorf("Got: %q, want: %q", val, expected)
    37  	}
    38  }