github.com/yandex/pandora@v0.5.32/components/providers/scenario/templater/func_test.go (about) 1 package templater 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestRandInt(t *testing.T) { 11 tests := []struct { 12 name string 13 args []any 14 want int 15 wantDelta float64 16 wantErr bool 17 }{ 18 { 19 name: "No args", 20 args: nil, 21 want: 15, 22 wantDelta: 15, 23 }, 24 { 25 name: "two args", 26 args: []any{10, 20}, 27 want: 15, 28 wantDelta: 5, 29 }, 30 { 31 name: "second arg is invalid", 32 args: []any{"26", "invalid"}, 33 wantErr: true, 34 }, 35 { 36 name: "two string args can be converted", 37 args: []any{"200", "300"}, 38 want: 250, 39 wantDelta: 50, 40 }, 41 { 42 name: "second arg is invalid", 43 args: []any{20, "invalid"}, 44 wantErr: true, 45 }, 46 { 47 name: "more than two args", 48 args: []any{100, 200, 30}, 49 wantErr: true, 50 }, 51 { 52 name: "one args", 53 args: []any{50}, 54 want: 25, 55 wantDelta: 25, 56 }, 57 { 58 name: "one arg", 59 args: []any{10}, 60 want: 5, 61 wantDelta: 5, 62 }, 63 { 64 name: "two args", 65 args: []any{-10, 10}, 66 want: 0, 67 wantDelta: 10, 68 }, 69 { 70 name: "one negative arg", 71 args: []any{-5}, 72 want: -3, 73 wantDelta: 2, 74 }, 75 } 76 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 get, err := RandInt(tt.args...) 80 if tt.wantErr { 81 require.Error(t, err) 82 return 83 } 84 require.NoError(t, err) 85 g, err := strconv.Atoi(get) 86 require.NoError(t, err) 87 require.InDelta(t, tt.want, g, tt.wantDelta) 88 }) 89 } 90 } 91 92 func TestRandString(t *testing.T) { 93 tests := []struct { 94 name string 95 args []any 96 wantErr bool 97 wantLength int 98 }{ 99 { 100 name: "No args, default length", 101 args: nil, 102 wantLength: 1, 103 }, 104 { 105 name: "Specific length", 106 args: []any{5}, 107 wantLength: 5, 108 }, 109 { 110 name: "Specific length and characters", 111 args: []any{10, "abc"}, 112 wantLength: 10, 113 }, 114 { 115 name: "Invalid length argument", 116 args: []any{"invalid"}, 117 wantErr: true, 118 }, 119 { 120 name: "Invalid length, valid characters", 121 args: []any{"invalid", "def"}, 122 wantErr: true, 123 }, 124 { 125 name: "More than two args", 126 args: []any{5, "gh", "extra"}, 127 wantErr: true, 128 }, 129 } 130 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 got, err := RandString(tt.args...) 134 if tt.wantErr { 135 require.Error(t, err) 136 return 137 } 138 require.NoError(t, err) 139 require.Len(t, got, tt.wantLength) 140 }) 141 } 142 } 143 144 func TestRandStringLetters(t *testing.T) { 145 tests := []struct { 146 name string 147 length int 148 letters string 149 wantErr bool 150 }{ 151 { 152 name: "Simple", 153 length: 10, 154 letters: "ab", 155 }, 156 { 157 name: "Simple", 158 length: 100, 159 letters: "absdfave", 160 }, 161 } 162 163 for _, tt := range tests { 164 t.Run(tt.name, func(t *testing.T) { 165 got, err := RandString(tt.length, tt.letters) 166 if tt.wantErr { 167 require.Error(t, err) 168 return 169 } 170 require.NoError(t, err) 171 require.Len(t, got, tt.length) 172 173 l := map[rune]int{} 174 for _, r := range got { 175 l[r]++ 176 } 177 gotCount := 0 178 for _, c := range l { 179 gotCount += c 180 } 181 require.Equal(t, tt.length, gotCount) 182 }) 183 } 184 } 185 186 func TestParseFunc(t *testing.T) { 187 tests := []struct { 188 name string 189 arg string 190 wantF any 191 wantArgs []string 192 }{ 193 { 194 name: "Two args", 195 arg: "randInt(10, 20)", 196 wantF: RandInt, 197 wantArgs: []string{"10", "20"}, 198 }, 199 { 200 name: "One arg", 201 arg: "randInt(10)", 202 wantF: RandInt, 203 wantArgs: []string{"10"}, 204 }, 205 { 206 name: "No args", 207 arg: "randInt()", 208 wantF: RandInt, 209 wantArgs: nil, 210 }, 211 } 212 for _, tt := range tests { 213 t.Run(tt.name, func(t *testing.T) { 214 gotF, gotArgs := ParseFunc(tt.arg) 215 f := gotF.(func(args ...any) (string, error)) 216 a := []any{} 217 for _, arg := range gotArgs { 218 a = append(a, arg) 219 } 220 _, _ = f(a...) 221 require.Equal(t, tt.wantArgs, gotArgs) 222 }) 223 } 224 }