2015-07-24 14:16:31 +00:00
|
|
|
// Copyright 2015 The go-python Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-27 16:46:30 +00:00
|
|
|
"bytes"
|
2015-07-24 14:16:31 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-07-27 16:46:30 +00:00
|
|
|
"reflect"
|
2015-07-24 14:16:31 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBind(t *testing.T) {
|
|
|
|
// mk && rm -rf toto $TMPDIR/gopy-* && gopy bind -output=./toto ./_examples/hi && (echo "=== testing..."; cd toto; cp ../_examples/hi/test.py .; python2 ./test.py && echo "[ok]" || echo "ERR")
|
|
|
|
//
|
|
|
|
|
|
|
|
workdir, err := ioutil.TempDir("", "gopy-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create workdir: %v\n", err)
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(workdir, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create workdir: %v\n", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(workdir)
|
|
|
|
|
|
|
|
cmd := exec.Command("gopy", "bind", "-output="+workdir, "./_examples/hi")
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error running gopy-bind: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(
|
|
|
|
"/bin/cp", "./_examples/hi/test.py",
|
|
|
|
filepath.Join(workdir, "test.py"),
|
|
|
|
)
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error copying 'test.py': %v\n", err)
|
|
|
|
}
|
|
|
|
|
2015-07-27 16:46:30 +00:00
|
|
|
want := []byte(`hi from go
|
|
|
|
hello you from go
|
|
|
|
--- doc(hi)...
|
|
|
|
package hi exposes a few Go functions to be wrapped and used from Python.
|
|
|
|
|
|
|
|
--- doc(hi.Hi)...
|
|
|
|
Hi()
|
|
|
|
|
|
|
|
Hi prints hi from Go
|
|
|
|
|
|
|
|
--- hi.Hi()...
|
|
|
|
--- doc(hi.Hello)...
|
|
|
|
Hello(str s)
|
|
|
|
|
|
|
|
Hello prints a greeting from Go
|
|
|
|
|
|
|
|
--- hi.Hello('you')...
|
|
|
|
--- doc(hi.Add)...
|
|
|
|
Add(int i, int j) int
|
|
|
|
|
|
|
|
Add returns the sum of its arguments.
|
|
|
|
|
|
|
|
--- hi.Add(1, 41)...
|
|
|
|
42
|
|
|
|
--- hi.Concat('4', '2')...
|
|
|
|
42
|
|
|
|
--- doc(hi.Person):
|
|
|
|
Person is a simple struct
|
|
|
|
|
|
|
|
--- p = hi.Person()...
|
|
|
|
['Age', 'Greet', 'Name', 'String', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
|
2015-07-29 09:44:37 +00:00
|
|
|
--- p.Name:
|
|
|
|
--- p.Age: 0
|
2015-07-27 16:57:27 +00:00
|
|
|
--- doc(hi.Greet):
|
|
|
|
Greet() str
|
|
|
|
|
|
|
|
Greet sends greetings
|
|
|
|
|
|
|
|
--- p.Greet()...
|
2015-07-28 08:06:20 +00:00
|
|
|
Hello, I am
|
2015-07-28 08:08:27 +00:00
|
|
|
--- p.String()...
|
|
|
|
hi.Person{Name="", Age=0}
|
2015-07-27 16:46:30 +00:00
|
|
|
--- doc(p):
|
|
|
|
Person is a simple struct
|
|
|
|
|
2015-07-29 09:44:37 +00:00
|
|
|
--- p.Name = "foo"...
|
|
|
|
--- p.Age = 42...
|
|
|
|
--- p.String()...
|
|
|
|
hi.Person{Name="foo", Age=42}
|
|
|
|
--- p.Age: 42
|
|
|
|
--- p.Name: foo
|
2015-07-27 16:46:30 +00:00
|
|
|
`)
|
|
|
|
buf := new(bytes.Buffer)
|
2015-07-24 14:16:31 +00:00
|
|
|
cmd = exec.Command("python2", "./test.py")
|
|
|
|
cmd.Dir = workdir
|
|
|
|
cmd.Stdin = os.Stdin
|
2015-07-27 16:46:30 +00:00
|
|
|
cmd.Stdout = buf
|
|
|
|
cmd.Stderr = buf
|
2015-07-24 14:16:31 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2015-07-27 16:46:30 +00:00
|
|
|
t.Fatalf(
|
|
|
|
"error running python module: %v\n%v\n", err,
|
|
|
|
string(buf.Bytes()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(string(buf.Bytes()), string(want)) {
|
|
|
|
t.Fatalf("error running python module:\nwant:\n%s\n\ngot:\n%s\n",
|
|
|
|
string(want), string(buf.Bytes()),
|
|
|
|
)
|
2015-07-24 14:16:31 +00:00
|
|
|
}
|
|
|
|
}
|