Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests, update readme #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ Some things to try:
6. **Refactoring - extract:**
- Open `hello.go` and select string, press <kbd>F1</kbd> and run the **Go: Extract to variable** command.
- Open `hello.go` and select line with return statement, press <kbd>F1</kbd> and run the **Go: Extract to function** command.
7. **Generate tests:**
- Open `hello.go` and press <kbd>F1</kbd> 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 <kbd>F1</kbd> and run the **Go: Test file** command.
- You can toggle between implementation file and test file with press <kbd>F1</kbd> and run the **Go: Toggle Test File**
- Tests can also run as benchmarks: Open file `hello_test.go`, press <kbd>F1</kbd> and run the **Go: Benchmark File**
8. **Stub generation:** ( [details](https://github.com/josharian/impl))
Expand Down
21 changes: 21 additions & 0 deletions hello/hello_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
23 changes: 23 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}