Skip to content

Commit

Permalink
Started writing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vorlias committed Jan 19, 2022
1 parent 166ad6e commit 6176c46
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions foreman.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tools]
rojo = { source = "rojo-rbx/rojo", version = "=7.0.0" }
run-in-roblox = { source = "rojo-rbx/run-in-roblox", version = "0.3.0" }
6 changes: 5 additions & 1 deletion lib/MockMemoryStoreService/MockMemoryStoreSortedMap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ function MockMemoryStoreSortedMap:SetAsync(key, value, expiration)
end

function MockMemoryStoreSortedMap:UpdateAsync(key, transformFunction, expiration)
assert(typeof(key) == "string", "Expects key (argument #1)")
assert(typeof(transformFunction) == "function", "Expects transformFunction (argument #2)")
assert(typeof(expiration) == "number", "Expects expiration (argument #3)")

local oldValue = self:GetAsync(key)

local newValue = transformFunction(if oldValue then oldValue.innerValue else nil)
local newValue = transformFunction(oldValue)
if newValue ~= nil then
self:SetAsync(key, newValue, expiration)
return newValue
Expand Down
2 changes: 2 additions & 0 deletions lib/MockMemoryStoreService/MockMemoryStoreUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local MockMemoryStoreUtils = {}
local MAX_KEY_LENGTH = 128

function MockMemoryStoreUtils.AssertKeyIsValid(key: string)
assert(type(key) == "string", "Expects string got " .. typeof(key))

if #key >= MAX_KEY_LENGTH then
error("Key '" .. tostring(key) .. "' exceeds maximum length of " .. MAX_KEY_LENGTH, 3)
end
Expand Down
10 changes: 10 additions & 0 deletions scripts/tests.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TestEZ = require(ReplicatedStorage.rbxts.testez.src)

local results = TestEZ.TestBootstrap:run({
ReplicatedStorage.Tests
})

if (#results.errors > 0 or results.failureCount > 0) then
error("Tests failed!")
end
18 changes: 18 additions & 0 deletions tests.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "mock-tests",
"tree": {
"$className": "DataModel",
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
"MockMemoryStore": {
"$path": "./lib"
},
"Tests": {
"$path": "./tests"
},
"rbxts": {
"$path": "node_modules/@rbxts"
}
}
}
}
57 changes: 57 additions & 0 deletions tests/MockMemoryStoreSortedMap.spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local MockMemoryStoreService = require(game:GetService("ReplicatedStorage").MockMemoryStore)



return function ()
it("Should retrieve the same map", function()
local TestSortedMap = MockMemoryStoreService:GetSortedMap("TestSortedMap")
expect(TestSortedMap).to.be.ok()

local TestSortedMap2 = MockMemoryStoreService:GetSortedMap("TestSortedMap")
expect(TestSortedMap2).to.equal(TestSortedMap) -- Should be same reference
end)

it("Should correctly use GetAsync/SetAsync", function()
local TEST_VALUE = 10

local TestSortedMap = MockMemoryStoreService:GetSortedMap("TestSortedMap")
expect(TestSortedMap:SetAsync("TestSet", TEST_VALUE, 30)).to.equal(false) -- false because new value, not update.

expect(TestSortedMap:GetAsync("TestSet")).to.equal(TEST_VALUE)

expect(TestSortedMap:SetAsync("TestSet", TEST_VALUE + 5, 30)).to.equal(true) -- true, because updating existing value
end)

it("Should correctly use UpdateAsync", function()
local TEST_VALUE = 32

local TestSortedMap = MockMemoryStoreService:GetSortedMap("TestSortedMap")

-- This should return a value, since we've returned a value
local value = TestSortedMap:UpdateAsync("TestUpdateKey", function(value)
expect(value).to.be.equal(nil) -- value shouldn't be set since it's y'know, not set yet.
return TEST_VALUE
end, 20)

-- Should have returned TEST_VALUE
expect(value).to.be.equal(TEST_VALUE)

local nilValue = TestSortedMap:UpdateAsync("TestUpdateKey", function(value)
expect(value).to.equal(TEST_VALUE)
return nil
end, 20)

-- Should be nil since we didn't transform
expect(nilValue).to.equal(nil)
end)

it("Should correctly use RemoveAsync", function()
local TEST_VALUE = 32
local TestSortedMap = MockMemoryStoreService:GetSortedMap("TestSortedMap")

expect(TestSortedMap:SetAsync("RemoveMe", TEST_VALUE, 30)).to.equal(false) -- new value, again

TestSortedMap:RemoveAsync("RemoveMe")
expect(TestSortedMap:GetAsync("RemoveMe")).to.equal(nil)
end)
end

0 comments on commit 6176c46

Please sign in to comment.