...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/expr/expr_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/expr

     1  // Copyright 2021 Chaos Mesh Authors.
     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  
    16  package expr
    17  
    18  import "testing"
    19  
    20  func TestEvalBool(t *testing.T) {
    21  	type args struct {
    22  		expression string
    23  		env        map[string]interface{}
    24  	}
    25  	tests := []struct {
    26  		name    string
    27  		args    args
    28  		want    bool
    29  		wantErr bool
    30  	}{
    31  		{
    32  			name: "expect true",
    33  			args: args{
    34  				expression: "0 == 0",
    35  				env:        nil,
    36  			},
    37  			want:    true,
    38  			wantErr: false,
    39  		}, {
    40  			name: "expect false",
    41  			args: args{
    42  				expression: "0 != 0",
    43  				env:        nil,
    44  			},
    45  			want:    false,
    46  			wantErr: false,
    47  		}, {
    48  			name: "exitCode is 0",
    49  			args: args{
    50  				expression: "exitCode == 0",
    51  				env: map[string]interface{}{
    52  					"exitCode": 0,
    53  				},
    54  			},
    55  			want:    true,
    56  			wantErr: false,
    57  		}, {
    58  			name: "stdout is empty",
    59  			args: args{
    60  				expression: "len(stdout) == 0 && stdout == \"\"",
    61  				env: map[string]interface{}{
    62  					"stdout": "",
    63  				},
    64  			},
    65  			want:    true,
    66  			wantErr: false,
    67  		}, {
    68  			name: "embedded value assertion",
    69  			args: args{
    70  				expression: "obj.name == \"foo\"",
    71  				env: map[string]interface{}{
    72  					"obj": map[string]interface{}{
    73  						"name": "foo",
    74  					},
    75  				},
    76  			},
    77  			want:    true,
    78  			wantErr: false,
    79  		}, {
    80  			name: "not a bool expression",
    81  			args: args{
    82  				expression: "0",
    83  				env:        nil,
    84  			},
    85  			want:    false,
    86  			wantErr: true,
    87  		},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			got, err := EvalBool(tt.args.expression, tt.args.env)
    92  			if (err != nil) != tt.wantErr {
    93  				t.Errorf("EvalBool() error = %v, wantErr %v", err, tt.wantErr)
    94  				return
    95  			}
    96  			if got != tt.want {
    97  				t.Errorf("EvalBool() got = %v, want %v", got, tt.want)
    98  			}
    99  		})
   100  	}
   101  }
   102