vcs_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package main
  2. import (
  3. "testing"
  4. )
  5. func isEqual(a, b []string) bool {
  6. if a == nil && b == nil {
  7. return true
  8. }
  9. if a == nil || b == nil {
  10. return false
  11. }
  12. if len(a) != len(b) {
  13. return false
  14. }
  15. for i := range a {
  16. if a[i] != b[i] {
  17. return false
  18. }
  19. }
  20. return true
  21. }
  22. func TestParsing(t *testing.T) {
  23. type source struct {
  24. URL string
  25. Branch string
  26. Protocols []string
  27. }
  28. urls := []string{
  29. "git+https://github.com/neovim/neovim.git",
  30. "git://github.com/jguer/yay.git#branch=master",
  31. "git://github.com/davidgiven/ack",
  32. "git://github.com/jguer/yay.git#tag=v3.440",
  33. "git://github.com/jguer/yay.git#commit=e5470c88c6e2f9e0f97deb4728659ffa70ef5d0c",
  34. "a+b+c+d+e+f://github.com/jguer/yay.git#branch=foo",
  35. }
  36. sources := []source{
  37. {"github.com/neovim/neovim.git", "HEAD", []string{"https"}},
  38. {"github.com/jguer/yay.git", "master", []string{"git"}},
  39. {"github.com/davidgiven/ack", "HEAD", []string{"git"}},
  40. {"", "", nil},
  41. {"", "", nil},
  42. {"", "", nil},
  43. }
  44. for n, url := range urls {
  45. url, branch, protocols := parseSource(url)
  46. compare := sources[n]
  47. if url != compare.URL ||
  48. branch != compare.Branch ||
  49. !isEqual(protocols, compare.Protocols) {
  50. t.Fatalf("Test %d failed: Expected: url=%+v branch=%+v protocols=%+v\ngot url=%+v branch=%+v protocols=%+v", n+1, compare.URL, compare.Branch, compare.Protocols, url, branch, protocols)
  51. }
  52. }
  53. }