printer_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package printer
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. "github.com/kyleconroy/sqlc/internal/python/ast"
  7. )
  8. type testcase struct {
  9. Node *ast.Node
  10. Expected string
  11. }
  12. func TestPrinter(t *testing.T) {
  13. for name, tc := range map[string]testcase{
  14. "assign": {
  15. Node: &ast.Node{
  16. Node: &ast.Node_Assign{
  17. Assign: &ast.Assign{
  18. Targets: []*ast.Node{
  19. {
  20. Node: &ast.Node_Name{
  21. Name: &ast.Name{Id: "FICTION"},
  22. },
  23. },
  24. },
  25. Value: &ast.Node{
  26. Node: &ast.Node_Constant{
  27. Constant: &ast.Constant{
  28. Value: &ast.Constant_Str{
  29. Str: "FICTION",
  30. },
  31. },
  32. },
  33. },
  34. },
  35. },
  36. },
  37. Expected: `FICTION = "FICTION"`,
  38. },
  39. "class-base": {
  40. Node: &ast.Node{
  41. Node: &ast.Node_ClassDef{
  42. ClassDef: &ast.ClassDef{
  43. Name: "Foo",
  44. Bases: []*ast.Node{
  45. {
  46. Node: &ast.Node_Name{
  47. Name: &ast.Name{Id: "str"},
  48. },
  49. },
  50. {
  51. Node: &ast.Node_Attribute{
  52. Attribute: &ast.Attribute{
  53. Value: &ast.Node{
  54. Node: &ast.Node_Name{
  55. Name: &ast.Name{Id: "enum"},
  56. },
  57. },
  58. Attr: "Enum",
  59. },
  60. },
  61. },
  62. },
  63. },
  64. },
  65. },
  66. Expected: `class Foo(str, enum.Enum):`,
  67. },
  68. "dataclass": {
  69. Node: &ast.Node{
  70. Node: &ast.Node_ClassDef{
  71. ClassDef: &ast.ClassDef{
  72. Name: "Foo",
  73. DecoratorList: []*ast.Node{
  74. {
  75. Node: &ast.Node_Name{
  76. Name: &ast.Name{
  77. Id: "dataclass",
  78. },
  79. },
  80. },
  81. },
  82. Body: []*ast.Node{
  83. {
  84. Node: &ast.Node_AnnAssign{
  85. AnnAssign: &ast.AnnAssign{
  86. Target: &ast.Name{Id: "bar"},
  87. Annotation: &ast.Node{
  88. Node: &ast.Node_Name{
  89. Name: &ast.Name{Id: "int"},
  90. },
  91. },
  92. },
  93. },
  94. },
  95. {
  96. Node: &ast.Node_AnnAssign{
  97. AnnAssign: &ast.AnnAssign{
  98. Target: &ast.Name{Id: "bat"},
  99. Annotation: &ast.Node{
  100. Node: &ast.Node_Subscript{
  101. Subscript: &ast.Subscript{
  102. Value: &ast.Name{Id: "Optional"},
  103. Slice: &ast.Node{
  104. Node: &ast.Node_Name{
  105. Name: &ast.Name{Id: "int"},
  106. },
  107. },
  108. },
  109. },
  110. },
  111. },
  112. },
  113. },
  114. },
  115. },
  116. },
  117. },
  118. Expected: `
  119. @dataclass
  120. class Foo:
  121. bar: int
  122. bat: Optional[int]
  123. `,
  124. },
  125. "call": {
  126. Node: &ast.Node{
  127. Node: &ast.Node_Call{
  128. Call: &ast.Call{
  129. Func: &ast.Node{
  130. Node: &ast.Node_Alias{
  131. Alias: &ast.Alias{
  132. Name: "foo",
  133. },
  134. },
  135. },
  136. },
  137. },
  138. },
  139. Expected: `foo()`,
  140. },
  141. "import": {
  142. Node: &ast.Node{
  143. Node: &ast.Node_Import{
  144. Import: &ast.Import{
  145. Names: []*ast.Node{
  146. {
  147. Node: &ast.Node_Alias{
  148. Alias: &ast.Alias{
  149. Name: "foo",
  150. },
  151. },
  152. },
  153. },
  154. },
  155. },
  156. },
  157. Expected: `import foo`,
  158. },
  159. "import-from": {
  160. Node: &ast.Node{
  161. Node: &ast.Node_ImportFrom{
  162. ImportFrom: &ast.ImportFrom{
  163. Module: "pkg",
  164. Names: []*ast.Node{
  165. {
  166. Node: &ast.Node_Alias{
  167. Alias: &ast.Alias{
  168. Name: "foo",
  169. },
  170. },
  171. },
  172. {
  173. Node: &ast.Node_Alias{
  174. Alias: &ast.Alias{
  175. Name: "bar",
  176. },
  177. },
  178. },
  179. },
  180. },
  181. },
  182. },
  183. Expected: `from pkg import foo, bar`,
  184. },
  185. } {
  186. tc := tc
  187. t.Run(name, func(t *testing.T) {
  188. result := Print(tc.Node, Options{})
  189. if diff := cmp.Diff(strings.TrimSpace(tc.Expected), strings.TrimSpace(string(result.Python))); diff != "" {
  190. t.Errorf("print mismatch (-want +got):\n%s", diff)
  191. }
  192. })
  193. }
  194. }