Skip to content

Commit

Permalink
Test: Add tests for the grammar.
Browse files Browse the repository at this point in the history
  • Loading branch information
robloxiandemo committed Nov 10, 2023
1 parent 1ae4723 commit e73baee
Show file tree
Hide file tree
Showing 5 changed files with 482 additions and 0 deletions.
170 changes: 170 additions & 0 deletions test/detect/luau/luaudetext.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
--// Vector
--// Written by Demo (R0BL0XIAN_D3M0)
--// [https://www.roblox.com/users/289025524/profile]
--// 11/09/2023

--// Types
type TVector = Vector2 | Vector3

--[=[
@class Vector

A collection of very useful vector-related functions.
]=]
local Vector = {}

--// Functions

--[=[
@within Vector

@param vector Vector2 -- The `Vector2` coordinate.

@return Vector3 -- Return the newly created `Vector3`.

Create a `Vector3` from a `Vector2` within the XY plane.
]=]
function Vector.FromVector2XY(vector: Vector2): Vector3
return (Vector3.new(vector.X, vector.Y, 0))
end

--[=[
@within Vector

@param vector Vector2 -- The `Vector2` coordinate.

@return Vector3 -- Return the newly created `Vector3`.

Create a `Vector3` from a `Vector2` within the XZ plane, unlike `FromVector2XY`.
]=]
function Vector.FromVector2XZ(vector: Vector2): Vector3
return (Vector3.new(vector.X, 0, vector.Y))
end

--[=[
@within Vector

@param vector Vector2 -- The initial `Vector2` coordinate.

@param _vector Vector2 -- The secondary `Vector2` coordinate.

@return number -- Return the computed angle in a numerical form or nil.

Compute the angle between two vectors in radians.
]=]
function Vector.RetrieveAngleRadian(vector: Vector2, _vector: Vector2): number?
if vector.Magnitude == 0 then
return nil
end

return (math.acos(vector:Dot(_vector)))
end

--[=[
@within Vector

@param vector Vector2 -- The initial `Vector2` coordinate.

@param _vector Vector2 -- The secondary `Vector2` coordinate.

@return number -- Return the computed angle in a numerical form.

Compute the angle between two vectors.
]=]
function Vector.AngleBetweenVectors(vector: Vector2, _vector: Vector2): number
local newVector: Vector2 = (_vector.Magnitude * vector)
local _newVector: Vector2 = (vector.Magnitude * _vector)

return (2 * (math.atan2((_newVector - newVector).Magnitude, (newVector + _newVector).Magnitude)))
end

--[=[
@within Vector

@param vector Vector3 -- The original `Vector3` coordinate.

@param amount number -- The primary amount.

@return Vector3 -- Return the rounded `Vector3`.

Round the specified `Vector3` to the nearest number.
]=]
function Vector.Round(vector: Vector3, amount: number): Vector3
return (
Vector3.new(
((math.round(vector.X / amount)) * amount),
((math.round(vector.Y / amount)) * amount),
((math.round(vector.Z / amount)) * amount)
)
)
end

--[=[
@within Vector

@param vector TVector -- The vector coordinate (`Vector2` or `Vector3`).

@param maxMagnitude number -- The maximum magnitude.

@return number -- Return the clamped magnitude.

Clamp the magnitude of a vector so it is only a certain length.
]=]
function Vector.ClampMagnitude(vector: TVector, maxMagnitude: number): number
return ((vector.Magnitude > maxMagnitude) and (vector.Unit * maxMagnitude) or vector)
end

--[=[
@within Vector

@param vector TVector -- The initial vector coordinate (`Vector2` or `Vector3`).

@param _vector TVector -- The secondary vector coordinate (`Vector2` or `Vector3`).

@return number -- Return the radianed angle.

Finds the angle in radians between two vectors.
]=]
function Vector.AngleBetween(vector: TVector, _vector: TVector): number
return (math.acos(math.clamp(vector.Unit:Dot(_vector.Unit), -1, 1)))
end

