tdata

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2018 License: MIT Imports: 11 Imported by: 0

README

tdata - testdata load utility

GoDoc CircleCI Go Report Card

tdata helps to access files in testdata/ directory for test.

Getting Started

This example load a JSON file testdata/withjson.json. The name of the file is determined from t.Name() automatically.

import (
	"testing"
	"github.com/koron-go/tdata"
)

type data struct {
	Foo string `json:"foo"`
	Bar int    `json:"bar"`
}

func TestWithJSON(t *testing.T) {
	var v []data
	tdata.New(t).JSON().Decode(&v)
	// use v as test data: load from "testdata/withjson.json"
}

Contents example of testdata/withjson.json:

[
  { "foo": "abc", "bar": 123 },
  { "foo": "xyz", "bar": 999 }
]
To load a YAML

When you want to decode YAML, replace Decode() line like this:

	tdata.New(t).YAML().Decode(&v)

This loads a file testdata/withjson.yaml. Thus, extension of filename is determined by intermediate of chain.

Convert YAML to JSON

This is useful when destination structure doesn't have any yaml tags, but you want to write data in YAML format.

	tdata.New(t).YAML().JSON().Decode(&v)

This loads a file testdata/withjson.json.yaml.

Templating
	tdata.New(t).Template(g).YAML().JSON().Decode(&v)

This loads a file testdata/withjson.json.yaml.tmpl.

See text/template for syntax of template.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONDecoder

type JSONDecoder interface {
	Decode(v interface{})
}

JSONDecoder decodes a testdata/file as JSON.

type Loader

type Loader interface {
	Template(d interface{}) TemplateLoader
	YAML() YAMLDecoder
	JSON() JSONDecoder
}

Loader is a testdata/file loader.

func New

func New(tb testing.TB) Loader

New creates a testdata loader.

func NewName

func NewName(name string) Loader

NewName creates a file loader.

type TemplateLoader

type TemplateLoader interface {
	YAML() YAMLDecoder
	JSON() JSONDecoder
}

TemplateLoader is a testdata/file loader with template.

type YAMLDecoder

type YAMLDecoder interface {
	Decode(v interface{})
	JSON() JSONDecoder
}

YAMLDecoder decodes a testdata/file as YAML.

Directories

Path Synopsis
internal
yaml
Package yaml implements YAML support for the Go language.
Package yaml implements YAML support for the Go language.

Jump to

Keyboard shortcuts

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