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