plugin_enter_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package ast
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestPluginEnter_Injection(t *testing.T) {
  8. type fields struct {
  9. Type Type
  10. Path string
  11. ImportPath string
  12. StructName string
  13. StructCamelName string
  14. ModuleName string
  15. GroupName string
  16. PackageName string
  17. ServiceName string
  18. }
  19. tests := []struct {
  20. name string
  21. fields fields
  22. wantErr bool
  23. }{
  24. {
  25. name: "测试 Gva插件UserApi 注入",
  26. fields: fields{
  27. Type: TypePluginApiEnter,
  28. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "api", "enter.go"),
  29. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/service"`,
  30. StructName: "User",
  31. StructCamelName: "user",
  32. ModuleName: "serviceUser",
  33. GroupName: "Service",
  34. PackageName: "service",
  35. ServiceName: "User",
  36. },
  37. wantErr: false,
  38. },
  39. {
  40. name: "测试 Gva插件UserRouter 注入",
  41. fields: fields{
  42. Type: TypePluginRouterEnter,
  43. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "router", "enter.go"),
  44. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/api"`,
  45. StructName: "User",
  46. StructCamelName: "user",
  47. ModuleName: "userApi",
  48. GroupName: "Api",
  49. PackageName: "api",
  50. ServiceName: "User",
  51. },
  52. wantErr: false,
  53. },
  54. {
  55. name: "测试 Gva插件UserService 注入",
  56. fields: fields{
  57. Type: TypePluginServiceEnter,
  58. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "service", "enter.go"),
  59. ImportPath: "",
  60. StructName: "User",
  61. StructCamelName: "user",
  62. ModuleName: "",
  63. GroupName: "",
  64. PackageName: "",
  65. ServiceName: "",
  66. },
  67. wantErr: false,
  68. },
  69. {
  70. name: "测试 gva的User 注入",
  71. fields: fields{
  72. Type: TypePluginServiceEnter,
  73. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "service", "enter.go"),
  74. ImportPath: "",
  75. StructName: "User",
  76. StructCamelName: "user",
  77. ModuleName: "",
  78. GroupName: "",
  79. PackageName: "",
  80. ServiceName: "",
  81. },
  82. wantErr: false,
  83. },
  84. }
  85. for _, tt := range tests {
  86. t.Run(tt.name, func(t *testing.T) {
  87. a := &PluginEnter{
  88. Type: tt.fields.Type,
  89. Path: tt.fields.Path,
  90. ImportPath: tt.fields.ImportPath,
  91. StructName: tt.fields.StructName,
  92. StructCamelName: tt.fields.StructCamelName,
  93. ModuleName: tt.fields.ModuleName,
  94. GroupName: tt.fields.GroupName,
  95. PackageName: tt.fields.PackageName,
  96. ServiceName: tt.fields.ServiceName,
  97. }
  98. file, err := a.Parse(a.Path, nil)
  99. if err != nil {
  100. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  101. }
  102. a.Injection(file)
  103. err = a.Format(a.Path, nil, file)
  104. if (err != nil) != tt.wantErr {
  105. t.Errorf("Injection() error = %v, wantErr %v", err, tt.wantErr)
  106. }
  107. })
  108. }
  109. }
  110. func TestPluginEnter_Rollback(t *testing.T) {
  111. type fields struct {
  112. Type Type
  113. Path string
  114. ImportPath string
  115. StructName string
  116. StructCamelName string
  117. ModuleName string
  118. GroupName string
  119. PackageName string
  120. ServiceName string
  121. }
  122. tests := []struct {
  123. name string
  124. fields fields
  125. wantErr bool
  126. }{
  127. {
  128. name: "测试 Gva插件UserRouter 回滚",
  129. fields: fields{
  130. Type: TypePluginRouterEnter,
  131. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "router", "enter.go"),
  132. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/api"`,
  133. StructName: "User",
  134. StructCamelName: "user",
  135. ModuleName: "userApi",
  136. GroupName: "Api",
  137. PackageName: "api",
  138. ServiceName: "User",
  139. },
  140. wantErr: false,
  141. },
  142. {
  143. name: "测试 Gva插件UserApi 回滚",
  144. fields: fields{
  145. Type: TypePluginApiEnter,
  146. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "api", "enter.go"),
  147. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/service"`,
  148. StructName: "User",
  149. StructCamelName: "user",
  150. ModuleName: "serviceUser",
  151. GroupName: "Service",
  152. PackageName: "service",
  153. ServiceName: "User",
  154. },
  155. wantErr: false,
  156. },
  157. {
  158. name: "测试 Gva插件UserService 回滚",
  159. fields: fields{
  160. Type: TypePluginServiceEnter,
  161. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "service", "enter.go"),
  162. ImportPath: "",
  163. StructName: "User",
  164. StructCamelName: "user",
  165. ModuleName: "",
  166. GroupName: "",
  167. PackageName: "",
  168. ServiceName: "",
  169. },
  170. wantErr: false,
  171. },
  172. }
  173. for _, tt := range tests {
  174. t.Run(tt.name, func(t *testing.T) {
  175. a := &PluginEnter{
  176. Type: tt.fields.Type,
  177. Path: tt.fields.Path,
  178. ImportPath: tt.fields.ImportPath,
  179. StructName: tt.fields.StructName,
  180. StructCamelName: tt.fields.StructCamelName,
  181. ModuleName: tt.fields.ModuleName,
  182. GroupName: tt.fields.GroupName,
  183. PackageName: tt.fields.PackageName,
  184. ServiceName: tt.fields.ServiceName,
  185. }
  186. file, err := a.Parse(a.Path, nil)
  187. if err != nil {
  188. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  189. }
  190. a.Rollback(file)
  191. err = a.Format(a.Path, nil, file)
  192. if (err != nil) != tt.wantErr {
  193. t.Errorf("Rollback() error = %v, wantErr %v", err, tt.wantErr)
  194. }
  195. })
  196. }
  197. }