mirror of
https://github.com/mt-mods/mail.git
synced 2025-07-12 09:22:20 -04:00
tests
This commit is contained in:
parent
dff068d75b
commit
e8c7fde4c3
8 changed files with 95 additions and 20 deletions
12
api.spec.lua
Normal file
12
api.spec.lua
Normal file
|
@ -0,0 +1,12 @@
|
|||
mtt.register("send mail", function(callback)
|
||||
-- send a mail
|
||||
local success, err = mail.send({from = "player1", to = "player2", subject = "something", body = "blah"})
|
||||
assert(success)
|
||||
assert(not err)
|
||||
|
||||
-- check the receivers inbox
|
||||
local entry = mail.get_storage_entry("player2")
|
||||
assert(entry)
|
||||
assert(#entry.inbox > 0)
|
||||
callback()
|
||||
end)
|
3
init.lua
3
init.lua
|
@ -61,4 +61,7 @@ mail.migrate()
|
|||
|
||||
if minetest.get_modpath("mtt") then
|
||||
dofile(MP .. "/mtt.lua")
|
||||
dofile(MP .. "/api.spec.lua")
|
||||
dofile(MP .. "/util/uuid.spec.lua")
|
||||
dofile(MP .. "/util/normalize.spec.lua")
|
||||
end
|
||||
|
|
15
mtt.lua
15
mtt.lua
|
@ -1,15 +1,10 @@
|
|||
mtt.register("send mail", function(callback)
|
||||
-- create "player2"
|
||||
|
||||
mtt.register("setup", function(callback)
|
||||
-- create test players
|
||||
local auth_handler = minetest.get_auth_handler()
|
||||
auth_handler.set_password("player1", "")
|
||||
auth_handler.set_password("player2", "")
|
||||
auth_handler.set_password("player3", "")
|
||||
|
||||
-- send a mail
|
||||
local success, err = mail.send({from = "player1", to = "player2", subject = "something", body = "blah"})
|
||||
assert(success)
|
||||
assert(not err)
|
||||
|
||||
-- check the receivers inbox
|
||||
local entry = mail.get_storage_entry("player2")
|
||||
assert(entry ~= nil and #entry.inbox > 0)
|
||||
callback()
|
||||
end)
|
22
storage.lua
22
storage.lua
|
@ -1,23 +1,29 @@
|
|||
-- storage getter/setter
|
||||
|
||||
-- create or populate empty fields on an entry
|
||||
local function populate_entry(e)
|
||||
e = e or {}
|
||||
e.contacts = e.contacts or {}
|
||||
e.inbox = e.inbox or {}
|
||||
e.outbox = e.outbox or {}
|
||||
e.lists = e.lists or {}
|
||||
return e
|
||||
end
|
||||
|
||||
function mail.get_storage_entry(playername)
|
||||
local str = mail.storage:get_string(playername)
|
||||
if str == "" then
|
||||
-- new entry
|
||||
return {
|
||||
contacts = {},
|
||||
inbox = {},
|
||||
outbox = {},
|
||||
lists = {}
|
||||
}
|
||||
return populate_entry()
|
||||
else
|
||||
-- deserialize existing entry
|
||||
return minetest.parse_json(str)
|
||||
local e = minetest.parse_json(str)
|
||||
return populate_entry(e)
|
||||
end
|
||||
end
|
||||
|
||||
function mail.set_storage_entry(playername, entry)
|
||||
mail.storage:get_string(playername, minetest.write_json(entry))
|
||||
mail.storage:set_string(playername, minetest.write_json(entry))
|
||||
end
|
||||
|
||||
-- get a mail by id from the players in- or outbox
|
||||
|
|
39
storage.spec.lua
Normal file
39
storage.spec.lua
Normal file
|
@ -0,0 +1,39 @@
|
|||
|
||||
mtt.register("storage", function(callback)
|
||||
-- sanity checks
|
||||
local playername = "player1"
|
||||
local entry = mail.get_storage_entry(playername)
|
||||
assert(entry)
|
||||
|
||||
-- create
|
||||
local contact = {
|
||||
name = "other-player",
|
||||
note = "my-note"
|
||||
}
|
||||
mail.update_contact(playername, contact)
|
||||
|
||||
-- read
|
||||
local contacts = mail.get_contacts(playername)
|
||||
assert(#contacts == 1)
|
||||
assert(contacts[1].note == contact.note)
|
||||
assert(contacts[1].name == contact.name)
|
||||
|
||||
-- update
|
||||
mail.update_contact(playername, {
|
||||
name = contact.name,
|
||||
note = "xy"
|
||||
})
|
||||
|
||||
-- read updated
|
||||
contacts = mail.get_contacts(playername)
|
||||
assert(#contacts == 1)
|
||||
assert(contacts[1].note == "xy")
|
||||
assert(contacts[1].name == contact.name)
|
||||
|
||||
-- delete
|
||||
mail.delete_contact(playername, contact.name)
|
||||
contacts = mail.get_contacts(playername)
|
||||
assert(#contacts == 0)
|
||||
|
||||
callback()
|
||||
end)
|
|
@ -1,3 +1,4 @@
|
|||
default_game = minetest_game
|
||||
mg_name = v7
|
||||
mtt_enable = true
|
||||
mtt_filter = mail
|
12
util/normalize.spec.lua
Normal file
12
util/normalize.spec.lua
Normal file
|
@ -0,0 +1,12 @@
|
|||
|
||||
mtt.register("util/normalize_players_and_add_recipients", function(callback)
|
||||
local recipients = {}
|
||||
local undeliverable = {}
|
||||
local to = mail.normalize_players_and_add_recipients("player1,player2", recipients, undeliverable)
|
||||
|
||||
assert(to == "player1, player2")
|
||||
assert(not next(undeliverable))
|
||||
assert(recipients["player1"])
|
||||
assert(recipients["player2"])
|
||||
callback()
|
||||
end)
|
7
util/uuid.spec.lua
Normal file
7
util/uuid.spec.lua
Normal file
|
@ -0,0 +1,7 @@
|
|||
|
||||
|
||||
mtt.register("uuid", function(callback)
|
||||
assert(mail.new_uuid())
|
||||
assert(mail.new_uuid() ~= mail.new_uuid())
|
||||
callback()
|
||||
end)
|
Loading…
Add table
Add a link
Reference in a new issue