aboutsummaryrefslogtreecommitdiff
path: root/scfg/reader_test.go
blob: 5a4af289839be25f6e04c313c2506f499679a72b (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package scfg

import (
	"fmt"
	"reflect"
	"strings"
	"testing"
)

var readTests = []struct {
	name string
	src  string
	want Block
}{
	{
		name: "flat",
		src: `dir1 param1 param2 "" param3
dir2
dir3 param1

# comment
dir4 "param 1" 'param 2'`,
		want: Block{
			{Name: "dir1", Params: []string{"param1", "param2", "", "param3"}},
			{Name: "dir2", Params: []string{}},
			{Name: "dir3", Params: []string{"param1"}},
			{Name: "dir4", Params: []string{"param 1", "param 2"}},
		},
	},
	{
		name: "simpleBlocks",
		src: `block1 {
	dir1 param1 param2
	dir2 param1
}

block2 {
}

block3 {
	# comment
}

block4 param1 "param2" {
	dir1
}`,
		want: Block{
			{
				Name:   "block1",
				Params: []string{},
				Children: Block{
					{Name: "dir1", Params: []string{"param1", "param2"}},
					{Name: "dir2", Params: []string{"param1"}},
				},
			},
			{Name: "block2", Params: []string{}, Children: Block{}},
			{Name: "block3", Params: []string{}, Children: Block{}},
			{
				Name:   "block4",
				Params: []string{"param1", "param2"},
				Children: Block{
					{Name: "dir1", Params: []string{}},
				},
			},
		},
	},
	{
		name: "nested",
		src: `block1 {
	block2 {
		dir1 param1
	}

	block3 {
	}
}

block4 {
	block5 {
		block6 param1 {
			dir1
		}
	}

	dir1
}`,
		want: Block{
			{
				Name:   "block1",
				Params: []string{},
				Children: Block{
					{
						Name:   "block2",
						Params: []string{},
						Children: Block{
							{Name: "dir1", Params: []string{"param1"}},
						},
					},
					{
						Name:     "block3",
						Params:   []string{},
						Children: Block{},
					},
				},
			},
			{
				Name:   "block4",
				Params: []string{},
				Children: Block{
					{
						Name:   "block5",
						Params: []string{},
						Children: Block{{
							Name:   "block6",
							Params: []string{"param1"},
							Children: Block{{
								Name:   "dir1",
								Params: []string{},
							}},
						}},
					},
					{
						Name:   "dir1",
						Params: []string{},
					},
				},
			},
		},
	},
	{
		name: "quotes",
		src:  `"a \b ' \" c" 'd \e \' " f' a\"b`,
		want: Block{
			{Name: "a b ' \" c", Params: []string{"d e ' \" f", "a\"b"}},
		},
	},
	{
		name: "quotes-2",
		src:  `dir arg1 "arg2" ` + `\"\"`,
		want: Block{
			{Name: "dir", Params: []string{"arg1", "arg2", "\"\""}},
		},
	},
	{
		name: "quotes-3",
		src:  `dir arg1 "\"\"\"\"" arg3`,
		want: Block{
			{Name: "dir", Params: []string{"arg1", `"` + "\"\"" + `"`, "arg3"}},
		},
	},
}

func TestRead(t *testing.T) {
	for _, test := range readTests {
		t.Run(test.name, func(t *testing.T) {
			r := strings.NewReader(test.src)
			got, err := Read(r)
			if err != nil {
				t.Fatalf("Read() = %v", err)
			}
			stripLineno(got)
			if !reflect.DeepEqual(got, test.want) {
				t.Error(fmt.Sprintf("Read() = %#v but want %#v", got, test.want))
			}
		})
	}
}

func stripLineno(block Block) {
	for _, dir := range block {
		dir.lineno = 0
		stripLineno(dir.Children)
	}
}