| 1 |
// Copyright 2022 Google LLC |
| 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 |
// https://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 |
package repl |
| 16 |
|
| 17 |
import ( |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/google/cel-go/cel" |
| 22 |
"github.com/google/go-cmp/cmp" |
| 23 |
|
| 24 |
proto2pb "github.com/google/cel-go/test/proto2pb" |
| 25 |
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 26 |
) |
| 27 |
|
| 28 |
var testTextDescriptorFile string = "testdata/attribute_context_fds.textproto" |
| 29 |
var testYAMLEnvFile string = "testdata/test_env.yaml" |
| 30 |
var testMultilineYAMLEnvFile string = "testdata/multiline_test_env.yaml" |
| 31 |
|
| 32 |
func mustParseType(t testing.TB, name string) *exprpb.Type { |
| 33 |
t.Helper() |
| 34 |
ty, err := ParseType(name) |
| 35 |
if err != nil { |
| 36 |
t.Fatalf("ParseType(%s) failed", name) |
| 37 |
} |
| 38 |
return ty |
| 39 |
} |
| 40 |
|
| 41 |
func TestEvalSimple(t *testing.T) { |
| 42 |
eval, err := NewEvaluator() |
| 43 |
if err != nil { |
| 44 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 45 |
} |
| 46 |
|
| 47 |
_, _, err = eval.Evaluate("[1, 2, 3]") |
| 48 |
|
| 49 |
if err != nil { |
| 50 |
t.Errorf("eval.Evaluate('[1, 2, 3]') got %v, wanted non-error", err) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func TestEvalSingleLetVar(t *testing.T) { |
| 55 |
eval, err := NewEvaluator() |
| 56 |
if err != nil { |
| 57 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 58 |
} |
| 59 |
|
| 60 |
err = eval.AddLetVar("x", "2 + 2", nil) |
| 61 |
if err != nil { |
| 62 |
t.Errorf("eval.AddLetVar('x', '2 + 2') got %v, wanted non-error", err) |
| 63 |
} |
| 64 |
|
| 65 |
_, _, err = eval.Evaluate("[1, 2, 3, x]") |
| 66 |
|
| 67 |
if err != nil { |
| 68 |
t.Errorf("eval.Evaluate('[1, 2, 3, x]') got %v, wanted non-error", err) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestEvalMultiLet(t *testing.T) { |
| 73 |
eval, err := NewEvaluator() |
| 74 |
if err != nil { |
| 75 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 76 |
} |
| 77 |
|
| 78 |
eval.AddLetVar("x", "20/5", nil) |
| 79 |
eval.AddLetVar("y", "x * 3", nil) |
| 80 |
eval.AddLetVar("x", "20", nil) |
| 81 |
|
| 82 |
_, _, err = eval.Evaluate("[1, 2, 3, x, y]") |
| 83 |
if err != nil { |
| 84 |
t.Errorf("eval.Evaluate('[1, 2, 3, x, y]') got %v, wanted non-error", err) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func TestEvalError(t *testing.T) { |
| 89 |
eval, err := NewEvaluator() |
| 90 |
if err != nil { |
| 91 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 92 |
} |
| 93 |
|
| 94 |
eval.AddLetVar("x", "1", nil) |
| 95 |
eval.AddLetVar("y", "0", nil) |
| 96 |
|
| 97 |
_, _, err = eval.Evaluate("x / y") |
| 98 |
if err == nil { |
| 99 |
t.Errorf("eval.Evaluate('x / y') got non-error, wanted division by zero") |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func TestLetError(t *testing.T) { |
| 104 |
eval, err := NewEvaluator() |
| 105 |
if err != nil { |
| 106 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 107 |
} |
| 108 |
|
| 109 |
err = eval.AddLetVar("y", "z + 1", nil) |
| 110 |
if err == nil { |
| 111 |
t.Errorf("eval.AddLetVar('y', 'z + 1') got %v, wanted error", err) |
| 112 |
} |
| 113 |
|
| 114 |
result, _, err := eval.Evaluate("y") |
| 115 |
if err == nil { |
| 116 |
t.Errorf("eval.Evaluate('y') got result %v, wanted error", result.Value()) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func TestLetTypeHintError(t *testing.T) { |
| 121 |
eval, err := NewEvaluator() |
| 122 |
if err != nil { |
| 123 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 124 |
} |
| 125 |
|
| 126 |
err = eval.AddLetVar("y", "10u", mustParseType(t, "int")) |
| 127 |
if err == nil { |
| 128 |
t.Errorf("eval.AddLetVar('y', '10u') got %v, wanted error", err) |
| 129 |
} |
| 130 |
|
| 131 |
result, _, err := eval.Evaluate("y") |
| 132 |
if err == nil { |
| 133 |
t.Errorf("eval.Evaluate('y') got result %v, wanted error", result) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func TestDeclareError(t *testing.T) { |
| 138 |
eval, err := NewEvaluator() |
| 139 |
if err != nil { |
| 140 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 141 |
} |
| 142 |
|
| 143 |
eval.AddDeclVar("z", mustParseType(t, "double")) |
| 144 |
eval.AddLetVar("y", "z + 10.0", nil) |
| 145 |
err = eval.AddLetVar("z", "'2.0'", nil) |
| 146 |
if err == nil { |
| 147 |
t.Errorf("eval.AddLetVar('z', '\"2.0\"') got %v, wanted error", err) |
| 148 |
} |
| 149 |
|
| 150 |
err = eval.AddDeclVar("z", mustParseType(t, "string")) |
| 151 |
if err == nil { |
| 152 |
t.Errorf("eval.AddDeclVar('z', string) got %v, wanted error", err) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
func TestDelError(t *testing.T) { |
| 157 |
eval, err := NewEvaluator() |
| 158 |
if err != nil { |
| 159 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 160 |
} |
| 161 |
|
| 162 |
eval.AddLetVar("z", "41", nil) |
| 163 |
eval.AddLetVar("y", "z + 1", nil) |
| 164 |
|
| 165 |
err = eval.DelLetVar("z") |
| 166 |
if err == nil { |
| 167 |
t.Errorf("eval.DelLetVar('z') got %v, wanted error", err) |
| 168 |
} |
| 169 |
|
| 170 |
val, _, err := eval.Evaluate("y") |
| 171 |
if err != nil { |
| 172 |
t.Errorf("eval.Evaluate('y') failed %v, wanted non-error", err) |
| 173 |
} else if val.Value().(int64) != 42 { |
| 174 |
t.Errorf("eval.Evaluate('y') got %v, wanted 42", val.Value()) |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
func TestAddLetFn(t *testing.T) { |
| 180 |
eval, err := NewEvaluator() |
| 181 |
if err != nil { |
| 182 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 183 |
} |
| 184 |
|
| 185 |
eval.AddLetFn("fn", []letFunctionParam{ |
| 186 |
{identifier: "x", typeHint: mustParseType(t, "int")}, |
| 187 |
{identifier: "y", typeHint: mustParseType(t, "int")}}, |
| 188 |
mustParseType(t, "int"), |
| 189 |
"x * x - y * y") |
| 190 |
|
| 191 |
eval.AddLetVar("testcases", "[[1, 2], [2, 3], [3, 4], [10, 20]]", mustParseType(t, "list(list(int))")) |
| 192 |
|
| 193 |
result, _, err := eval.Evaluate("testcases.all(e, fn(e[0], e[1]) == (e[0] - e[1]) * (e[0] + e[1]))") |
| 194 |
|
| 195 |
if err != nil { |
| 196 |
t.Errorf("eval.Evaluate() got error %v wanted nil", err) |
| 197 |
} else if !result.Value().(bool) { |
| 198 |
t.Errorf("eval.Evaluate() got %v wanted true", result.Value()) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func TestAddLetFnComposed(t *testing.T) { |
| 203 |
eval, err := NewEvaluator() |
| 204 |
if err != nil { |
| 205 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 206 |
} |
| 207 |
|
| 208 |
err = eval.AddLetFn("square", []letFunctionParam{{ |
| 209 |
identifier: "x", |
| 210 |
typeHint: mustParseType(t, "int"), |
| 211 |
}}, mustParseType(t, "int"), "x * x") |
| 212 |
|
| 213 |
if err != nil { |
| 214 |
t.Errorf("eval.AddLetFn(square x -> int) got error %v expected nil", err) |
| 215 |
} |
| 216 |
|
| 217 |
err = eval.AddLetFn("squareDiff", []letFunctionParam{ |
| 218 |
{ |
| 219 |
identifier: "x", |
| 220 |
typeHint: mustParseType(t, "int"), |
| 221 |
}, |
| 222 |
{ |
| 223 |
identifier: "y", |
| 224 |
typeHint: mustParseType(t, "int"), |
| 225 |
}}, mustParseType(t, "int"), "square(x) - square(y)") |
| 226 |
|
| 227 |
if err != nil { |
| 228 |
t.Errorf("eval.AddLetFn(squareDiff x, y -> int) got error %v expected nil", err) |
| 229 |
} |
| 230 |
|
| 231 |
result, _, err := eval.Evaluate("squareDiff(4, 3)") |
| 232 |
|
| 233 |
if err != nil { |
| 234 |
t.Errorf("eval.Evaluate() got error %v wanted nil", err) |
| 235 |
} else if result.Value().(int64) != 7 { |
| 236 |
t.Errorf("eval.Evaluate() got %v wanted true", result.Value()) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func TestAddLetFnErrorOnTypeChange(t *testing.T) { |
| 241 |
eval, err := NewEvaluator() |
| 242 |
if err != nil { |
| 243 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 244 |
} |
| 245 |
|
| 246 |
err = eval.AddLetFn("square", []letFunctionParam{ |
| 247 |
{identifier: "x", typeHint: mustParseType(t, "int")}}, |
| 248 |
mustParseType(t, "int"), |
| 249 |
"x * x") |
| 250 |
|
| 251 |
if err != nil { |
| 252 |
t.Errorf("eval.AddLetFn('square x -> int') got error %v expected nil", err) |
| 253 |
} |
| 254 |
|
| 255 |
eval.AddLetVar("y", "square(1) + 1", nil) |
| 256 |
|
| 257 |
// Overloads not yet supported |
| 258 |
err = eval.AddLetFn("square", []letFunctionParam{ |
| 259 |
{identifier: "x", typeHint: mustParseType(t, "double")}}, |
| 260 |
mustParseType(t, "double"), |
| 261 |
"x * x") |
| 262 |
|
| 263 |
if err == nil { |
| 264 |
t.Error("eval.AddLetFn('square x -> double') got nil, expected error") |
| 265 |
} |
| 266 |
|
| 267 |
result, _, err := eval.Evaluate("y") |
| 268 |
|
| 269 |
if err != nil { |
| 270 |
t.Errorf("eval.Evaluate() got error %v wanted nil", err) |
| 271 |
} else if result.Value().(int64) != 2 { |
| 272 |
t.Errorf("eval.Evaluate() got %v wanted true", result.Value()) |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
func TestAddLetFnErrorTypeMismatch(t *testing.T) { |
| 277 |
eval, err := NewEvaluator() |
| 278 |
if err != nil { |
| 279 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 280 |
} |
| 281 |
|
| 282 |
err = eval.AddLetFn("fn", []letFunctionParam{ |
| 283 |
{identifier: "x", typeHint: mustParseType(t, "int")}, |
| 284 |
{identifier: "y", typeHint: mustParseType(t, "int")}}, |
| 285 |
mustParseType(t, "double"), |
| 286 |
"x * x - y * y") |
| 287 |
|
| 288 |
if err == nil { |
| 289 |
t.Error("eval.AddLetFn('fn x : int, y : int -> double') got nil expected error") |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
func TestAddLetFnErrorBadExpr(t *testing.T) { |
| 294 |
eval, err := NewEvaluator() |
| 295 |
if err != nil { |
| 296 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 297 |
} |
| 298 |
|
| 299 |
err = eval.AddLetFn("fn", []letFunctionParam{ |
| 300 |
{identifier: "y", typeHint: mustParseType(t, "string")}}, |
| 301 |
mustParseType(t, "int"), |
| 302 |
"2 - y") |
| 303 |
|
| 304 |
if err == nil { |
| 305 |
t.Error("eval.AddLetFn('fn y : string -> int = 2 - y') got nil wanted error") |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
func TestAddDeclFn(t *testing.T) { |
| 310 |
eval, err := NewEvaluator() |
| 311 |
if err != nil { |
| 312 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 313 |
} |
| 314 |
|
| 315 |
err = eval.AddDeclFn("fn", []letFunctionParam{ |
| 316 |
{identifier: "x", typeHint: mustParseType(t, "int")}, |
| 317 |
{identifier: "y", typeHint: mustParseType(t, "int")}}, |
| 318 |
mustParseType(t, "int")) |
| 319 |
|
| 320 |
if err != nil { |
| 321 |
t.Errorf("eval.AddDeclFn('fn(x:int, y:int): int') got error %v wanted nil", err) |
| 322 |
} |
| 323 |
|
| 324 |
err = eval.AddLetVar("z", "fn(1, 2)", nil) |
| 325 |
|
| 326 |
if err != nil { |
| 327 |
t.Errorf("eval.AddLetVar('z = fn(1, 2)') got error %v wanted nil", err) |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func TestAddDeclFnError(t *testing.T) { |
| 332 |
eval, err := NewEvaluator() |
| 333 |
if err != nil { |
| 334 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 335 |
} |
| 336 |
|
| 337 |
err = eval.AddDeclFn("fn", []letFunctionParam{ |
| 338 |
{identifier: "x", typeHint: mustParseType(t, "int")}, |
| 339 |
{identifier: "y", typeHint: mustParseType(t, "int")}}, |
| 340 |
mustParseType(t, "int")) |
| 341 |
|
| 342 |
if err != nil { |
| 343 |
t.Errorf("eval.AddDeclFn('fn(x:int, y:int): int') got error %v wanted nil", err) |
| 344 |
} |
| 345 |
|
| 346 |
err = eval.AddLetVar("z", "fn(1, 2)", nil) |
| 347 |
|
| 348 |
if err != nil { |
| 349 |
t.Errorf("eval.AddLetVar('z = fn(1, 2)') got error %v wanted nil", err) |
| 350 |
} |
| 351 |
|
| 352 |
err = eval.AddDeclFn("fn", []letFunctionParam{ |
| 353 |
{identifier: "x", typeHint: mustParseType(t, "string")}, |
| 354 |
{identifier: "y", typeHint: mustParseType(t, "string")}}, |
| 355 |
mustParseType(t, "int")) |
| 356 |
|
| 357 |
if err == nil { |
| 358 |
t.Errorf("eval.AddDeclFn('fn(x:string, y:string): int') got nil wanted error") |
| 359 |
} |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
func TestDelLetFn(t *testing.T) { |
| 364 |
eval, err := NewEvaluator() |
| 365 |
if err != nil { |
| 366 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 367 |
} |
| 368 |
|
| 369 |
err = eval.AddLetFn("fn", []letFunctionParam{ |
| 370 |
{identifier: "y", typeHint: mustParseType(t, "int")}}, |
| 371 |
mustParseType(t, "int"), |
| 372 |
"2 - y") |
| 373 |
|
| 374 |
if err != nil { |
| 375 |
t.Fatalf("eval.AddLetFn('fn (y : string): int -> 2 - y') failed: %s", err) |
| 376 |
} |
| 377 |
|
| 378 |
err = eval.DelLetFn("fn") |
| 379 |
if err != nil { |
| 380 |
t.Errorf("eval.DelLetFn('fn') got error %s, wanted nil", err) |
| 381 |
} |
| 382 |
|
| 383 |
err = eval.AddLetVar("x", "fn(2)", nil) |
| 384 |
if err == nil { |
| 385 |
t.Error("eval.AddLetVar('fn(2)') got nil, wanted error") |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
func TestDelLetFnError(t *testing.T) { |
| 390 |
eval, err := NewEvaluator() |
| 391 |
if err != nil { |
| 392 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 393 |
} |
| 394 |
|
| 395 |
err = eval.AddLetFn("fn", []letFunctionParam{ |
| 396 |
{identifier: "y", typeHint: mustParseType(t, "int")}}, |
| 397 |
mustParseType(t, "int"), |
| 398 |
"2 - y") |
| 399 |
|
| 400 |
if err != nil { |
| 401 |
t.Fatalf("eval.AddLetFn('fn (y : string): int -> 2 - y') failed: %s", err) |
| 402 |
} |
| 403 |
|
| 404 |
err = eval.AddLetVar("x", "fn(2)", nil) |
| 405 |
if err != nil { |
| 406 |
t.Errorf("eval.AddLetVar('fn(2)') got error %s, wanted nil", err) |
| 407 |
} |
| 408 |
|
| 409 |
err = eval.DelLetFn("fn") |
| 410 |
if err == nil { |
| 411 |
t.Error("eval.DelLetFn('fn') got nil, wanted error") |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
func TestInstanceFunction(t *testing.T) { |
| 416 |
eval, err := NewEvaluator() |
| 417 |
if err != nil { |
| 418 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 419 |
} |
| 420 |
|
| 421 |
err = eval.AddLetFn("int.plus", []letFunctionParam{ |
| 422 |
{identifier: "y", typeHint: mustParseType(t, "int")}}, |
| 423 |
mustParseType(t, "int"), |
| 424 |
"this + y") |
| 425 |
|
| 426 |
if err != nil { |
| 427 |
t.Fatalf("eval.AddLetFn('int.plus(y : int): int -> this + y') failed: %s", err) |
| 428 |
} |
| 429 |
|
| 430 |
val, _, err := eval.Evaluate("40.plus(2)") |
| 431 |
if err != nil { |
| 432 |
t.Errorf("eval.Eval('40.plus(2)') got error %v, wanted nil", err) |
| 433 |
} |
| 434 |
if val.Value().(int64) != 42 { |
| 435 |
t.Errorf("eval.Eval('40.plus(2)') got %s, wanted 42", val) |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
type testContainerOption struct { |
| 440 |
container string |
| 441 |
} |
| 442 |
|
| 443 |
func (o *testContainerOption) String() string { |
| 444 |
return o.container |
| 445 |
} |
| 446 |
|
| 447 |
func (o *testContainerOption) Option() cel.EnvOption { |
| 448 |
return cel.Container(o.container) |
| 449 |
} |
| 450 |
|
| 451 |
func TestSetOption(t *testing.T) { |
| 452 |
eval, err := NewEvaluator() |
| 453 |
if err != nil { |
| 454 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 455 |
} |
| 456 |
|
| 457 |
err = eval.AddSerializableOption(&testContainerOption{container: "google.protobuf"}) |
| 458 |
if err != nil { |
| 459 |
t.Errorf("eval.AddSerializableOption() got error: %v, wanted nil", err) |
| 460 |
} |
| 461 |
|
| 462 |
val, _, err := eval.Evaluate("Int64Value{value: 42} == 42") |
| 463 |
if err != nil { |
| 464 |
t.Errorf("eval.Evaluate() got error: %v, expected nil", err) |
| 465 |
} |
| 466 |
|
| 467 |
if !val.Value().(bool) { |
| 468 |
t.Error("eval.Evaluate() got false expected true") |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
func TestSetOptionError(t *testing.T) { |
| 473 |
eval, err := NewEvaluator() |
| 474 |
if err != nil { |
| 475 |
t.Fatalf("NewEvaluator() failed with: %v", err) |
| 476 |
} |
| 477 |
|
| 478 |
err = eval.AddSerializableOption(&testContainerOption{container: "google.protobuf"}) |
| 479 |
if err != nil { |
| 480 |
t.Errorf("eval.AddSerializableOption() got error: %v, wanted nil", err) |
| 481 |
} |
| 482 |
|
| 483 |
err = eval.AddLetVar("x", "Int64Value{value: 42} == 42", nil) |
| 484 |
if err != nil { |
| 485 |
t.Errorf("eval.Evaluate() got error: %v, expected nil", err) |
| 486 |
} |
| 487 |
|
| 488 |
err = eval.AddSerializableOption(&testContainerOption{container: ""}) |
| 489 |
if err == nil { |
| 490 |
t.Error("eval.AddSerializableOption() got nil expected error") |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
func TestProcess(t *testing.T) { |
| 495 |
var testCases = []struct { |
| 496 |
name string |
| 497 |
commands []Cmder |
| 498 |
wantText string |
| 499 |
wantExit bool |
| 500 |
wantError bool |
| 501 |
ignoreWhitespace bool |
| 502 |
}{ |
| 503 |
{ |
| 504 |
name: "CompileResult", |
| 505 |
commands: []Cmder{ |
| 506 |
&compileCmd{ |
| 507 |
expr: "3u", |
| 508 |
}, |
| 509 |
}, |
| 510 |
wantText: `type_map: { |
| 511 |
key: 1 |
| 512 |
value: { |
| 513 |
primitive: UINT64 |
| 514 |
} |
| 515 |
} |
| 516 |
source_info: { |
| 517 |
location: "<input>" |
| 518 |
line_offsets: 3 |
| 519 |
positions: { |
| 520 |
key: 1 |
| 521 |
value: 0 |
| 522 |
} |
| 523 |
} |
| 524 |
expr: { |
| 525 |
id: 1 |
| 526 |
const_expr: { |
| 527 |
uint64_value: 3 |
| 528 |
} |
| 529 |
}`, |
| 530 |
wantExit: false, |
| 531 |
wantError: false, |
| 532 |
ignoreWhitespace: true, |
| 533 |
}, |
| 534 |
{ |
| 535 |
name: "FormatNumberResult", |
| 536 |
commands: []Cmder{ |
| 537 |
&evalCmd{ |
| 538 |
expr: "1u + 2u", |
| 539 |
}, |
| 540 |
}, |
| 541 |
wantText: "3u : uint", |
| 542 |
wantExit: false, |
| 543 |
wantError: false, |
| 544 |
}, |
| 545 |
{ |
| 546 |
name: "FormatStringResult", |
| 547 |
commands: []Cmder{ |
| 548 |
&evalCmd{ |
| 549 |
expr: `'a' + r'b\1'`, |
| 550 |
}, |
| 551 |
}, |
| 552 |
wantText: `"ab\\1" : string`, |
| 553 |
wantExit: false, |
| 554 |
wantError: false, |
| 555 |
}, |
| 556 |
{ |
| 557 |
name: "FormatListResult", |
| 558 |
commands: []Cmder{ |
| 559 |
&evalCmd{ |
| 560 |
expr: `['abc', 123, 3.14, duration('2m')]`, |
| 561 |
}, |
| 562 |
}, |
| 563 |
wantText: `["abc", 123, 3.14, duration("120s")] : list(dyn)`, |
| 564 |
wantExit: false, |
| 565 |
wantError: false, |
| 566 |
}, |
| 567 |
{ |
| 568 |
name: "FormatMapResult", |
| 569 |
commands: []Cmder{ |
| 570 |
&evalCmd{ |
| 571 |
expr: `{1: 123, 2: 3.14, 3: duration('2m'), 4: b'123'}`, |
| 572 |
}, |
| 573 |
}, |
| 574 |
wantText: `{1: 123, 2: 3.14, 3: duration("120s"), 4: b"\061\062\063"} : map(int, dyn)`, |
| 575 |
wantExit: false, |
| 576 |
wantError: false, |
| 577 |
}, |
| 578 |
{ |
| 579 |
name: "OptionBasic", |
| 580 |
commands: []Cmder{ |
| 581 |
&simpleCmd{ |
| 582 |
cmd: "option", |
| 583 |
args: []string{ |
| 584 |
"--container", |
| 585 |
"google.protobuf", |
| 586 |
}, |
| 587 |
}, |
| 588 |
}, |
| 589 |
wantText: "", |
| 590 |
wantExit: false, |
| 591 |
wantError: false, |
| 592 |
}, |
| 593 |
{ |
| 594 |
name: "OptionContainer", |
| 595 |
commands: []Cmder{ |
| 596 |
&simpleCmd{ |
| 597 |
cmd: "option", |
| 598 |
args: []string{ |
| 599 |
"--container", |
| 600 |
"google.protobuf", |
| 601 |
}, |
| 602 |
}, |
| 603 |
&evalCmd{ |
| 604 |
expr: "Int64Value{value: 20}", |
| 605 |
}, |
| 606 |
}, |
| 607 |
wantText: "20 : wrapper(int)", |
| 608 |
wantExit: false, |
| 609 |
wantError: false, |
| 610 |
}, |
| 611 |
{ |
| 612 |
name: "OptionExtensionOptional", |
| 613 |
commands: []Cmder{ |
| 614 |
&simpleCmd{ |
| 615 |
cmd: "option", |
| 616 |
args: []string{ |
| 617 |
"--extension", |
| 618 |
"optional", |
| 619 |
}, |
| 620 |
}, |
| 621 |
&evalCmd{ |
| 622 |
expr: "optional.none().orValue('default')", |
| 623 |
}, |
| 624 |
}, |
| 625 |
wantText: "\"default\" : string", |
| 626 |
wantExit: false, |
| 627 |
wantError: false, |
| 628 |
}, |
| 629 |
{ |
| 630 |
name: "OptionExtensionOptionalTypeHint", |
| 631 |
commands: []Cmder{ |
| 632 |
&simpleCmd{ |
| 633 |
cmd: "option", |
| 634 |
args: []string{ |
| 635 |
"--extension", |
| 636 |
"optional", |
| 637 |
}, |
| 638 |
}, |
| 639 |
&letVarCmd{ |
| 640 |
identifier: "x", |
| 641 |
typeHint: mustParseType(t, "optional_type(string)"), |
| 642 |
src: "optional.of('foo')", |
| 643 |
}, |
| 644 |
&evalCmd{ |
| 645 |
expr: "x.or(optional.of('bar'))", |
| 646 |
}, |
| 647 |
}, |
| 648 |
wantText: "optional.of(\"foo\") : optional_type(string)", |
| 649 |
wantExit: false, |
| 650 |
wantError: false, |
| 651 |
}, |
| 652 |
{ |
| 653 |
name: "OptionExtensionStrings", |
| 654 |
commands: []Cmder{ |
| 655 |
&simpleCmd{ |
| 656 |
cmd: "option", |
| 657 |
args: []string{ |
| 658 |
"--extension", |
| 659 |
"strings", |
| 660 |
}, |
| 661 |
}, |
| 662 |
&evalCmd{ |
| 663 |
expr: "'test'.substring(2)", |
| 664 |
}, |
| 665 |
}, |
| 666 |
wantText: "\"st\" : string", |
| 667 |
wantExit: false, |
| 668 |
wantError: false, |
| 669 |
}, |
| 670 |
{ |
| 671 |
name: "OptionExtensionProtos", |
| 672 |
commands: []Cmder{ |
| 673 |
&simpleCmd{ |
| 674 |
cmd: "option", |
| 675 |
args: []string{ |
| 676 |
"--extension", |
| 677 |
"protos", |
| 678 |
}, |
| 679 |
}, |
| 680 |
&evalCmd{ |
| 681 |
expr: "proto.getExt(google.expr.proto2.test.ExampleType{}, google.expr.proto2.test.int32_ext) == 0", |
| 682 |
}, |
| 683 |
}, |
| 684 |
wantText: "true : bool", |
| 685 |
wantExit: false, |
| 686 |
wantError: false, |
| 687 |
}, |
| 688 |
{ |
| 689 |
name: "OptionExtensionMath", |
| 690 |
commands: []Cmder{ |
| 691 |
&simpleCmd{ |
| 692 |
cmd: "option", |
| 693 |
args: []string{ |
| 694 |
"--extension", |
| 695 |
"math", |
| 696 |
}, |
| 697 |
}, |
| 698 |
&evalCmd{ |
| 699 |
expr: "math.greatest(1,2)", |
| 700 |
}, |
| 701 |
}, |
| 702 |
wantText: "2 : int", |
| 703 |
wantExit: false, |
| 704 |
wantError: false, |
| 705 |
}, |
| 706 |
{ |
| 707 |
name: "OptionExtensionEncoders", |
| 708 |
commands: []Cmder{ |
| 709 |
&simpleCmd{ |
| 710 |
cmd: "option", |
| 711 |
args: []string{ |
| 712 |
"--extension", |
| 713 |
"encoders", |
| 714 |
}, |
| 715 |
}, |
| 716 |
&evalCmd{ |
| 717 |
expr: "base64.encode(b'hello')", |
| 718 |
}, |
| 719 |
}, |
| 720 |
wantText: "\"aGVsbG8=\" : string", |
| 721 |
wantExit: false, |
| 722 |
wantError: false, |
| 723 |
}, |
| 724 |
{ |
| 725 |
name: "OptionExtensionAll", |
| 726 |
commands: []Cmder{ |
| 727 |
&simpleCmd{ |
| 728 |
cmd: "option", |
| 729 |
args: []string{ |
| 730 |
"--extension", |
| 731 |
"all", |
| 732 |
}, |
| 733 |
}, |
| 734 |
&evalCmd{ |
| 735 |
expr: "'test'.substring(2) == 'st' && " + |
| 736 |
"proto.getExt(google.expr.proto2.test.ExampleType{}, google.expr.proto2.test.int32_ext) == 0 && " + |
| 737 |
"math.greatest(1,2) == 2 && " + |
| 738 |
"base64.encode(b'hello') == 'aGVsbG8=' && " + |
| 739 |
"{'key': 1}.exists(k, v, k == 'key' && v == 1)", |
| 740 |
}, |
| 741 |
}, |
| 742 |
wantText: "true : bool", |
| 743 |
wantExit: false, |
| 744 |
wantError: false, |
| 745 |
}, |
| 746 |
{ |
| 747 |
name: "OptionEnablePartialEval", |
| 748 |
commands: []Cmder{ |
| 749 |
&simpleCmd{ |
| 750 |
cmd: "option", |
| 751 |
args: []string{ |
| 752 |
"--enable_partial_eval", |
| 753 |
}, |
| 754 |
}, |
| 755 |
&letVarCmd{ |
| 756 |
identifier: "x", |
| 757 |
typeHint: mustParseType(t, "int"), |
| 758 |
src: "", |
| 759 |
}, |
| 760 |
&letVarCmd{ |
| 761 |
identifier: "y", |
| 762 |
typeHint: mustParseType(t, "int"), |
| 763 |
src: "10", |
| 764 |
}, |
| 765 |
&evalCmd{ |
| 766 |
expr: "x + y > 10 || y > 10", |
| 767 |
}, |
| 768 |
}, |
| 769 |
wantText: "Unknown x (1)", |
| 770 |
wantExit: false, |
| 771 |
wantError: false, |
| 772 |
}, |
| 773 |
{ |
| 774 |
name: "PartialEvalDisabled", |
| 775 |
commands: []Cmder{ |
| 776 |
&letVarCmd{ |
| 777 |
identifier: "x", |
| 778 |
typeHint: mustParseType(t, "int"), |
| 779 |
src: "", |
| 780 |
}, |
| 781 |
&letVarCmd{ |
| 782 |
identifier: "y", |
| 783 |
typeHint: mustParseType(t, "int"), |
| 784 |
src: "10", |
| 785 |
}, |
| 786 |
&evalCmd{ |
| 787 |
expr: "x + y > 10 || y > 10", |
| 788 |
}, |
| 789 |
}, |
| 790 |
wantText: "", |
| 791 |
wantExit: false, |
| 792 |
wantError: true, |
| 793 |
}, |
| 794 |
{ |
| 795 |
name: "PartialEvalFiltered", |
| 796 |
commands: []Cmder{ |
| 797 |
&simpleCmd{ |
| 798 |
cmd: "option", |
| 799 |
args: []string{ |
| 800 |
"--enable_partial_eval", |
| 801 |
}, |
| 802 |
}, |
| 803 |
&letVarCmd{ |
| 804 |
identifier: "x", |
| 805 |
typeHint: mustParseType(t, "int"), |
| 806 |
src: "", |
| 807 |
}, |
| 808 |
&letVarCmd{ |
| 809 |
identifier: "y", |
| 810 |
typeHint: mustParseType(t, "int"), |
| 811 |
src: "11", |
| 812 |
}, |
| 813 |
&evalCmd{ |
| 814 |
expr: "x + y > 10 || y > 10", |
| 815 |
}, |
| 816 |
}, |
| 817 |
wantText: "true : bool", |
| 818 |
wantExit: false, |
| 819 |
wantError: false, |
| 820 |
}, |
| 821 |
{ |
| 822 |
name: "OptionEnableEscapedFields", |
| 823 |
commands: []Cmder{ |
| 824 |
&simpleCmd{ |
| 825 |
cmd: "option", |
| 826 |
args: []string{ |
| 827 |
"--enable_escaped_fields", |
| 828 |
}, |
| 829 |
}, |
| 830 |
&letVarCmd{ |
| 831 |
identifier: "foo", |
| 832 |
typeHint: mustParseType(t, "map(string, string)"), |
| 833 |
src: "{'example.com': 'great'}", |
| 834 |
}, |
| 835 |
&evalCmd{ |
| 836 |
expr: "foo.`example.com`", |
| 837 |
}, |
| 838 |
}, |
| 839 |
wantText: "\"great\" : string", |
| 840 |
wantExit: false, |
| 841 |
wantError: false, |
| 842 |
}, |
| 843 |
{ |
| 844 |
name: "LoadDescriptorsError", |
| 845 |
commands: []Cmder{ |
| 846 |
&simpleCmd{ |
| 847 |
cmd: "load_descriptors", |
| 848 |
args: []string{ |
| 849 |
"<not a file>", |
| 850 |
}, |
| 851 |
}, |
| 852 |
}, |
| 853 |
wantText: "", |
| 854 |
wantExit: false, |
| 855 |
wantError: true, |
| 856 |
}, |
| 857 |
{ |
| 858 |
name: "LoadDescriptorsAddsTypes", |
| 859 |
commands: []Cmder{ |
| 860 |
&simpleCmd{ |
| 861 |
cmd: "load_descriptors", |
| 862 |
args: []string{ |
| 863 |
testTextDescriptorFile, |
| 864 |
}, |
| 865 |
}, |
| 866 |
&simpleCmd{ |
| 867 |
cmd: "option", |
| 868 |
args: []string{ |
| 869 |
"--container", |
| 870 |
"google.rpc.context", |
| 871 |
}, |
| 872 |
}, |
| 873 |
&evalCmd{ |
| 874 |
expr: "AttributeContext.Request{host: 'www.example.com'}", |
| 875 |
}, |
| 876 |
}, |
| 877 |
wantText: `google.rpc.context.AttributeContext.Request{host: "www.example.com"} : google.rpc.context.AttributeContext.Request`, |
| 878 |
wantExit: false, |
| 879 |
wantError: false, |
| 880 |
}, |
| 881 |
{ |
| 882 |
name: "LoadDescriptorsPackageSpec", |
| 883 |
commands: []Cmder{ |
| 884 |
&simpleCmd{ |
| 885 |
cmd: "load_descriptors", |
| 886 |
args: []string{ |
| 887 |
"--pkg", |
| 888 |
"cel-spec-test-types", |
| 889 |
}, |
| 890 |
}, |
| 891 |
&simpleCmd{ |
| 892 |
cmd: "option", |
| 893 |
args: []string{ |
| 894 |
"--container", |
| 895 |
"cel.expr.conformance", |
| 896 |
}, |
| 897 |
}, |
| 898 |
&evalCmd{ |
| 899 |
expr: "proto2.TestAllTypes{single_int64: 42}.single_int64 == proto3.TestAllTypes{single_int64: 42}.single_int64", |
| 900 |
}, |
| 901 |
}, |
| 902 |
wantText: `true : bool`, |
| 903 |
wantExit: false, |
| 904 |
wantError: false, |
| 905 |
}, |
| 906 |
{ |
| 907 |
name: "LoadDescriptorsPackageRpc", |
| 908 |
commands: []Cmder{ |
| 909 |
&simpleCmd{ |
| 910 |
cmd: "load_descriptors", |
| 911 |
args: []string{ |
| 912 |
"--pkg", |
| 913 |
"google-rpc", |
| 914 |
}, |
| 915 |
}, |
| 916 |
&simpleCmd{ |
| 917 |
cmd: "option", |
| 918 |
args: []string{ |
| 919 |
"--container", |
| 920 |
"google.rpc.context", |
| 921 |
}, |
| 922 |
}, |
| 923 |
&evalCmd{ |
| 924 |
expr: "AttributeContext{source: AttributeContext.Peer{ip: '192.168.0.1'}}.source.ip == '192.168.0.1'", |
| 925 |
}, |
| 926 |
}, |
| 927 |
wantText: `true : bool`, |
| 928 |
wantExit: false, |
| 929 |
wantError: false, |
| 930 |
}, |
| 931 |
{ |
| 932 |
name: "LoadDescriptorsPackageUnknown", |
| 933 |
commands: []Cmder{ |
| 934 |
&simpleCmd{ |
| 935 |
cmd: "load_descriptors", |
| 936 |
args: []string{ |
| 937 |
"--pkg", |
| 938 |
"'unknown'", |
| 939 |
}, |
| 940 |
}, |
| 941 |
}, |
| 942 |
wantText: "", |
| 943 |
wantExit: false, |
| 944 |
wantError: true, |
| 945 |
}, |
| 946 |
{ |
| 947 |
name: "Status", |
| 948 |
commands: []Cmder{ |
| 949 |
&simpleCmd{ |
| 950 |
cmd: "option", |
| 951 |
args: []string{ |
| 952 |
"--container", |
| 953 |
"google", |
| 954 |
}, |
| 955 |
}, |
| 956 |
&letVarCmd{ |
| 957 |
identifier: "x", |
| 958 |
typeHint: mustParseType(t, "int"), |
| 959 |
src: "1", |
| 960 |
}, |
| 961 |
&letFnCmd{ |
| 962 |
identifier: "fn", |
| 963 |
src: "2", |
| 964 |
resultType: mustParseType(t, "int"), |
| 965 |
params: nil, |
| 966 |
}, |
| 967 |
&simpleCmd{ |
| 968 |
cmd: "status", |
| 969 |
args: []string{}, |
| 970 |
}, |
| 971 |
}, |
| 972 |
wantText: `// Options |
| 973 |
%option --container 'google' |
| 974 |
|
| 975 |
// Functions |
| 976 |
%let fn() : int -> 2 |
| 977 |
|
| 978 |
// Variables |
| 979 |
%let x = 1 |
| 980 |
|
| 981 |
`, |
| 982 |
wantExit: false, |
| 983 |
wantError: false, |
| 984 |
}, |
| 985 |
{ |
| 986 |
name: "StatusYAML", |
| 987 |
commands: []Cmder{ |
| 988 |
&simpleCmd{ |
| 989 |
cmd: "option", |
| 990 |
args: []string{ |
| 991 |
"--container", |
| 992 |
"google", |
| 993 |
}, |
| 994 |
}, |
| 995 |
&letVarCmd{ |
| 996 |
identifier: "x", |
| 997 |
typeHint: mustParseType(t, "int"), |
| 998 |
src: "1", |
| 999 |
}, |
| 1000 |
&letFnCmd{ |
| 1001 |
identifier: "fn", |
| 1002 |
src: "2", |
| 1003 |
resultType: mustParseType(t, "int"), |
| 1004 |
params: nil, |
| 1005 |
}, |
| 1006 |
&simpleCmd{ |
| 1007 |
cmd: "status", |
| 1008 |
args: []string{"--yaml"}, |
| 1009 |
}, |
| 1010 |
}, |
| 1011 |
wantText: `# CEL environment YAML |
| 1012 |
name: repl-session |
| 1013 |
container: google |
| 1014 |
variables: |
| 1015 |
- name: x |
| 1016 |
type_name: int |
| 1017 |
functions: |
| 1018 |
- name: fn |
| 1019 |
overloads: |
| 1020 |
- id: fn |
| 1021 |
return: |
| 1022 |
type_name: int |
| 1023 |
features: |
| 1024 |
- name: cel.feature.macro_call_tracking |
| 1025 |
enabled: true |
| 1026 |
|
| 1027 |
|
| 1028 |
# REPL Bindings: |
| 1029 |
# %let fn() : int -> 2 |
| 1030 |
# |
| 1031 |
# %let x = 1 |
| 1032 |
# |
| 1033 |
`, |
| 1034 |
wantExit: false, |
| 1035 |
wantError: false, |
| 1036 |
}, |
| 1037 |
{ |
| 1038 |
name: "StatusYAMLRoundTrip", |
| 1039 |
commands: []Cmder{ |
| 1040 |
&simpleCmd{ |
| 1041 |
cmd: "configure", |
| 1042 |
args: []string{ |
| 1043 |
"--yaml", |
| 1044 |
`# CEL environment YAML |
| 1045 |
name: repl-session |
| 1046 |
container: google |
| 1047 |
variables: |
| 1048 |
- name: x |
| 1049 |
type_name: int |
| 1050 |
functions: |
| 1051 |
- name: fn |
| 1052 |
overloads: |
| 1053 |
- id: fn |
| 1054 |
return: |
| 1055 |
type_name: int |
| 1056 |
features: |
| 1057 |
- name: cel.feature.macro_call_tracking |
| 1058 |
enabled: true |
| 1059 |
|
| 1060 |
|
| 1061 |
# REPL Bindings: |
| 1062 |
# %let fn() : int -> 2 |
| 1063 |
# |
| 1064 |
# %let x = 1 |
| 1065 |
#`, |
| 1066 |
}, |
| 1067 |
}, |
| 1068 |
&simpleCmd{ |
| 1069 |
cmd: "status", |
| 1070 |
args: []string{"--yaml"}, |
| 1071 |
}, |
| 1072 |
}, |
| 1073 |
wantText: `# CEL environment YAML |
| 1074 |
name: repl-session |
| 1075 |
container: google |
| 1076 |
variables: |
| 1077 |
- name: x |
| 1078 |
type_name: int |
| 1079 |
functions: |
| 1080 |
- name: fn |
| 1081 |
overloads: |
| 1082 |
- id: fn |
| 1083 |
return: |
| 1084 |
type_name: int |
| 1085 |
features: |
| 1086 |
- name: cel.feature.macro_call_tracking |
| 1087 |
enabled: true |
| 1088 |
|
| 1089 |
|
| 1090 |
# REPL Bindings: |
| 1091 |
# %let fn() : int -> 2 |
| 1092 |
# |
| 1093 |
# %let x = 1 |
| 1094 |
# |
| 1095 |
`, |
| 1096 |
wantExit: false, |
| 1097 |
wantError: false, |
| 1098 |
}, |
| 1099 |
{ |
| 1100 |
name: "ConfigureYAML", |
| 1101 |
commands: []Cmder{ |
| 1102 |
&simpleCmd{ |
| 1103 |
cmd: "configure", |
| 1104 |
args: []string{ |
| 1105 |
"--yaml", |
| 1106 |
"--file", |
| 1107 |
testYAMLEnvFile, |
| 1108 |
}, |
| 1109 |
}, |
| 1110 |
&evalCmd{ |
| 1111 |
expr: "optional.of(math.least([1, 2, 3])).value()", |
| 1112 |
}, |
| 1113 |
}, |
| 1114 |
wantText: `1 : int`, |
| 1115 |
wantExit: false, |
| 1116 |
wantError: false, |
| 1117 |
}, |
| 1118 |
{ |
| 1119 |
name: "ConfigureYAMLReplTrailer", |
| 1120 |
commands: []Cmder{ |
| 1121 |
&simpleCmd{ |
| 1122 |
cmd: "configure", |
| 1123 |
args: []string{ |
| 1124 |
"--yaml", |
| 1125 |
"--file", |
| 1126 |
testYAMLEnvFile, |
| 1127 |
}, |
| 1128 |
}, |
| 1129 |
&evalCmd{ |
| 1130 |
expr: "has(pb3.single_int64)", |
| 1131 |
}, |
| 1132 |
}, |
| 1133 |
wantText: `false : bool`, |
| 1134 |
wantExit: false, |
| 1135 |
wantError: false, |
| 1136 |
}, |
| 1137 |
{ |
| 1138 |
name: "ConfigureYAMLReplMultiline", |
| 1139 |
commands: []Cmder{ |
| 1140 |
&simpleCmd{ |
| 1141 |
cmd: "configure", |
| 1142 |
args: []string{ |
| 1143 |
"--yaml", |
| 1144 |
"--file", |
| 1145 |
testMultilineYAMLEnvFile, |
| 1146 |
}, |
| 1147 |
}, |
| 1148 |
&evalCmd{ |
| 1149 |
expr: "pb3.single_int64", |
| 1150 |
}, |
| 1151 |
}, |
| 1152 |
wantText: `42 : int`, |
| 1153 |
wantExit: false, |
| 1154 |
wantError: false, |
| 1155 |
}, |
| 1156 |
{ |
| 1157 |
name: "ConfigureError", |
| 1158 |
commands: []Cmder{ |
| 1159 |
&simpleCmd{ |
| 1160 |
cmd: "configure", |
| 1161 |
args: []string{ |
| 1162 |
"--yaml", |
| 1163 |
`name: repl-session |
| 1164 |
container: cel.expr.conformance |
| 1165 |
extensions: |
| 1166 |
- name: unknown-ext |
| 1167 |
version: latest`, |
| 1168 |
}, |
| 1169 |
}, |
| 1170 |
}, |
| 1171 |
wantText: "", |
| 1172 |
wantExit: false, |
| 1173 |
wantError: true, |
| 1174 |
}, |
| 1175 |
{ |
| 1176 |
name: "ConfigureErrorReplTrailer", |
| 1177 |
commands: []Cmder{ |
| 1178 |
&simpleCmd{ |
| 1179 |
cmd: "configure", |
| 1180 |
args: []string{ |
| 1181 |
"--yaml", |
| 1182 |
`name: repl-session |
| 1183 |
container: cel.expr.conformance |
| 1184 |
extensions: |
| 1185 |
- name: optional |
| 1186 |
version: latest |
| 1187 |
|
| 1188 |
# REPL Bindings: |
| 1189 |
# %let foo : int = "bar" |
| 1190 |
# |
| 1191 |
`, |
| 1192 |
}, |
| 1193 |
}, |
| 1194 |
}, |
| 1195 |
wantText: "", |
| 1196 |
wantExit: false, |
| 1197 |
wantError: true, |
| 1198 |
}, |
| 1199 |
{ |
| 1200 |
name: "Reset", |
| 1201 |
commands: []Cmder{ |
| 1202 |
&simpleCmd{ |
| 1203 |
cmd: "option", |
| 1204 |
args: []string{ |
| 1205 |
"--container", |
| 1206 |
"google", |
| 1207 |
}, |
| 1208 |
}, |
| 1209 |
&letVarCmd{ |
| 1210 |
identifier: "x", |
| 1211 |
typeHint: mustParseType(t, "int"), |
| 1212 |
src: "1", |
| 1213 |
}, |
| 1214 |
&letFnCmd{ |
| 1215 |
identifier: "fn", |
| 1216 |
src: "2", |
| 1217 |
resultType: mustParseType(t, "int"), |
| 1218 |
params: nil, |
| 1219 |
}, |
| 1220 |
&simpleCmd{ |
| 1221 |
cmd: "reset", |
| 1222 |
args: nil, |
| 1223 |
}, |
| 1224 |
&simpleCmd{ |
| 1225 |
cmd: "status", |
| 1226 |
args: []string{}, |
| 1227 |
}, |
| 1228 |
}, |
| 1229 |
wantText: ``, |
| 1230 |
wantExit: false, |
| 1231 |
wantError: false, |
| 1232 |
}, |
| 1233 |
} |
| 1234 |
|
| 1235 |
for _, tc := range testCases { |
| 1236 |
t.Run(tc.name, func(t *testing.T) { |
| 1237 |
eval, err := NewEvaluator() |
| 1238 |
if err != nil { |
| 1239 |
t.Fatalf("NewEvaluator returned error: %v, wanted nil", err) |
| 1240 |
} |
| 1241 |
eval.env, _ = eval.env.Extend(cel.Types(&proto2pb.ExampleType{}, &proto2pb.ExternalMessageType{})) |
| 1242 |
n := len(tc.commands) |
| 1243 |
for _, cmd := range tc.commands[:n-1] { |
| 1244 |
// only need output of last command |
| 1245 |
eval.Process(cmd) |
| 1246 |
} |
| 1247 |
text, exit, err := eval.Process(tc.commands[n-1]) |
| 1248 |
|
| 1249 |
gotErr := false |
| 1250 |
if err != nil { |
| 1251 |
gotErr = true |
| 1252 |
} |
| 1253 |
wantText := tc.wantText |
| 1254 |
if tc.ignoreWhitespace { |
| 1255 |
text = stripWhitespace(text) |
| 1256 |
wantText = stripWhitespace(wantText) |
| 1257 |
} |
| 1258 |
diff := cmp.Diff(text, wantText) |
| 1259 |
if diff != "" || exit != tc.wantExit || (gotErr != tc.wantError) { |
| 1260 |
t.Errorf("For command %s got (diff: '%s' exit: %v err: %v (%v)) wanted (output: '%s' exit: %v err: %v)", |
| 1261 |
tc.commands[n-1], diff, exit, gotErr, err, tc.wantText, tc.wantExit, tc.wantError) |
| 1262 |
} |
| 1263 |
}) |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
func TestProcessOptionError(t *testing.T) { |
| 1268 |
var testCases = []struct { |
| 1269 |
name string |
| 1270 |
command Cmder |
| 1271 |
errorMsg string |
| 1272 |
}{ |
| 1273 |
{ |
| 1274 |
name: "OptionContainerNotEnoughArgs", |
| 1275 |
command: &simpleCmd{ |
| 1276 |
cmd: "option", |
| 1277 |
args: []string{ |
| 1278 |
"--container", |
| 1279 |
}, |
| 1280 |
}, |
| 1281 |
errorMsg: "container: not enough arguments", |
| 1282 |
}, |
| 1283 |
{ |
| 1284 |
name: "OptionExtensionNotEnoughArgs", |
| 1285 |
command: &simpleCmd{ |
| 1286 |
cmd: "option", |
| 1287 |
args: []string{ |
| 1288 |
"--extension", |
| 1289 |
}, |
| 1290 |
}, |
| 1291 |
errorMsg: "extension: not enough arguments", |
| 1292 |
}, |
| 1293 |
{ |
| 1294 |
name: "OptionExtensionInvalid", |
| 1295 |
command: &simpleCmd{ |
| 1296 |
cmd: "option", |
| 1297 |
args: []string{ |
| 1298 |
"--extension", |
| 1299 |
"'bogus'", |
| 1300 |
}, |
| 1301 |
}, |
| 1302 |
errorMsg: "extension: unknown option: 'bogus'. Available options are: ['all', 'bindings', 'encoders', 'lists', 'math', 'optional', 'protos', 'sets', 'strings', 'two_var_comprehensions']", |
| 1303 |
}, |
| 1304 |
} |
| 1305 |
|
| 1306 |
for _, tc := range testCases { |
| 1307 |
t.Run(tc.name, func(t *testing.T) { |
| 1308 |
eval, err := NewEvaluator() |
| 1309 |
if err != nil { |
| 1310 |
t.Fatalf("NewEvaluator returned error: %v, wanted nil", err) |
| 1311 |
} |
| 1312 |
_, _, err = eval.Process(tc.command) |
| 1313 |
|
| 1314 |
if err == nil { |
| 1315 |
t.Fatalf("Expected an error processing command: %s", tc.command) |
| 1316 |
} |
| 1317 |
|
| 1318 |
if err.Error() != tc.errorMsg { |
| 1319 |
t.Errorf("For command %s got (error: '%s') wanted (error: '%s')", |
| 1320 |
tc.command, err.Error(), tc.errorMsg) |
| 1321 |
} |
| 1322 |
}) |
| 1323 |
} |
| 1324 |
} |
| 1325 |
|
| 1326 |
func stripWhitespace(a string) string { |
| 1327 |
a = strings.ReplaceAll(a, " ", "") |
| 1328 |
a = strings.ReplaceAll(a, "\n", "") |
| 1329 |
a = strings.ReplaceAll(a, "\t", "") |
| 1330 |
return strings.ReplaceAll(a, "\r", "") |
| 1331 |
} |