testutil

package module
v0.0.0-...-743c8ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2014 License: MIT Imports: 9 Imported by: 0

README

GOLANG project template

This is my personal GOLANG project template. It is built with these goals in mind:

  • Everything should be local to the development folder as much as is possible without resorting to ugly hacks.

  • 1-step build or test without any special setup after any git clone on a newly minted machine except for installing the go compiler itself.

  • Do not rely on global GOPATH and yet still allows you to check your entire source folder in as if you would a normal go program inside one. This makes your repository plays well with other go coders who will surely be using the global GOPATH convention.

Getting Started

Run this one-liner to dump the repository content into current directory.

curl -L https://github.com/chakrit/go-scratch/archive/master.tar.gz | tar -xzv --strip 1

Do not forget to update the LICENSE file to match what you need. Or simply remove it.

Example for a full setup for a new project:

mkdir your-new-shiny-project    # Make a new shiny folder for your new project.
cd your-new-shiny-project       # Get into the folder first, of course.
git init                        # Or not, doesn't matter.

# Download the scratch template
curl -L https://github.com/chakrit/go-scratch/archive/master.tar.gz | tar -xzv --strip 1

git add .
git commit -m "Initialize GOLANG project. (github.com/chakrit/go-scratch)"

Makefile

Everything is done through the Makefile for convenience. A wrapper script ./go is also provided that invokes go with GOPATH sets to the local .go folder.

Makefile tasks defaults to test. The all task is simply an alias for the build task. All common tasks you'd do with go is given the same name in the Makefile so go vet is, through the Makefile, make vet or via the wrapper script ./go vet.

Dependencies

Dependencies are handled implicitly and automatically as you run tasks that involve import paths thanks to the powerful go get command.

Specifically, make deps and make test-deps will download the dependencies into place and subsequent make test or make build will automaticaly compiles the downloaded dependencies as needed.

The initial main.go file provided with this project contains some dependencies as well as tests (and test dependencies) to demonstrate this.

Subpackages

This template supports multiple packages development inside a single folder out of the box. You are, however, required to list all the subpackage paths inside the Makefile PKG variable as automatically detecting them is tricky and error prone.

For example, if you have a context subpackage, edit the PKG line like so:

PKG := . ./context

... or if you have nothing in the root folder and only subpackages uno dos and tres:

PKG := ./uno ./dos ./tres

Example

Here's a sample output of what happens when you simply cloned this repository and issue make:

$ make
/Users/chakrit/Documents/go-scratch/go get -d -t .
/Users/chakrit/Documents/go-scratch/go test .
ok    _/Users/chakrit/Documents/go-scratch0.010s

Silent run

Uses make's -s flag to prevent make from echoing commands (useful for reducing clutter to standard output.)

$ make -s
ok      _/Users/chakrit/Documents/go-scratch    0.011s

LICENSE

WTFPL

TODO:

  • Package the curl installation into a script.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTestContext

func NewTestContext() *z.Context

NewTestContext() creates and returns a new test context by using the response and request pair from NewTestRequestPair()

func NewTestRequestPair

func NewTestRequestPair() (http.ResponseWriter, *http.Request)

NewTestRequestPair() creates a new test request/response pair for testing against a Context or any roundtripping code. The request is a simple GET / request and the response is an instance of httptest.ResponseRecorder.

Types

type ResponseExpectable

type ResponseExpectable struct {
	T        *testing.T
	Response *http.Response
	Error    error
}

ResponseExpectable saves current calling context when chaining exepctation methods. Do not use directly. Use one of the Http* method to obtain this and then use Expect method to setup test expectations.

func HttpGet

func HttpGet(t *testing.T, url string) *ResponseExpectable

HttpGet() starts a new HTTP GET request and returns an object for setting up expectation for the result.

func HttpPost

func HttpPost(t *testing.T, url string, data url.Values) *ResponseExpectable

HttpPost() starts a new HTTP POST request with the given data payload and returns an object for setting up expectation for the result.

func (*ResponseExpectable) Expect

func (r *ResponseExpectable) Expect(code int, body string)

Expect() reads the response body and tests if the response status code and body content matches the supplied values.

func (*ResponseExpectable) ExpectPattern

func (r *ResponseExpectable) ExpectPattern(code int, pattern string)

ExpectPattern() is similar to Expect() but the response body is matched against the given regular expression pattern instead.

type TestFilter

type TestFilter struct {
	z.Filter
	Called     bool
	CallTime   time.Time
	Finished   bool
	FinishTime time.Time
}

func NewTestFilter

func NewTestFilter() *TestFilter

func (*TestFilter) Reset

func (tf *TestFilter) Reset()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL