@@ -14,6 +14,7 @@ func TestSourceAnalyzer_Analyze(t *testing.T) {
1414 name string
1515 sourceContent string
1616 expectedImports []string
17+ expectedVariables []string
1718 expectedFunctions []string
1819 expectError bool
1920 }{
@@ -34,7 +35,8 @@ func regularFunction() {
3435func exportedFunction() string {
3536 return "exported"
3637}` ,
37- expectedImports : []string {`"fmt"` , `"strings"` },
38+ expectedImports : []string {`"fmt"` , `"strings"` },
39+ expectedVariables : nil ,
3840 expectedFunctions : []string {
3941 `func regularFunction() {
4042 fmt.Println("hello")
@@ -53,7 +55,8 @@ import (
5355)
5456
5557func test() {}` ,
56- expectedImports : []string {`custom "fmt"` , `. "strings"` , `_ "os"` },
58+ expectedImports : []string {`custom "fmt"` , `. "strings"` , `_ "os"` },
59+ expectedVariables : nil ,
5760 expectedFunctions : []string {
5861 `func test() {}` ,
5962 },
@@ -82,7 +85,8 @@ func internalTwo() string {
8285func exportedTwo() bool {
8386 return true
8487}` ,
85- expectedImports : []string {},
88+ expectedImports : []string {},
89+ expectedVariables : nil ,
8690 expectedFunctions : []string {
8791 `func internalOne() {
8892 // some code
@@ -116,7 +120,8 @@ func exportedComplex() {
116120 }
117121 fmt.Println(obj)
118122}` ,
119- expectedImports : []string {},
123+ expectedImports : []string {},
124+ expectedVariables : nil ,
120125 expectedFunctions : []string {
121126 `func complexFunction() {
122127 if true {
@@ -163,10 +168,50 @@ func shouldNotBeExported() {}
163168func normalFunction() {
164169 //export_php:function inside function should not count
165170}` ,
166- expectedImports : []string {},
171+ expectedImports : []string {},
172+ expectedVariables : nil ,
167173 expectedFunctions : []string {
168174 `func normalFunction() {
169175 //export_php:function inside function should not count
176+ }` ,
177+ },
178+ expectError : false ,
179+ },
180+ {
181+ name : "file with variable blocks" ,
182+ sourceContent : `package main
183+
184+ import (
185+ "sync"
186+ )
187+
188+ var (
189+ mu sync.RWMutex
190+ store = map[string]struct {
191+ val string
192+ expires int64
193+ }{}
194+ )
195+
196+ var singleVar = "test"
197+
198+ func testFunction() {
199+ // test function
200+ }` ,
201+ expectedImports : []string {`"sync"` },
202+ expectedVariables : []string {
203+ `var (
204+ mu sync.RWMutex
205+ store = map[string]struct {
206+ val string
207+ expires int64
208+ }{}
209+ )` ,
210+ `var singleVar = "test"` ,
211+ },
212+ expectedFunctions : []string {
213+ `func testFunction() {
214+ // test function
170215}` ,
171216 },
172217 expectError : false ,
@@ -181,7 +226,7 @@ func normalFunction() {
181226 require .NoError (t , os .WriteFile (filename , []byte (tt .sourceContent ), 0644 ))
182227
183228 analyzer := & SourceAnalyzer {}
184- imports , functions , err := analyzer .analyze (filename )
229+ imports , variables , functions , err := analyzer .analyze (filename )
185230
186231 if tt .expectError {
187232 assert .Error (t , err , "expected error" )
@@ -194,6 +239,7 @@ func normalFunction() {
194239 assert .Equal (t , tt .expectedImports , imports , "imports mismatch" )
195240 }
196241
242+ assert .Equal (t , tt .expectedVariables , variables , "variables mismatch" )
197243 assert .Len (t , functions , len (tt .expectedFunctions ), "function count mismatch" )
198244
199245 for i , expected := range tt .expectedFunctions {
@@ -207,7 +253,7 @@ func TestSourceAnalyzer_Analyze_InvalidFile(t *testing.T) {
207253 analyzer := & SourceAnalyzer {}
208254
209255 t .Run ("nonexistent file" , func (t * testing.T ) {
210- _ , _ , err := analyzer .analyze ("/nonexistent/file.go" )
256+ _ , _ , _ , err := analyzer .analyze ("/nonexistent/file.go" )
211257 assert .Error (t , err , "expected error for nonexistent file" )
212258 })
213259
@@ -222,7 +268,7 @@ func TestSourceAnalyzer_Analyze_InvalidFile(t *testing.T) {
222268
223269 require .NoError (t , os .WriteFile (filename , []byte (invalidContent ), 0644 ))
224270
225- _ , _ , err := analyzer .analyze (filename )
271+ _ , _ , _ , err := analyzer .analyze (filename )
226272 assert .Error (t , err , "expected error for invalid syntax" )
227273 })
228274}
@@ -372,7 +418,7 @@ func internalTwo() {
372418 analyzer := & SourceAnalyzer {}
373419
374420 for b .Loop () {
375- _ , _ , err := analyzer .analyze (filename )
421+ _ , _ , _ , err := analyzer .analyze (filename )
376422 require .NoError (b , err )
377423 }
378424}
0 commit comments