a simple go tool vanity url server.
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.

vain_test.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package vain
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. )
  7. func TestString(t *testing.T) {
  8. p := Package{
  9. Vcs: "git",
  10. Path: "mcquay.me/bps",
  11. Repo: "https://s.mcquay.me/sm/bps",
  12. }
  13. got := fmt.Sprintf("%s", p)
  14. want := `<meta name="go-import" content="mcquay.me/bps git https://s.mcquay.me/sm/bps">`
  15. if got != want {
  16. t.Errorf(
  17. "incorrect converstion to meta; got %s, want %s",
  18. got,
  19. want,
  20. )
  21. }
  22. }
  23. func TestSupportedVcsStrings(t *testing.T) {
  24. tests := []struct {
  25. in string
  26. want bool
  27. }{
  28. {"hg", true},
  29. {"git", true},
  30. {"bzr", true},
  31. {"", false},
  32. {"bazar", false},
  33. {"mercurial", false},
  34. }
  35. for _, test := range tests {
  36. if got, want := valid(test.in), test.want; got != want {
  37. t.Errorf("%s: %t is incorrect validity", test.in, got)
  38. }
  39. }
  40. }
  41. func TestValid(t *testing.T) {
  42. tests := []struct {
  43. pkgs []Package
  44. in string
  45. want bool
  46. }{
  47. {
  48. pkgs: []Package{},
  49. in: "bobo",
  50. want: true,
  51. },
  52. {
  53. pkgs: []Package{
  54. {Path: "bobo"},
  55. },
  56. in: "bobo",
  57. want: false,
  58. },
  59. {
  60. pkgs: []Package{
  61. {Path: "a/b/c"},
  62. },
  63. in: "a/b/c",
  64. want: false,
  65. },
  66. {
  67. pkgs: []Package{
  68. {Path: "a/b/c"},
  69. },
  70. in: "a/b",
  71. want: false,
  72. },
  73. {
  74. pkgs: []Package{
  75. {Path: "name/db"},
  76. {Path: "name/lib"},
  77. },
  78. in: "name/foo",
  79. want: true,
  80. },
  81. {
  82. pkgs: []Package{
  83. {Path: "a"},
  84. },
  85. in: "a/b",
  86. want: false,
  87. },
  88. {
  89. pkgs: []Package{
  90. {Path: "foo"},
  91. },
  92. in: "foo/bar",
  93. want: false,
  94. },
  95. {
  96. pkgs: []Package{
  97. {Path: "foo/bar"},
  98. {Path: "foo/baz"},
  99. },
  100. in: "foo",
  101. want: false,
  102. },
  103. {
  104. pkgs: []Package{
  105. {Path: "bilbo"},
  106. {Path: "frodo"},
  107. },
  108. in: "foo/bar/baz",
  109. want: true,
  110. },
  111. {
  112. pkgs: []Package{
  113. {Path: "a/b"},
  114. },
  115. in: "a/bb",
  116. want: true,
  117. },
  118. }
  119. for _, test := range tests {
  120. got := Valid(test.in, test.pkgs)
  121. if got != test.want {
  122. t.Errorf("Incorrect testing of %q against %#v; got %t, want %t", test.in, test.pkgs, got, test.want)
  123. }
  124. }
  125. }
  126. func TestNamespaceParsing(t *testing.T) {
  127. tests := []struct {
  128. input string
  129. want namespace
  130. err error
  131. }{
  132. {
  133. input: "/sm/foo",
  134. want: "sm",
  135. },
  136. {
  137. input: "/a/b/c/d",
  138. want: "a",
  139. },
  140. {
  141. input: "/dm/bar",
  142. want: "dm",
  143. },
  144. {
  145. input: "/ud",
  146. want: "ud",
  147. },
  148. // test stripping
  149. {
  150. input: "ud",
  151. want: "ud",
  152. },
  153. {
  154. input: "/",
  155. err: errors.New("should find no namespace"),
  156. },
  157. {
  158. input: "",
  159. err: errors.New("should find no namespace"),
  160. },
  161. }
  162. for _, test := range tests {
  163. got, err := parseNamespace(test.input)
  164. if err != nil && test.err == nil {
  165. t.Errorf("unexpected error parsing %q; got %q, want %q, error: %v", test.input, got, test.want, err)
  166. }
  167. if got != test.want {
  168. t.Errorf("parse failure: got %q, want %q", got, test.want)
  169. }
  170. }
  171. }