require 'fileutils' require 'pathname' require 'test/unit' require "#{File.expand_path(File.dirname(__FILE__))}/../lib/asciidoctor.rb" begin require 'mocha/setup' rescue LoadError require 'mocha' end require 'htmlentities' require 'nokogiri' require 'pending' ENV['SUPPRESS_DEBUG'] ||= 'true' class Test::Unit::TestCase def windows? RbConfig::CONFIG['host_os'] =~ /win|ming/ end def disk_root "#{windows? ? File.expand_path(__FILE__).split('/').first : nil}/" end 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.expand_path(File.dirname(__FILE__)), 'fixtures', name) end def example_document(name, opts = {}) opts[:header_footer] = true unless opts.has_key?(:header_footer) Asciidoctor::Document.new(File.readlines(sample_doc_path(name)), opts) 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 xmlnodes_at_css(css, content, count = nil) xmlnodes_at_path(:css, css, content) end def xmlnodes_at_xpath(xpath, content, count = nil) xmlnodes_at_path(:xpath, xpath, content) end def xmlnodes_at_path(type, path, content, count = nil) doc = xmldoc_from_string content case type when :xpath results = doc.xpath("#{path.sub('/', './')}") when :css results = doc.css(path) end count == 1 ? results.first : results end def assert_css(css, content, count = nil) assert_path(:css, css, content, count) end def assert_xpath(xpath, content, count = nil) assert_path(:xpath, xpath, content, count) end def assert_path(type, path, content, count = nil) case type when :xpath type_name = 'XPath' when :css type_name = 'CSS' end results = xmlnodes_at_path type, path, content if (count && results.length != count) flunk "#{type_name} #{path} yielded #{results.length} elements rather than #{count} for:\n#{content}" elsif (count.nil? && results.empty?) flunk "#{type_name} #{path} not found in:\n#{content}" else assert true end end def xmldoc_from_string(content) match = content.match(/\s*