github.com/pingcap/failpoint@v0.0.0-20240412033321-fd0796e60f86/terms_test.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     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.
    14  
    15  // Copyright 2016 CoreOS, Inc.
    16  //
    17  // Licensed under the Apache License, Version 2.0 (the "License");
    18  // you may not use this file except in compliance with the License.
    19  // You may obtain a copy of the License at
    20  //
    21  //     http://www.apache.org/licenses/LICENSE-2.0
    22  //
    23  // Unless required by applicable law or agreed to in writing, software
    24  // distributed under the License is distributed on an "AS IS" BASIS,
    25  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    26  // See the License for the specific language governing permissions and
    27  // limitations under the License.
    28  
    29  package failpoint
    30  
    31  import (
    32  	"reflect"
    33  	"testing"
    34  )
    35  
    36  func TestTermsString(t *testing.T) {
    37  	tests := []struct {
    38  		desc  string
    39  		weval []string
    40  	}{
    41  		{`off`, []string{""}},
    42  		{`2*return("abc")`, []string{"abc", "abc", ""}},
    43  		{`0%return("abc")`, []string{"", "", ""}},
    44  		{`100%return("abc")`, []string{"abc", "abc", "abc"}},
    45  		{`100.0%return("abc")`, []string{"abc", "abc", "abc"}},
    46  		{`100%2*return("abc")`, []string{"abc", "abc", ""}},
    47  		{`2*return("abc")->1*return("def")`, []string{"abc", "abc", "def", ""}},
    48  		{`1*return("abc")->return("def")`, []string{"abc", "def", "def"}},
    49  	}
    50  	for _, tt := range tests {
    51  		ter, err := newTerms(tt.desc, nil)
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  		for _, w := range tt.weval {
    56  			v, err := ter.eval()
    57  			if v == nil && w == "" {
    58  				continue
    59  			}
    60  			if err != nil {
    61  				continue
    62  			}
    63  			if v.(string) != w {
    64  				t.Fatalf("got %q, expected %q", v, w)
    65  			}
    66  		}
    67  	}
    68  }
    69  
    70  func TestTermsTypes(t *testing.T) {
    71  	tests := []struct {
    72  		desc  string
    73  		weval interface{}
    74  	}{
    75  		{`off`, nil},
    76  		{`return("abc")`, "abc"},
    77  		{`return(true)`, true},
    78  		{`return(1)`, 1},
    79  		{`return()`, struct{}{}},
    80  	}
    81  	for _, tt := range tests {
    82  		ter, err := newTerms(tt.desc, nil)
    83  		if err != nil {
    84  			t.Fatal(err)
    85  		}
    86  		v, _ := ter.eval()
    87  		if v == nil && tt.weval == nil {
    88  			continue
    89  		}
    90  		if !reflect.DeepEqual(v, tt.weval) {
    91  			t.Fatalf("got %v, expected %v", v, tt.weval)
    92  		}
    93  	}
    94  }