This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
forked from golang/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
playground: support multiple input files in txtar format
Updates golang/go#32040 Updates golang/go#31944 (Notably, you can now include a go.mod file) Change-Id: I56846e86d3d98fdf4cac388b5b284dbc187e3b36 Reviewed-on: https://go-review.googlesource.com/c/playground/+/177043 Reviewed-by: Dmitri Shuralyov <[email protected]>
- Loading branch information
Showing
11 changed files
with
540 additions
and
47 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
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
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,84 @@ | ||
// Copyright 2019 The Go 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 ( | ||
"encoding/json" | ||
"net/http/httptest" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestHandleFmt(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
body string | ||
imports bool | ||
want string | ||
wantErr string | ||
}{ | ||
{ | ||
name: "classic", | ||
body: " package main\n func main( ) { }\n", | ||
want: "package main\n\nfunc main() {}\n", | ||
}, | ||
{ | ||
name: "classic_goimports", | ||
body: " package main\nvar _ = fmt.Printf", | ||
imports: true, | ||
want: "package main\n\nimport \"fmt\"\n\nvar _ = fmt.Printf\n", | ||
}, | ||
{ | ||
name: "single_go_with_header", | ||
body: "-- prog.go --\n package main", | ||
want: "-- prog.go --\npackage main\n", | ||
}, | ||
{ | ||
name: "multi_go_with_header", | ||
body: "-- prog.go --\n package main\n\n\n-- two.go --\n package main\n var X = 5", | ||
want: "-- prog.go --\npackage main\n-- two.go --\npackage main\n\nvar X = 5\n", | ||
}, | ||
{ | ||
name: "multi_go_without_header", | ||
body: " package main\n\n\n-- two.go --\n package main\n var X = 5", | ||
want: "package main\n-- two.go --\npackage main\n\nvar X = 5\n", | ||
}, | ||
{ | ||
name: "only_format_go", | ||
body: " package main\n\n\n-- go.mod --\n module foo\n", | ||
want: "package main\n-- go.mod --\n module foo\n", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
form := url.Values{} | ||
form.Set("body", tt.body) | ||
if tt.imports { | ||
form.Set("imports", "true") | ||
} | ||
req := httptest.NewRequest("POST", "/fmt", strings.NewReader(form.Encode())) | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
handleFmt(rec, req) | ||
resp := rec.Result() | ||
if resp.StatusCode != 200 { | ||
t.Fatalf("code = %v", resp.Status) | ||
} | ||
if ct := resp.Header.Get("Content-Type"); ct != "application/json" { | ||
t.Fatalf("Content-Type = %q; want application/json", ct) | ||
} | ||
var got fmtResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&got); err != nil { | ||
t.Fatal(err) | ||
} | ||
if got.Body != tt.want { | ||
t.Errorf("wrong output\n got: %q\nwant: %q\n", got.Body, tt.want) | ||
} | ||
if got.Error != tt.wantErr { | ||
t.Errorf("wrong error\n got err: %q\nwant err: %q\n", got.Error, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.