difftest

package
v0.0.0-...-f32120d Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package difftest supplies a set of tests that will operate on any implementation of a diff algorithm as exposed by "golang.org/x/tools/internal/diff"

Package testenv contains helper functions for skipping tests based on which tools are present in the environment.

Index

Constants

View Source
const (
	FileA         = "from"
	FileB         = "to"
	UnifiedPrefix = "--- " + FileA + "\n+++ " + FileB + "\n"
)

Variables

View Source
var TestCases = []struct {
	Name, In, Out, Unified string
	Edits, LineEdits       []diff.Edit
	NoDiff                 bool
}{{
	Name: "empty",
	In:   "",
	Out:  "",
}, {
	Name: "no_diff",
	In:   "gargantuan\n",
	Out:  "gargantuan\n",
}, {
	Name: "replace_all",
	In:   "fruit\n",
	Out:  "cheese\n",
	Unified: UnifiedPrefix + `
@@ -1 +1 @@
-fruit
+cheese
`[1:],
	Edits:     []diff.Edit{{Start: 0, End: 5, New: "cheese"}},
	LineEdits: []diff.Edit{{Start: 0, End: 6, New: "cheese\n"}},
}, {
	Name: "insert_rune",
	In:   "gord\n",
	Out:  "gourd\n",
	Unified: UnifiedPrefix + `
@@ -1 +1 @@
-gord
+gourd
`[1:],
	Edits:     []diff.Edit{{Start: 2, End: 2, New: "u"}},
	LineEdits: []diff.Edit{{Start: 0, End: 5, New: "gourd\n"}},
}, {
	Name: "delete_rune",
	In:   "groat\n",
	Out:  "goat\n",
	Unified: UnifiedPrefix + `
@@ -1 +1 @@
-groat
+goat
`[1:],
	Edits:     []diff.Edit{{Start: 1, End: 2, New: ""}},
	LineEdits: []diff.Edit{{Start: 0, End: 6, New: "goat\n"}},
}, {
	Name: "replace_rune",
	In:   "loud\n",
	Out:  "lord\n",
	Unified: UnifiedPrefix + `
@@ -1 +1 @@
-loud
+lord
`[1:],
	Edits:     []diff.Edit{{Start: 2, End: 3, New: "r"}},
	LineEdits: []diff.Edit{{Start: 0, End: 5, New: "lord\n"}},
}, {
	Name: "replace_partials",
	In:   "blanket\n",
	Out:  "bunker\n",
	Unified: UnifiedPrefix + `
@@ -1 +1 @@
-blanket
+bunker
`[1:],
	Edits: []diff.Edit{
		{Start: 1, End: 3, New: "u"},
		{Start: 6, End: 7, New: "r"},
	},
	LineEdits: []diff.Edit{{Start: 0, End: 8, New: "bunker\n"}},
}, {
	Name: "insert_line",
	In:   "1: one\n3: three\n",
	Out:  "1: one\n2: two\n3: three\n",
	Unified: UnifiedPrefix + `
@@ -1,2 +1,3 @@
 1: one
+2: two
 3: three
`[1:],
	Edits: []diff.Edit{{Start: 7, End: 7, New: "2: two\n"}},
}, {
	Name: "replace_no_newline",
	In:   "A",
	Out:  "B",
	Unified: UnifiedPrefix + `
@@ -1 +1 @@
-A
\ No newline at end of file
+B
\ No newline at end of file
`[1:],
	Edits: []diff.Edit{{Start: 0, End: 1, New: "B"}},
}, {
	Name: "append_empty",
	In:   "",
	Out:  "AB\nC",
	Unified: UnifiedPrefix + `
@@ -0,0 +1,2 @@
+AB
+C
\ No newline at end of file
`[1:],
	Edits:     []diff.Edit{{Start: 0, End: 0, New: "AB\nC"}},
	LineEdits: []diff.Edit{{Start: 0, End: 0, New: "AB\nC"}},
},

	{
		Name: "add_end",
		In:   "A",
		Out:  "AB",
		Unified: UnifiedPrefix + `
@@ -1 +1 @@
-A
\ No newline at end of file
+AB
\ No newline at end of file
`[1:],
		Edits:     []diff.Edit{{Start: 1, End: 1, New: "B"}},
		LineEdits: []diff.Edit{{Start: 0, End: 1, New: "AB"}},
	}, {
		Name: "add_empty",
		In:   "",
		Out:  "AB\nC",
		Unified: UnifiedPrefix + `
@@ -0,0 +1,2 @@
+AB
+C
\ No newline at end of file
`[1:],
		Edits:     []diff.Edit{{Start: 0, End: 0, New: "AB\nC"}},
		LineEdits: []diff.Edit{{Start: 0, End: 0, New: "AB\nC"}},
	}, {
		Name: "add_newline",
		In:   "A",
		Out:  "A\n",
		Unified: UnifiedPrefix + `
@@ -1 +1 @@
-A
\ No newline at end of file
+A
`[1:],
		Edits:     []diff.Edit{{Start: 1, End: 1, New: "\n"}},
		LineEdits: []diff.Edit{{Start: 0, End: 1, New: "A\n"}},
	}, {
		Name: "delete_front",
		In:   "A\nB\nC\nA\nB\nB\nA\n",
		Out:  "C\nB\nA\nB\nA\nC\n",
		Unified: UnifiedPrefix + `
@@ -1,7 +1,6 @@
-A
-B
 C
+B
 A
 B
-B
 A
+C
`[1:],
		NoDiff: true,
		Edits: []diff.Edit{
			{Start: 0, End: 4, New: ""},
			{Start: 6, End: 6, New: "B\n"},
			{Start: 10, End: 12, New: ""},
			{Start: 14, End: 14, New: "C\n"},
		},
		LineEdits: []diff.Edit{
			{Start: 0, End: 6, New: "C\n"},
			{Start: 6, End: 8, New: "B\nA\n"},
			{Start: 10, End: 14, New: "A\n"},
			{Start: 14, End: 14, New: "C\n"},
		},
	}, {
		Name: "replace_last_line",
		In:   "A\nB\n",
		Out:  "A\nC\n\n",
		Unified: UnifiedPrefix + `
@@ -1,2 +1,3 @@
 A
-B
+C
+
`[1:],
		Edits:     []diff.Edit{{Start: 2, End: 3, New: "C\n"}},
		LineEdits: []diff.Edit{{Start: 2, End: 4, New: "C\n\n"}},
	},
	{
		Name: "multiple_replace",
		In:   "A\nB\nC\nD\nE\nF\nG\n",
		Out:  "A\nH\nI\nJ\nE\nF\nK\n",
		Unified: UnifiedPrefix + `
@@ -1,7 +1,7 @@
 A
-B
-C
-D
+H
+I
+J
 E
 F
-G
+K
`[1:],
		Edits: []diff.Edit{
			{Start: 2, End: 8, New: "H\nI\nJ\n"},
			{Start: 12, End: 14, New: "K\n"},
		},
		NoDiff: true,
	},
	{
		Name:  "extra_newline",
		In:    "\nA\n",
		Out:   "A\n",
		Edits: []diff.Edit{{Start: 0, End: 1, New: ""}},
		Unified: UnifiedPrefix + `@@ -1,2 +1 @@
-
 A
`,
	},
}

