Debian Codegraph @

extract/extract_test.go

raw symbols SCIP
1 package extractscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src go1.21 bytes/"
5 "osscip-go gomod github.com/golang/go/src go1.21 os/"
6 "pathscip-go gomod github.com/golang/go/src go1.21 path/"
7 "stringsscip-go gomod github.com/golang/go/src go1.21 strings/"
8 "testingscip-go gomod github.com/golang/go/src go1.21 testing/"
9 )
10
11 var expectedOutputscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/expectedOutput. = `// Code generated by 'yaegi extract guthib.com/baz'. DO NOT EDIT.
12
13 package bar
14
15 import (
16 "guthib.com/baz"
17 "reflect"
18 )
19
20 func init() {
21 Symbols["guthib.com/baz/baz"] = map[string]reflect.Value{
22 // function, constant and variable definitions
23 "Hello": reflect.ValueOf(baz.Hello),
24 }
25 }
26 `
27
28 func TestPackagesscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/TestPackages().(t *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Tscip-go gomod github.com/golang/go/src go1.21 testing/T#) {
29 testCases := []struct {
30 desc string
31 moduleOn string
32 wd string
33 arg string
34 importPath string
35 expected string
36 contains string
37 dest string
38 }{
39 {
40 desc: "stdlib math pkg, using go/importer",
41 dest: "math",
42 arg: "math",
43 // We check this one because it shows both defects when we break it: the value
44 // gets corrupted, and the type becomes token.INT
45 // TODO(mpl): if the ident between key and value becomes annoying, be smarter about it.
46 contains: `"MaxFloat32": reflect.ValueOf(constant.MakeFromLiteral("340282346638528859811704183484516925440", token.FLOAT, 0)),`,
47 },
48 {
49 desc: "using relative path, using go.mod",
50 wd: "./testdata/1/src/guthib.com/bar",
51 arg: "../baz",
52 expected: expectedOutputscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/expectedOutput.,
53 },
54 {
55 desc: "using relative path, manual import path",
56 wd: "./testdata/2/src/guthib.com/bar",
57 arg: "../baz",
58 importPath: "guthib.com/baz",
59 expected: expectedOutputscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/expectedOutput.,
60 },
61 {
62 desc: "using relative path, go.mod is ignored, because manual path",
63 wd: "./testdata/3/src/guthib.com/bar",
64 arg: "../baz",
65 importPath: "guthib.com/baz",
66 expected: expectedOutputscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/expectedOutput.,
67 },
68 {
69 desc: "using relative path, dep in vendor, using go.mod",
70 wd: "./testdata/4/src/guthib.com/bar",
71 arg: "./vendor/guthib.com/baz",
72 expected: expectedOutputscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/expectedOutput.,
73 },
74 {
75 desc: "using relative path, dep in vendor, manual import path",
76 wd: "./testdata/5/src/guthib.com/bar",
77 arg: "./vendor/guthib.com/baz",
78 importPath: "guthib.com/baz",
79 expected: expectedOutputscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/expectedOutput.,
80 },
81 {
82 desc: "using relative path, package name is not same as import path",
83 wd: "./testdata/6/src/guthib.com/bar",
84 arg: "../baz-baz",
85 importPath: "guthib.com/baz",
86 expected: expectedOutputscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/expectedOutput.,
87 },
88 {
89 desc: "using relative path, interface method parameter is variadic",
90 wd: "./testdata/7/src/guthib.com/variadic",
91 arg: "../variadic",
92 importPath: "guthib.com/variadic",
93 expected: `
94 // Code generated by 'yaegi extract guthib.com/variadic'. DO NOT EDIT.
95
96 package variadic
97
98 import (
99 "guthib.com/variadic"
100 "reflect"
101 )
102
103 func init() {
104 Symbols["guthib.com/variadic/variadic"] = map[string]reflect.Value{
105 // type definitions
106 "Variadic": reflect.ValueOf((*variadic.Variadic)(nil)),
107
108 // interface wrapper definitions
109 "_Variadic": reflect.ValueOf((*_guthib_com_variadic_Variadic)(nil)),
110 }
111 }
112
113 // _guthib_com_variadic_Variadic is an interface wrapper for Variadic type
114 type _guthib_com_variadic_Variadic struct {
115 IValue interface{}
116 WCall func(method string, args ...[]interface{}) (interface{}, error)
117 }
118
119 func (W _guthib_com_variadic_Variadic) Call(method string, args ...[]interface{}) (interface{}, error) {
120 return W.WCall(method, args...)
121 }
122 `[1:],
123 },
124 }
125
126 for _, test := range testCases {
127 test := test
128 t.Runscip-go gomod github.com/golang/go/src go1.21 testing/T#Run().(test.desc, func(t *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Tscip-go gomod github.com/golang/go/src go1.21 testing/T#) {
129 cwd, err := osscip-go gomod github.com/golang/go/src go1.21 os/.Getwdscip-go gomod github.com/golang/go/src go1.21 os/Getwd().()
130 if err != nil {
131 t.Fatalscip-go gomod github.com/golang/go/src go1.21 testing/common#Fatal().(err)
132 }
133 wd := test.wd
134 if wd == "" {
135 wd = cwd
136 } else {
137 if err := osscip-go gomod github.com/golang/go/src go1.21 os/.Chdirscip-go gomod github.com/golang/go/src go1.21 os/Chdir().(wd); err != nil {
138 t.Fatalscip-go gomod github.com/golang/go/src go1.21 testing/common#Fatal().(err)
139 }
140 defer func() {
141 if err := osscip-go gomod github.com/golang/go/src go1.21 os/.Chdirscip-go gomod github.com/golang/go/src go1.21 os/Chdir().(cwd); err != nil {
142 t.Fatalscip-go gomod github.com/golang/go/src go1.21 testing/common#Fatal().(err)
143 }
144 }()
145 }
146
147 dest := pathscip-go gomod github.com/golang/go/src go1.21 path/.Basescip-go gomod github.com/golang/go/src go1.21 path/Base().(wd)
148 if test.dest != "" {
149 dest = test.dest
150 }
151 ext := Extractorscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/Extractor#{
152 Destscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/Extractor#Dest.: dest,
153 }
154
155 var out bytesscip-go gomod github.com/golang/go/src go1.21 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#
156 if _, err := ext.Extractscip-go gomod github.com/traefik/yaegi . `github.com/traefik/yaegi/extract`/Extractor#Extract().(test.arg, test.importPath, &out); err != nil {
157 t.Fatalscip-go gomod github.com/golang/go/src go1.21 testing/common#Fatal().(err)
158 }
159
160 if test.expected != "" {
161 if out.Stringscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#String().() != test.expected {
162 t.Fatalfscip-go gomod github.com/golang/go/src go1.21 testing/common#Fatalf().("\nGot:\n%q\nWant: \n%q", out.Stringscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#String().(), test.expected)
163 }
164 }
165
166 if test.contains != "" {
167 if !stringsscip-go gomod github.com/golang/go/src go1.21 strings/.Containsscip-go gomod github.com/golang/go/src go1.21 strings/Contains().(out.Stringscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#String().(), test.contains) {
168 t.Fatalfscip-go gomod github.com/golang/go/src go1.21 testing/common#Fatalf().("Missing expected part: %s in %s", test.contains, out.Stringscip-go gomod github.com/golang/go/src go1.21 bytes/Buffer#String().())
169 }
170 }
171 })
172 }
173 }