Last active
December 17, 2015 10:19
-
-
Save apendley/5594141 to your computer and use it in GitHub Desktop.
cmodule_tests: simple tests and examples of cmodule usage. Requires the cmodule project as a Codea dependency, located at: https://gist.github.com/apendley/5411561
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Main | |
-- Main | |
-- tests current as of version 0.1.0 | |
function setup() | |
-- initialize cmodule with the name of this project. | |
-- note the syntactic sugar: when calling a function that | |
-- takes a string literal as it's only argument, the parenthesis | |
-- may be omitted. | |
cmodule "cmodule_tests" | |
-- import the TestClass2 module, which depends | |
-- on the TestClass module. Again note the syntactic | |
-- sugar. Note that when module path is not absolute (i.e. "project:module"), | |
-- cimport will check the running project, then the search path for the module. | |
local TestClass2 = cimport "TestClass2" | |
local obj = TestClass2(20) | |
print('\nTestClass2 instance value:', obj.value) | |
-- load the ModuleTest module | |
local ModuleTest = cimport "ModuleTest" | |
-- call moduleFunction from the imported ModuleTest table | |
ModuleTest.moduleFunction() | |
-- since ModuleTest doesn't return anything, | |
-- the module's environment (_M) is imported. | |
print("\ncontents of ModuleTest's _M") | |
for k, v in pairs(ModuleTest) do | |
print(k, v) | |
end | |
-- load a file with a sandboxed environment. | |
-- loaded module does *not* get cached; it is | |
-- recompiled and reloaded every time. | |
-- cload() is intended for loading tabs that | |
-- primarily contain data. Note that we can't | |
-- use Lua's syntactic sugar for function calls | |
-- when passing more than 1 argument. | |
local env = {CONSTANT = 12, color=color} | |
local data = cload('data', env) | |
print('\nloaded data:') | |
for k,v in pairs(data) do | |
print(k, tostring(v)) | |
end | |
-- print out our global that was set in TestClass | |
print('\naGlobal, set in TestClass:', aGlobal) | |
-- print out our loaded modules | |
local loaded = cmodule.loaded() | |
print('\nloaded modules:') | |
for _, v in ipairs(loaded) do | |
print(v) | |
end | |
end | |
function draw() | |
background(0) | |
end | |
--# data | |
--[[ | |
return { | |
value2 = CONSTANT, | |
color = color(255), | |
value1 = '10', | |
value2 = 'I am a string!', | |
-- constants provided to this module by cmodule | |
myProj = __proj, | |
myFile = __file, | |
myPath = __pathto(__file) | |
} | |
--]] | |
--# ModuleTest | |
--[[ | |
-- because of the way cmodule works, | |
-- ModuleTest is assigned to this module's | |
-- environment (_M), not _G | |
ModuleTest = class() | |
function ModuleTest:init(val) | |
self.value = val | |
end | |
-- as is moduleFunction | |
function moduleFunction() | |
print('\nModuleTest.moduleFunction() called\n') | |
end | |
-- also note that we aren't returning anything. | |
-- in this case, our module's _M table will be returned | |
-- from import. This module's _M table will contain | |
-- ModuleTest and moduleFunction() | |
-- generally though, it's better to use locals, and to | |
-- explicitly return our values, as it generates faster | |
-- code. | |
--]] | |
--# TestClass | |
--[[ | |
-- uncomment to generate a circular dependency exception | |
--local circular = import "TestClass2" | |
local TestClass = class() | |
function TestClass:init(val) | |
self.value = val | |
end | |
-- you must explicitly use _G to write to the global environment | |
-- from within a module, otherwise your "global" will go to _M | |
_G.aGlobal = "_G.aGlobal set" | |
-- note however that you don't have to prefix globals | |
-- when simply reading them. | |
local aLocal = aGlobal | |
print("aLocal value:", aLocal) | |
-- you may also export several variables to _G at once by using | |
-- cmodule.gexport: | |
cmodule.gexport { | |
myGlobal1 = 50, | |
myGlobal2 = 100, | |
MyClass = TestClass | |
} | |
return TestClass | |
--]] | |
--# TestClass2 | |
--[[ | |
-- you can call your imported modules whatever you want. | |
-- also, within a module we have an additional API, cinclude, | |
-- which is syntactic sugar for cimport(__proj "TestClass") | |
super = cinclude "TestClass" | |
TestClass2 = class(super) | |
function TestClass2:init(val) | |
super.init(self, val*10) | |
self.color = color(255) | |
end | |
return TestClass2 | |
--]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment