From 290c401b2891b1f210cac2f90c831c305c86ecb6 Mon Sep 17 00:00:00 2001 From: Evgeniy Dolzhenko Date: Sun, 10 Nov 2013 11:50:49 +0400 Subject: [PATCH] Fix failing assertion message to include asserted values properly (with spec) --- spec/assertions_spec.lua | 9 +++++++++ telescope.lua | 14 +++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 spec/assertions_spec.lua diff --git a/spec/assertions_spec.lua b/spec/assertions_spec.lua new file mode 100644 index 0000000..2d4aecb --- /dev/null +++ b/spec/assertions_spec.lua @@ -0,0 +1,9 @@ +describe('assertions', function() + local M = require 'telescope' + + it('should correctly print arguments asserions', function() + local res, err = pcall(function() assert_equal(1, 2) end) + assert(res == false) + assert(err[1] == "Assert failed: expected '1' to be equal to '2'") + end) +end) diff --git a/telescope.lua b/telescope.lua index 7250653..5758d14 100644 --- a/telescope.lua +++ b/telescope.lua @@ -155,7 +155,7 @@ local function make_assertion(name, message, func) local a = {} local args = {...} local nargs = select('#', ...) - if nargs > num_vars then + if nargs > num_vars then local userErrorMessage = args[num_vars+1] if type(userErrorMessage) == "string" then return(assertion_message_prefix .. userErrorMessage) @@ -163,7 +163,7 @@ local function make_assertion(name, message, func) error(string.format('assert_%s expected %d arguments but got %d', name, num_vars, #args)) end end - for i = 1, nargs do a[i] = tostring(v) end + for i = 1, nargs do a[i] = tostring(args[i]) end for i = nargs+1, num_vars do a[i] = 'nil' end return (assertion_message_prefix .. message):format(unpack(a)) end @@ -443,12 +443,12 @@ local function run(contexts, callbacks, test_filter) table.sort(ancestors) -- this "before" is the test callback passed into the runner invoke_callback("before", result) - + -- run all the "before" blocks/functions for _, a in ipairs(ancestors) do - if contexts[a].before then + if contexts[a].before then setfenv(contexts[a].before, env) - contexts[a].before() + contexts[a].before() end end @@ -465,9 +465,9 @@ local function run(contexts, callbacks, test_filter) -- Run all the "after" blocks/functions table.reverse(ancestors) for _, a in ipairs(ancestors) do - if contexts[a].after then + if contexts[a].after then setfenv(contexts[a].after, env) - contexts[a].after() + contexts[a].after() end end