-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathStatementExecutionTests.cs
More file actions
421 lines (374 loc) · 15.9 KB
/
Copy pathStatementExecutionTests.cs
File metadata and controls
421 lines (374 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------
namespace CosmosShell.Tests.Parser;
using System;
using System.IO;
using System.Threading.Tasks;
using Azure.Data.Cosmos.Shell.Parser;
/// <summary>
/// Executes whole scripts to drive the <c>RunAsync</c> branches of control-flow
/// statements (if/else, while, do-while, for, loop, break, continue, return, blocks)
/// together with the condition-evaluation paths in the expression parser.
/// </summary>
public class StatementExecutionTests : TestBase
{
public static System.Collections.Generic.IEnumerable<object[]> ValueOriginCases()
{
var operations = new (string Left, string Operator, string Right, string Type, string Expected)[]
{
("\"2\"", "+", "\"2\"", "Text", "22"),
("\"hello\"", "+", "\"world\"", "Text", "helloworld"),
("\"\"", "+", "\"text\"", "Text", "text"),
("\"value=\"", "+", "2", "Text", "value=2"),
("2", "+", "\"px\"", "Text", "2px"),
("3", "+", "2", "Number", "5"),
("3.5", "+", "2", "Decimal", "5.5"),
("3", "/", "2", "Number", "1"),
("3.0", "/", "2", "Decimal", "1.5"),
("[1]", "+", "[2]", "Json", "[1,2]"),
("\"a\"", "==", "\"a\"", "Boolean", "true"),
("true", "&&", "false", "Boolean", "false"),
};
foreach (var operation in operations)
{
var leftOrigins = new[] { operation.Left, "$source.left", "$leftItem", "(identity $source.left)" };
var rightOrigins = new[] { operation.Right, "$source.right", "$rightItem", "(identity $source.right)" };
foreach (var left in leftOrigins)
{
foreach (var right in rightOrigins)
{
var script = $"def identity [value] {{ return $value }}; " +
$"for $leftItem in $source.leftItems {{ for $rightItem in $source.rightItems {{ " +
$"$actual = {left} {operation.Operator} {right} }} }}";
var source = $"{{\"left\":{operation.Left},\"right\":{operation.Right}," +
$"\"leftItems\":[{operation.Left}],\"rightItems\":[{operation.Right}]}}";
yield return [source, script, operation.Type, operation.Expected];
yield return [source, $"$source = {source}; " + script, operation.Type, operation.Expected];
}
}
}
}
[Theory]
[MemberData(nameof(ValueOriginCases))]
public async Task Operators_PreserveResultsAcrossValueOrigins(string source, string script, string expectedType, string expected)
{
using var document = System.Text.Json.JsonDocument.Parse(source);
SetVariable("source", new ShellJson(document.RootElement.Clone()));
var state = await Shell.RunCommandAsync(new(), script, TestContext.Current.CancellationToken);
Assert.False(state.IsError);
var actual = GetVariable("actual")!;
Assert.Equal(expectedType, actual.DataType.ToString());
Assert.Equal(expected, actual.ConvertShellObject(DataType.Text));
}
[Theory]
[InlineData("3.0", 1.5)]
[InlineData("-3.0", -1.5)]
[InlineData("0.0", 0.0)]
[InlineData("1.5 * 2", 1.5)]
[InlineData("3.5", 1.75)]
public async Task DecimalValues_SurviveRepeatedJsonConstruction(string expression, double expected)
{
var script = $"$initial = {expression}; $object = {{\"value\":$initial}}; " +
"$array = [$object.value]; for $item in $array { " +
"$rebuilt = {\"value\":$item}; $result = $rebuilt.value / 2 }";
var state = await Shell.RunCommandAsync(new(), script, TestContext.Current.CancellationToken);
Assert.False(state.IsError);
Assert.Equal(expected, Assert.IsType<ShellDecimal>(GetVariable("result")).Value);
var rebuilt = Assert.IsType<ShellJson>(GetVariable("rebuilt"));
Assert.IsType<ShellDecimal>(ShellNumber.FromJson(rebuilt.Value.GetProperty("value")));
}
[Fact]
public async Task SyntaxError_PreventsEarlierAssignment()
{
SetVariable("value", new ShellNumber(1));
var state = await Shell.RunCommandAsync(new(), "$value = 2; if true {", System.Threading.CancellationToken.None);
Assert.True(state.IsError);
Assert.Equal(1, GetInt("value"));
}
[Theory]
[InlineData("break")]
[InlineData("continue")]
[InlineData("return 1")]
[InlineData("if false { break }")]
[InlineData("def duplicate [value value] { return $value }")]
[InlineData("for $item in [1] { def invalid { break } }")]
public async Task SemanticError_PreventsEarlierAssignment(string invalid)
{
SetVariable("value", new ShellNumber(1));
var state = await Shell.RunCommandAsync(new(), $"$value = 2; {invalid}", System.Threading.CancellationToken.None);
Assert.True(state.IsError);
Assert.Equal(1, GetInt("value"));
}
private int GetInt(string name)
{
var value = GetVariable(name);
return (int)Assert.IsType<ShellNumber>(value).Value;
}
[Theory]
[InlineData("if true { return 1 }")]
[InlineData("while true { if true { return 1 } }")]
[InlineData("do { if true { return 1 } } while true")]
[InlineData("loop { if true { return 1 } }")]
[InlineData("for $item in [1,2] { if true { return 1 } }")]
public async Task NestedReturn_ExitsFunction(string body)
{
var state = await RunScriptAsync($"def probe {{ {body}; return 2 }}; $result = (probe)");
Assert.False(state.IsError);
Assert.Equal(1, GetInt("result"));
Assert.False(state.ReturnFunc);
}
[Theory]
[InlineData("for $item in [1,2] { if true { continue }; $count = $count + 1 }")]
[InlineData("while $index < 2 { $index = $index + 1; if true { continue }; $count = $count + 1 }")]
[InlineData("do { $index = $index + 1; if true { continue }; $count = $count + 1 } while $index < 2")]
[InlineData("loop { $index = $index + 1; if $index > 2 { break }; if true { continue }; $count = $count + 1 }")]
public async Task NestedContinue_SkipsRemainderOfIteration(string body)
{
var state = await RunScriptAsync($"$count = 0; $index = 0; {body}");
Assert.False(state.IsError);
Assert.Equal(0, GetInt("count"));
Assert.False(state.ContinueBlock);
}
[Fact]
public async Task If_TrueCondition_ExecutesThenBranch()
{
var state = await RunScriptAsync("$x = 0\nif 1 < 2 { $x = 10 } else { $x = 20 }");
Assert.False(state.IsError);
Assert.Equal(10, GetInt("x"));
}
[Theory]
[InlineData("dir \"*.missing-regression-file\" --directory .")]
[InlineData("$files = (dir \"*.missing-regression-file\" --directory .)")]
public async Task ReusedCommand_BindsOptionsWithoutMutatingAst(string command)
{
var state = await RunScriptAsync($"def probe {{ {command} }}; probe; probe");
Assert.False(state.IsError);
}
[Fact]
public async Task If_FalseCondition_ExecutesElseBranch()
{
var state = await RunScriptAsync("$x = 0\nif 1 > 2 { $x = 10 } else { $x = 20 }");
Assert.False(state.IsError);
Assert.Equal(20, GetInt("x"));
}
[Fact]
public async Task If_FalseCondition_NoElse_DoesNothing()
{
var state = await RunScriptAsync("$x = 5\nif false { $x = 99 }");
Assert.False(state.IsError);
Assert.Equal(5, GetInt("x"));
}
[Fact]
public async Task While_Counts_UntilConditionFalse()
{
var state = await RunScriptAsync("$i = 0\nwhile $i < 3 { $i = ($i + 1) }");
Assert.False(state.IsError);
Assert.Equal(3, GetInt("i"));
}
[Fact]
public async Task While_Break_ExitsEarly()
{
var state = await RunScriptAsync("$i = 0\nwhile $i < 100 { $i = ($i + 1)\nif $i == 5 break }");
Assert.False(state.IsError);
Assert.Equal(5, GetInt("i"));
}
[Fact]
public async Task While_Continue_SkipsRemainderOfBody()
{
var script = "$i = 0\n$count = 0\nwhile $i < 5 { $i = ($i + 1)\nif $i == 3 continue\n$count = ($count + 1) }";
var state = await RunScriptAsync(script);
Assert.False(state.IsError);
Assert.Equal(5, GetInt("i"));
Assert.Equal(4, GetInt("count"));
}
[Fact]
public async Task DoWhile_ExecutesBodyAtLeastOnce_WhenConditionFalse()
{
var state = await RunScriptAsync("$x = 0\ndo { $x = ($x + 1) } while $x < 0");
Assert.False(state.IsError);
Assert.Equal(1, GetInt("x"));
}
[Fact]
public async Task For_OverArray_AccumulatesValues()
{
var state = await RunScriptAsync("$sum = 0\nfor $i in [1, 2, 3, 4] { $sum = ($sum + $i) }");
Assert.False(state.IsError);
Assert.Equal(10, GetInt("sum"));
}
[Fact]
public async Task For_Break_StopsIteration()
{
var state = await RunScriptAsync("$sum = 0\nfor $i in [1, 2, 3, 4] { if $i == 3 break\n$sum = ($sum + $i) }");
Assert.False(state.IsError);
Assert.Equal(3, GetInt("sum"));
}
[Fact]
public async Task For_Continue_SkipsSelectedIterations()
{
var state = await RunScriptAsync("$sum = 0\nfor $i in [1, 2, 3, 4] { if $i == 2 continue\n$sum = ($sum + $i) }");
Assert.False(state.IsError);
Assert.Equal(8, GetInt("sum"));
}
[Fact]
public async Task Loop_Break_TerminatesWithCounter()
{
var state = await RunScriptAsync("$i = 0\nloop { $i = ($i + 1)\nif $i == 7 break }");
Assert.False(state.IsError);
Assert.Equal(7, GetInt("i"));
}
[Fact]
public async Task NestedControlFlow_ComputesExpected()
{
var script = "$total = 0\nfor $i in [1, 2, 3] { $j = 0\nwhile $j < $i { $total = ($total + 1)\n$j = ($j + 1) } }";
var state = await RunScriptAsync(script);
Assert.False(state.IsError);
Assert.Equal(6, GetInt("total"));
}
[Fact]
public async Task Block_ExecutesStatementsSequentially()
{
var state = await RunScriptAsync("{ $a = 1\n$b = ($a + 1)\n$c = ($b + 1) }");
Assert.False(state.IsError);
Assert.Equal(1, GetInt("a"));
Assert.Equal(2, GetInt("b"));
Assert.Equal(3, GetInt("c"));
}
[Fact]
public async Task Block_PrintFailure_ReturnsError()
{
Shell.StdOutRedirect = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"), "out.txt");
var state = await RunScriptAsync("{ echo value }");
Assert.True(state.IsError);
}
[Fact]
public async Task Pipe_PrintFailure_ReturnsError()
{
Shell.StdOutRedirect = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"), "out.txt");
var state = await RunScriptAsync("echo value | echo");
Assert.True(state.IsError);
}
[Fact]
public async Task Pipe_FinalReturn_DoesNotRenderPreviousResult()
{
var output = Path.GetTempFileName();
Shell.StdOutRedirect = output;
try
{
var state = await RunScriptAsync("def run { echo value | return }; run");
Assert.False(state.IsError);
Assert.Empty(await File.ReadAllTextAsync(output, TestContext.Current.CancellationToken));
}
finally
{
Shell.StdOutRedirect = null;
File.Delete(output);
}
}
[Fact]
public async Task Assignment_ChainedArithmetic_ComputesExpected()
{
var state = await RunScriptAsync("$x = ((2 + 3) * 4 - 1)");
Assert.False(state.IsError);
Assert.Equal(19, GetInt("x"));
}
[Theory]
[InlineData("+=", 9)]
[InlineData("-=", 3)]
[InlineData("*=", 18)]
[InlineData("/=", 2)]
public async Task CompoundAssignment_UsesArithmeticRules(string assignment, int expected)
{
var script = $"$value = 6; $value {assignment} 3";
var parser = new StatementParser(script);
var statements = parser.ParseStatements();
Assert.False(parser.Errors.HasErrors);
Assert.Equal($"$value {assignment} 3", statements[1].ToString());
var state = await RunScriptAsync(script);
Assert.False(state.IsError);
Assert.Equal(expected, GetInt("value"));
}
[Fact]
public async Task CompoundAssignment_InFunction_RemainsLocal()
{
var state = await RunScriptAsync("$value = 1; def increment { $value += 2; return $value }; $local = (increment)");
Assert.False(state.IsError);
Assert.Equal(1, GetInt("value"));
Assert.Equal(3, GetInt("local"));
}
[Theory]
[InlineData("while true {}")]
[InlineData("do {} while true")]
[InlineData("loop {}")]
[InlineData("for $value in [1] {}")]
public async Task PureLoop_ObservesCancellation(string script)
{
var statement = Assert.Single(new StatementParser(script).ParseStatements());
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => statement.RunAsync(Shell, new(), new System.Threading.CancellationToken(true)));
}
[Fact]
public async Task For_OverStrings_BindsTextElements()
{
var state = await RunScriptAsync("for $x in [\"a\", \"b\"] { }");
Assert.False(state.IsError);
Assert.Equal("b", Assert.IsType<ShellText>(GetVariable("x")).Text);
}
[Fact]
public async Task For_OverBooleans_BindsBoolElements()
{
var state = await RunScriptAsync("for $x in [true, false] { }");
Assert.False(state.IsError);
Assert.False(Assert.IsType<ShellBool>(GetVariable("x")).Value);
}
[Fact]
public async Task For_OverNull_PreservesNullThroughFunctionAndArray()
{
var state = await RunScriptAsync("def identity [value] { return $value }; for $x in [null] { $result = [(identity $x)] }");
Assert.False(state.IsError);
Assert.Equal(System.Text.Json.JsonValueKind.Null, Assert.IsType<ShellJson>(GetVariable("x")).Value.ValueKind);
Assert.Equal("[null]", Assert.IsType<ShellJson>(GetVariable("result")).Value.GetRawText());
}
[Theory]
[InlineData("1.5", true)]
[InlineData("-0.5", true)]
[InlineData("2147483648.0", true)]
[InlineData("0.0", false)]
public async Task NumericTruthiness_IsIndependentOfValueOrigin(string number, bool expected)
{
var state = await RunScriptAsync($"$direct = false; if {number} {{ $direct = true }}; $obj = {{value: {number}}}; $json = false; if $obj.value {{ $json = true }}; def truth [value] {{ if $value {{ return true }}; return false }}; $function = (truth $obj.value); for $item in [{number}] {{ $loop = (truth $item) }}");
Assert.False(state.IsError);
foreach (var name in new[] { "direct", "json", "function", "loop" })
{
Assert.Equal(expected, Assert.IsType<ShellBool>(GetVariable(name)).Value);
}
}
[Fact]
public async Task For_OverObjects_BindsJsonElements()
{
var state = await RunScriptAsync("for $x in [{ id: 1 }] { }");
Assert.False(state.IsError);
Assert.IsType<ShellJson>(GetVariable("x"));
}
[Fact]
public async Task For_OverNestedArrays_BindsJsonElements()
{
var state = await RunScriptAsync("for $x in [[1, 2], [3, 4]] { }");
Assert.False(state.IsError);
Assert.IsType<ShellJson>(GetVariable("x"));
}
[Fact]
public async Task For_OverNonArray_Throws()
{
await Assert.ThrowsAsync<InvalidOperationException>(
() => RunScriptAsync("for $x in 5 { }"));
}
[Theory]
[InlineData("[1.5,2.5]", 4.0)]
[InlineData("[2147483648.0,1]", 2147483649.0)]
public async Task For_OverDecimalAndLargeNumbers_PreservesValues(string values, double expected)
{
var state = await RunScriptAsync($"$sum = 0; for $value in {values} {{ $sum = $sum + $value }}");
Assert.False(state.IsError);
Assert.Equal(expected, Assert.IsType<ShellDecimal>(GetVariable("sum")).Value);
}
}