aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/kconfig/expr_test.go
blob: 6ae2701122d0db8ea49db3a061aaa0885dc5ce51 (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
92
93
94
95
96
97
98
// 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 (
	"fmt"
	"reflect"
	"testing"
)

func TestParseExpr(t *testing.T) {
	type Test struct {
		in   string
		out  string
		deps map[string]bool
		err  bool
	}
	tests := []Test{
		{
			in:  ` `,
			err: true,
		},
		{
			in:   `A`,
			out:  `A`,
			deps: map[string]bool{"A": true},
		},
		{
			in:   `A=B`,
			out:  `(A = B)`,
			deps: map[string]bool{"A": true, "B": true},
		},
		{
			in:   `!A && B`,
			out:  `(!(A) && B)`,
			deps: map[string]bool{"B": true},
		},
		{
			in:  `$(A "B")`,
			out: `$(A "B")`,
		},
		{
			in:  `"A"`,
			out: `"A"`,
		},
		{
			in:  `A||B&&C`,
			out: `(A || (B && C))`,
		},
	}
	for i, test := range tests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			t.Logf("input: %v", test.in)
			in := test.in
			if !test.err {
				in += " Z"
			}
			p := newParser([]byte(in), "file")
			if !p.nextLine() {
				t.Fatal("nextLine failed")
			}
			ex := p.parseExpr()
			if test.err {
				if p.err == nil {
					t.Fatal("not failed")
				}
				return
			}
			if p.err != nil {
				t.Fatalf("failed: %v", p.err)
			}
			if ex.String() != test.out {
				t.Fatalf("\ngot:  %q\nwant: %q", ex, test.out)
			}
			deps := make(map[string]bool)
			ex.collectDeps(deps)
			if len(deps) != 0 && len(test.deps) != 0 && !reflect.DeepEqual(deps, test.deps) {
				t.Fatalf("\ndeps: %v\nwant: %v", deps, test.deps)
			}
			if p.Ident() != "Z" {
				t.Fatal("parsing consumed unrelated token")
			}
		})
	}
}

func TestFuzzParseExpr(t *testing.T) {
	for _, data := range []string{
		``,
		`A`,
		`A = B`,
		`A || B && C`,
		`$(A"B")`,
	} {
		FuzzParseExpr([]byte(data)[:len(data):len(data)])
	}
}