models.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package model
  2. import (
  3. "strings"
  4. "unicode"
  5. )
  6. type Expression struct {
  7. Value string
  8. IsStar bool
  9. IsVariadic bool
  10. IsWriter bool
  11. Underlying string
  12. }
  13. func (e *Expression) String() string {
  14. value := e.Value
  15. if e.IsStar {
  16. value = "*" + value
  17. }
  18. if e.IsVariadic {
  19. return "[]" + value
  20. }
  21. return value
  22. }
  23. type Field struct {
  24. Name string
  25. Type *Expression
  26. Index int
  27. }
  28. func (f *Field) IsWriter() bool {
  29. return f.Type.IsWriter
  30. }
  31. func (f *Field) IsStruct() bool {
  32. return strings.HasPrefix(f.Type.Underlying, "struct")
  33. }
  34. func (f *Field) IsBasicType() bool {
  35. return isBasicType(f.Type.String()) || isBasicType(f.Type.Underlying)
  36. }
  37. func isBasicType(t string) bool {
  38. switch t {
  39. case "bool", "string", "int", "int8", "int16", "int32", "int64", "uint",
  40. "uint8", "uint16", "uint32", "uint64", "uintptr", "byte", "rune",
  41. "float32", "float64", "complex64", "complex128":
  42. return true
  43. default:
  44. return false
  45. }
  46. }
  47. func (f *Field) IsNamed() bool {
  48. return f.Name != "" && f.Name != "_"
  49. }
  50. func (f *Field) ShortName() string {
  51. return strings.ToLower(string([]rune(f.Type.Value)[0]))
  52. }
  53. type Receiver struct {
  54. *Field
  55. Fields []*Field
  56. }
  57. type Function struct {
  58. Name string
  59. IsExported bool
  60. Receiver *Receiver
  61. Parameters []*Field
  62. Results []*Field
  63. ReturnsError bool
  64. }
  65. func (f *Function) TestParameters() []*Field {
  66. var ps []*Field
  67. for _, p := range f.Parameters {
  68. if p.IsWriter() {
  69. continue
  70. }
  71. ps = append(ps, p)
  72. }
  73. return ps
  74. }
  75. func (f *Function) TestResults() []*Field {
  76. var ps []*Field
  77. ps = append(ps, f.Results...)
  78. for _, p := range f.Parameters {
  79. if !p.IsWriter() {
  80. continue
  81. }
  82. ps = append(ps, &Field{
  83. Name: p.Name,
  84. Type: &Expression{
  85. Value: "string",
  86. IsWriter: true,
  87. Underlying: "string",
  88. },
  89. Index: len(ps),
  90. })
  91. }
  92. return ps
  93. }
  94. func (f *Function) ReturnsMultiple() bool {
  95. return len(f.Results) > 1
  96. }
  97. func (f *Function) OnlyReturnsOneValue() bool {
  98. return len(f.Results) == 1 && !f.ReturnsError
  99. }
  100. func (f *Function) OnlyReturnsError() bool {
  101. return len(f.Results) == 0 && f.ReturnsError
  102. }
  103. func (f *Function) FullName() string {
  104. var r string
  105. if f.Receiver != nil {
  106. r = f.Receiver.Type.Value
  107. }
  108. return strings.Title(r) + strings.Title(f.Name)
  109. }
  110. func (f *Function) TestName() string {
  111. if f.Receiver != nil {
  112. receiverType := f.Receiver.Type.Value
  113. if unicode.IsLower([]rune(receiverType)[0]) {
  114. receiverType = "_" + receiverType
  115. }
  116. return "Test" + receiverType + "_" + f.Name
  117. }
  118. if unicode.IsLower([]rune(f.Name)[0]) {
  119. return "Test_" + f.Name
  120. }
  121. return "Test" + f.Name
  122. }
  123. func (f *Function) IsNaked() bool {
  124. return f.Receiver == nil && len(f.Parameters) == 0 && len(f.Results) == 0
  125. }
  126. type Import struct {
  127. Name, Path string
  128. }
  129. type Header struct {
  130. Comments []string
  131. Package string
  132. Imports []*Import
  133. Code []byte
  134. }
  135. type Path string
  136. func (p Path) TestPath() string {
  137. return strings.TrimSuffix(string(p), ".go") + "_test.go"
  138. }
  139. func (p Path) IsTestPath() bool {
  140. return strings.HasSuffix(string(p), "_test.go")
  141. }