lunarmodules / luassert

Assertion library for Lua
MIT License
206 stars 77 forks source link

Sample has_property registration code has empty error messages #11

Closed kikito closed 12 years ago

kikito commented 12 years ago

I have tried the code in the README file, with the addition of a namespace, as per #10:

local assert = require "luassert"
local s = require("say") --our i18n lib, installed through luarocks, included as a luassert dependency

local function has_property(table, prop)
  for _, value in pairs(table) do
    if value == prop then
      return true
    end
  end
  return false, {prop, table}
end

s:set("en", "assertion.has_property.positive", "Expected property %s in:\n%s")
s:set("en", "assertion.has_property.negative", "Expected property %s to not be in:\n%s")
assert:register("assertion", "has_property", has_property, "assertion.has_property.positive", "assertion.has_property.negative")

assert.has_property({ name = "jack" }, "name") -- this should "pass"
assert.has_property({ name = "jack" }, "foo") -- this should "fail"

Unfortunately the error message I get when executing this is empty:

➜  lua  lua test_has_property_assertion.lua 
lua: /usr/local/share/lua/5.1/luassert/assert.lua:12: 
stack traceback:
    [C]: in function 'error'
    /usr/local/share/lua/5.1/luassert/assert.lua:12: in function 'has_property'
    test_has_property_assertion.lua:18: in main chunk
    [C]: ?

I expected to see the message "Expected property foo in table #..." somewhere on that text, but it was not.

kikito commented 12 years ago

This is fixed in #12. Closing down this issue for clarity.