File: encode_test.go

package info (click to toggle)
golang-pault-go-debian 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 360 kB
  • sloc: makefile: 2
file content (139 lines) | stat: -rw-r--r-- 2,456 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
136
137
138
139
package control_test

import (
	"bytes"
	"strings"
	"testing"

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

type TestMarshalStruct struct {
	Foo string
}

type SomeComplexStruct struct {
	control.Paragraph

	Version    version.Version
	Dependency dependency.Dependency
}

type TestParaMarshalStruct struct {
	control.Paragraph
	Foo string
}

func TestExtraMarshal(t *testing.T) {
	el := TestParaMarshalStruct{}

	isok(t, control.Unmarshal(&el, strings.NewReader(`Foo: test
X-A-Test: Foo
`)))

	assert(t, el.Foo == "test")

	writer := bytes.Buffer{}
	isok(t, control.Marshal(&writer, el))
	assert(t, writer.String() == `Foo: test
X-A-Test: Foo
`)
}

func TestBasicMarshal(t *testing.T) {
	testStruct := TestMarshalStruct{Foo: "Hello"}

	writer := bytes.Buffer{}
	err := control.Marshal(&writer, testStruct)
	isok(t, err)

	assert(t, writer.String() == `Foo: Hello
`)

	writer = bytes.Buffer{}
	err = control.Marshal(&writer, []TestMarshalStruct{
		testStruct,
	})
	isok(t, err)
	assert(t, writer.String() == `Foo: Hello
`)

	writer = bytes.Buffer{}
	err = control.Marshal(&writer, []TestMarshalStruct{
		testStruct,
		testStruct,
	})
	isok(t, err)

	assert(t, writer.String() == `Foo: Hello

Foo: Hello
`)
}

func TestExternalMarshal(t *testing.T) {
	testStruct := SomeComplexStruct{}
	isok(t, control.Unmarshal(&testStruct, strings.NewReader(`Version: 1.0-1
Dependency: foo, bar
X-Foo: bar

`)))
	writer := bytes.Buffer{}

	err := control.Marshal(&writer, testStruct)
	isok(t, err)

	assert(t, testStruct.Dependency.Relations[0].Possibilities[0].Name == "foo")

	assert(t, writer.String() == `Version: 1.0-1
Dependency: foo, bar
X-Foo: bar
`)
}

func TestMultilineMarshal(t *testing.T) {
	testStruct := TestMarshalStruct{Foo: `Hello
This
Is

A Test`}
	writer := bytes.Buffer{}

	err := control.Marshal(&writer, testStruct)
	isok(t, err)

	assert(t, writer.String() == `Foo: Hello
 This
 Is
 .
 A Test
`)
}

type boolStruct struct {
	ExtraSourceOnly bool `control:"Extra-Source-Only"`
}

func TestBoolMarshal(t *testing.T) {
	bs := boolStruct{ExtraSourceOnly: true}

	writer := bytes.Buffer{}
	err := control.Marshal(&writer, bs)
	isok(t, err)

	assert(t, writer.String() == `Extra-Source-Only: yes
`)

	bs = boolStruct{ExtraSourceOnly: false}

	writer = bytes.Buffer{}
	err = control.Marshal(&writer, bs)
	isok(t, err)

	assert(t, writer.String() == `Extra-Source-Only: no
`)
}

// vim: foldmethod=marker