dataloc

package
v0.0.0-...-af6add1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 9 Imported by: 0

README

dataloc

PkgGoDev

Package dataloc provides functionality to find the source code location of table-driven test cases.

Example

import (
	"fmt"

	"github.com/motemen/go-testutil/dataloc"
)

func Example() {
	testcases := []struct {
		name string
		a, b int
		sum  int
	}{
		{
			name: "100+200",
			a:    100,
			b:    200,
			sum:  -1,
		},
		{
			name: "1+1",
			a:    1,
			b:    1,
			sum:  99,
		},
	}

	for _, testcase := range testcases {
		if expected, got := testcase.sum, testcase.a+testcase.b; got != expected {
			fmt.Printf("expected %d but got %d, test case at %s\n", expected, got, dataloc.L(testcase.name))
		}
	}

	// Output:
	// expected -1 but got 300, test case at example_test.go:15
	// expected 99 but got 2, test case at example_test.go:21
}

Documentation

Overview

Package dataloc provides functionality to find the source code location of table-driven test cases.

Example
package main

import (
	"fmt"

	"github.com/motemen/go-testutil/dataloc"
)

func main() {
	testcases := []struct {
		name string
		a, b int
		sum  int
	}{
		{
			name: "100+200",
			a:    100,
			b:    200,
			sum:  -1,
		},
		{
			name: "1+1",
			a:    1,
			b:    1,
			sum:  99,
		},
	}

	for _, testcase := range testcases {
		if expected, got := testcase.sum, testcase.a+testcase.b; got != expected {
			fmt.Printf("expected %d but got %d, test case at %s\n", expected, got, dataloc.L(testcase.name))
		}
	}

}
Output:

expected -1 but got 300, test case at example_test.go:15
expected 99 but got 2, test case at example_test.go:21

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func L

func L(name string) string

L returns the source code location of the test case identified by its name. It attempts runtime source code analysis to find the location by using the expression passed to dataloc.L(). So some restrictions apply:

  • The function must be invoked as "dataloc.L".
  • The argument must be an expression of the form "dataloc.L(testcase.key)" , where "testcase" is a variable declared as "for _, testcase := range testcases" , and "testcases" is a slice of a struct type , whose "key" field is a string which is passsed to L().
  • or "dataloc.L(key)" , where key is a variable declared as "for key, value := range testcases" , and "testcases" is a map of string to any type , and "key" is the string which is passed to L().

See Example.

Types

This section is empty.

Jump to

Keyboard shortcuts

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