aboutsummaryrefslogtreecommitdiff
path: root/objects/properties_test.go
blob: 7243ce343a1d31065e0e617908a051151087f315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package objects

import (
	"testing"
)

func TestPropEscape(t *testing.T) {
	tests := []struct{ name, in, want string }{
		{"empty", "", ""},
		{"no escape", "foo:bar_BAZ-123", "foo:bar_BAZ-123"},
		{"reserved chars", "foo=bar%baz%%=", "foo%3dbar%25baz%25%25%3d"},
	}

	for _, subtest := range tests {
		have := string(escapePropertyString(subtest.in))
		if have != subtest.want {
			t.Errorf("%s: want: '%s', have: '%s'", subtest.name, subtest.want, have)
		}
	}
}

func TestPropertyMarshalling(t *testing.T) {
	tests := []struct {
		name string
		in   Properties
		want string
	}{
		{"empty", Properties{}, ""},
		{"single", Properties{"foo": "bar"}, "foo=bar"},
		{"simple", Properties{"foo": "bar", "bar": "baz"}, "bar=baz&foo=bar"},
		{"escapes", Properties{"foo&bar": "%=baz", "?": "!"}, "%3f=%21&foo%26bar=%25%3dbaz"},
	}

	for _, subtest := range tests {
		have, err := subtest.in.MarshalText()
		if err != nil {
			t.Errorf("%s: Got an error: %s", err)
			continue
		}

		if string(have) != subtest.want {
			t.Errorf("%s: want: '%s', have: '%s'", subtest.name, subtest.want, have)
		}
	}
}

func TestPropertyUnmarshalling(t *testing.T) {
	tests := []struct {
		name string
		in   string
		want Properties
	}{
		{"empty", "", Properties{}},
		{"single", "foo=bar", Properties{"foo": "bar"}},
		{"simple", "bar=baz&foo=bar", Properties{"foo": "bar", "bar": "baz"}},
		{"escapes", "%3f=%21&foo%26bar=%25%3dbaz", Properties{"foo&bar": "%=baz", "?": "!"}},
	}

	for _, subtest := range tests {
		have := make(Properties)
		err := have.UnmarshalText([]byte(subtest.in))

		if err != nil {
			t.Errorf("%s: Got an error: %s", err)
			continue
		}

		if !have.Equals(subtest.want) {
			t.Errorf("%s: want: '%v', have: '%v'", subtest.name, subtest.want, have)
		}
	}
}