@@ -11,9 +11,10 @@ import (
1111
1212func TestClassParser (t * testing.T ) {
1313 tests := []struct {
14- name string
15- input string
16- expected int
14+ name string
15+ input string
16+ expect int
17+ assert func (t * testing.T , classes []phpClass )
1718 }{
1819 {
1920 name : "single class" ,
@@ -24,7 +25,13 @@ type UserStruct struct {
2425 name string
2526 Age int
2627}` ,
27- expected : 1 ,
28+ expect : 1 ,
29+ assert : func (t * testing.T , classes []phpClass ) {
30+ c := classes [0 ]
31+ assert .Equal (t , "User" , c .Name )
32+ assert .Equal (t , "UserStruct" , c .GoStruct )
33+ assert .Len (t , c .Properties , 2 )
34+ },
2835 },
2936 {
3037 name : "multiple classes" ,
@@ -41,7 +48,7 @@ type ProductStruct struct {
4148 Title string
4249 Price float64
4350}` ,
44- expected : 2 ,
51+ expect : 2 ,
4552 },
4653 {
4754 name : "no php classes" ,
@@ -50,7 +57,7 @@ type ProductStruct struct {
5057type RegularStruct struct {
5158 Data string
5259}` ,
53- expected : 0 ,
60+ expect : 0 ,
5461 },
5562 {
5663 name : "class with nullable fields" ,
@@ -62,7 +69,14 @@ type OptionalStruct struct {
6269 Optional *string
6370 Count *int
6471}` ,
65- expected : 1 ,
72+ expect : 1 ,
73+ assert : func (t * testing.T , classes []phpClass ) {
74+ require .Len (t , classes [0 ].Properties , 3 )
75+ props := classes [0 ].Properties
76+ assert .False (t , props [0 ].IsNullable , "Required field should not be nullable" )
77+ assert .True (t , props [1 ].IsNullable , "Optional field should be nullable" )
78+ assert .True (t , props [2 ].IsNullable , "Count field should be nullable" )
79+ },
6680 },
6781 {
6882 name : "class with methods" ,
@@ -83,7 +97,7 @@ func GetUserName(u UserStruct) string {
8397func SetUserAge(u *UserStruct, age int) {
8498 u.Age = age
8599}` ,
86- expected : 1 ,
100+ expect : 1 ,
87101 },
88102 }
89103
@@ -96,23 +110,10 @@ func SetUserAge(u *UserStruct, age int) {
96110 parser := classParser {}
97111 classes , err := parser .parse (fileName )
98112 require .NoError (t , err )
113+ require .Len (t , classes , tt .expect )
99114
100- assert .Len (t , classes , tt .expected , "parse() got wrong number of classes" )
101-
102- if tt .name == "single class" && len (classes ) > 0 {
103- class := classes [0 ]
104- assert .Equal (t , "User" , class .Name , "Expected class name 'User'" )
105- assert .Equal (t , "UserStruct" , class .GoStruct , "Expected Go struct 'UserStruct'" )
106- assert .Len (t , class .Properties , 2 , "Expected 2 properties" )
107- }
108-
109- if tt .name == "class with nullable fields" && len (classes ) > 0 {
110- class := classes [0 ]
111- if len (class .Properties ) >= 3 {
112- assert .False (t , class .Properties [0 ].IsNullable , "Required field should not be nullable" )
113- assert .True (t , class .Properties [1 ].IsNullable , "Optional field should be nullable" )
114- assert .True (t , class .Properties [2 ].IsNullable , "Count field should be nullable" )
115- }
115+ if tt .assert != nil {
116+ tt .assert (t , classes )
116117 }
117118 })
118119 }
0 commit comments