github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/equity/compiler/checks_test.go (about)

     1  package compiler
     2  
     3  import "testing"
     4  
     5  func TestRequireAllParamsUsedInClauses(t *testing.T) {
     6  	clauses := []*Clause{
     7  		&Clause{
     8  			statements: []statement{
     9  				&verifyStatement{expr: varRef("foo")},
    10  				&verifyStatement{
    11  					expr: &binaryExpr{
    12  						left:  varRef("foo"),
    13  						right: varRef("bar"),
    14  					},
    15  				},
    16  				&lockStatement{
    17  					lockedAmount: varRef("10000"),
    18  					lockedAsset:  varRef("baz"),
    19  					program:      varRef("foo"),
    20  				},
    21  			},
    22  		},
    23  		&Clause{
    24  			statements: []statement{
    25  				&verifyStatement{expr: varRef("foo")},
    26  				&verifyStatement{
    27  					expr: &binaryExpr{
    28  						left:  varRef("foo"),
    29  						right: varRef("plugh"),
    30  					},
    31  				},
    32  				&lockStatement{
    33  					lockedAmount: varRef("20000"),
    34  					lockedAsset:  varRef("xyzzy"),
    35  					program:      varRef("foo"),
    36  				},
    37  			},
    38  		},
    39  	}
    40  
    41  	cases := []struct {
    42  		name   string
    43  		params []string
    44  		want   string
    45  	}{
    46  		{
    47  			name:   "contract param used in both clauses",
    48  			params: []string{"foo"},
    49  		},
    50  		{
    51  			name:   "contract param used in one clause",
    52  			params: []string{"bar"},
    53  		},
    54  		{
    55  			name:   "contract param used in no clauses",
    56  			params: []string{"y2"},
    57  			want:   "parameter \"y2\" is unused",
    58  		},
    59  	}
    60  	for _, c := range cases {
    61  		t.Run(c.name, func(t *testing.T) {
    62  			var params []*Param
    63  			for _, p := range c.params {
    64  				params = append(params, &Param{Name: p})
    65  			}
    66  			err := requireAllParamsUsedInClauses(params, clauses)
    67  			if err == nil && c.want == "" {
    68  				return
    69  			}
    70  			if err == nil {
    71  				t.Errorf("got err==nil, want %s", c.want)
    72  				return
    73  			}
    74  			if err.Error() != c.want {
    75  				t.Errorf("got %s, want %s", err, c.want)
    76  			}
    77  		})
    78  	}
    79  }