Skip to content

Commit f9f3d79

Browse files
authored
Merge pull request #29 from beme/sjkaliski-improve-godoc
improve go documentation
2 parents 86adc0a + 4c02c48 commit f9f3d79

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

assert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func AssertHTTPResponse(t *testing.T, id string, w *http.Response) {
6868
createOrUpdateSnapshot(t, id, data)
6969
}
7070

71+
// AssertReader asserts the value of an io.Reader.
7172
func AssertReader(t *testing.T, id string, r io.Reader) {
7273
data, err := ioutil.ReadAll(r)
7374
if err != nil {

doc.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
11
// Package abide is a testing utility for http response snapshots inspired
22
// by Facebook's Jest.
3+
//
4+
// It is designed to be used alongside Go's existing testing package
5+
// and enable broader coverage of http APIs. When included in version control
6+
// it can provide a historical log of API and application changes over time.
7+
//
8+
// Snapshot
9+
//
10+
// A snapshot is essentially a lockfile representing an http response.
11+
// /* snapshot: api endpoint */
12+
// HTTP/1.1 200 OK
13+
// Connection: close
14+
// Content-Type: application/json
15+
//
16+
// {
17+
// "foo": "bar"
18+
// }
19+
//
20+
// In addition to testing `http.Response`, abide provides methods for testing
21+
// `io.Reader` and any object that implements `Assertable`.
22+
//
23+
// Snapshots are saved in a directory named __snapshots__ at the root of the package.
24+
// These files are intended to be saved and included in version control.
25+
//
26+
// Creating a Snapshot
27+
//
28+
// Snapshots are automatically generated during the initial test run. For example
29+
// this will create a snapshot identified by "example" for this http.Response.
30+
// func TestFunction(t *testing.T) {
31+
// req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
32+
// w := httptest.NewRecorder()
33+
// handler(w, req)
34+
// res := w.Result()
35+
// abide.AssertHTTPResponse(t, "example", res)
36+
// }
37+
//
38+
// Comparing and Updating
39+
//
40+
// In subsequent test runs the existing snapshot is compared to the new results.
41+
// In the event they do not match, the test will fail, and the diff will be printed.
42+
// If the change was intentional, the snapshot can be updated.
43+
// $ go test -- -u
344
package abide

example_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package abide_test
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"os"
7+
"testing"
8+
9+
"github.com/beme/abide"
10+
)
11+
12+
var (
13+
handler = func(*httptest.ResponseRecorder, *http.Request) {}
14+
t = &testing.T{}
15+
)
16+
17+
func ExampleAssertHTTPResponse() {
18+
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
19+
w := httptest.NewRecorder()
20+
handler(w, req)
21+
res := w.Result()
22+
abide.AssertHTTPResponse(t, "http response", res)
23+
}
24+
25+
func ExampleAssertReader() {
26+
file, _ := os.Open("/path/to/file")
27+
abide.AssertReader(t, "io reader", file)
28+
}

0 commit comments

Comments
 (0)