aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/kconfig/minimize_test.go
blob: 877df89b5912ed4a3dfd1e28deeca3a7026c67c3 (plain)
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
// Copyright 2020 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package kconfig

import (
	"bytes"
	"fmt"
	"testing"
)

func TestMinimize(t *testing.T) {
	const (
		kconfig = `
mainmenu "test"
config A
config B
config C
config D
config I
config S
`
		baseConfig = `
CONFIG_A=y
CONFIG_I=1
`
		fullConfig = `
CONFIG_A=y
CONFIG_B=y
CONFIG_C=y
CONFIG_D=y
CONFIG_I=42
CONFIG_S="foo"
`
	)
	type Test struct {
		pred   func(*ConfigFile) (bool, error)
		result string
	}
	tests := []Test{
		{
			pred: func(cf *ConfigFile) (bool, error) {
				return true, nil
			},
			result: baseConfig,
		},
		{
			pred: func(cf *ConfigFile) (bool, error) {
				return false, nil
			},
			result: fullConfig,
		},
		{
			pred: func(cf *ConfigFile) (bool, error) {
				return cf.Value("C") != No, nil
			},
			result: `
CONFIG_A=y
CONFIG_I=42
CONFIG_S="foo"
CONFIG_C=y
`,
		},
	}
	kconf, err := ParseData([]byte(kconfig), "kconf")
	if err != nil {
		t.Fatal(err)
	}
	base, err := ParseConfigData([]byte(baseConfig), "base")
	if err != nil {
		t.Fatal(err)
	}
	full, err := ParseConfigData([]byte(fullConfig), "full")
	if err != nil {
		t.Fatal(err)
	}
	for i, test := range tests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			trace := new(bytes.Buffer)
			res, err := kconf.Minimize(base, full, test.pred, trace)
			t.Log(trace.String())
			if err != nil {
				t.Fatal(err)
			}
			result := string(res.Serialize())
			if result != test.result {
				t.Fatalf("got:\n%v\n\nwant:\n%s", result, test.result)
			}
		})
	}
}