--[=[
@within Vector

@param vector TVector -- The initial vector coordinate (`Vector2` or `Vector3`).

@param _vector TVector -- The secondary vector coordinate (`Vector2` or `Vector3`).

@param axisVector Vector -- The axis vector coordinate (`Vector` or `Vector3`).

@return number -- Return the radianed angle.

Finds the angle in radians between two vectors and returns a signed value.
]=]
function Vector.AngleBetweenSigned(vector: TVector, _vector: TVector, axisVector: TVector): number
local angle: number = Vector.AngleBetween(vector, _vector)

return (angle * (math.sign(axisVector:Dot(vector:Cross(_vector)))))
end

--[=[
@within Vector

@return Vector3 -- Return the random `Vector3` coordinate.

Return a random unit vector (could be used for equal distribution around a sphere).
]=]
function Vector.RetrieveRandomUnitVector(): Vector3
local randomX: number = (2 * ((math.random()) - 0.5))
local randomY: number = (6.2831853071796 * (math.random()))
local randomZ: number = ((1 - (randomX * randomX)) ^ 0.5)

local X: number = randomX
local Y: number = (randomZ * (math.cos(randomZ)))
local Z: number = (randomZ * (math.sin(randomY)))

return (Vector3.new(X, Y, Z))
end

return Vector
37 changes: 37 additions & 0 deletions test/markup/luau/default.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<span class="hljs-comment">--[[
Simple signal/slot implementation
]]</span>
<span class="hljs-keyword">local</span> signal_mt = {
<span class="hljs-built_in">__index</span> = {
register = <span class="hljs-built_in">table</span>.<span class="hljs-built_in">insert</span>
}
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">signal_mt.__index:emit</span><span class="hljs-params">(... <span class="hljs-comment">--[[ Comment in params ]]</span>)</span></span>
<span class="hljs-keyword">for</span> _, slot <span class="hljs-keyword">in</span> <span class="hljs-built_in">ipairs</span>(<span class="hljs-built_in">self</span>) <span class="hljs-keyword">do</span>
slot(<span class="hljs-built_in">self</span>, ...)
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">local</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">create_signal</span><span class="hljs-params">()</span></span>
<span class="hljs-keyword">return</span> <span class="hljs-built_in">setmetatable</span>({}, signal_mt)
<span class="hljs-keyword">end</span>

<span class="hljs-comment">-- Signal test</span>
<span class="hljs-keyword">local</span> signal = create_signal()
signal:register(<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(signal, ...)</span></span>
<span class="hljs-built_in">print</span>(...)
<span class="hljs-keyword">end</span>)
signal:emit(<span class="hljs-string">&#x27;Answer to Life, the Universe, and Everything:&#x27;</span>, <span class="hljs-number">42</span>)

<span class="hljs-comment">-- Output test</span>
<span class="hljs-keyword">local</span> testNumber: <span class="hljs-built_in">number</span> = <span class="hljs-number">10</span>

<span class="hljs-built_in">print</span>(<span class="hljs-string">`this is line, and "testNumber" equals to: {testNumber}.`</span>)

<span class="hljs-comment">--[==[ [=[ [[
Nested ]]
multi-line ]=]
comment ]==]</span>
<span class="hljs-string">[==[ Nested
[=[ multi-line
[[ string
]] ]=] ]==]</span>
37 changes: 37 additions & 0 deletions test/markup/luau/default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--[[
Simple signal/slot implementation
]]
local signal_mt = {
__index = {
register = table.insert
}
}
function signal_mt.__index:emit(... --[[ Comment in params ]])
for _: number, slot: any in ipairs(self) do
slot(self, ...)
end
end
local function create_signal(): typeof(signal_mt)
return setmetatable({}, signal_mt)
end

-- Signal test
local signal: typeof(signal_mt) = create_signal()
signal:register(function(signal, ...)
print(...)
end)
signal:emit(Answer to Life, the Universe, and Everything:', 42)

-- Output test
local testNumber: number = 10

print(`this is line, and "testNumber" equals to: {testNumber}.`)

--[==[ [=[ [[
Nested ]]
multi-line ]=]
comment ]==]
[==[ Nested
[=[ multi-line
[[ string
]] ]=] ]==]
68 changes: 68 additions & 0 deletions test/markup/luau/luaucode.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<span class="hljs-comment">--// Vector</span>
<span class="hljs-comment">--// Written by Demo (R0BL0XIAN_D3M0)</span>
<span class="hljs-comment">--// [https://www.roblox.com/users/289025524/profile]</span>
<span class="hljs-comment">--// 11/09/2023</span>

<span class="hljs-keyword">type</span> TVector = Vector2 | Vector3

<span class="hljs-keyword">local</span> Vector = {}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vector.FromVector2XY</span><span class="hljs-params">(vector: Vector2)</span></span>
<span class="hljs-keyword">return</span> (Vector3.new(vector.X, vector.Y, 0))
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vector.FromVector2XZ</span><span class="hljs-params">(vector: Vector2)</span></span>
<span class="hljs-keyword">return</span> (Vector3.new(vector.X, 0, vector.Y))
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vector.RetrieveAngleRadian</span><span class="hljs-params">(vector: Vector2, _vector: Vector2)</span></span>
<span class="hljs-keyword">if</span> vector.Magnitude == 0 <span class="hljs-keyword">then</span>
<span class="hljs-keyword">return</span> nil
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">return</span> (math.acos(vector:Dot(_vector)))
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vector.AngleBetweenVectors</span><span class="hljs-params">(vector: Vector2, _vector: Vector2)</span></span>
<span class="hljs-keyword">local</span> newVector: Vector2 = (_vector.Magnitude * vector)
<span class="hljs-keyword">local</span> _newVector: Vector2 = (vector.Magnitude * _vector)

<span class="hljs-keyword">return</span> (2 * (math.atan2((_newVector - newVector).Magnitude, (newVector + _newVector).Magnitude)))
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vector.Round</span><span class="hljs-params">(vector: Vector3, amount: number)</span></span>
<span class="hljs-keyword">return</span> (
Vector3.new(
((math.round(vector.X / amount)) * amount),
((math.round(vector.Y / amount)) * amount),
((math.round(vector.Z / amount)) * amount)
)
)
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vector.ClampMagnitude</span><span class="hljs-params">(vector: TVector, maxMagnitude: number)</span></span>
<span class="hljs-keyword">return</span> ((vector.Magnitude > maxMagnitude) <span class="hljs-keyword">and</span> (vector.Unit * maxMagnitude) <span class="hljs-keyword">or</span> vector)
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vector.AngleBetween</span><span class="hljs-params">(vector: TVector, _vector: TVector)</span></span>
<span class="hljs-keyword">return</span> (math.acos(math.clamp(vector.Unit:Dot(_vector.Unit), -1, 1)))
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vector.AngleBetweenSigned</span><span class="hljs-params">(vector: TVector, _vector: TVector, axisVector: TVector)</span></span>
<span class="hljs-keyword">local</span> angle: number = Vector.AngleBetween(vector, _vector)

<span class="hljs-keyword">return</span> (angle * (math.sign(axisVector:Dot(vector:Cross(_vector)))))
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vector.RetrieveRandomUnitVector</span><span class="hljs-params">()</span></span>
<span class="hljs-keyword">local</span> randomX: number = (2 * ((math.random()) - 0.5))
<span class="hljs-keyword">local</span> randomY: number = (6.2831853071796 * (math.random()))
<span class="hljs-keyword">local</span> randomZ: number = ((1 - (randomX * randomX)) ^ 0.5)

<span class="hljs-keyword">local</span> X: number = randomX
<span class="hljs-keyword">local</span> Y: number = (randomZ * (math.cos(randomZ)))
<span class="hljs-keyword">local</span> Z: number = (randomZ * (math.sin(randomY)))

<span class="hljs-keyword">return</span> (Vector3.new(X, Y, Z))
<span class="hljs-keyword">end</span>

<span class="hljs-keyword">return</span> Vector
Loading

0 comments on commit e73baee

Please sign in to comment.