@@ -221,7 +221,7 @@ func TestConstantParserIotaSequence(t *testing.T) {
221221//export_php:const
222222const FirstIota = iota
223223
224- //export_php:const
224+ //export_php:const
225225const SecondIota = iota
226226
227227//export_php:const
@@ -244,6 +244,179 @@ const ThirdIota = iota`
244244 }
245245}
246246
247+ func TestConstantParserConstBlock (t * testing.T ) {
248+ input := `package main
249+
250+ const (
251+ // export_php:const
252+ STATUS_PENDING = iota
253+
254+ // export_php:const
255+ STATUS_PROCESSING
256+
257+ // export_php:const
258+ STATUS_COMPLETED
259+ )`
260+
261+ tmpDir := t .TempDir ()
262+ fileName := filepath .Join (tmpDir , "test.go" )
263+ require .NoError (t , os .WriteFile (fileName , []byte (input ), 0644 ))
264+
265+ parser := & ConstantParser {}
266+ constants , err := parser .parse (fileName )
267+ assert .NoError (t , err , "parse() error" )
268+
269+ assert .Len (t , constants , 3 , "Expected 3 constants" )
270+
271+ expectedNames := []string {"STATUS_PENDING" , "STATUS_PROCESSING" , "STATUS_COMPLETED" }
272+ expectedValues := []string {"0" , "1" , "2" }
273+
274+ for i , c := range constants {
275+ assert .Equal (t , expectedNames [i ], c .Name , "Expected constant %d name to be '%s'" , i , expectedNames [i ])
276+ assert .True (t , c .IsIota , "Expected constant %d to be iota" , i )
277+ assert .Equal (t , expectedValues [i ], c .Value , "Expected constant %d value to be '%s'" , i , expectedValues [i ])
278+ assert .Equal (t , phpInt , c .PhpType , "Expected constant %d to be phpInt type" , i )
279+ }
280+ }
281+
282+ func TestConstantParserConstBlockWithBlockLevelDirective (t * testing.T ) {
283+ input := `package main
284+
285+ // export_php:const
286+ const (
287+ STATUS_PENDING = iota
288+ STATUS_PROCESSING
289+ STATUS_COMPLETED
290+ )`
291+
292+ tmpDir := t .TempDir ()
293+ fileName := filepath .Join (tmpDir , "test.go" )
294+ require .NoError (t , os .WriteFile (fileName , []byte (input ), 0644 ))
295+
296+ parser := & ConstantParser {}
297+ constants , err := parser .parse (fileName )
298+ assert .NoError (t , err , "parse() error" )
299+
300+ assert .Len (t , constants , 3 , "Expected 3 constants" )
301+
302+ expectedNames := []string {"STATUS_PENDING" , "STATUS_PROCESSING" , "STATUS_COMPLETED" }
303+ expectedValues := []string {"0" , "1" , "2" }
304+
305+ for i , c := range constants {
306+ assert .Equal (t , expectedNames [i ], c .Name , "Expected constant %d name to be '%s'" , i , expectedNames [i ])
307+ assert .True (t , c .IsIota , "Expected constant %d to be iota" , i )
308+ assert .Equal (t , expectedValues [i ], c .Value , "Expected constant %d value to be '%s'" , i , expectedValues [i ])
309+ assert .Equal (t , phpInt , c .PhpType , "Expected constant %d to be phpInt type" , i )
310+ }
311+ }
312+
313+ func TestConstantParserMixedConstBlockAndIndividual (t * testing.T ) {
314+ input := `package main
315+
316+ // export_php:const
317+ const INDIVIDUAL = 42
318+
319+ const (
320+ // export_php:const
321+ BLOCK_ONE = iota
322+
323+ // export_php:const
324+ BLOCK_TWO
325+ )
326+
327+ // export_php:const
328+ const ANOTHER_INDIVIDUAL = "test"`
329+
330+ tmpDir := t .TempDir ()
331+ fileName := filepath .Join (tmpDir , "test.go" )
332+ require .NoError (t , os .WriteFile (fileName , []byte (input ), 0644 ))
333+
334+ parser := & ConstantParser {}
335+ constants , err := parser .parse (fileName )
336+ assert .NoError (t , err , "parse() error" )
337+
338+ assert .Len (t , constants , 4 , "Expected 4 constants" )
339+
340+ assert .Equal (t , "INDIVIDUAL" , constants [0 ].Name )
341+ assert .Equal (t , "42" , constants [0 ].Value )
342+ assert .Equal (t , phpInt , constants [0 ].PhpType )
343+
344+ assert .Equal (t , "BLOCK_ONE" , constants [1 ].Name )
345+ assert .Equal (t , "0" , constants [1 ].Value )
346+ assert .True (t , constants [1 ].IsIota )
347+
348+ assert .Equal (t , "BLOCK_TWO" , constants [2 ].Name )
349+ assert .Equal (t , "1" , constants [2 ].Value )
350+ assert .True (t , constants [2 ].IsIota )
351+
352+ assert .Equal (t , "ANOTHER_INDIVIDUAL" , constants [3 ].Name )
353+ assert .Equal (t , `"test"` , constants [3 ].Value )
354+ assert .Equal (t , phpString , constants [3 ].PhpType )
355+ }
356+
357+ func TestConstantParserClassConstBlock (t * testing.T ) {
358+ input := `package main
359+
360+ // export_php:classconst Config
361+ const (
362+ MODE_DEBUG = 1
363+ MODE_PRODUCTION = 2
364+ MODE_TEST = 3
365+ )`
366+
367+ tmpDir := t .TempDir ()
368+ fileName := filepath .Join (tmpDir , "test.go" )
369+ require .NoError (t , os .WriteFile (fileName , []byte (input ), 0644 ))
370+
371+ parser := & ConstantParser {}
372+ constants , err := parser .parse (fileName )
373+ assert .NoError (t , err , "parse() error" )
374+
375+ assert .Len (t , constants , 3 , "Expected 3 class constants" )
376+
377+ expectedNames := []string {"MODE_DEBUG" , "MODE_PRODUCTION" , "MODE_TEST" }
378+ expectedValues := []string {"1" , "2" , "3" }
379+
380+ for i , c := range constants {
381+ assert .Equal (t , expectedNames [i ], c .Name , "Expected constant %d name to be '%s'" , i , expectedNames [i ])
382+ assert .Equal (t , "Config" , c .ClassName , "Expected constant %d to belong to Config class" , i )
383+ assert .Equal (t , expectedValues [i ], c .Value , "Expected constant %d value to be '%s'" , i , expectedValues [i ])
384+ assert .Equal (t , phpInt , c .PhpType , "Expected constant %d to be phpInt type" , i )
385+ }
386+ }
387+
388+ func TestConstantParserClassConstBlockWithIota (t * testing.T ) {
389+ input := `package main
390+
391+ // export_php:classconst Status
392+ const (
393+ STATUS_PENDING = iota
394+ STATUS_ACTIVE
395+ STATUS_COMPLETED
396+ )`
397+
398+ tmpDir := t .TempDir ()
399+ fileName := filepath .Join (tmpDir , "test.go" )
400+ require .NoError (t , os .WriteFile (fileName , []byte (input ), 0644 ))
401+
402+ parser := & ConstantParser {}
403+ constants , err := parser .parse (fileName )
404+ assert .NoError (t , err , "parse() error" )
405+
406+ assert .Len (t , constants , 3 , "Expected 3 class constants" )
407+
408+ expectedNames := []string {"STATUS_PENDING" , "STATUS_ACTIVE" , "STATUS_COMPLETED" }
409+ expectedValues := []string {"0" , "1" , "2" }
410+
411+ for i , c := range constants {
412+ assert .Equal (t , expectedNames [i ], c .Name , "Expected constant %d name to be '%s'" , i , expectedNames [i ])
413+ assert .Equal (t , "Status" , c .ClassName , "Expected constant %d to belong to Status class" , i )
414+ assert .True (t , c .IsIota , "Expected constant %d to be iota" , i )
415+ assert .Equal (t , expectedValues [i ], c .Value , "Expected constant %d value to be '%s'" , i , expectedValues [i ])
416+ assert .Equal (t , phpInt , c .PhpType , "Expected constant %d to be phpInt type" , i )
417+ }
418+ }
419+
247420func TestConstantParserTypeDetection (t * testing.T ) {
248421 tests := []struct {
249422 name string
0 commit comments