github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/utilts_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "testing" 10 11 "github.com/GuanceCloud/platypus/pkg/ast" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestReIndexFuncArgs(t *testing.T) { 16 cases := []struct { 17 name string 18 keyList []string 19 reqParm int 20 fnArgs *ast.CallExpr 21 exp *ast.CallExpr 22 fail bool 23 }{ 24 { 25 name: "t1", 26 keyList: []string{"a", "b", "c"}, 27 reqParm: 2, 28 fnArgs: &ast.CallExpr{ 29 Param: ast.Stmts{ 30 ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), 31 ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), 32 }, 33 }, 34 exp: &ast.CallExpr{ 35 Param: ast.Stmts{ 36 ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), 37 ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), 38 nil, 39 }, 40 }, 41 }, 42 { 43 name: "t1-fail-pos-after-kw", 44 keyList: []string{"a", "b", "c"}, 45 reqParm: 2, 46 fnArgs: &ast.CallExpr{ 47 Param: ast.Stmts{ 48 ast.WrapAssignmentStmt(&ast.AssignmentExpr{ 49 LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), 50 RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 51 }), 52 ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), 53 }, 54 }, 55 fail: true, 56 }, 57 { 58 name: "t1-fail-key-not-exist", 59 keyList: []string{"a", "b", "c"}, 60 reqParm: 2, 61 fnArgs: &ast.CallExpr{ 62 Param: []*ast.Node{ 63 ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), 64 ast.WrapAssignmentStmt( 65 &ast.AssignmentExpr{ 66 LHS: ast.WrapIdentifier(&ast.Identifier{Name: "x"}), 67 RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 68 }, 69 ), 70 }, 71 }, 72 fail: true, 73 }, 74 { 75 name: "t1-nil-param", 76 keyList: []string{"a", "b", "c"}, 77 reqParm: 2, 78 fnArgs: &ast.CallExpr{ 79 Param: []*ast.Node{ 80 ast.WrapAssignmentStmt( 81 &ast.AssignmentExpr{ 82 LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), 83 RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 84 }, 85 ), 86 }, 87 }, 88 fail: true, 89 }, 90 { 91 name: "t1-1", 92 keyList: []string{"a", "b", "c"}, 93 reqParm: 2, 94 fnArgs: &ast.CallExpr{ 95 Param: []*ast.Node{ 96 ast.WrapAssignmentStmt( 97 &ast.AssignmentExpr{ 98 LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), 99 RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 100 }, 101 ), 102 ast.WrapAssignmentStmt( 103 &ast.AssignmentExpr{ 104 LHS: ast.WrapIdentifier(&ast.Identifier{Name: "a"}), 105 RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), 106 }, 107 ), 108 }, 109 }, 110 exp: &ast.CallExpr{ 111 Param: []*ast.Node{ 112 ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), 113 ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 114 nil, 115 }, 116 }, 117 }, 118 { 119 name: "t2", 120 keyList: []string{"a", "b", "c"}, 121 reqParm: -1, 122 fnArgs: &ast.CallExpr{ 123 Param: []*ast.Node{ 124 ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), 125 ast.WrapAssignmentStmt( 126 &ast.AssignmentExpr{ 127 LHS: ast.WrapIdentifier(&ast.Identifier{Name: "c"}), 128 RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 129 }, 130 ), 131 ast.WrapAssignmentStmt( 132 &ast.AssignmentExpr{ 133 LHS: ast.WrapIdentifier(&ast.Identifier{Name: "b"}), 134 RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), 135 }, 136 ), 137 }, 138 }, 139 exp: &ast.CallExpr{ 140 Param: []*ast.Node{ 141 ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), 142 ast.WrapBoolLiteral(&ast.BoolLiteral{Val: false}), 143 ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 144 }, 145 }, 146 }, 147 { 148 name: "t3", 149 keyList: []string{"a", "b", "c"}, 150 reqParm: 1, 151 fnArgs: &ast.CallExpr{ 152 Param: []*ast.Node{ 153 ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), 154 ast.WrapAssignmentStmt( 155 &ast.AssignmentExpr{ 156 LHS: ast.WrapIdentifier(&ast.Identifier{Name: "c"}), 157 RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 158 }, 159 ), 160 }, 161 }, 162 exp: &ast.CallExpr{ 163 Param: []*ast.Node{ 164 ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), 165 nil, 166 ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 167 }, 168 }, 169 }, 170 { 171 name: "t3-fail", 172 keyList: []string{"a"}, 173 reqParm: 1, 174 fnArgs: &ast.CallExpr{ 175 Param: []*ast.Node{ 176 ast.WrapIdentifier(&ast.Identifier{Name: "p1"}), 177 ast.WrapAssignmentStmt( 178 &ast.AssignmentExpr{ 179 LHS: ast.WrapIdentifier(&ast.Identifier{Name: "c"}), 180 RHS: ast.WrapBoolLiteral(&ast.BoolLiteral{Val: true}), 181 }, 182 ), 183 }, 184 }, 185 fail: true, 186 }, 187 } 188 189 for _, v := range cases { 190 t.Run(v.name, func(t *testing.T) { 191 err := normalizeFuncArgsDeprecated(v.fnArgs, v.keyList, v.reqParm) 192 if err != nil { 193 if !v.fail { 194 t.Error(err) 195 } 196 return 197 } 198 for i, p := range v.fnArgs.Param { 199 assert.Equal(t, v.exp.Param[i], p) 200 } 201 }) 202 } 203 }