Skip to content

Commit

Permalink
Added Stack and gmod functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelIT7 committed Sep 21, 2024
1 parent 3a94929 commit 05e968d
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lua/tests/gmod/classes/stack/Pop.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
return {
groupName = "Stack:Pop",

cases = {
{
name = "Pops the value off the stack properly",
func = function()
local stack = util.Stack()
stack[0] = 2
stack[1] = "Hello"
stack[2] = "World"

stack:Pop( 1 )

expect( stack[0] ).to.equal( 1 )
expect( stack[1] ).to.equal( "Hello" )
expect( stack[2] ).to.equal( nil )

stack:Pop( 1 )

expect( stack[0] ).to.equal( 0 )
expect( stack[1] ).to.equal( nil )
end
},

{
name = "Errors if we pop too much off the stack",
func = function()
local stack = util.Stack()
stack[0] = 0

local test = function()
stack:Pop( 1 )
end

expect( test ).to.errWith( [[attempted to pop 1 element in stack of length 0]] )
end
},
}
}
35 changes: 35 additions & 0 deletions lua/tests/gmod/classes/stack/PopMulti.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
return {
groupName = "Stack:PopMulti",

cases = {
{
name = "Pops the values off the stack properly",
func = function()
local stack = util.Stack()
stack[0] = 2
stack[1] = "Hello"
stack[2] = "World"

stack:PopMulti( 2 )

expect( stack[0] ).to.equal( 0 )
expect( stack[1] ).to.equal( nil )
expect( stack[2] ).to.equal( nil )
end
},

{
name = "Errors if we pop too much off the stack",
func = function()
local stack = util.Stack()
stack[0] = 0

local test = function()
stack:PopMulti( 2 )
end

expect( test ).to.errWith( [[attempted to pop 2 elements in stack of length 0]] )
end
},
}
}
25 changes: 25 additions & 0 deletions lua/tests/gmod/classes/stack/Push.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
return {
groupName = "Stack:Push",

cases = {
{
name = "Pushes the value onto the stack properly",
func = function()
local stack = util.Stack()
stack[0] = 0

stack:Push("Hello")

expect( stack[0] ).to.equal( 1 )
expect( stack[1] ).to.equal( "Hello" )
expect( stack[2] ).to.equal( nil )

stack:Push("World")

expect( stack[0] ).to.equal( 2 )
expect( stack[1] ).to.equal( "Hello" )
expect( stack[2] ).to.equal( "World" )
end
},
}
}
22 changes: 22 additions & 0 deletions lua/tests/gmod/classes/stack/Size.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
return {
groupName = "Stack:Size",

cases = {
{
name = "Returns the stack size properly",
func = function()
local stack = util.Stack()
stack[0] = 10

local size = stack:Size()

expect( size ).to.equal( 10 )

stack[0] = -10
local size = stack:Size()

expect( size ).to.equal( -10 )
end
},
}
}
43 changes: 43 additions & 0 deletions lua/tests/gmod/classes/stack/Top.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
return {
groupName = "Stack:Top",

cases = {
{
name = "returns the top of the stack properly",
func = function()
local stack = util.Stack()
stack[0] = 2
stack[1] = "Hello"
stack[2] = "World"

local top = stack:Top()

expect( top ).to.equal( "World" ) -- top == stack[ stack[0] ]

stack[0] = 1
local top = stack:Top()

expect( top ).to.equal( "Hello" )
end
},

{
name = "Return nil if stack length is zero",
func = function()
local stack = util.Stack()
stack[-1] = "Hello"
stack[0] = 0
stack[1] = "World"

local top = stack:Top()

expect( top ).to.equal( nil )

stack[0] = -1 -- It checks it with len == 0 and which should allow this
local top = stack:Top()

expect( top ).to.equal( "Hello" )
end
},
}
}
34 changes: 34 additions & 0 deletions lua/tests/gmod/libraries/gmod/GetGamemode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
return {
groupName = "gmod.GetGamemode",

cases = {
{
name = "Exists on the gmod table",
func = function()
expect( gmod.GetGamemode ).to.beA( "function" )
end
},

{
name = "Always returns the valid GAMEMODE table",
func = function()
local gm = gmod.GetGamemode()

expect( gm ).to.equal( GAMEMODE or GM )
end
},

{
name = "Always returns the valid GAMEMODE table",
func = function()
GAMEMODE = nil

local gm = gmod.GetGamemode()

expect( gm ).to.beA( "table" )

GAMEMODE = gm
end
},
}
}

0 comments on commit 05e968d

Please sign in to comment.