forked from traefik/yaegi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for issue traefik#1634 -- for passing a closure to exported Go fu…
…nction
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package interp | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestExportClosureArg(t *testing.T) { | ||
outExp := []byte("0\n1\n2\n") | ||
// catch stdout | ||
backupStdout := os.Stdout | ||
defer func() { | ||
os.Stdout = backupStdout | ||
}() | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
i := New(Options{}) | ||
i.Use(Exports{ | ||
"tmp/tmp": map[string]reflect.Value{ | ||
"Func": reflect.ValueOf(func(s *[]func(), f func()) { *s = append(*s, f) }), | ||
}, | ||
}) | ||
i.ImportUsed() | ||
|
||
_, err := i.Eval(` | ||
func main() { | ||
fs := []func(){} | ||
for i := 0; i < 3; i++ { | ||
i := i | ||
tmp.Func(&fs, func() { println(i) }) | ||
} | ||
for _, f := range fs { | ||
f() | ||
} | ||
} | ||
`) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// read stdout | ||
if err = w.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
outInterp, err := io.ReadAll(r) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(outInterp, outExp) { | ||
t.Errorf("\nGot: %q,\n want: %q", string(outInterp), string(outExp)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters