File: source_test.go

package info (click to toggle)
gotest.tools 3.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates, bullseye, sid
  • size: 600 kB
  • sloc: makefile: 11; sh: 6
file content (94 lines) | stat: -rw-r--r-- 1,868 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package source_test

// using a separate package for test to avoid circular imports with the assert
// package

import (
	"fmt"
	"runtime"
	"strings"
	"testing"

	"gotest.tools/v3/assert"
	"gotest.tools/v3/internal/source"
	"gotest.tools/v3/skip"
)

func TestFormattedCallExprArg_SingleLine(t *testing.T) {
	msg, err := shim("not", "this", "this text")
	assert.NilError(t, err)
	assert.Equal(t, `"this text"`, msg)
}

func TestFormattedCallExprArg_MultiLine(t *testing.T) {
	msg, err := shim(
		"first",
		"second",
		"this text",
	)
	assert.NilError(t, err)
	assert.Equal(t, `"this text"`, msg)
}

func TestFormattedCallExprArg_IfStatement(t *testing.T) {
	if msg, err := shim(
		"first",
		"second",
		"this text",
	); true {
		assert.NilError(t, err)
		assert.Equal(t, `"this text"`, msg)
	}
}

func shim(_, _, _ string) (string, error) {
	return source.FormattedCallExprArg(1, 2)
}

func TestFormattedCallExprArg_InDefer(t *testing.T) {
	skip.If(t, isGoVersion18)
	cap := &capture{}
	func() {
		defer cap.shim("first", "second")
	}()

	assert.NilError(t, cap.err)
	assert.Equal(t, cap.value, `"second"`)
}

func isGoVersion18() bool {
	return strings.HasPrefix(runtime.Version(), "go1.8.")
}

type capture struct {
	value string
	err   error
}

func (c *capture) shim(_, _ string) {
	c.value, c.err = source.FormattedCallExprArg(1, 1)
}

func TestFormattedCallExprArg_InAnonymousDefer(t *testing.T) {
	cap := &capture{}
	func() {
		fmt.Println()
		defer fmt.Println()
		defer func() { cap.shim("first", "second") }()
	}()

	assert.NilError(t, cap.err)
	assert.Equal(t, cap.value, `"second"`)
}

func TestFormattedCallExprArg_InDeferMultipleDefers(t *testing.T) {
	skip.If(t, isGoVersion18)
	cap := &capture{}
	func() {
		fmt.Println()
		defer fmt.Println()
		defer cap.shim("first", "second")
	}()

	assert.ErrorContains(t, cap.err, "ambiguous call expression")
}