File size: 10,739 Bytes
1ae2e8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
namespace RobloxCS.Tests
{
    public class CodeGenerator_Should
    {
        [Fact]
        public void NativeAttribute_GeneratesLuauAttribute()
        {
            var cleanedLua = GetCleanLua("using Roblox; [Native] void doSomething() { }");
            var lines = GetLines(cleanedLua);
            var expectedLines = new List<string>
            {
                "@native",
                "local function doSomething(): nil",
                    "return nil :: any",
                "end"
            };

            AssertEqualLines(lines, expectedLines);
        }

        [Fact]
        public void IfStatements_GeneratesIf()
        {
            var cleanedLua = GetCleanLua("using static Roblox.Globals; var x = 1; if (x == 4) print(\"x is 4\"); else if (x == \"abc\") print(\"x is abc\"); else print(\"x is unknown\");", 1);
            var lines = GetLines(cleanedLua);
            var expectedLines = new List<string>
            {
                "if x == 4 then",
                    "print(\"x is 4\")",
                "elseif x == \"abc\" then",
                    "print(\"x is abc\")",
                "else",
                    "print(\"x is unknown\")",
                "end"
            };

            AssertEqualLines(lines, expectedLines);
        }

        [Theory]
        [InlineData("var @abc = 1")]
        [InlineData("var @hello_brah = 1")]
        [InlineData("var @SIGMA = 1")]
        public void IdentifierWithAtSymbol_HasAtSymbolRemoved(string expression)
        {
            var cleanedLua = GetCleanLua(expression);
            Assert.Equal(expression.Replace("@", "").Replace("var", "local"), cleanedLua);
        }

        [Theory]
        [InlineData("ToNumber(\"69\")")]
        [InlineData("ToFloat(\"69\")")]
        [InlineData("ToDouble(\"69\")")]
        [InlineData("ToInt(\"69\")")]
        [InlineData("ToUInt(\"69\")")]
        [InlineData("ToShort(\"69\")")]
        [InlineData("ToUShort(\"69\")")]
        [InlineData("ToByte(\"69\")")]
        [InlineData("ToSByte(\"69\")")]
        public void NumberParsingMethods_MacroToToNumber(string methodCall)
        {
            var cleanedLua = GetCleanLua(methodCall);
            Assert.Equal("tonumber(\"69\")", cleanedLua);
        }

        [Fact]
        public void NamespaceDeclaration_GeneratesRuntimeCalls()
        {

            var cleanedLua = GetCleanLua("namespace Test { }");
            var expectedLua = "CS.namespace(\"Test\", @native function(namespace: CS.Namespace)\nend)";
            Assert.Equal(expectedLua, cleanedLua);
        }

        [Fact]
        public void NestedNamespaceDeclaration_GeneratesRuntimeCalls()
        {

            var cleanedLua = GetCleanLua("namespace Test.Nested { }");
            var lines = GetLines(cleanedLua);
            var expectedLines = new List<string>
            {
                "CS.namespace(\"Test\", @native function(namespace: CS.Namespace)",
                    "namespace:namespace(\"Nested\", @native function(namespace: CS.Namespace)",
                    "end)",
                "end)"
            };

            AssertEqualLines(lines, expectedLines);
        }

        [Fact]
        public void ClassDeclaration_GeneratesRuntimeCalls()
        {
            var cleanedLua = GetCleanLua("namespace Test { class HelloWorld { } }");
            var lines = GetLines(cleanedLua);
            var expectedLines = new List<string>
            {
                "CS.namespace(\"Test\", @native function(namespace: CS.Namespace)",
                    "namespace:class(\"HelloWorld\", @native function(namespace: CS.Namespace)",
                        "local class = CS.classDef(\"HelloWorld\", namespace)",
                        "",
                        "function class.new()",
                            "local mt = {}",
                            "local self = CS.classInstance(class, mt, namespace)",
                            "",
                            "",
                            "return self",
                        "end",
                        "",
                        "return class",
                    "end)",
                "end)"
            };

            AssertEqualLines(lines, expectedLines);
        }

        [Theory]
        [InlineData("var x = 5;", "local x = 5")]
        [InlineData("char f = 'f'", "local f: string = \"f\"")]
        [InlineData("object a = 123", "local a: any = 123")]
        public void VariableDeclaration_GeneratesLocal(string input, string expected)
        {
            var cleanedLua = GetCleanLua(input);
            Assert.Equal(expected, cleanedLua);
        }

        [Theory]
        [InlineData("(1 + 2) * 4")]
        [InlineData("(44 / (4 % 6) * 12) - 2")]
        public void Parentheses_GenerateParentheses(string input)
        {
            var cleanedLua = GetCleanLua(input);
            Assert.Equal(input, cleanedLua);
        }

        [Theory]
        [InlineData("69", "69")]
        [InlineData("420.0f", "420")]
        [InlineData("'h'", "\"h\"")]
        [InlineData("\"abcefg\"", "\"abcefg\"")]
        [InlineData("true", "true")]
        [InlineData("false", "false")]
        [InlineData("null", "nil")]
        public void Literal_GeneratesLiteral(string input, string expected)
        {
            var cleanedLua = GetCleanLua(input);
            Assert.Equal(expected, cleanedLua);
        }

