You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

216 lines
4.8 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "flag"
  18. "io/ioutil"
  19. "path/filepath"
  20. "strings"
  21. "testing"
  22. )
  23. var updateGolden = flag.Bool("update_golden", false, "If true, causes TestAPIs to update golden files")
  24. func TestAPIs(t *testing.T) {
  25. *copyrightYear = "YEAR"
  26. names := []string{
  27. "any",
  28. "arrayofarray-1",
  29. "arrayofenum",
  30. "arrayofmapofobjects",
  31. "arrayofmapofstrings",
  32. "blogger-3",
  33. "floats",
  34. "getwithoutbody",
  35. "json-body",
  36. "mapofany",
  37. "mapofarrayofobjects",
  38. "mapofint64strings",
  39. "mapofobjects",
  40. "mapofstrings-1",
  41. "param-rename",
  42. "quotednum",
  43. "repeated",
  44. "required-query",
  45. "resource-named-service", // appengine/v1/appengine-api.json
  46. "unfortunatedefaults",
  47. "variants",
  48. "wrapnewlines",
  49. }
  50. for _, name := range names {
  51. t.Logf("TEST %s", name)
  52. api, err := apiFromFile(filepath.Join("testdata", name+".json"))
  53. if err != nil {
  54. t.Errorf("Error loading API testdata/%s.json: %v", name, err)
  55. continue
  56. }
  57. clean, err := api.GenerateCode()
  58. if err != nil {
  59. t.Errorf("Error generating code for %s: %v", name, err)
  60. continue
  61. }
  62. goldenFile := filepath.Join("testdata", name+".want")
  63. if *updateGolden {
  64. if err := ioutil.WriteFile(goldenFile, clean, 0644); err != nil {
  65. t.Fatal(err)
  66. }
  67. }
  68. want, err := ioutil.ReadFile(goldenFile)
  69. if err != nil {
  70. t.Error(err)
  71. continue
  72. }
  73. if !bytes.Equal(want, clean) {
  74. tf, _ := ioutil.TempFile("", "api-"+name+"-got-json.")
  75. tf.Write(clean)
  76. tf.Close()
  77. t.Errorf("Output for API %s differs: diff -u %s %s", name, goldenFile, tf.Name())
  78. }
  79. }
  80. }
  81. func TestScope(t *testing.T) {
  82. tests := [][]string{
  83. {
  84. "https://www.googleapis.com/auth/somescope",
  85. "SomescopeScope",
  86. },
  87. {
  88. "https://mail.google.com/somescope",
  89. "MailGoogleComSomescopeScope",
  90. },
  91. {
  92. "https://mail.google.com/",
  93. "MailGoogleComScope",
  94. },
  95. }
  96. for _, test := range tests {
  97. if got := scopeIdentifierFromURL(test[0]); got != test[1] {
  98. t.Errorf("scopeIdentifierFromURL(%q) got %q, want %q", test[0], got, test[1])
  99. }
  100. }
  101. }
  102. func TestDepunct(t *testing.T) {
  103. tests := []struct {
  104. needCap bool
  105. in, want string
  106. }{
  107. {
  108. needCap: true,
  109. in: "part__description",
  110. want: "Part__description",
  111. },
  112. {
  113. needCap: true,
  114. in: "Part_description",
  115. want: "PartDescription",
  116. },
  117. {
  118. needCap: false,
  119. in: "part_description",
  120. want: "partDescription",
  121. },
  122. {
  123. needCap: false,
  124. in: "part-description",
  125. want: "partDescription",
  126. },
  127. {
  128. needCap: false,
  129. in: "part.description",
  130. want: "partDescription",
  131. },
  132. {
  133. needCap: false,
  134. in: "part$description",
  135. want: "partDescription",
  136. },
  137. {
  138. needCap: false,
  139. in: "part/description",
  140. want: "partDescription",
  141. },
  142. {
  143. needCap: true,
  144. in: "Part__description_name",
  145. want: "Part__descriptionName",
  146. },
  147. {
  148. needCap: true,
  149. in: "Part_description_name",
  150. want: "PartDescriptionName",
  151. },
  152. {
  153. needCap: true,
  154. in: "Part__description__name",
  155. want: "Part__description__name",
  156. },
  157. {
  158. needCap: true,
  159. in: "Part_description__name",
  160. want: "PartDescription__name",
  161. },
  162. }
  163. for _, test := range tests {
  164. if got := depunct(test.in, test.needCap); got != test.want {
  165. t.Errorf("depunct(%q,%v) = %q; want %q", test.in, test.needCap, got, test.want)
  166. }
  167. }
  168. }
  169. func TestRenameVersion(t *testing.T) {
  170. tests := []struct {
  171. version, want string
  172. }{
  173. {
  174. version: "directory_v1",
  175. want: "directory/v1",
  176. },
  177. {
  178. version: "email_migration_v1",
  179. want: "email_migration/v1",
  180. },
  181. {
  182. version: "my_api_v1.2",
  183. want: "my_api/v1.2",
  184. },
  185. }
  186. for _, test := range tests {
  187. if got := renameVersion(test.version); got != test.want {
  188. t.Errorf("renameVersion(%q) = %q; want %q", test.version, got, test.want)
  189. }
  190. }
  191. }
  192. func TestSupportsPaging(t *testing.T) {
  193. api, err := apiFromFile(filepath.Join("testdata", "paging.json"))
  194. if err != nil {
  195. t.Fatalf("Error loading API testdata/paging.json: %v", err)
  196. }
  197. api.PopulateSchemas()
  198. res := api.doc.Resources[0]
  199. for _, meth := range api.resourceMethods(res) {
  200. _, _, got := meth.supportsPaging()
  201. want := strings.HasPrefix(meth.m.Name, "yes")
  202. if got != want {
  203. t.Errorf("method %s supports paging: got %t, want %t", meth.m.Name, got, want)
  204. }
  205. }
  206. }