| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package repl |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"google.golang.org/protobuf/proto" |
| 24 |
) |
| 25 |
|
| 26 |
func cmdMatches(t testing.TB, got Cmder, expected Cmder) (result bool) { |
| 27 |
t.Helper() |
| 28 |
|
| 29 |
defer func() { |
| 30 |
|
| 31 |
if v := recover(); v != nil { |
| 32 |
result = false |
| 33 |
} |
| 34 |
}() |
| 35 |
|
| 36 |
switch want := expected.(type) { |
| 37 |
case *compileCmd: |
| 38 |
gotCompile := got.(*compileCmd) |
| 39 |
return gotCompile.expr == want.expr |
| 40 |
case *evalCmd: |
| 41 |
gotEval := got.(*evalCmd) |
| 42 |
return gotEval.expr == want.expr |
| 43 |
case *delCmd: |
| 44 |
gotDel := got.(*delCmd) |
| 45 |
return gotDel.identifier == want.identifier |
| 46 |
case *simpleCmd: |
| 47 |
gotSimple := got.(*simpleCmd) |
| 48 |
if len(gotSimple.args) != len(want.args) { |
| 49 |
return false |
| 50 |
} |
| 51 |
for i, a := range want.args { |
| 52 |
if gotSimple.args[i] != a { |
| 53 |
return false |
| 54 |
} |
| 55 |
} |
| 56 |
return gotSimple.cmd == want.cmd |
| 57 |
case *letFnCmd: |
| 58 |
gotLetFn := got.(*letFnCmd) |
| 59 |
if gotLetFn.identifier != want.identifier || |
| 60 |
gotLetFn.src != want.src || |
| 61 |
!proto.Equal(gotLetFn.resultType, want.resultType) || |
| 62 |
len(gotLetFn.params) != len(want.params) { |
| 63 |
return false |
| 64 |
} |
| 65 |
for i, wantP := range want.params { |
| 66 |
if gotLetFn.params[i].identifier != wantP.identifier || |
| 67 |
!proto.Equal(gotLetFn.params[i].typeHint, wantP.typeHint) { |
| 68 |
return false |
| 69 |
} |
| 70 |
} |
| 71 |
return true |
| 72 |
case *letVarCmd: |
| 73 |
gotLetVar := got.(*letVarCmd) |
| 74 |
return gotLetVar.identifier == want.identifier && |
| 75 |
proto.Equal(gotLetVar.typeHint, want.typeHint) && |
| 76 |
gotLetVar.src == want.src |
| 77 |
} |
| 78 |
return false |
| 79 |
} |
| 80 |
|
| 81 |
func (c *evalCmd) String() string { |
| 82 |
return fmt.Sprintf("%%eval %s", c.expr) |
| 83 |
} |
| 84 |
|
| 85 |
func (c *letVarCmd) String() string { |
| 86 |
return fmt.Sprintf("%%let %s : %s = %s", c.identifier, UnparseType(c.typeHint), c.src) |
| 87 |
} |
| 88 |
|
| 89 |
func fmtParam(p letFunctionParam) string { |
| 90 |
return fmt.Sprintf("%s : %s", p.identifier, UnparseType(p.typeHint)) |
| 91 |
} |
| 92 |
|
| 93 |
func fmtParams(ps []letFunctionParam) string { |
| 94 |
buf := make([]string, len(ps)) |
| 95 |
for i, p := range ps { |
| 96 |
buf[i] = fmtParam(p) |
| 97 |
} |
| 98 |
return strings.Join(buf, ", ") |
| 99 |
} |
| 100 |
|
| 101 |
func (c *letFnCmd) String() string { |
| 102 |
return fmt.Sprintf("%%let %s (%s) : %s -> %s", c.identifier, fmtParams(c.params), UnparseType(c.resultType), c.src) |
| 103 |
} |
| 104 |
|
| 105 |
func (c *delCmd) String() string { |
| 106 |
return fmt.Sprintf("%%delete %s", c.identifier) |
| 107 |
} |
| 108 |
|
| 109 |
func (c *simpleCmd) String() string { |
| 110 |
flagFmt := strings.Join(c.args, " ") |
| 111 |
return fmt.Sprintf("%%%s %s", c.cmd, flagFmt) |
| 112 |
} |
| 113 |
|
| 114 |
func TestParse(t *testing.T) { |
| 115 |
var testCases = []struct { |
| 116 |
commandLine string |
| 117 |
wantCmd Cmder |
| 118 |
wantErr error |
| 119 |
}{ |
| 120 |
{ |
| 121 |
commandLine: "%let x = 1", |
| 122 |
wantCmd: &letVarCmd{ |
| 123 |
identifier: "x", |
| 124 |
typeHint: nil, |
| 125 |
src: "1", |
| 126 |
}, |
| 127 |
}, |
| 128 |
{ |
| 129 |
commandLine: "%let com.google.x = 1", |
| 130 |
wantCmd: &letVarCmd{ |
| 131 |
identifier: "com.google.x", |
| 132 |
typeHint: nil, |
| 133 |
src: "1", |
| 134 |
}, |
| 135 |
}, |
| 136 |
{ |
| 137 |
commandLine: "%let x: int = 1", |
| 138 |
wantCmd: &letVarCmd{ |
| 139 |
identifier: "x", |
| 140 |
typeHint: mustParseType(t, "int"), |
| 141 |
src: "1", |
| 142 |
}, |
| 143 |
}, |
| 144 |
{ |
| 145 |
commandLine: `%compile x + 2`, |
| 146 |
wantCmd: &compileCmd{expr: "x + 2"}, |
| 147 |
}, |
| 148 |
{ |
| 149 |
commandLine: `%eval x + 2`, |
| 150 |
wantCmd: &evalCmd{expr: "x + 2"}, |
| 151 |
}, |
| 152 |
{ |
| 153 |
commandLine: "x + 2", |
| 154 |
wantCmd: &evalCmd{expr: "x + 2"}, |
| 155 |
}, |
| 156 |
{ |
| 157 |
commandLine: `%exit`, |
| 158 |
wantCmd: &simpleCmd{cmd: "exit"}, |
| 159 |
}, |
| 160 |
{ |
| 161 |
commandLine: `%help`, |
| 162 |
wantErr: errors.New(`Compile emits a textproto representation of the compiled expression. |
| 163 |
%compile <expr> |
| 164 |
|
| 165 |
Parse emits a textproto representation of the parsed expression. |
| 166 |
%parse <expr> |
| 167 |
|
| 168 |
Declare introduces a variable or function for type checking, but |
| 169 |
doesn't define a value for it: |
| 170 |
%declare <identifier> : <type> |
| 171 |
%declare <identifier> (<param_identifier> : <param_type>, ...) : <result-type> |
| 172 |
|
| 173 |
Delete removes a variable or function declaration from the evaluation context. |
| 174 |
%delete <identifier> |
| 175 |
|
| 176 |
Let introduces a variable or function defined by a sub-CEL expression. |
| 177 |
%let <identifier> (: <type>)? = <expr> |
| 178 |
%let <identifier> (<param_identifier> : <param_type>, ...) : <result-type> -> <expr> |
| 179 |
|
| 180 |
Status prints the current state of the REPL session |
| 181 |
%status |
| 182 |
%status --yaml |
| 183 |
|
| 184 |
Config loads a canned REPL state from a config file |
| 185 |
%configure """%let foo : int = 42""" |
| 186 |
%configure --yaml --file 'path/to/env.yaml' |
| 187 |
|
| 188 |
Option enables a CEL environment option which enables configuration and |
| 189 |
optional language features. |
| 190 |
%option --container 'google.protobuf' |
| 191 |
%option --extension 'all' |
| 192 |
|
| 193 |
LoadDescriptors loads a protobuf descriptor file (google.protobuf.FileDescriptorSet) |
| 194 |
from disk or from a predefined package. Supported packages are "cel-spec-test-types" |
| 195 |
(TestAllTypes) and "google-rpc" (AttributeContext). |
| 196 |
%load_descriptors 'path/to/descriptor_set.binarypb' |
| 197 |
%load_descriptors --pkg 'cel-spec-test-types' |
| 198 |
|
| 199 |
Help prints usage information for the commands supported by the REPL. |
| 200 |
%help |
| 201 |
|
| 202 |
Exit terminates the REPL. |
| 203 |
%exit`), |
| 204 |
}, |
| 205 |
{ |
| 206 |
commandLine: `%arbitrary --flag --another-flag 'string literal\n'`, |
| 207 |
wantCmd: &simpleCmd{cmd: "arbitrary", |
| 208 |
args: []string{ |
| 209 |
"--flag", "--another-flag", "string literal\\n", |
| 210 |
}, |
| 211 |
}, |
| 212 |
}, |
| 213 |
{ |
| 214 |
commandLine: " ", |
| 215 |
wantCmd: &simpleCmd{cmd: "null"}, |
| 216 |
}, |
| 217 |
{ |
| 218 |
commandLine: `%delete x`, |
| 219 |
wantCmd: &delCmd{identifier: "x"}, |
| 220 |
}, |
| 221 |
{ |
| 222 |
commandLine: `%delete com.google.x`, |
| 223 |
wantCmd: &delCmd{identifier: "com.google.x"}, |
| 224 |
}, |
| 225 |
{ |
| 226 |
commandLine: `%declare x: int`, |
| 227 |
wantCmd: &letVarCmd{ |
| 228 |
identifier: "x", |
| 229 |
typeHint: mustParseType(t, "int"), |
| 230 |
src: "", |
| 231 |
}, |
| 232 |
}, |
| 233 |
{ |
| 234 |
commandLine: `%let fn (x : int) : int -> x + 2`, |
| 235 |
wantCmd: &letFnCmd{ |
| 236 |
identifier: "fn", |
| 237 |
params: []letFunctionParam{ |
| 238 |
{identifier: "x", typeHint: mustParseType(t, "int")}, |
| 239 |
}, |
| 240 |
resultType: mustParseType(t, "int"), |
| 241 |
src: "x + 2", |
| 242 |
}, |
| 243 |
}, |
| 244 |
{ |
| 245 |
commandLine: `%let int.plus (x : int) : int -> this + x`, |
| 246 |
wantCmd: &letFnCmd{ |
| 247 |
identifier: "int.plus", |
| 248 |
params: []letFunctionParam{ |
| 249 |
{identifier: "x", typeHint: mustParseType(t, "int")}, |
| 250 |
}, |
| 251 |
resultType: mustParseType(t, "int"), |
| 252 |
src: "this + x", |
| 253 |
}, |
| 254 |
}, |
| 255 |
{ |
| 256 |
commandLine: `%let fn () : int -> 2 + 2`, |
| 257 |
wantCmd: &letFnCmd{ |
| 258 |
identifier: "fn", |
| 259 |
params: []letFunctionParam{}, |
| 260 |
resultType: mustParseType(t, "int"), |
| 261 |
src: "2 + 2", |
| 262 |
}, |
| 263 |
}, |
| 264 |
{ |
| 265 |
commandLine: `%let fn (x:int, y :int) : int -> x + y`, |
| 266 |
wantCmd: &letFnCmd{ |
| 267 |
identifier: "fn", |
| 268 |
params: []letFunctionParam{ |
| 269 |
{identifier: "x", typeHint: mustParseType(t, "int")}, |
| 270 |
{identifier: "y", typeHint: mustParseType(t, "int")}, |
| 271 |
}, |
| 272 |
resultType: mustParseType(t, "int"), |
| 273 |
src: "x + y", |
| 274 |
}, |
| 275 |
}, |
| 276 |
{ |
| 277 |
commandLine: `%declare fn (x : int) : int`, |
| 278 |
wantCmd: &letFnCmd{ |
| 279 |
identifier: "fn", |
| 280 |
params: []letFunctionParam{ |
| 281 |
{identifier: "x", typeHint: mustParseType(t, "int")}, |
| 282 |
}, |
| 283 |
resultType: mustParseType(t, "int"), |
| 284 |
src: "", |
| 285 |
}, |
| 286 |
}, |
| 287 |
{ |
| 288 |
commandLine: `%eval --parse-only -- foo`, |
| 289 |
wantCmd: &evalCmd{ |
| 290 |
parseOnly: true, |
| 291 |
expr: `foo`, |
| 292 |
}, |
| 293 |
}, |
| 294 |
} |
| 295 |
|
| 296 |
for _, tc := range testCases { |
| 297 |
tc := tc |
| 298 |
t.Run(tc.commandLine, func(t *testing.T) { |
| 299 |
cmd, err := Parse(tc.commandLine) |
| 300 |
if err != nil { |
| 301 |
if tc.wantErr != nil && stripWhitespace(tc.wantErr.Error()) == stripWhitespace(err.Error()) { |
| 302 |
return |
| 303 |
} |
| 304 |
t.Errorf("Parse(\"%s\") failed: %s", tc.commandLine, err) |
| 305 |
} |
| 306 |
if cmd == nil || !cmdMatches(t, cmd, tc.wantCmd) { |
| 307 |
t.Errorf("Parse('%s') got (%s) wanted (%s)", tc.commandLine, |
| 308 |
cmd, tc.wantCmd) |
| 309 |
} |
| 310 |
}) |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
func TestParseErrors(t *testing.T) { |
| 315 |
var testCases = []struct { |
| 316 |
commandLine string |
| 317 |
}{ |
| 318 |
{ |
| 319 |
|
| 320 |
commandLine: "%let 123 = 1", |
| 321 |
}, |
| 322 |
{ |
| 323 |
|
| 324 |
commandLine: "%let x: int", |
| 325 |
}, |
| 326 |
{ |
| 327 |
|
| 328 |
commandLine: "%let fn() : int", |
| 329 |
}, |
| 330 |
{ |
| 331 |
|
| 332 |
commandLine: "%let fn (x) -> x + 1", |
| 333 |
}, |
| 334 |
{ |
| 335 |
|
| 336 |
commandLine: "%let fn (: int) : int -> x + 1", |
| 337 |
}, |
| 338 |
{ |
| 339 |
|
| 340 |
commandLine: "%declare x", |
| 341 |
}, |
| 342 |
{ |
| 343 |
|
| 344 |
commandLine: "%declare 123", |
| 345 |
}, |
| 346 |
{ |
| 347 |
|
| 348 |
commandLine: "%declare 1() : int", |
| 349 |
}, |
| 350 |
{ |
| 351 |
|
| 352 |
commandLine: "%delete 123", |
| 353 |
}, |
| 354 |
{ |
| 355 |
commandLine: "%eval --badflag foo", |
| 356 |
}, |
| 357 |
{ |
| 358 |
commandLine: "%eval --bad-flag foo", |
| 359 |
}, |
| 360 |
{ |
| 361 |
commandLine: "%eval -badflag foo", |
| 362 |
}, |
| 363 |
{ |
| 364 |
commandLine: "%eval -bad-flag foo", |
| 365 |
}, |
| 366 |
{ |
| 367 |
commandLine: "%eval --parse-only foo", |
| 368 |
}, |
| 369 |
{ |
| 370 |
commandLine: "%eval -parse-only foo", |
| 371 |
}, |
| 372 |
} |
| 373 |
for _, tc := range testCases { |
| 374 |
_, err := Parse(tc.commandLine) |
| 375 |
if err == nil { |
| 376 |
t.Errorf("Parse(\"%s\") ok wanted error", tc.commandLine) |
| 377 |
} |
| 378 |
} |
| 379 |
} |