capturer

package module
v0.0.0-...-e492ea4 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2017 License: MIT Imports: 3 Imported by: 0

README

go-capturer

GoDoc Build Status Go Report Card

Capture os.Stdout and/or os.Stderr in Go. This package is useful for writing tests which print some outputs using fmt package.

Usage

This package provides CaptureStdout(), CaptureStderr() and CaptureOutput() functions to capture outputs.

package main

import (
	"fmt"
	"os"

	"github.com/kami-zh/go-capturer"
)

func ExampleCaptureStdout() {
	out := capturer.CaptureStdout(func() {
		fmt.Fprint(os.Stdout, "foo")
	})

	fmt.Println(out)
	// Output: foo
}

func ExampleCaptureStderr() {
	out := capturer.CaptureStderr(func() {
		fmt.Fprint(os.Stderr, "bar")
	})

	fmt.Println(out)
	// Output: bar
}

func ExampleCaptureOutput() {
	out := capturer.CaptureOutput(func() {
		fmt.Fprint(os.Stdout, "foo")
		fmt.Fprint(os.Stderr, "bar")
	})

	fmt.Println(out)
	// Output: foobar
}

Installation

$ go get github.com/kami-zh/go-capturer

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kami-zh/go-capturer.

License

The package is available as open source under the terms of the MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureOutput

func CaptureOutput(f func()) string

CaptureOutput captures stdout and stderr.

Example
out := CaptureOutput(func() {
	fmt.Fprint(os.Stdout, "foo")
	fmt.Fprint(os.Stderr, "bar")
})

fmt.Println(out)
Output:

foobar

func CaptureStderr

func CaptureStderr(f func()) string

CaptureStderr captures stderr.

Example
out := CaptureStderr(func() {
	fmt.Fprint(os.Stderr, "bar")
})

fmt.Println(out)
Output:

bar

func CaptureStdout

func CaptureStdout(f func()) string

CaptureStdout captures stdout.

Example
out := CaptureStdout(func() {
	fmt.Fprint(os.Stdout, "foo")
})

fmt.Println(out)
Output:

foo

Types

type Capturer

type Capturer struct {
	// contains filtered or unexported fields
}

Capturer has flags whether capture stdout/stderr or not.

Jump to

Keyboard shortcuts

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