xref: /aosp_15_r20/external/bazelbuild-rules_android/src/common/golang/ini_test.go (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package ini
15
16import (
17	"bytes"
18	"reflect"
19	"strings"
20	"testing"
21)
22
23func TestParseFunc(t *testing.T) {
24	tests := []struct {
25		name string
26		in   string
27		want map[string]string
28	}{
29		{
30			name: "ini_single_line",
31			in:   "test=abc",
32			want: map[string]string{"test": "abc"},
33		},
34		{
35			name: "ini_multi_line",
36			in: `key=data
37key2=more data`,
38			want: map[string]string{"key": "data", "key2": "more data"},
39		},
40		{
41			name: "ini_with_comment",
42			in: `key=data
43;key2=irrelevant data
44#key3=more irrelevant data`,
45			want: map[string]string{"key": "data"},
46		},
47		{
48			name: "ini_with_whitespace",
49			in: `key = data
50another_key = The data
51yet_another_key	=	more data`,
52			want: map[string]string{"key": "data", "another_key": "The data", "yet_another_key": "more data"},
53		},
54		{
55			name: "ini_with_empty_data",
56			in: `key=data
57key2=
58key3=more data`,
59			want: map[string]string{"key": "data", "key2": "", "key3": "more data"},
60		},
61		{
62			name: "invalid_ini",
63			in: `key=data
64invalid line
65key2=The data`,
66			want: map[string]string{"key": "data", "key2": "The data"},
67		},
68		{
69			name: "ini_with_duplicate",
70			in: `key=data
71key=duplicate`,
72			want: map[string]string{"key": "duplicate"},
73		},
74	}
75	for _, test := range tests {
76		t.Run(test.name, func(t *testing.T) {
77			iniOut := parse(test.in)
78			if eq := reflect.DeepEqual(iniOut, test.want); !eq {
79				t.Errorf("Parsing ini failed for: %q got: %v wanted: %v", test.in, iniOut, test.want)
80			}
81		})
82	}
83}
84
85func TestWriteFunc(t *testing.T) {
86	tests := []struct {
87		name string
88		in   map[string]string
89		want string
90	}{
91		{
92			name: "ini_single_line",
93			in:   map[string]string{"test": "abc"},
94			want: "test=abc\n",
95		},
96		{
97			name: "ini_multi_line",
98			in:   map[string]string{"key": "data", "key2": "more data"},
99			want: `key=data
100key2=more data
101`,
102		},
103	}
104	for _, test := range tests {
105		t.Run(test.name, func(t *testing.T) {
106			b := new(bytes.Buffer)
107			write(b, test.in)
108			if strings.Compare(b.String(), test.want) != 0 {
109				t.Errorf("Writing ini failed for: %q got: %v wanted: %v", test.in, b.String(), test.want)
110			}
111		})
112	}
113}
114