util_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package dartgen
  2. import (
  3. "testing"
  4. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/api/spec"
  5. )
  6. func Test_getPropertyFromMember(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. member spec.Member
  10. want string
  11. }{
  12. {
  13. name: "json tag should be ok",
  14. member: spec.Member{
  15. Tag: "`json:\"foo\"`",
  16. Name: "Foo",
  17. },
  18. want: "foo",
  19. },
  20. {
  21. name: "form tag should be ok",
  22. member: spec.Member{
  23. Tag: "`form:\"bar\"`",
  24. Name: "Bar",
  25. },
  26. want: "bar",
  27. },
  28. }
  29. for _, tt := range tests {
  30. t.Run(tt.name, func(t *testing.T) {
  31. if got := getPropertyFromMember(tt.member); got != tt.want {
  32. t.Errorf("getPropertyFromMember() = %v, want %v", got, tt.want)
  33. }
  34. })
  35. }
  36. }
  37. func Test_specTypeToDart(t *testing.T) {
  38. tests := []struct {
  39. name string
  40. specType spec.Type
  41. want string
  42. wantErr bool
  43. }{
  44. {
  45. name: "[]string should return List<String>",
  46. specType: spec.ArrayType{RawName: "[]string", Value: spec.PrimitiveType{RawName: "string"}},
  47. want: "List<String>",
  48. },
  49. {
  50. name: "[]Foo should return List<Foo>",
  51. specType: spec.ArrayType{RawName: "[]Foo", Value: spec.DefineStruct{RawName: "Foo"}},
  52. want: "List<Foo>",
  53. },
  54. }
  55. for _, tt := range tests {
  56. t.Run(tt.name, func(t *testing.T) {
  57. got, err := specTypeToDart(tt.specType)
  58. if (err != nil) != tt.wantErr {
  59. t.Errorf("specTypeToDart() error = %v, wantErr %v", err, tt.wantErr)
  60. return
  61. }
  62. if got != tt.want {
  63. t.Errorf("specTypeToDart() = %v, want %v", got, tt.want)
  64. }
  65. })
  66. }
  67. }