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.
 
 
 

203 lines
4.8 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package database
  7. import (
  8. "reflect"
  9. "sort"
  10. "testing"
  11. "github.com/golang/gddo/doc"
  12. )
  13. var indexTests = []struct {
  14. pdoc *doc.Package
  15. terms []string
  16. }{
  17. {&doc.Package{
  18. ImportPath: "strconv",
  19. ProjectRoot: "",
  20. ProjectName: "Go",
  21. Name: "strconv",
  22. Synopsis: "Package strconv implements conversions to and from string representations of basic data types.",
  23. Doc: "Package strconv implements conversions to and from string representations\nof basic data types.",
  24. Imports: []string{"errors", "math", "unicode/utf8"},
  25. Funcs: []*doc.Func{{}},
  26. },
  27. []string{
  28. "bas",
  29. "convert",
  30. "dat",
  31. "import:errors",
  32. "import:math",
  33. "import:unicode/utf8",
  34. "project:go",
  35. "repres",
  36. "strconv",
  37. "string",
  38. "typ"},
  39. },
  40. {&doc.Package{
  41. ImportPath: "github.com/user/repo/dir",
  42. ProjectRoot: "github.com/user/repo",
  43. ProjectName: "go-oauth",
  44. ProjectURL: "https://github.com/user/repo/",
  45. Name: "dir",
  46. Synopsis: "Package dir implements a subset of the OAuth client interface as defined in RFC 5849.",
  47. Doc: "Package oauth implements a subset of the OAuth client interface as defined in RFC 5849.\n\n" +
  48. "This package assumes that the application writes request URL paths to the\nnetwork using " +
  49. "the encoding implemented by the net/url URL RequestURI method.\n" +
  50. "The HTTP client in the standard net/http package uses this encoding.",
  51. IsCmd: false,
  52. Imports: []string{
  53. "bytes",
  54. "crypto/hmac",
  55. "crypto/sha1",
  56. "encoding/base64",
  57. "encoding/binary",
  58. "errors",
  59. "fmt",
  60. "io",
  61. "io/ioutil",
  62. "net/http",
  63. "net/url",
  64. "regexp",
  65. "sort",
  66. "strconv",
  67. "strings",
  68. "sync",
  69. "time",
  70. },
  71. TestImports: []string{"bytes", "net/url", "testing"},
  72. Funcs: []*doc.Func{{}},
  73. },
  74. []string{
  75. "all:",
  76. "5849", "cly", "defin", "dir", "github.com", "go",
  77. "import:bytes", "import:crypto/hmac", "import:crypto/sha1",
  78. "import:encoding/base64", "import:encoding/binary", "import:errors",
  79. "import:fmt", "import:io", "import:io/ioutil", "import:net/http",
  80. "import:net/url", "import:regexp", "import:sort", "import:strconv",
  81. "import:strings", "import:sync", "import:time", "interfac",
  82. "oau", "project:github.com/user/repo", "repo", "rfc", "subset", "us",
  83. },
  84. },
  85. }
  86. func TestDocTerms(t *testing.T) {
  87. for _, tt := range indexTests {
  88. score := documentScore(tt.pdoc)
  89. terms := documentTerms(tt.pdoc, score)
  90. sort.Strings(terms)
  91. sort.Strings(tt.terms)
  92. if !reflect.DeepEqual(terms, tt.terms) {
  93. t.Errorf("documentTerms(%s) ->\n got: %#v\nwant: %#v", tt.pdoc.ImportPath, terms, tt.terms)
  94. }
  95. }
  96. }
  97. var vendorPatTests = []struct {
  98. path string
  99. match bool
  100. }{
  101. {"camlistore.org/third_party/github.com/user/repo", true},
  102. {"camlistore.org/third_party/dir", false},
  103. {"camlistore.org/third_party", false},
  104. {"camlistore.org/xthird_party/github.com/user/repo", false},
  105. {"camlistore.org/third_partyx/github.com/user/repo", false},
  106. {"example.org/_third_party/github.com/user/repo/dir", true},
  107. {"example.org/_third_party/dir", false},
  108. {"github.com/user/repo/Godeps/_workspace/src/github.com/user/repo", true},
  109. {"github.com/user/repo/Godeps/_workspace/src/dir", false},
  110. {"github.com/user/repo", false},
  111. }
  112. func TestVendorPat(t *testing.T) {
  113. for _, tt := range vendorPatTests {
  114. match := vendorPat.MatchString(tt.path)
  115. if match != tt.match {
  116. t.Errorf("match(%q) = %v, want %v", tt.path, match, match)
  117. }
  118. }
  119. }
  120. var synopsisTermTests = []struct {
  121. synopsis string
  122. terms []string
  123. }{
  124. {
  125. "Package foo implements bar.",
  126. []string{"bar", "foo"},
  127. },
  128. {
  129. "Package foo provides bar.",
  130. []string{"bar", "foo"},
  131. },
  132. {
  133. "The foo package provides bar.",
  134. []string{"bar", "foo"},
  135. },
  136. {
  137. "Package foo contains an implementation of bar.",
  138. []string{"bar", "foo", "impl"},
  139. },
  140. {
  141. "Package foo is awesome",
  142. []string{"awesom", "foo"},
  143. },
  144. {
  145. "The foo package is awesome",
  146. []string{"awesom", "foo"},
  147. },
  148. {
  149. "The foo command is awesome",
  150. []string{"awesom", "foo"},
  151. },
  152. {
  153. "Command foo is awesome",
  154. []string{"awesom", "foo"},
  155. },
  156. {
  157. "The foo package",
  158. []string{"foo"},
  159. },
  160. {
  161. "Package foo",
  162. []string{"foo"},
  163. },
  164. {
  165. "Command foo",
  166. []string{"foo"},
  167. },
  168. {
  169. "Package",
  170. []string{},
  171. },
  172. {
  173. "Command",
  174. []string{},
  175. },
  176. }
  177. func TestSynopsisTerms(t *testing.T) {
  178. for _, tt := range synopsisTermTests {
  179. terms := make(map[string]bool)
  180. collectSynopsisTerms(terms, tt.synopsis)
  181. actual := termSlice(terms)
  182. expected := tt.terms
  183. sort.Strings(actual)
  184. sort.Strings(expected)
  185. if !reflect.DeepEqual(actual, expected) {
  186. t.Errorf("%q ->\n got: %#v\nwant: %#v", tt.synopsis, actual, expected)
  187. }
  188. }
  189. }