Skip to content
Browse files

Set context and test functions in module's metatable

See the specs for Lua Haml for a good example of how this is used.

https://github.com/haml/haml-spec/blob/master/lua_haml_spec.lua
  • Loading branch information...
1 parent 50ce53c commit 5f1ae579c1b8471fc44eab6f75af8c8097694232 @norman committed Jul 1, 2012
Showing with 26 additions and 19 deletions.
  1. +26 −19 telescope.lua
View
45 telescope.lua
@@ -5,6 +5,7 @@
-- @release 0.5
-- @class module
-- @module 'telescope'
+local _M = {}
local compat_env = require 'telescope.compat_env'
@@ -303,7 +304,15 @@ local function load_contexts(target, contexts)
for _, v in ipairs(context_aliases) do env[v] = context_block end
for _, v in ipairs(test_aliases) do env[v] = test_block end
+ -- Set these functions in the module's meta table to allow accessing
+ -- telescope's test and context functions without env tricks. This will
+ -- however add tests to a context table used inside the module, so multiple
+ -- test files will add tests to the same top-level context, which may or may
+ -- not be desired.
+ setmetatable(_M, {__index = env})
+
setmetatable(env, {__index = _G})
+
local func, err = type(target) == 'string' and assert(loadfile(target)) or target
if err then error(err) end
setfenv(func, env)()
@@ -532,22 +541,20 @@ local function summary_report(contexts, results)
return table.concat(buffer, " "), r
end
-local telescope = {
- after_aliases = after_aliases,
- make_assertion = make_assertion,
- assertion_message_prefix = assertion_message_prefix,
- before_aliases = before_aliases,
- context_aliases = context_aliases,
- error_report = error_report,
- load_contexts = load_contexts,
- run = run,
- test_report = test_report,
- status_codes = status_codes,
- status_labels = status_labels,
- summary_report = summary_report,
- test_aliases = test_aliases,
- version = _VERSION,
- _VERSION = _VERSION
-}
-
-return telescope
+_M.after_aliases = after_aliases
+_M.make_assertion = make_assertion
+_M.assertion_message_prefix = assertion_message_prefix
+_M.before_aliases = before_aliases
+_M.context_aliases = context_aliases
+_M.error_report = error_report
+_M.load_contexts = load_contexts
+_M.run = run
+_M.test_report = test_report
+_M.status_codes = status_codes
+_M.status_labels = status_labels
+_M.summary_report = summary_report
+_M.test_aliases = test_aliases
+_M.version = _VERSION
+_M._VERSION = _VERSION
+
+return _M

0 comments on commit 5f1ae57

Please sign in to comment.
Something went wrong with that request. Please try again.