Quantcast

Add partial implementation of WoW API for unit-testing and development.

Johnny C. Lam [04-06-14 - 18:45]
Add partial implementation of WoW API for unit-testing and development.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1262 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
WoWAPI.lua
diff --git a/WoWAPI.lua b/WoWAPI.lua
new file mode 100644
index 0000000..934cc93
--- /dev/null
+++ b/WoWAPI.lua
@@ -0,0 +1,87 @@
+--[[--------------------------------------------------------------------
+    Ovale Spell Priority
+    Copyright (C) 2013 Johnny C. Lam
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License in the LICENSE
+    file accompanying this program.
+--]]--------------------------------------------------------------------
+
+--[[
+	This file implements parts of the WoW API for development and unit-testing.
+
+	This file is not meant to be loaded into the addon.  It should be used only
+	outside of the WoW environment, such as when loaded by a standalone Lua 5.1
+	interpreter.
+
+	It provides global symbols.
+--]]
+
+-- RAID_CLASS_COLORS is useful mostly as a table to loop through the keys, which are
+-- the class tokens for all playable classes.
+RAID_CLASS_COLORS = RAID_CLASS_COLORS or {
+	DEATHKNIGHT = true,
+	DRUID = true,
+	HUNTER = true,
+	MAGE = true,
+	MONK = true,
+	PALADIN = true,
+	PRIEST = true,
+	ROGUE = true,
+	SHAMAN = true,
+	WARLOCK = true,
+	WARRIOR = true,
+}
+
+-- wipe() is a non-standard Lua function that clears the contents of a table
+-- and leaves the table pointer intact.
+wipe = wipe or function(t)
+	for k in pairs(t) do
+		t[k] = nil
+	end
+end
+table.wipe = table.wipe or wipe
+
+-- strsplit() is a non-standard Lua function that splits a string and returns
+-- multiple return values for each substring delimited by the named delimiter
+-- character.
+--
+-- This implementaiton is taken verbatim from http://lua-users.org/wiki/SplitJoin
+strsplit = strsplit or function(delim, str, maxNb)
+	-- Eliminate bad cases...
+	if string.find(str, delim) == nil then
+		return str
+	end
+	if maxNb == nil or maxNb < 1 then
+		maxNb = 0    -- No limit
+	end
+	local result = {}
+	local pat = "(.-)" .. delim .. "()"
+	local nb = 0
+	local lastPos
+	for part, pos in string.gfind(str, pat) do
+		nb = nb + 1
+		result[nb] = part
+		lastPos = pos
+		if nb == maxNb then break end
+	end
+	-- Handle the last field
+	if nb ~= maxNb then
+		result[nb + 1] = string.sub(str, lastPos)
+	end
+	return unpack(result)
+end
+
+-- LoadAddonFile() does the equivalent of dofile(), but strips out the WoW addon
+-- file line that uses ... to get the file arguments.
+LoadAddonFile = LoadAddonFile or function(filename)
+	local lineList = {}
+	for line in io.lines(filename) do
+		if not string.match(line, "^%s*local%s+[%w%s_,]*%s*=%s*[.][.][.]%s*$") then
+			table.insert(lineList, line)
+		end
+	end
+	local fileString = table.concat(lineList, "\n")
+	local func = loadstring(fileString)
+	func()
+end