<commit_before>require 'fileutils'
require 'test/unit'

require "#{File.expand_path(File.dirname(__FILE__))}/../lib/waldo.rb"

require 'mocha'
require 'htmlentities'
require 'nokogiri'

#ENV['SUPPRESS_DEBUG'] = 'true'

class Test::Unit::TestCase
  def sample_doc_path(name)
    name = name.to_s
    unless name.include?('.')
      ['asciidoc', 'txt'].each do |ext|
        if File.exist?(fixture_path("#{name}.#{ext}"))
          name = "#{name}.#{ext}"
          break
        end
      end
    end
    fixture_path(name)
  end

  def fixture_path(name)
    File.join(File.dirname(__FILE__), "fixtures", name )
  end

  def example_document(name)
    Asciidoc::Document.new(File.readlines(sample_doc_path(name)))
  end

  def assert_difference(expression, difference = 1, message = nil, &block)
    expressions = [expression]

    exps = expressions.map { |e|
      e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
    }
    before = exps.map { |e| e.call }

    yield

    expressions.zip(exps).each_with_index do |(code, e), i|
      error  = "#{code.inspect} didn't change by #{difference}"
      error  = "#{message}.\n#{error}" if message
      assert_equal(before[i] + difference, e.call, error)
    end
  end

  def assert_xpath(xpath, html, count = nil)
    results = Nokogiri::HTML::DocumentFragment.parse(html).xpath(".#{xpath}")

    if (count && results.length != count)
      flunk "XPath #{xpath} yielded #{results.length} elements rather than #{count} for:\n#{html}"
    elsif (count.nil? && results.empty?)
      flunk "XPath #{xpath} not found in:\n#{html}"
    else
      assert true
    end
  end

  def render_string(src)
    Asciidoc::Document.new(("Header\n======\n\n" + src).split("\n")).render
  end
end

##
# test/spec/mini 5
# http://gist.github.com/307649
# <EMAIL>
#
def context(*args, &block)
  return super unless (name = args.first) && block

  klass = Class.new(Test::Unit::TestCase) do
    def self.test(desc, &block)
      define_method("test_#{name.to_s.downcase}_#{desc.to_s.gsub(/\W/,'_')}", &block) if block
    end
    def self.xtest(*args) end
    def self.context(*args, &block) instance_eval(&block) end
    def self.setup(&block)
      define_method(:setup) { self.class.setups.each { |s| instance_eval(&s) } }
      setups << block
    end
    def self.setups; @setups ||= [] end
    def self.teardown(&block) define_method(:teardown, &block) end
  end

  (class << klass; self end).send(:define_method, :name) { name.gsub(/\W/,'_') }
  
  klass.class_eval &block
end<commit_msg>Really fix name clashes.  Ew.<commit_after>require 'fileutils'
require 'test/unit'

require "#{File.expand_path(File.dirname(__FILE__))}/../lib/waldo.rb"

require 'mocha'
require 'htmlentities'
require 'nokogiri'

#ENV['SUPPRESS_DEBUG'] = 'true'

class Test::Unit::TestCase
  def sample_doc_path(name)
    name = name.to_s
    unless name.include?('.')
      ['asciidoc', 'txt'].each do |ext|
        if File.exist?(fixture_path("#{name}.#{ext}"))
          name = "#{name}.#{ext}"
          break
        end
      end
    end
    fixture_path(name)
  end

  def fixture_path(name)
    File.join(File.dirname(__FILE__), "fixtures", name )
  end

  def example_document(name)
    Asciidoc::Document.new(File.readlines(sample_doc_path(name)))
  end

  def assert_difference(expression, difference = 1, message = nil, &block)
    expressions = [expression]

    exps = expressions.map { |e|
      e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
    }
    before = exps.map { |e| e.call }

    yield

    expressions.zip(exps).each_with_index do |(code, e), i|
      error  = "#{code.inspect} didn't change by #{difference}"
      error  = "#{message}.\n#{error}" if message
      assert_equal(before[i] + difference, e.call, error)
    end
  end

  def assert_xpath(xpath, html, count = nil)
    results = Nokogiri::HTML::DocumentFragment.parse(html).xpath(".#{xpath}")

    if (count && results.length != count)
      flunk "XPath #{xpath} yielded #{results.length} elements rather than #{count} for:\n#{html}"
    elsif (count.nil? && results.empty?)
      flunk "XPath #{xpath} not found in:\n#{html}"
    else
      assert true
    end
  end

  def render_string(src)
    Asciidoc::Document.new(("Header\n======\n\n" + src).split("\n")).render
  end
end

##
# test/spec/mini 5
# http://gist.github.com/307649
# <EMAIL>
#
def context(*args, &block)
  return super unless (name = args.first) && block

  klass = Class.new(Test::Unit::TestCase) do
    def self.test(desc, &block)
      define_method("test_#{name.to_s.downcase}_#{desc.to_s.gsub(/\W/,'_')}_#{Time.now.to_i}", &block) if block
    end
    def self.xtest(*args) end
    def self.context(*args, &block) instance_eval(&block) end
    def self.setup(&block)
      define_method(:setup) { self.class.setups.each { |s| instance_eval(&s) } }
      setups << block
    end
    def self.setups; @setups ||= [] end
    def self.teardown(&block) define_method(:teardown, &block) end
  end

  (class << klass; self end).send(:define_method, :name) { name.gsub(/\W/,'_') }
  
  klass.class_eval &block
end
{'fraction_non_alphanumeric': 0.16709108716944174, 'fraction_numerical': 0.0031341821743388833, 'mean_word_length': 3.3492333901192506, 'pattern_counts': {'":': 0, '<': 13, '<?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': '7278293', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1824, 'n_words': 383}