File: decode_test.go

package info (click to toggle)
golang-pault-go-debian 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 412 kB
  • sloc: makefile: 2
file content (135 lines) | stat: -rw-r--r-- 3,202 bytes parent folder | download | duplicates (3)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package control_test

import (
	"strings"
	"testing"

	"pault.ag/go/debian/control"
	"pault.ag/go/debian/dependency"
	"pault.ag/go/debian/version"
)

type TestStruct struct {
	Value      string `required:"true"`
	ValueTwo   string `control:"Value-Two"`
	ValueThree []string
	Depends    dependency.Dependency
	Version    version.Version
	Arch       dependency.Arch
	Arches     []dependency.Arch
	Fnord      struct {
		FooBar string `control:"Fnord-Foo-Bar"`
	}
	ExtraSourceOnly bool `control:"Extra-Source-Only"`
}

func TestBasicUnmarshal(t *testing.T) {
	foo := TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Foo-Bar: baz
`)))
	assert(t, foo.Value == "foo")
}

func TestBasicArrayUnmarshal(t *testing.T) {
	foo := []TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Foo-Bar: baz

Value: Bar

Value: Baz
`)))
	assert(t, len(foo) == 3)
	assert(t, foo[0].Value == "foo")
}

func TestTagUnmarshal(t *testing.T) {
	foo := TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Value-Two: baz
`)))
	assert(t, foo.Value == "foo")
	assert(t, foo.ValueTwo == "baz")
}

func TestDependsUnmarshal(t *testing.T) {
	foo := TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Depends: foo, bar
`)))
	assert(t, foo.Value == "foo")
	assert(t, foo.Depends.Relations[0].Possibilities[0].Name == "foo")

	/* Actually invalid below */
	notok(t, control.Unmarshal(&foo, strings.NewReader(`Depends: foo (>= 1.0) (<= 1.0)
`)))
}

func TestVersionUnmarshal(t *testing.T) {
	foo := TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Version: 1.0-1
`)))
	assert(t, foo.Value == "foo")
	assert(t, foo.Version.Revision == "1")
}

func TestArchUnmarshal(t *testing.T) {
	foo := TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Arch: amd64
`)))
	assert(t, foo.Value == "foo")
	assert(t, foo.Arch.CPU == "amd64")

	foo = TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Arches: amd64 sparc any
`)))
	assert(t, foo.Value == "foo")
	assert(t, foo.Arches[0].CPU == "amd64")
	assert(t, foo.Arches[1].CPU == "sparc")
	assert(t, foo.Arches[2].CPU == "any")
}

func TestNestedUnmarshal(t *testing.T) {
	foo := TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Fnord-Foo-Bar: Thing
`)))
	assert(t, foo.Value == "foo")
	assert(t, foo.Fnord.FooBar == "Thing")
}

func TestListUnmarshal(t *testing.T) {
	foo := TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
ValueThree: foo bar baz
`)))
	assert(t, foo.Value == "foo")
	assert(t, foo.ValueThree[0] == "foo")
}

func TestRequiredUnmarshal(t *testing.T) {
	foo := TestStruct{}
	notok(t, control.Unmarshal(&foo, strings.NewReader(`Foo-Bar: baz
`)))
}

func TestBoolUnmarshal(t *testing.T) {
	foo := TestStruct{}
	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
`)))
	assert(t, !foo.ExtraSourceOnly)

	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Extra-Source-Only: no
`)))
	assert(t, !foo.ExtraSourceOnly)

	isok(t, control.Unmarshal(&foo, strings.NewReader(`Value: foo
Extra-Source-Only: yes
`)))
	assert(t, foo.ExtraSourceOnly)
}