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.
 
 
 

115 rivejä
3.2 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 doc
  7. import (
  8. "go/ast"
  9. "testing"
  10. )
  11. var badSynopsis = []string{
  12. "+build !release",
  13. "COPYRIGHT Jimmy Bob",
  14. "### Markdown heading",
  15. "-*- indent-tabs-mode: nil -*-",
  16. "vim:set ts=2 sw=2 et ai ft=go:",
  17. }
  18. func TestBadSynopsis(t *testing.T) {
  19. for _, s := range badSynopsis {
  20. if synopsis(s) != "" {
  21. t.Errorf(`synopsis(%q) did not return ""`, s)
  22. }
  23. }
  24. }
  25. const readme = `
  26. $ go get github.com/user/repo/pkg1
  27. [foo](http://gopkgdoc.appspot.com/pkg/github.com/user/repo/pkg2)
  28. [foo](http://go.pkgdoc.org/github.com/user/repo/pkg3)
  29. [foo](http://godoc.org/github.com/user/repo/pkg4)
  30. <http://go.pkgdoc.org/github.com/user/repo/pkg5>
  31. [foo](http://godoc.org/github.com/user/repo/pkg6#Export)
  32. http://gowalker.org/github.com/user/repo/pkg7
  33. Build Status: [![Build Status](https://drone.io/github.com/user/repo1/status.png)](https://drone.io/github.com/user/repo1/latest)
  34. 'go get example.org/package1' will install package1.
  35. (http://go.pkgdoc.org/example.org/package2 "Package2's documentation on GoPkgDoc").
  36. import "example.org/package3"
  37. `
  38. var expectedReferences = []string{
  39. "github.com/user/repo/pkg1",
  40. "github.com/user/repo/pkg2",
  41. "github.com/user/repo/pkg3",
  42. "github.com/user/repo/pkg4",
  43. "github.com/user/repo/pkg5",
  44. "github.com/user/repo/pkg6",
  45. "github.com/user/repo/pkg7",
  46. "github.com/user/repo1",
  47. "example.org/package1",
  48. "example.org/package2",
  49. "example.org/package3",
  50. }
  51. func TestReferences(t *testing.T) {
  52. references := make(map[string]bool)
  53. addReferences(references, []byte(readme))
  54. for _, r := range expectedReferences {
  55. if !references[r] {
  56. t.Errorf("missing %s", r)
  57. }
  58. delete(references, r)
  59. }
  60. for r := range references {
  61. t.Errorf("extra %s", r)
  62. }
  63. }
  64. var simpleImporterTests = []struct {
  65. path string
  66. name string
  67. }{
  68. // Last element with .suffix removed.
  69. {"example.com/user/name.git", "name"},
  70. {"example.com/user/name.svn", "name"},
  71. {"example.com/user/name.hg", "name"},
  72. {"example.com/user/name.bzr", "name"},
  73. {"example.com/name.v0", "name"},
  74. {"example.com/user/repo/name.v11", "name"},
  75. // Last element with "go" prefix or suffix removed.
  76. {"github.com/user/go-name", "name"},
  77. {"github.com/user/go.name", "name"},
  78. {"github.com/user/name.go", "name"},
  79. {"github.com/user/name-go", "name"},
  80. // Special cases for popular repos.
  81. {"code.google.com/p/biogo.name", "name"},
  82. {"code.google.com/p/google-api-go-client/name/v3", "name"},
  83. // Use last element of path.
  84. {"example.com/user/name.other", "name.other"},
  85. {"example.com/.v0", ".v0"},
  86. {"example.com/user/repo.v2/name", "name"},
  87. {"github.com/user/namev0", "namev0"},
  88. {"github.com/user/goname", "goname"},
  89. {"github.com/user/namego", "namego"},
  90. {"github.com/user/name", "name"},
  91. {"name", "name"},
  92. {"user/name", "name"},
  93. }
  94. func TestSimpleImporter(t *testing.T) {
  95. for _, tt := range simpleImporterTests {
  96. m := make(map[string]*ast.Object)
  97. obj, _ := simpleImporter(m, tt.path)
  98. if obj.Name != tt.name {
  99. t.Errorf("simpleImporter(%q) = %q, want %q", tt.path, obj.Name, tt.name)
  100. }
  101. }
  102. }