Permalink
Browse files

Add Example of Client Run, Compile and Share

  • Loading branch information...
1 parent 4def8d1 commit b31db47576a1f57a6c32305c85375c521f4894db @haya14busa committed Sep 2, 2016
Showing with 114 additions and 0 deletions.
  1. +114 −0 goplay_test.go
View
@@ -4,13 +4,25 @@ import (
"bytes"
"encoding/json"
"fmt"
+ "io/ioutil"
+ "log"
"net/http"
"net/http/httptest"
+ "os"
"reflect"
+ "strings"
"testing"
"time"
)
+type fakeTransPort struct {
+ FakeRoundTrip func(*http.Request) (*http.Response, error)
+}
+
+func (f *fakeTransPort) RoundTrip(r *http.Request) (*http.Response, error) {
+ return f.FakeRoundTrip(r)
+}
+
func TestClient_Run(t *testing.T) {
saveDelay := delay
defer func() { delay = saveDelay }()
@@ -74,6 +86,34 @@ func TestClient_Run(t *testing.T) {
}
}
+var mockRunClient = &http.Client{Transport: &fakeTransPort{
+ FakeRoundTrip: func(r *http.Request) (*http.Response, error) {
+ mes := []*Event{
+ {Message: "Hello, 世界!\n", Kind: "stdout", Delay: 0},
+ }
+ b, _ := json.Marshal(&Response{Errors: "", Events: mes})
+ return &http.Response{Body: ioutil.NopCloser(bytes.NewReader(b))}, nil
+ },
+}}
+
+func ExampleClient_Run() {
+ code := `
+package main
+
+import "fmt"
+
+func main() {
+ fmt.Println("Hello, 世界!")
+}
+`
+ // You can use `DefaultClient` instead of creating `Client` if you want
+ cli := &Client{HTTPClient: mockRunClient}
+ if err := cli.Run(strings.NewReader(code), os.Stdout, os.Stderr); err != nil {
+ log.Fatal(err)
+ }
+ // Output: Hello, 世界!
+}
+
func TestClient_Compile(t *testing.T) {
wantResp := &Response{
Errors: "",
@@ -108,6 +148,54 @@ func TestClient_Compile(t *testing.T) {
}
}
+var mockCompileClient = &http.Client{Transport: &fakeTransPort{
+ FakeRoundTrip: func(r *http.Request) (*http.Response, error) {
+ mes := []*Event{
+ {Message: "3...\n", Kind: "stdout", Delay: 0},
+ {Message: "2...\n", Kind: "stdout", Delay: 1 * time.Second},
+ {Message: "1...\n", Kind: "stdout", Delay: 1 * time.Second},
+ {Message: "GO!\n", Kind: "stdout", Delay: 1 * time.Second},
+ }
+ b, _ := json.Marshal(&Response{Errors: "", Events: mes})
+ return &http.Response{Body: ioutil.NopCloser(bytes.NewReader(b))}, nil
+ },
+}}
+
+func ExampleClient_Compile() {
+ code := `
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func main() {
+ for i := 3; i > 0; i-- {
+ fmt.Printf("%d...\n", i)
+ time.Sleep(1 * time.Second)
+ }
+ fmt.Println("GO!")
+}
+`
+ // You can use `DefaultClient` instead of creating `Client` if you want
+ cli := &Client{HTTPClient: mockCompileClient}
+ resp, err := cli.Compile(strings.NewReader(code))
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, e := range resp.Events {
+ // do not emulate delay.
+ // time.Sleep(e.Delay)
+ fmt.Print(e.Message)
+ }
+ // Output:
+ // 3...
+ // 2...
+ // 1...
+ // GO!
+}
+
func TestClient_Share(t *testing.T) {
mu := http.NewServeMux()
mu.HandleFunc("/share", func(w http.ResponseWriter, r *http.Request) {
@@ -131,3 +219,29 @@ func TestClient_Share(t *testing.T) {
t.Errorf("Share(code) == %v, want %v", link, want)
}
}
+
+var mockShareClient = &http.Client{Transport: &fakeTransPort{
+ FakeRoundTrip: func(r *http.Request) (*http.Response, error) {
+ return &http.Response{Body: ioutil.NopCloser(strings.NewReader("OclbDkg7kv"))}, nil
+ },
+}}
+
+func ExampleClient_Share() {
+ const code = `
+package main
+
+import "fmt"
+
+func main() {
+ fmt.Println("Hello, 世界!")
+}
+`
+ // You can use `DefaultClient` instead of creating `Client` if you want
+ cli := &Client{HTTPClient: mockShareClient}
+ url, err := cli.Share(strings.NewReader(code))
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(url)
+ // Output: https://play.golang.org/p/OclbDkg7kv
+}

0 comments on commit b31db47

Please sign in to comment.