Functions

func DiffTest

func DiffTest(t *testing.T, compute func(before, after string) []diff.Edit)

func ExitIfSmallMachine

func ExitIfSmallMachine()

ExitIfSmallMachine emits a helpful diagnostic and calls os.Exit(0) if the current machine is a builder known to have scarce resources.

It should be called from within a TestMain function.

func Go1Point

func Go1Point() int

Go1Point returns the x in Go 1.x.

func NeedsGo1Point

func NeedsGo1Point(t Testing, x int)

NeedsGo1Point skips t if the Go version used to run the test is older than 1.x.

func NeedsGoBuild

func NeedsGoBuild(t Testing)

NeedsGoBuild skips t if the current system can't build programs with “go build” and then run them with os.StartProcess or exec.Command. android, and darwin/arm systems don't have the userspace go build needs to run, and js/wasm doesn't support running subprocesses.

func NeedsGoPackages

func NeedsGoPackages(t Testing)

NeedsGoPackages skips t if the go/packages driver (or 'go' tool) implied by the current process environment is not present in the path.

func NeedsGoPackagesEnv

func NeedsGoPackagesEnv(t Testing, env []string)

NeedsGoPackagesEnv skips t if the go/packages driver (or 'go' tool) implied by env is not present in the path.

func NeedsTool

func NeedsTool(t Testing, tool string)

NeedsTool skips t if the named tool is not present in the path. As a special case, "cgo" means "go" is present and can compile cgo programs.

func SkipAfterGo1Point

func SkipAfterGo1Point(t Testing, x int)

SkipAfterGo1Point skips t if the Go version used to run the test is newer than 1.x.

Types

type Testing

type Testing interface {
	Skipf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
}

Testing is an abstraction of a *testing.T.

Jump to

Keyboard shortcuts

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