Skip to content

Commit

Permalink
Add angle Class methods (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonsturgeon authored Jun 28, 2024
1 parent e7ffc4a commit c0f449b
Show file tree
Hide file tree
Showing 19 changed files with 970 additions and 3 deletions.
44 changes: 44 additions & 0 deletions lua/tests/gmod/classes/angle/Add.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
return {
group = "Angle:Add",

cases = {
{
name = "Exists on the Angle meta table",
func = function()
local meta = FindMetaTable( "Angle" )
expect( meta.Add ).to.beA( "function" )
end
},

{
name = "Returns nothing",
func = function()
local a = Angle( 1, 1, 1 )
local b = Angle( 1, 1, 1 )

local out = a:Add( b )
expect( out ).to.beNil()
end
},

{
name = "Modifies only the base Angle",
func = function()
local a = Angle( 1, 1, 1 )
local b = Angle( 1, 1, 1 )

a:Add( b )

local p1, y1, r1 = a[1], a[2], a[3]
expect( p1 ).to.equal( 2 )
expect( y1 ).to.equal( 2 )
expect( r1 ).to.equal( 2 )

local p2, y2, r2 = b[1], b[2], b[3]
expect( p2 ).to.equal( 1 )
expect( y2 ).to.equal( 1 )
expect( r2 ).to.equal( 1 )
end
}
}
}
76 changes: 76 additions & 0 deletions lua/tests/gmod/classes/angle/Div.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
return {
groupName = "Angle:Div",
cases = {
{
name = "Exits on the Angle meta table",
func = function()
local meta = FindMetaTable( "Angle" )
expect( meta.Div ).to.beA( "function" )
end
},

{
name = "Returns nothing",
func = function()
local a = Angle( 2, 2, 2 )

local out = a:Div( 2 )
expect( out ).to.beNil()
end
},

{
name = "Modifies the base Angle",
func = function()
local a = Angle( 2, 2, 2 )

a:Div( 2 )

local p1, y1, r1 = a[1], a[2], a[3]
expect( p1 ).to.equal( 1 )
expect( y1 ).to.equal( 1 )
expect( r1 ).to.equal( 1 )
end
},

{
name = "Works when given a negative number",
func = function()
local a = Angle( 2, 2, 2 )

a:Div( -2 )

local p1, y1, r1 = a[1], a[2], a[3]
expect( p1 ).to.equal( -1 )
expect( y1 ).to.equal( -1 )
expect( r1 ).to.equal( -1 )
end
},

{
name = "Functions correctly when given a numeric string",
func = function()
local a = Angle( 2, 2, 2 )

a:Div( "2" )
local p1, y1, r1 = a[1], a[2], a[3]
expect( p1 ).to.equal( 1 )
expect( y1 ).to.equal( 1 )
expect( r1 ).to.equal( 1 )
end
},
{
name = "Errors when given a non-numeric string",
func = function()
local a = Angle( 2, 2, 2 )

local test = function()
a:Div( "a" )
end

expect( test ).to.errWith( [[bad argument #1 to 'Div' (number expected, got string)]] )
end
},

}
}
51 changes: 51 additions & 0 deletions lua/tests/gmod/classes/angle/Forward.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
return {
groupName = "Angle:Forward",

cases = {
{
name = "Exists on the Angle meta table",
func = function()
local meta = FindMetaTable( "Angle" )
expect( meta.Forward ).to.beA( "function" )
end
},

{
name = "Returns a Vector",
func = function()
local a = Angle( 0, 0, 0 )

local out = a:Forward()
expect( out ).to.beA( "Vector" )
end
},

{
name = "Returns the correct Vector",
func = function()
local a = Angle( 0, 0, 0 )

local out = a:Forward()
local x, y, z = out[1], out[2], out[3]

expect( x ).to.equal( 1 )
expect( y ).to.equal( 0 )
expect( z ).to.equal( 0 )
end
},

{
name = "Does not modify the Angle",
func = function()
local a = Angle( 1, 1, 1 )

a:Forward()

local p, y, r = a[1], a[2], a[3]
expect( p ).to.equal( 1 )
expect( y ).to.equal( 1 )
expect( r ).to.equal( 1 )
end
}
}
}
113 changes: 113 additions & 0 deletions lua/tests/gmod/classes/angle/IsEqualTo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
return {
groupName = "Angle:IsEqualTol",

cases = {
{
name = "Exists on the Angle meta table",
func = function()
local meta = FindMetaTable( "Angle" )
expect( meta.IsEqualTol ).to.beA( "function" )
end
},

{
name = "Returns a boolean",
func = function()
local a = Angle( 0, 0, 0 )
local b = Angle( 0, 0, 0 )

local out = a:IsEqualTol( b, 0 )
expect( out ).to.beA( "boolean" )
end
},

{
name = "Returns true when the Angles are equal",
func = function()
local a = Angle( 0, 0, 0 )
local b = Angle( 0, 0, 0 )

local out = a:IsEqualTol( b, 0 )
expect( out ).to.beTrue()
end
},

{
name = "Returns false when the Angles are not equal",
func = function()
local a = Angle( 0, 0, 0 )
local b = Angle( 1, 1, 1 )

local out = a:IsEqualTol( b, 0 )
expect( out ).to.beFalse()
end
},

{
name = "Returns true when the Angles are within the tolerance",
func = function()
local a = Angle( 0, 0, 0 )
local b = Angle( 0.1, 0.1, 0.1 )

local out = a:IsEqualTol( b, 0.1 )
expect( out ).to.beTrue()
end
},

{
name = "Returns false when the Angles are not within the tolerance",
func = function()
local a = Angle( 0, 0, 0 )
local b = Angle( 0.1, 0.1, 0.1 )

local out = a:IsEqualTol( b, 0 )
expect( out ).to.beFalse()
end
},

{
name = "Does not modify the Angles",
func = function()
local a = Angle( 1, 1, 1 )
local b = Angle( 1, 1, 1 )

a:IsEqualTol( b, 0 )

local p1, y1, r1 = a[1], a[2], a[3]
expect( p1 ).to.equal( 1 )
expect( y1 ).to.equal( 1 )
expect( r1 ).to.equal( 1 )

local p2, y2, r2 = b[1], b[2], b[3]
expect( p2 ).to.equal( 1 )
expect( y2 ).to.equal( 1 )
expect( r2 ).to.equal( 1 )
end
},

{
name = "Works when given a numeric string for the tolerance",
func = function()
local a = Angle( 0, 0, 0 )
local b = Angle( 0.1, 0.1, 0.1 )

local out = a:IsEqualTol( b, "0.1" )
expect( out ).to.beTrue()
end
},

{
name = "Errors when given a non-numeric string for the tolerance",
func = function()
local a = Angle( 0, 0, 0 )
local b = Angle( 0.1, 0.1, 0.1 )

local test = function()
a:IsEqualTol( b, "a" )
end

expect( test ).to.errWith( [[bad argument #2 to 'IsEqualTol' (number expected, got string)]] )
end
}
}
}
43 changes: 43 additions & 0 deletions lua/tests/gmod/classes/angle/IsZero.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
return {
groupName = "Angle:IsZero",

cases = {
{
name = "Exists on the Angle meta table",
func = function()
local meta = FindMetaTable( "Angle" )
expect( meta.IsZero ).to.beA( "function" )
end
},

{
name = "Returns a boolean",
func = function()
local a = Angle( 0, 0, 0 )

local out = a:IsZero()
expect( out ).to.beA( "boolean" )
end
},

{
name = "Returns true when the Angle is zero",
func = function()
local a = Angle( 0, 0, 0 )

local out = a:IsZero()
expect( out ).to.beTrue()
end
},

{
name = "Returns false when the Angle has no 0's",
func = function()
local a = Angle( 1, 1, 1 )

local out = a:IsZero()
expect( out ).to.beFalse()
end
}
}
}
65 changes: 65 additions & 0 deletions lua/tests/gmod/classes/angle/Mul.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
return {
groupName = "Angle:Mul",

cases = {
{
name = "Exists on the Angle meta table",
func = function()
local meta = FindMetaTable( "Angle" )
expect( meta.Mul ).to.beA( "function" )
end
},

{
name = "Returns nothing",
func = function()
local a = Angle( 2, 2, 2 )

local out = a:Mul( 2 )
expect( out ).to.beNil()
end
},

{
name = "Modifies the base Angle",
func = function()
local a = Angle( 2, 2, 2 )

a:Mul( 2 )

local p1, y1, r1 = a[1], a[2], a[3]
expect( p1 ).to.equal( 4 )
expect( y1 ).to.equal( 4 )
expect( r1 ).to.equal( 4 )
end
},

{
name = "Modifies the base Angle with a string scalar",
func = function()
local a = Angle( 2, 2, 2 )

a:Mul( "2" )

local p1, y1, r1 = a[1], a[2], a[3]
expect( p1 ).to.equal( 4 )
expect( y1 ).to.equal( 4 )
expect( r1 ).to.equal( 4 )
end
},

-- non numeric string
{
name = "Does not modify the Angle with a non numeric string",
func = function()
local a = Angle( 2, 2, 2 )

local function test()
a:Mul( "a" )
end

expect( test ).to.errWith( [[bad argument #1 to 'Mul' (number expected, got string)]] )
end
}
}
}
Loading

0 comments on commit c0f449b

Please sign in to comment.