File: decode_test.go

package info (click to toggle)
golang-pault-go-debian 0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 332 kB
  • ctags: 335
  • sloc: makefile: 3
file content (117 lines) | stat: -rw-r--r-- 2,738 bytes parent folder | download
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
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"`
	}
}

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
`)))
}