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.
 
 
 

212 lines
4.7 KiB

  1. // Copyright 2017 Google Inc. All Rights Reserved.
  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. names := []string{
  26. "any",
  27. "arrayofarray-1",
  28. "arrayofenum",
  29. "arrayofmapofobjects",
  30. "arrayofmapofstrings",
  31. "blogger-3",
  32. "floats",
  33. "getwithoutbody",
  34. "mapofany",
  35. "mapofarrayofobjects",
  36. "mapofint64strings",
  37. "mapofobjects",
  38. "mapofstrings-1",
  39. "param-rename",
  40. "quotednum",
  41. "repeated",
  42. "resource-named-service", // appengine/v1/appengine-api.json
  43. "unfortunatedefaults",
  44. "variants",
  45. "wrapnewlines",
  46. }
  47. for _, name := range names {
  48. t.Logf("TEST %s", name)
  49. api, err := apiFromFile(filepath.Join("testdata", name+".json"))
  50. if err != nil {
  51. t.Errorf("Error loading API testdata/%s.json: %v", name, err)
  52. continue
  53. }
  54. clean, err := api.GenerateCode()
  55. if err != nil {
  56. t.Errorf("Error generating code for %s: %v", name, err)
  57. continue
  58. }
  59. goldenFile := filepath.Join("testdata", name+".want")
  60. if *updateGolden {
  61. if err := ioutil.WriteFile(goldenFile, clean, 0644); err != nil {
  62. t.Fatal(err)
  63. }
  64. }
  65. want, err := ioutil.ReadFile(goldenFile)
  66. if err != nil {
  67. t.Error(err)
  68. continue
  69. }
  70. if !bytes.Equal(want, clean) {
  71. tf, _ := ioutil.TempFile("", "api-"+name+"-got-json.")
  72. tf.Write(clean)
  73. tf.Close()
  74. t.Errorf("Output for API %s differs: diff -u %s %s", name, goldenFile, tf.Name())
  75. }
  76. }
  77. }
  78. func TestScope(t *testing.T) {
  79. tests := [][]string{
  80. {
  81. "https://www.googleapis.com/auth/somescope",
  82. "SomescopeScope",
  83. },
  84. {
  85. "https://mail.google.com/somescope",
  86. "MailGoogleComSomescopeScope",
  87. },
  88. {
  89. "https://mail.google.com/",
  90. "MailGoogleComScope",
  91. },
  92. }
  93. for _, test := range tests {
  94. if got := scopeIdentifierFromURL(test[0]); got != test[1] {
  95. t.Errorf("scopeIdentifierFromURL(%q) got %q, want %q", test[0], got, test[1])
  96. }
  97. }
  98. }
  99. func TestDepunct(t *testing.T) {
  100. tests := []struct {
  101. needCap bool
  102. in, want string
  103. }{
  104. {
  105. needCap: true,
  106. in: "part__description",
  107. want: "Part__description",
  108. },
  109. {
  110. needCap: true,
  111. in: "Part_description",
  112. want: "PartDescription",
  113. },
  114. {
  115. needCap: false,
  116. in: "part_description",
  117. want: "partDescription",
  118. },
  119. {
  120. needCap: false,
  121. in: "part-description",
  122. want: "partDescription",
  123. },
  124. {
  125. needCap: false,
  126. in: "part.description",
  127. want: "partDescription",
  128. },
  129. {
  130. needCap: false,
  131. in: "part$description",
  132. want: "partDescription",
  133. },
  134. {
  135. needCap: false,
  136. in: "part/description",
  137. want: "partDescription",
  138. },
  139. {
  140. needCap: true,
  141. in: "Part__description_name",
  142. want: "Part__descriptionName",
  143. },
  144. {
  145. needCap: true,
  146. in: "Part_description_name",
  147. want: "PartDescriptionName",
  148. },
  149. {
  150. needCap: true,
  151. in: "Part__description__name",
  152. want: "Part__description__name",
  153. },
  154. {
  155. needCap: true,
  156. in: "Part_description__name",
  157. want: "PartDescription__name",
  158. },
  159. }
  160. for _, test := range tests {
  161. if got := depunct(test.in, test.needCap); got != test.want {
  162. t.Errorf("depunct(%q,%v) = %q; want %q", test.in, test.needCap, got, test.want)
  163. }
  164. }
  165. }
  166. func TestRenameVersion(t *testing.T) {
  167. tests := []struct {
  168. version, want string
  169. }{
  170. {
  171. version: "directory_v1",
  172. want: "directory/v1",
  173. },
  174. {
  175. version: "email_migration_v1",
  176. want: "email_migration/v1",
  177. },
  178. {
  179. version: "my_api_v1.2",
  180. want: "my_api/v1.2",
  181. },
  182. }
  183. for _, test := range tests {
  184. if got := renameVersion(test.version); got != test.want {
  185. t.Errorf("renameVersion(%q) = %q; want %q", test.version, got, test.want)
  186. }
  187. }
  188. }
  189. func TestSupportsPaging(t *testing.T) {
  190. api, err := apiFromFile(filepath.Join("testdata", "paging.json"))
  191. if err != nil {
  192. t.Fatalf("Error loading API testdata/paging.json: %v", err)
  193. }
  194. api.PopulateSchemas()
  195. res := api.doc.Resources[0]
  196. for _, meth := range api.resourceMethods(res) {
  197. _, _, got := meth.supportsPaging()
  198. want := strings.HasPrefix(meth.m.Name, "yes")
  199. if got != want {
  200. t.Errorf("method %s supports paging: got %t, want %t", meth.m.Name, got, want)
  201. }
  202. }
  203. }