Skip to content

Commit cfdadd9

Browse files
Merge pull request #1981 from wiremod/e2-tests
Add some initial E2 tests
2 parents e8237d9 + ff8ab0d commit cfdadd9

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

data/expression2/tests/parsing.txt

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
@inputs In
2+
@outputs Out
3+
4+
local A = 0
5+
6+
if (A) { A = 0 }
7+
if (A) { A = 0 } else { A = 0 }
8+
if (A) { A = 0 } elseif (A) { A = 0 }
9+
if (A) { A = 0 } elseif (A) { A = 0 }
10+
11+
while (A) { A = 0 }
12+
13+
for (B = 1, 2) { A = 0 }
14+
for (B = 1, 2, 3) { A = 0 }
15+
16+
foreach (K, V: number = array(1, 2, 3)) { A = 0 }
17+
18+
while (A) { break }
19+
while (A) { continue }
20+
21+
A++
22+
A--
23+
A += 1
24+
A -= 1
25+
A *= 1
26+
A /= 1
27+
28+
local B = vec()
29+
B[1] = 2
30+
31+
switch (A) {
32+
case A + A,
33+
A = 0
34+
A = 0
35+
case A + A + A,
36+
A = 0
37+
break
38+
default,
39+
A = 0
40+
}
41+
42+
function f() {}
43+
function void f() {}
44+
function void f() { return }
45+
function number g() { return 0 }
46+
function number f(X) { return X }
47+
function vector f(X: vector) { return X }
48+
function number f(X, Y) { return X + Y }
49+
function number f(X, Y: vector) { return X }
50+
function number f([X Y]) { return X + Y }
51+
function number f([X Y]: number) { return X + Y }
52+
53+
A ? A : A
54+
A ?: A
55+
A | A
56+
A & A
57+
A || A
58+
A && A
59+
A ^^ A
60+
A == A
61+
A != A
62+
A > A
63+
A < A
64+
A >= A
65+
A <= A
66+
A << A
67+
A >> A
68+
A + A
69+
A - A
70+
A * A
71+
A / A
72+
A % A
73+
A ^ A
74+
+A
75+
-A
76+
!A
77+
78+
array():count()
79+
array()[0, number]
80+
(A)
81+
0
82+
"foo"
83+
~In
84+
$In
85+
->In
86+
A
87+
88+
table(1 = 1, 2 = 2)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
vec() * 0
2+
0 * vec()
3+
4+
local Calls = 0
5+
function number f() {
6+
Calls++
7+
return 1
8+
}
9+
f() * 0
10+
0 * f()
11+
assert(Calls == 2)

lua/entities/gmod_wire_expression2/core/core.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ e2function void error( string reason )
318318
error(reason, 2)
319319
end
320320

321+
e2function void assert(condition)
322+
if not condition then error("assert failed", 2) end
323+
end
324+
325+
e2function void assert(condition, string reason)
326+
if not condition then error(reason, 2) end
327+
end
328+
321329
--------------------------------------------------------------------------------
322330

323331
__e2setcost(100) -- approximation

lua/wire/client/e2descriptions.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,8 @@ E2Helper.Descriptions["setName(e:s)"] = "Set the name of another E2 or component
824824
E2Helper.Descriptions["cpuUsage()"] = "Returns the average time per tick the server spends running this E2, in seconds (multiply it by 1000000 to get the same value as is displayed on the E2 overlay)"
825825
E2Helper.Descriptions["cpuUsage(e:)"] = "Returns the average time per tick the server spends running the specified E2, in seconds (multiply it by 1000000 to get the same value as is displayed on the E2 overlay)"
826826
E2Helper.Descriptions["error(s)"] = "Shuts down the E2 with specified script error message"
827+
E2Helper.Descriptions["assert(n)"] = "If the argument is 0, shut down the E2 with an error message"
828+
E2Helper.Descriptions["assert(ns)"] = "If the first argument is 0, shut down the E2 with the given error message string"
827829
E2Helper.Descriptions["reset()"] = "Reset the expression itself as if it was just spawned, stops execution"
828830
E2Helper.Descriptions["exit()"] = "Stops the execution of any code after it"
829831
E2Helper.Descriptions["getCode()"] = "Returns the code of the E2 as a string"

lua/wire/default_data_decompressor.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
-- Garry has imposed a file extension whitelist for the Steam Workshop which does not permit the dangerous format .txt
22
-- Therefore, we must store our .txt's in default_data_files.lua, and then extract them when first run
33

4+
local ignored_dirs = {
5+
["expression2/tests"] = true,
6+
}
7+
48
-- Compress all files in addons/wire/data recursively into 1 json string
59
local function ReadDir(root)
10+
if ignored_dirs[root] then return nil end
611
local tab = {}
712
local files,dirs = file.Find("addons/wire/data/"..root.."*","GAME")
813
for _, f in pairs(files) do
@@ -16,7 +21,7 @@ local function ReadDir(root)
1621
return tab
1722
end
1823
-- Uncomment and Rename this file to wire/lua/wire/default_data_files.lua to update it
19-
//file.Write("default_data_files.txt", "//"..util.TableToJSON(ReadDir("")))
24+
-- file.Write("default_data_files.txt", "//"..util.TableToJSON(ReadDir("")))
2025

2126
-- Decompress the json string wire/lua/wire/default_data_files.lua into the corresponding 36+ default data files
2227
local function WriteDir(tab)
@@ -34,7 +39,7 @@ if not file.Exists("expression2/_helloworld_.txt", "DATA") then
3439
local compressed = file.Read("wire/default_data_files.lua","LUA")
3540
-- The client cannot read lua files sent by the server (for security?), so clientside this'll only work
3641
-- if the client actually has Wiremod installed, though with workshop autodownload that'll be common
37-
if compressed != nil then
42+
if compressed ~= nil then
3843
WriteDir(util.JSONToTable(string.sub(compressed, 3)))
3944
end
4045
end

0 commit comments

Comments
 (0)