-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionExecutionTests.cs
More file actions
99 lines (91 loc) · 2.93 KB
/
Copy pathExpressionExecutionTests.cs
File metadata and controls
99 lines (91 loc) · 2.93 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
using AuroraScript.Runtime;
using AuroraScript.Tests.Infrastructure;
using System;
using Xunit;
namespace AuroraScript.Tests;
public sealed class ExpressionExecutionTests
{
[Theory]
[MemberData(nameof(ExpressionCases))]
public void EvaluatesOperatorsAndLiteralsInReleaseMode(string expression, object? expected)
{
using var workspace = new TestWorkspace();
var engine = workspace.CreateEngine();
var block = engine.CompileBlock("return " + expression + ";");
ScriptAssert.Equal(expected, block.Invoke(Array.Empty<ScriptDatum>()));
}
public static TheoryData<string, object?> ExpressionCases => new()
{
{ "1 + 2 * 3", 7 },
{ "(1 + 2) * 3", 9 },
{ "20 / 4 + 7 % 4", 8 },
{ "-5 + 2", -3 },
{ "~1", -2 },
{ "1 << 5", 32 },
{ "32 >> 2", 8 },
{ "32 >>> 2", 8 },
{ "5 & 3", 1 },
{ "5 | 2", 7 },
{ "5 ^ 1", 4 },
{ "3 < 4", true },
{ "3 <= 3", true },
{ "4 > 3", true },
{ "4 >= 4", true },
{ "4 == 4", true },
{ "4 != 5", true },
{ "null + null", 0 },
{ "1 + null", 1 },
{ "null + 1", 1 },
{ "true && false", false },
{ "false || true", true },
{ "!false", true },
{ "null", null },
{ "'Aurora' + 'Script'", "AuroraScript" },
{ "typeof null", "null" },
{ "typeof 1", "number" },
{ "typeof 'x'", "string" },
{ "typeof true", "boolean" },
{ "typeof []", "array" },
{ "typeof {}", "object" },
{ "[1, 2, 3][1]", 2 },
{ "{ value: 9 }.value", 9 },
{ "'value' in { value: 1 }", true },
{ "`sum=${1 + 2}`", "sum=3" },
{ "[1, ...[2, 3], 4]", new object?[] { 1, 2, 3, 4 } }
};
[Fact]
public void EvaluatesAssignmentsAndIncrementOperators()
{
using var workspace = new TestWorkspace();
var engine = workspace.CreateEngine();
var block = engine.CompileBlock(
"""
var value = 2;
var before = value++;
var after = ++value;
value += 3;
value *= 2;
value -= 4;
value /= 2;
value %= 5;
return [before, after, value];
""");
ScriptAssert.Equal(new object?[] { 2, 4, 0 }, block.Invoke(Array.Empty<ScriptDatum>()));
}
[Fact]
public void EvaluatesMemberAndIndexMutationAndDelete()
{
using var workspace = new TestWorkspace();
var engine = workspace.CreateEngine();
var block = engine.CompileBlock(
"""
var object = { value: 1 };
object.value = 4;
var array = [1, 2];
array[1] = object.value;
delete object.value;
return [array[1], 'value' in object];
""");
ScriptAssert.Equal(new object?[] { 4, false }, block.Invoke(Array.Empty<ScriptDatum>()));
}
}