<commit_before>spec = {
  contexts = {}, passed = 0, failed = 0, pending = 0, current = nil
}

Report = {}
Report.__index = Report

function Report:new(spec)
  local report = {    
    num_passed = spec.passed,
    num_failed = spec.failed,
    num_pending = spec.pending,
    total = spec.passed + spec.failed + spec.pending,
    results = {}
  }
  
  report.percent = report.num_passed/report.total*100
    
  local contexts = spec.contexts
  
  for index = 1, #contexts do
    report.results[index] = {
      name = contexts[index],      
      spec_results = contexts[contexts[index]]
    }
  end    
  
  return report    
end

function spec:report(verbose)
  local report = Report:new(self)

  if report.num_failed == 0 and not verbose then
    print "all tests passed"
    return
  end
  
  for i, result in pairs(report.results) do
    print(("%s\n================================"):format(result.name))
    
    for description, r in pairs(result.spec_results) do
      local outcome = r.passed and 'pass' or "FAILED"

      if verbose or not (verbose and r.passed) then
        print(("%-70s [ %s ]"):format(" - " .. description, outcome))

        table.foreach(r.errors, function(index, error)
          print("   " .. index ..". Failed expectation : " .. error.message .. "\n   "..error.trace)
        end)
      end
    end
  end

  local summary = [[
  
  
========== Summary =============
%s Failed
%s Passed
--------------------------------
%s Run, %.2f%% Success rate
]]

  print(summary:format(report.num_failed, report.num_passed, report.total, report.percent))
end

function spec:add_results(success, message, trace)
  if self.current.passed then
    self.current.passed = success
  end

  if success then
    self.passed = self.passed + 1
  else
    table.insert(self.current.errors, { message = message, trace = trace })
    self.failed = self.failed + 1
  end
end

function spec:add_context(name)  
  self.contexts[#self.contexts+1] = name
  self.contexts[name] = {}  
end

function spec:add_spec(context_name, spec_name)
  local context = self.contexts[context_name]
  context[spec_name] = { passed = true, errors = {} }
  self.current = context[spec_name]
end

function spec:add_pending_spec(context_name, spec_name, pending_description)
end

-- create tables to support pending specifications
local pending = {}

function pending.__newindex() error("You can't set properties on pending") end

function pending.__index(_, key) 
  if key == "description" then 
    return nil 
  else
    error("You can't get properties on pending") 
  end
end

function pending.__call(_, description)
  local o = { description = description}
  setmetatable(o, pending)
  return o
end  

setmetatable(pending, pending)

--

-- define matchers

matchers = {
  should_be = function(value, expected)
    if value ~= expected then
      return false, "expecting "..tostring(expected)..", not ".. tostring(value)
    end
    return true
  end;
  
  should_not_be = function(value, expected)
    if value == expected then
      return false, "should not be "..tostring(value)
    end
    return true
  end;
  
  should_error = function(f)
    if pcall(f) then
      return false, "expecting an error but received none"
    end
    return true
  end;

  should_match = function(value, pattern) 
    if type(value) ~= 'string' then
      return false, "type error, should_match expecting target as string"
    end

    if not string.match(value, pattern) then
      return false, value .. "doesn't match pattern ".. pattern
    end
    return true
  end;
  
  should_be_kind_of = function(value, class)
    if type(value) == "userdata" then

      if not value:isKindOfClass(NSString) then
        return false, tostring(value) .. " is not a " .. tostring(class)
      end
    elseif type(value) ~= class then
      return false, type(value) .. " is not a " .. tostring(class)
    end
    
    return true
  end;
  
  should_exist = function(value)
    if not value then
      return false, tostring(value) .. " evaluates to false."
    else
      return true
    end
  end;<commit_msg>Fixing luaspec, should_be_kinf_of check real class not NSString class.<commit_after>spec = {
  contexts = {}, passed = 0, failed = 0, pending = 0, current = nil
}

Report = {}
Report.__index = Report

function Report:new(spec)
  local report = {    
    num_passed = spec.passed,
    num_failed = spec.failed,
    num_pending = spec.pending,
    total = spec.passed + spec.failed + spec.pending,
    results = {}
  }
  
  report.percent = report.num_passed/report.total*100
    
  local contexts = spec.contexts
  
  for index = 1, #contexts do
    report.results[index] = {
      name = contexts[index],      
      spec_results = contexts[contexts[index]]
    }
  end    
  
  return report    
end

function spec:report(verbose)
  local report = Report:new(self)

  if report.num_failed == 0 and not verbose then
    print "all tests passed"
    return
  end
  
  for i, result in pairs(report.results) do
    print(("%s\n================================"):format(result.name))
    
    for description, r in pairs(result.spec_results) do
      local outcome = r.passed and 'pass' or "FAILED"

      if verbose or not (verbose and r.passed) then
        print(("%-70s [ %s ]"):format(" - " .. description, outcome))

        table.foreach(r.errors, function(index, error)
          print("   " .. index ..". Failed expectation : " .. error.message .. "\n   "..error.trace)
        end)
      end
    end
  end

  local summary = [[
  
  
========== Summary =============
%s Failed
%s Passed
--------------------------------
%s Run, %.2f%% Success rate
]]

  print(summary:format(report.num_failed, report.num_passed, report.total, report.percent))
end

function spec:add_results(success, message, trace)
  if self.current.passed then
    self.current.passed = success
  end

  if success then
    self.passed = self.passed + 1
  else
    table.insert(self.current.errors, { message = message, trace = trace })
    self.failed = self.failed + 1
  end
end

function spec:add_context(name)  
  self.contexts[#self.contexts+1] = name
  self.contexts[name] = {}  
end

function spec:add_spec(context_name, spec_name)
  local context = self.contexts[context_name]
  context[spec_name] = { passed = true, errors = {} }
  self.current = context[spec_name]
end

function spec:add_pending_spec(context_name, spec_name, pending_description)
end

-- create tables to support pending specifications
local pending = {}

function pending.__newindex() error("You can't set properties on pending") end

function pending.__index(_, key) 
  if key == "description" then 
    return nil 
  else
    error("You can't get properties on pending") 
  end
end

function pending.__call(_, description)
  local o = { description = description}
  setmetatable(o, pending)
  return o
end  

setmetatable(pending, pending)

--

-- define matchers

matchers = {
  should_be = function(value, expected)
    if value ~= expected then
      return false, "expecting "..tostring(expected)..", not ".. tostring(value)
    end
    return true
  end;
  
  should_not_be = function(value, expected)
    if value == expected then
      return false, "should not be "..tostring(value)
    end
    return true
  end;
  
  should_error = function(f)
    if pcall(f) then
      return false, "expecting an error but received none"
    end
    return true
  end;

  should_match = function(value, pattern) 
    if type(value) ~= 'string' then
      return false, "type error, should_match expecting target as string"
    end

    if not string.match(value, pattern) then
      return false, value .. "doesn't match pattern ".. pattern
    end
    return true
  end;
  
  should_be_kind_of = function(value, class)
    if type(value) == "userdata" then

      if not value:isKindOfClass(class) then
        return false, tostring(value) .. " is not a " .. tostring(class)
      end
    elseif type(value) ~= class then
      return false, type(value) .. " is not a " .. tostring(class)
    end
    
    return true
  end;
  
  should_exist = function(value)
    if not value then
      return false, tostring(value) .. " evaluates to false."
    else
      return true
    end
  end;
{'fraction_non_alphanumeric': 0.12667152929734987, 'fraction_numerical': 0.0034038414782397277, 'mean_word_length': 2.8123262279888785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5431833', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2512, 'n_words': 741}