...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package unit
16
17 import (
18 "bytes"
19 "io/ioutil"
20 "testing"
21 )
22
23 func TestDeserializeAndReserialize(t *testing.T) {
24 tests := []struct {
25 in string
26 wout string
27 }{
28 {
29 `[Service]
30 ExecStart=/bin/bash -c "while true; do echo \"ping\"; sleep 1; done"
31 `,
32 `[Service]
33 ExecStart=/bin/bash -c "while true; do echo \"ping\"; sleep 1; done"
34 `},
35 {
36 `[Unit]
37 Description= Unnecessarily wrapped \
38 words here`,
39 `[Unit]
40 Description=Unnecessarily wrapped \
41 words here
42 `,
43 },
44 {
45 `[Unit]
46 Description=Demo \
47
48 Requires=docker.service
49 `,
50 `[Unit]
51 Description=Demo \
52
53 Requires=docker.service
54 `,
55 },
56 {
57 `; comment alpha
58 # comment bravo
59 [Unit]
60 ; comment charlie
61 # comment delta
62 #Description=Foo
63 Description=Bar
64 ; comment echo
65 # comment foxtrot
66 `,
67 `[Unit]
68 Description=Bar
69 `},
70 }
71 for i, tt := range tests {
72 ds, err := Deserialize(bytes.NewBufferString(tt.in))
73 if err != nil {
74 t.Errorf("case %d: unexpected error parsing unit: %v", i, err)
75 continue
76 }
77 out, err := ioutil.ReadAll(Serialize(ds))
78 if err != nil {
79 t.Errorf("case %d: unexpected error serializing unit: %v", i, err)
80 continue
81 }
82 if g := string(out); g != tt.wout {
83 t.Errorf("case %d: incorrect output", i)
84 t.Logf("Expected:\n%#v", tt.wout)
85 t.Logf("Actual:\n%#v", g)
86 }
87 }
88 }
89
View as plain text