        [Fact]
        public void StringInterpolation_GeneratesInterpolation()
        {
            var cleanedLua = GetCleanLua("int count = 6; $\"count: {count}\"", 1);
            var expectedLua = "`count: {count}`";
            Assert.Equal(expectedLua, cleanedLua);
        }

        [Theory]
        [InlineData("Vector3")]
        [InlineData("NumberRange")]
        [InlineData("BrickColor")]
        [InlineData("Instance")]
        public void RobloxType_DoesNotGenerateGetAssemblyTypeCall(string robloxType)
        {

            var cleanedLua = GetCleanLua($"using {Utility.RuntimeAssemblyName}; {robloxType}.a;");
            Assert.Equal(robloxType + ".a", cleanedLua);
        }

        [Theory]
        [InlineData("Instance")]
        [InlineData($"{Utility.RuntimeAssemblyName}.Instance")]
        public void InstanceCreate_Macros(string instanceClassPath)
        {
            var cleanedLua = GetCleanLua($"using {Utility.RuntimeAssemblyName}; var part = {instanceClassPath}.Create<Part>()");
            Assert.Equal("local part = Instance.new(\"Part\")", cleanedLua);
        }

        [Fact]
        public void InstanceIsA_Macros()
        {
            var cleanedLua = GetCleanLua($"using {Utility.RuntimeAssemblyName}; var part = Instance.Create<Part>(); part.IsA<Frame>();", 1);
            Assert.Equal("part:IsA(\"Frame\")", cleanedLua);
        }

        [Theory]
        [InlineData("using static Roblox.Globals; print")]
        [InlineData("Roblox.Globals.print")]
        public void ConsoleMethods_Macro(string fullMethodPath)
        {
            var cleanedLua = GetCleanLua($"{fullMethodPath}(\"hello world\")");
            Assert.Equal("print(\"hello world\")", cleanedLua);
        }

        [Theory]
        [InlineData("game")]
        [InlineData("script")]
        [InlineData("os")]
        [InlineData("task")]
        public void StaticClass_NoFullQualification(string memberName)
        {
            var cleanedLua = GetCleanLua($"{Utility.RuntimeAssemblyName}.Globals.{memberName}");
            Assert.Equal(memberName, cleanedLua);
        }

        [Theory]
        [InlineData("object obj; obj?.Name;")]
        [InlineData("object a; a.b?.c;")]
        public void SafeNavigation_GeneratesIfStatement(string source)
        {
            var cleanedLua = GetCleanLua(source, 1);
            switch (source)
            {
                case "object obj; obj?.Name;":
                    Assert.Equal("if obj == nil then nil else obj.Name", cleanedLua);
                    break;
                case "object a; a.b?.c;":
                    Assert.Equal("if a.b == nil then nil else a.b.c", cleanedLua);
                    break;
            }
        }

        [Fact]
        public void NullCoalescing_GeneratesIfStatement()
        {
            
            var cleanedLua = GetCleanLua("int? x; int? y; x ?? y", 2);
            Assert.Equal("if x == nil then y else x", cleanedLua);
        }

        [Fact]
        public void TupleExpression_GeneratesTable()
        {
            var cleanedLua = GetCleanLua("var tuple = (1, 2, 3)");
            Assert.Equal("local tuple = {1, 2, 3}", cleanedLua);
        }

        [Fact]
        public void TupleIndexing_GeneratesTableIndexing()
        {
            var cleanedLua = GetCleanLua("var tuple = (1, 2, 3);\ntuple.Item1;\ntuple.Item2;\ntuple.Item3;\n", 1);
            var lines = GetLines(cleanedLua);
            Assert.Equal("tuple[1]", lines[0]);
            Assert.Equal("tuple[2]", lines[1]);
            Assert.Equal("tuple[3]", lines[2]);
        }

        [Fact]
        public void CollectionInitializer_GeneratesTable()
        {
            var cleanedLua = GetCleanLua("int[] nums = [1, 2, 3]");
            Assert.Equal("local nums: { number } = {1, 2, 3}", cleanedLua);
        }

        [Theory]
        [InlineData("int[] nums = [1, 2, 3]; nums[0]", "nums[1]", 1)]
        [InlineData("int[] nums = [1, 2, 3]; int i = 4; nums[i]", "nums[i + 1]", 2)]
        public void CollectionIndexing_AddsOneToNumericalIndices(string input, string expected, int removeLines)
        {
            var cleanedLua = GetCleanLua(input, removeLines);
            Assert.Equal(expected, cleanedLua);
        }

        private static void AssertEqualLines(List<string> lines, List<string> expectedLines)
        {
            foreach (var line in lines)
            {
                var expectedLine = expectedLines.ElementAt(lines.IndexOf(line));
                Assert.Equal(expectedLine, line);
            }
        }

        private List<string> GetLines(string cleanLua)
        {
            return cleanLua.Split('\n').Select(line => line.Trim()).ToList();
        }

        private string GetCleanLua(string source, int extraLines = 0)
        {
            var cleanTree = TranspilerUtility.ParseTree(source);
            var transformedTree = TranspilerUtility.TransformTree(cleanTree, [BuiltInTransformers.Main()]);
            var compiler = TranspilerUtility.GetCompiler([transformedTree]);
            var memberCollector = new MemberCollector([cleanTree]);
            var generatedLua = TranspilerUtility.GenerateLua(transformedTree, compiler, memberCollector.Collect());
            return TranspilerUtility.CleanUpLuaForTests(generatedLua, extraLines);
        }
    }
}