path_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package amp
  2. import (
  3. "testing"
  4. )
  5. func TestDecodePath(t *testing.T) {
  6. for _, test := range []struct {
  7. path string
  8. expectedData string
  9. expectedErrStr string
  10. }{
  11. {"", "", "missing format indicator"},
  12. {"0", "", "missing data"},
  13. {"0foobar", "", "missing data"},
  14. {"/0/YWJj", "", "unknown format indicator '/'"},
  15. {"0/", "", ""},
  16. {"0foobar/", "", ""},
  17. {"0/YWJj", "abc", ""},
  18. {"0///YWJj", "abc", ""},
  19. {"0foobar/YWJj", "abc", ""},
  20. {"0/foobar/YWJj", "abc", ""},
  21. } {
  22. data, err := DecodePath(test.path)
  23. if test.expectedErrStr != "" {
  24. if err == nil || err.Error() != test.expectedErrStr {
  25. t.Errorf("%+q expected error %+q, got %+q",
  26. test.path, test.expectedErrStr, err)
  27. }
  28. } else if err != nil {
  29. t.Errorf("%+q expected no error, got %+q", test.path, err)
  30. } else if string(data) != test.expectedData {
  31. t.Errorf("%+q expected data %+q, got %+q",
  32. test.path, test.expectedData, data)
  33. }
  34. }
  35. }
  36. func TestPathRoundTrip(t *testing.T) {
  37. for _, data := range []string{
  38. "",
  39. "\x00",
  40. "/",
  41. "hello world",
  42. } {
  43. decoded, err := DecodePath(EncodePath([]byte(data)))
  44. if err != nil {
  45. t.Errorf("%+q roundtripped with error %v", data, err)
  46. } else if string(decoded) != data {
  47. t.Errorf("%+q roundtripped to %+q", data, decoded)
  48. }
  49. }
  50. }