File: parse_test.go

package info (click to toggle)
mockery 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid
  • size: 1,252 kB
  • sloc: makefile: 35; sh: 29
file content (91 lines) | stat: -rw-r--r-- 2,845 bytes parent folder | download
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
package pkg

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
)

var ctx = context.Background()

func TestFileParse(t *testing.T) {
	parser := NewParser(nil)

	err := parser.Parse(ctx, testFile)
	assert.NoError(t, err)

	err = parser.Load()
	assert.NoError(t, err)

	node, err := parser.Find("Requester")
	assert.NoError(t, err)
	assert.NotNil(t, node)
}

func TestBuildTagInFilename(t *testing.T) {
	parser := NewParser(nil)

	// Include the major OS values found on https://golang.org/dl/ so we're likely to match
	// anywhere the test is executed.
	err := parser.Parse(ctx, getFixturePath("buildtag", "filename", "iface_windows.go"))
	assert.NoError(t, err)
	err = parser.Parse(ctx, getFixturePath("buildtag", "filename", "iface_linux.go"))
	assert.NoError(t, err)
	err = parser.Parse(ctx, getFixturePath("buildtag", "filename", "iface_darwin.go"))
	assert.NoError(t, err)
	err = parser.Parse(ctx, getFixturePath("buildtag", "filename", "iface_freebsd.go"))
	assert.NoError(t, err)

	err = parser.Load()
	assert.NoError(t, err) // Expect "redeclared in this block" if tags aren't respected

	nodes := parser.Interfaces()
	assert.Equal(t, 1, len(nodes))
	assert.Equal(t, "IfaceWithBuildTagInFilename", nodes[0].Name)
}

func TestBuildTagInComment(t *testing.T) {
	parser := NewParser(nil)

	// Include the major OS values found on https://golang.org/dl/ so we're likely to match
	// anywhere the test is executed.
	err := parser.Parse(ctx, getFixturePath("buildtag", "comment", "windows_iface.go"))
	assert.NoError(t, err)
	err = parser.Parse(ctx, getFixturePath("buildtag", "comment", "linux_iface.go"))
	assert.NoError(t, err)
	err = parser.Parse(ctx, getFixturePath("buildtag", "comment", "darwin_iface.go"))
	assert.NoError(t, err)
	err = parser.Parse(ctx, getFixturePath("buildtag", "comment", "freebsd_iface.go"))
	assert.NoError(t, err)

	err = parser.Load()
	assert.NoError(t, err) // Expect "redeclared in this block" if tags aren't respected

	nodes := parser.Interfaces()
	assert.Equal(t, 1, len(nodes))
	assert.Equal(t, "IfaceWithBuildTagInComment", nodes[0].Name)
}

func TestCustomBuildTag(t *testing.T) {
	parser := NewParser([]string{"custom"})

	// Include two files that define the same interface, but with different
	// build tags. Only one should be loaded.
	err := parser.Parse(ctx, getFixturePath("buildtag", "comment", "custom_iface.go"))
	assert.NoError(t, err)
	err = parser.Parse(ctx, getFixturePath("buildtag", "comment", "custom2_iface.go"))
	assert.NoError(t, err)

	err = parser.Load()
	assert.NoError(t, err) // Expect "redeclared in this block" if tags aren't respected

	found := false
	for _, node := range parser.Interfaces() {
		if node.Name == "IfaceWithCustomBuildTagInComment" {
			found = true
			break
		}
	}
	assert.True(t, found, "IfaceWithCustomBuildTagInComment not parsed")
}