github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/kconfig/expr_test.go (about)

     1  // Copyright 2020 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package kconfig
     5  
     6  import (
     7  	"fmt"
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  func TestParseExpr(t *testing.T) {
    13  	type Test struct {
    14  		in   string
    15  		out  string
    16  		deps map[string]bool
    17  		err  bool
    18  	}
    19  	tests := []Test{
    20  		{
    21  			in:  ` `,
    22  			err: true,
    23  		},
    24  		{
    25  			in:   `A`,
    26  			out:  `A`,
    27  			deps: map[string]bool{"A": true},
    28  		},
    29  		{
    30  			in:   `A=B`,
    31  			out:  `(A = B)`,
    32  			deps: map[string]bool{"A": true, "B": true},
    33  		},
    34  		{
    35  			in:   `!A && B`,
    36  			out:  `(!(A) && B)`,
    37  			deps: map[string]bool{"B": true},
    38  		},
    39  		{
    40  			in:  `$(A "B")`,
    41  			out: `$(A "B")`,
    42  		},
    43  		{
    44  			in:  `"A"`,
    45  			out: `"A"`,
    46  		},
    47  		{
    48  			in:  `A||B&&C`,
    49  			out: `(A || (B && C))`,
    50  		},
    51  	}
    52  	for i, test := range tests {
    53  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    54  			t.Logf("input: %v", test.in)
    55  			in := test.in
    56  			if !test.err {
    57  				in += " Z"
    58  			}
    59  			p := newParser([]byte(in), "file")
    60  			if !p.nextLine() {
    61  				t.Fatal("nextLine failed")
    62  			}
    63  			ex := p.parseExpr()
    64  			if test.err {
    65  				if p.err == nil {
    66  					t.Fatal("not failed")
    67  				}
    68  				return
    69  			}
    70  			if p.err != nil {
    71  				t.Fatalf("failed: %v", p.err)
    72  			}
    73  			if ex.String() != test.out {
    74  				t.Fatalf("\ngot:  %q\nwant: %q", ex, test.out)
    75  			}
    76  			deps := make(map[string]bool)
    77  			ex.collectDeps(deps)
    78  			if len(deps) != 0 && len(test.deps) != 0 && !reflect.DeepEqual(deps, test.deps) {
    79  				t.Fatalf("\ndeps: %v\nwant: %v", deps, test.deps)
    80  			}
    81  			if p.Ident() != "Z" {
    82  				t.Fatal("parsing consumed unrelated token")
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestFuzzParseExpr(t *testing.T) {
    89  	for _, data := range []string{
    90  		``,
    91  		`A`,
    92  		`A = B`,
    93  		`A || B && C`,
    94  		`$(A"B")`,
    95  	} {
    96  		FuzzParseExpr([]byte(data)[:len(data):len(data)])
    97  	}
    98  }