differs

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package differs contains custom diffing test utilities

See the example for how to use them

Example
package main

import (
	"fmt"
	"time"

	"github.com/getoutreach/gobox/pkg/differs"
	_ "github.com/getoutreach/gobox/pkg/log"
	"github.com/google/go-cmp/cmp"
)

func main() {
	// RFC3339Time
	actual := map[string]interface{}{
		"rfc3339":       time.Now().Format(time.RFC3339),
		"any string":    "some string",
		"capture":       "captured value",
		"check capture": "captured value",
		"stack":         "some\nlong\ntack\ntrace",
		"float":         4.5,
	}
	capture := differs.CaptureString()
	expected := map[string]interface{}{
		"rfc3339":       differs.RFC3339Time(),
		"any string":    differs.AnyString(),
		"capture":       capture,
		"check capture": capture,
		"stack":         differs.StackLike("some\nlong\nstack"),
		"float":         differs.FloatRange(4, 5),
	}
	diff := cmp.Diff(expected, actual, differs.Custom())
	fmt.Println(diff)

	// RFC3339NanoTime
	actual = map[string]interface{}{
		"rfc3339nano":   time.Now().Format(time.RFC3339Nano),
		"any string":    "some string",
		"capture":       "captured value",
		"check capture": "captured value",
		"stack":         "some\nlong\ntack\ntrace",
		"float":         4.5,
	}
	capture = differs.CaptureString()
	expected = map[string]interface{}{
		"rfc3339nano":   differs.RFC3339NanoTime(),
		"any string":    differs.AnyString(),
		"capture":       capture,
		"check capture": capture,
		"stack":         differs.StackLike("some\nlong\nstack"),
		"float":         differs.FloatRange(4, 5),
	}
	diff = cmp.Diff(expected, actual, differs.Custom())
	fmt.Println(diff)

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Custom

func Custom() cmp.Option

func StackTrace

func StackTrace(want, got string) string

StackTrace returns the diff between two stack traces by comparing loosely

In particular, the expected stacktrace can be shorter as well only have a substring. This allows removing line numbers and memory addresses in the trace.

Types

type CustomComparer

type CustomComparer interface {
	CompareCustom(o interface{}) bool
}

CustomComparer is the type returned by custom comparisons

func AnyMap

func AnyMap() CustomComparer

AnyMap allows any map to be matched against it when differs.Custom is passed to cmp

func AnyString

func AnyString() CustomComparer

AnyString allows any string to be matched against it when differs.Custom is passed to cmp

func CaptureString

func CaptureString() CustomComparer

CaptureString matches any string the first time it is used but on the second attempt, the string has to match exactly the same as the first one.

func Contains

func Contains(ss string) CustomComparer

ContainsString matches any string where the provided string is a substring of the string it is being matched against

func FloatRange

func FloatRange(start, end float64) CustomComparer

FloatRange allows a float value between the start and end when differs.Custom is passed to cmp

func RFC3339NanoTime

func RFC3339NanoTime() CustomComparer

func RFC3339Time

func RFC3339Time() CustomComparer

RFC3339Time allows RFC3339Time string to be matched against it when differs.Custom is passed to cmp

func StackLike

func StackLike(want string) CustomComparer

StackLike allows a stacktrace to be matched against it when differs.Custom is passed to cmp

The StackTrace() function is used for the actual matching

type Customf

type Customf func(o interface{}) bool

Customf converts a function into a custom comparer

func (Customf) CompareCustom

func (c Customf) CompareCustom(o interface{}) bool

Jump to

Keyboard shortcuts

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