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" 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) + } +}