test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package test
  2. import (
  3. "testing"
  4. "os"
  5. "fmt"
  6. "errors"
  7. "strconv"
  8. "io/ioutil"
  9. "path/filepath"
  10. "kumachan/standalone/rx"
  11. "kumachan/interpreter"
  12. "kumachan/interpreter/runtime/vm"
  13. "kumachan/interpreter/runtime/lib/env"
  14. )
  15. func getTestDirPath(t *testing.T, kind string) string {
  16. var exe_path, err = os.Executable()
  17. if err != nil { t.Fatal(err) }
  18. var project_path = filepath.Dir(filepath.Dir(exe_path))
  19. return filepath.Join(project_path, "test", kind)
  20. }
  21. func expectStdIO(t *testing.T, path string, in string, expected_out string) {
  22. in_read, in_write, e := os.Pipe()
  23. if e != nil { panic(e) }
  24. out_read, out_write, e := os.Pipe()
  25. if e != nil { panic(e) }
  26. var test_env = env.NewDefaultEnvironment(nil)
  27. test_env.Stdin = rx.FileFrom(in_read)
  28. test_env.Stdout = rx.FileFrom(out_write)
  29. var options = &vm.Options { Environment: test_env }
  30. go (func() {
  31. interpreter.Run(path, options, nil)
  32. var e = out_write.Close()
  33. if e != nil { panic(e) }
  34. })()
  35. _, e = in_write.Write(([] byte)(in))
  36. if e != nil { panic(e) }
  37. e = in_write.Close()
  38. if e != nil { panic(e) }
  39. out, e := ioutil.ReadAll(out_read)
  40. if e != nil { panic(e) }
  41. var actual_out = string(out)
  42. if actual_out != expected_out {
  43. t.Fatal(errors.New(fmt.Sprintf(
  44. "stdout not matching\nexpected result:\n%s\nactual result:\n%s\n",
  45. strconv.Quote(expected_out), strconv.Quote(actual_out))))
  46. }
  47. }