-
Notifications
You must be signed in to change notification settings - Fork 2
/
funcRegistry_test.go
66 lines (63 loc) · 1.58 KB
/
funcRegistry_test.go
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
package main
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestAnalyzeFunc(t *testing.T) {
t.Run("simple func", func(t *testing.T) {
dir, dispose, err := MustGenMod(`package main
func Run(n int) string {
if n == 0 {
return "0"
} else if n == 1 {
return "1"
} else {
return "2"
}
}
`)
defer dispose()
project, err := LoadPackage(dir)
require.Nil(t, err)
require.Equal(t, FuncProps{DeterministicReturn: false}, analyzeFunc(project[0], MustExtractFunc(project)))
})
t.Run("ignore wrapped calls", func(t *testing.T) {
dir, dispose, err := MustGenMod(`package main
func Run(n int) string {
f := func() string {
return "0"
}
return f()
}
`)
defer dispose()
project, err := LoadPackage(dir)
require.Nil(t, err)
require.Equal(t, FuncProps{DeterministicReturn: false}, analyzeFunc(project[0], MustExtractFunc(project)))
})
t.Run("deterministic return", func(t *testing.T) {
dir, dispose, err := MustGenMod(`package main
func Run(n int) string {
return "1"
}
func main() {}
`)
defer dispose()
project, err := LoadPackage(dir)
require.Nil(t, err)
require.Equal(t, FuncProps{DeterministicReturn: true}, analyzeFunc(project[0], MustExtractFunc(project)))
})
t.Run("deterministic nil return", func(t *testing.T) {
dir, dispose, err := MustGenMod(`package main
func Write(b *strings.Builder, s string) error {
_, _ = b.WriteString(s)
return nil
}
func main() {}
`)
defer dispose()
project, err := LoadPackage(dir)
require.Nil(t, err)
require.Equal(t, FuncProps{DeterministicReturn: true}, analyzeFunc(project[0], MustExtractFunc(project)))
})
}