2
0

plugin_gen_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package ast
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestPluginGenModel_Injection(t *testing.T) {
  8. type fields struct {
  9. Type Type
  10. Path string
  11. ImportPath string
  12. PackageName string
  13. StructName string
  14. IsNew bool
  15. }
  16. tests := []struct {
  17. name string
  18. fields fields
  19. wantErr bool
  20. }{
  21. {
  22. name: "测试 GvaUser 结构体注入",
  23. fields: fields{
  24. Type: TypePluginGen,
  25. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "gen", "main.go"),
  26. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/model"`,
  27. PackageName: "model",
  28. StructName: "User",
  29. IsNew: false,
  30. },
  31. },
  32. {
  33. name: "测试 GvaUser 结构体注入",
  34. fields: fields{
  35. Type: TypePluginGen,
  36. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "gen", "main.go"),
  37. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/model"`,
  38. PackageName: "model",
  39. StructName: "User",
  40. IsNew: true,
  41. },
  42. },
  43. }
  44. for _, tt := range tests {
  45. t.Run(tt.name, func(t *testing.T) {
  46. a := &PluginGen{
  47. Type: tt.fields.Type,
  48. Path: tt.fields.Path,
  49. ImportPath: tt.fields.ImportPath,
  50. PackageName: tt.fields.PackageName,
  51. StructName: tt.fields.StructName,
  52. IsNew: tt.fields.IsNew,
  53. }
  54. file, err := a.Parse(a.Path, nil)
  55. if err != nil {
  56. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  57. }
  58. a.Injection(file)
  59. err = a.Format(a.Path, nil, file)
  60. if (err != nil) != tt.wantErr {
  61. t.Errorf("Injection() error = %v, wantErr %v", err, tt.wantErr)
  62. }
  63. })
  64. }
  65. }
  66. func TestPluginGenModel_Rollback(t *testing.T) {
  67. type fields struct {
  68. Type Type
  69. Path string
  70. ImportPath string
  71. PackageName string
  72. StructName string
  73. IsNew bool
  74. }
  75. tests := []struct {
  76. name string
  77. fields fields
  78. wantErr bool
  79. }{
  80. {
  81. name: "测试 GvaUser 回滚",
  82. fields: fields{
  83. Type: TypePluginGen,
  84. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "gen", "main.go"),
  85. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/model"`,
  86. PackageName: "model",
  87. StructName: "User",
  88. IsNew: false,
  89. },
  90. },
  91. {
  92. name: "测试 GvaUser 回滚",
  93. fields: fields{
  94. Type: TypePluginGen,
  95. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "gen", "main.go"),
  96. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva/model"`,
  97. PackageName: "model",
  98. StructName: "User",
  99. IsNew: true,
  100. },
  101. },
  102. }
  103. for _, tt := range tests {
  104. t.Run(tt.name, func(t *testing.T) {
  105. a := &PluginGen{
  106. Type: tt.fields.Type,
  107. Path: tt.fields.Path,
  108. ImportPath: tt.fields.ImportPath,
  109. PackageName: tt.fields.PackageName,
  110. StructName: tt.fields.StructName,
  111. IsNew: tt.fields.IsNew,
  112. }
  113. file, err := a.Parse(a.Path, nil)
  114. if err != nil {
  115. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  116. }
  117. a.Rollback(file)
  118. err = a.Format(a.Path, nil, file)
  119. if (err != nil) != tt.wantErr {
  120. t.Errorf("Rollback() error = %v, wantErr %v", err, tt.wantErr)
  121. }
  122. })
  123. }
  124. }