...
1 package source_test
2
3
4
5
6 import (
7 "fmt"
8 "runtime"
9 "strings"
10 "testing"
11
12 "gotest.tools/v3/assert"
13 "gotest.tools/v3/internal/source"
14 "gotest.tools/v3/skip"
15 )
16
17 func TestFormattedCallExprArg_SingleLine(t *testing.T) {
18 msg, err := shim("not", "this", "this text")
19 assert.NilError(t, err)
20 assert.Equal(t, `"this text"`, msg)
21 }
22
23 func TestFormattedCallExprArg_MultiLine(t *testing.T) {
24 msg, err := shim(
25 "first",
26 "second",
27 "this text",
28 )
29 assert.NilError(t, err)
30 assert.Equal(t, `"this text"`, msg)
31 }
32
33 func TestFormattedCallExprArg_IfStatement(t *testing.T) {
34 if msg, err := shim(
35 "first",
36 "second",
37 "this text",
38 ); true {
39 assert.NilError(t, err)
40 assert.Equal(t, `"this text"`, msg)
41 }
42 }
43
44 func shim(_, _, _ string) (string, error) {
45 return source.FormattedCallExprArg(1, 2)
46 }
47
48 func TestFormattedCallExprArg_InDefer(t *testing.T) {
49 skip.If(t, isGoVersion18)
50 c := &capture{}
51 func() {
52 defer c.shim("first", "second")
53 }()
54
55 assert.NilError(t, c.err)
56 assert.Equal(t, c.value, `"second"`)
57 }
58
59 func isGoVersion18() bool {
60 return strings.HasPrefix(runtime.Version(), "go1.8.")
61 }
62
63 type capture struct {
64 value string
65 err error
66 }
67
68 func (c *capture) shim(_, _ string) {
69 c.value, c.err = source.FormattedCallExprArg(1, 1)
70 }
71
72 func TestFormattedCallExprArg_InAnonymousDefer(t *testing.T) {
73 c := &capture{}
74 func() {
75 fmt.Println()
76 defer fmt.Println()
77 defer func() { c.shim("first", "second") }()
78 }()
79
80 assert.NilError(t, c.err)
81 assert.Equal(t, c.value, `"second"`)
82 }
83
84 func TestFormattedCallExprArg_InDeferMultipleDefers(t *testing.T) {
85 skip.If(t, isGoVersion18)
86 c := &capture{}
87 func() {
88 fmt.Println()
89 defer fmt.Println()
90 defer c.shim("first", "second")
91 }()
92
93 assert.ErrorContains(t, c.err, "ambiguous call expression")
94 }
95
View as plain text