dataloc

package
v0.0.0-...-f0ac19d Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 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.

func L3

func L3(name string) string

func L4

func L4(name string) string

func L5

func L5(name string) string

func L6

func L6(name string) string

Types

This section is empty.

Jump to

Keyboard shortcuts

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