From e775881f2e9ca75ccc8ef1374ca966de5243a5d2 Mon Sep 17 00:00:00 2001 From: Paulo Jr Date: Wed, 2 Feb 2022 18:22:02 +0000 Subject: [PATCH 1/2] Add tests, update readme --- README.md | 5 ++--- hello/hello_test.go | 21 +++++++++++++++++++++ server_test.go | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 hello/hello_test.go create mode 100644 server_test.go diff --git a/README.md b/README.md index cabbf1d..4ee5f01 100644 --- a/README.md +++ b/README.md @@ -71,9 +71,8 @@ Some things to try: 6. **Refactoring - extract:** - Open `hello.go` and select string, press F1 and run the **Go: Extract to variable** command. - Open `hello.go` and select line with return statement, press F1 and run the **Go: Extract to function** command. -7. **Generate tests:** - - Open `hello.go` and press F1 and run the **Go: Generate Unit Tests For File** command. - - Implement a test case: Open file `hello_test.go` and edit the line with the `TODO` comment: `{"hello without name", "Hello, "},` +7. **Execute tests:** + - Open `hello_test.go` and press F1 and run the **Go: Test file** command. - You can toggle between implementation file and test file with press F1 and run the **Go: Toggle Test File** - Tests can also run as benchmarks: Open file `hello_test.go`, press F1 and run the **Go: Benchmark File** 8. **Stub generation:** ( [details](https://github.com/josharian/impl)) diff --git a/hello/hello_test.go b/hello/hello_test.go new file mode 100644 index 0000000..c82384c --- /dev/null +++ b/hello/hello_test.go @@ -0,0 +1,21 @@ +package hello + +import ( + "fmt" + "testing" +) + +func TestHelloWithoutName(t *testing.T) { + want := "Hello, " + if got := Hello(); want != got { + t.Errorf("Hello() = %q, want = %q", got, want) + } +} + +func TestHelloWithName(t *testing.T) { + alex.Name = "Alex" + want := fmt.Sprintf("Hello, %s", alex.Name) + if got := Hello(); want != got { + t.Errorf("Hello() = %q, want = %q", got, want) + } +} diff --git a/server_test.go b/server_test.go new file mode 100644 index 0000000..9b93f1e --- /dev/null +++ b/server_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestHandle(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, "/", nil) + if err != nil { + t.Fatal("Failed to create 'GET /' request") + } + + res := httptest.NewRecorder() + + want := "Hello, " + + handle(res, req) + if got := res.Body.String(); want != got { + t.Errorf("Handle() = %q, want = %q", got, want) + } +} From 9916706a2e2bc52bcee4dfba4d9c4b840075e12c Mon Sep 17 00:00:00 2001 From: Paulo Jr Date: Thu, 3 Feb 2022 00:08:49 +0000 Subject: [PATCH 2/2] Add post create command to install gotests package --- .devcontainer/devcontainer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 7431681..4dbf805 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -45,7 +45,7 @@ // }, // Use 'postCreateCommand' to run commands after the container is created. - // "postCreateCommand": "go version", + "postCreateCommand": "go install -v github.com/cweill/gotests/gotests@latest", // Uncomment to connect as a non-root user. More info: https://aka.ms/vscode-remote/containers/non-root. "remoteUser": "vscode"