<commit_before>-- module will return spy table, and register its assertions with the main assert engine
local assert = require('luassert.assert')
local util = require 'luassert.util'
local spy   -- must make local before defining table, because table contents refers to the table (recursion)
spy = {
  new = function(self, callback)
    return setmetatable(
    {
      calls = {},
      callback = callback or function() end,

      called = function(self, times)
        if times then
          return #self.calls == times
        end

        return #self.calls > 0
      end,

      called_with = function(self, args)
        for _,v in ipairs(self.calls) do
          if util.deepcompare(v, args) then
            return true
          end
        end

        return false
      end
    },
    {
      __call = function(self, ...)
        table.insert(self.calls, { ... })
        return self.callback(...)
      end
    })
  end,

  on = function(self, callback_string)
    self[callback_string] = spy:new(self[callback_string])
    return self[callback_string]
  end
}

local function set_spy(state)
end

local function called_with(state, ...)
  if rawget(state, "payload") and rawget(state, "payload").called_with then
    return state.payload:called_with({...})
  else
    error("'called_with' must be chained after 'spy(aspy)'")
  end
end

local function called(state, num_times)
  if state.payload and state.payload.called then
    return state.payload:called(num_times)
  else
    error("'called_with' must be chained after 'spy(aspy)'")
  end
end

assert:register("modifier", "spy", set_spy)
assert:register("assertion", "called_with", called_with, "Function was not called with arguments")
assert:register("assertion", "called", called, "Function was not called")

return spy
<commit_msg>Fixed the spies, final tests failing on previous changes. Now only the actual bad error message test still fails.<commit_after>-- module will return spy table, and register its assertions with the main assert engine
local assert = require('luassert.assert')
local util = require 'luassert.util'
local spy   -- must make local before defining table, because table contents refers to the table (recursion)
spy = {
  new = function(self, callback)
    return setmetatable(
    {
      calls = {},
      callback = callback or function() end,

      called = function(self, times)
        if times then
          return #self.calls == times
        end

        return #self.calls > 0
      end,

      called_with = function(self, args)
        for _,v in ipairs(self.calls) do
          if util.deepcompare(v, args) then
            return true
          end
        end

        return false
      end
    },
    {
      __call = function(self, ...)
        local arguments = {...}
        arguments.n = select('#',...)  -- add argument count for trailing nils
        table.insert(self.calls, arguments)
        return self.callback(...)
      end
    })
  end,

  on = function(self, callback_string)
    self[callback_string] = spy:new(self[callback_string])
    return self[callback_string]
  end
}

local function set_spy(state)
end

local function called_with(state, arguments)
  if rawget(state, "payload") and rawget(state, "payload").called_with then
    return state.payload:called_with(arguments)
  else
    error("'called_with' must be chained after 'spy(aspy)'")
  end
end

local function called(state, arguments)
  local num_times = arguments[1]
  if state.payload and state.payload.called then
    return state.payload:called(num_times)
  else
    error("'called_with' must be chained after 'spy(aspy)'")
  end
end

assert:register("modifier", "spy", set_spy)
assert:register("assertion", "called_with", called_with, "Function was not called with arguments")
assert:register("assertion", "called", called, "Function was not called")

return spy

{'fraction_non_alphanumeric': 0.1046090108751942, 'fraction_numerical': 0.0007767995857068876, 'mean_word_length': 3.0072614107883817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2581942', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1112, 'n_words': 361}