test/test_helper.rb in asciidoctor-0.0.7 vs test/test_helper.rb in asciidoctor-0.0.9
- old
+ new
@@ -1,11 +1,15 @@
require 'fileutils'
require 'test/unit'
require "#{File.expand_path(File.dirname(__FILE__))}/../lib/asciidoctor.rb"
-require 'mocha/setup'
+begin
+ require 'mocha/setup'
+rescue LoadError
+ require 'mocha'
+end
require 'htmlentities'
require 'nokogiri'
require 'pending'
ENV['SUPPRESS_DEBUG'] ||= 'true'
@@ -47,28 +51,88 @@
error = "#{message}.\n#{error}" if message
assert_equal(before[i] + difference, e.call, error)
end
end
- def assert_xpath(xpath, html, count = nil)
- doc = (html =~ /\s*<!DOCTYPE/) ? Nokogiri::HTML::Document.parse(html) : Nokogiri::HTML::DocumentFragment.parse(html)
- results = doc.xpath("#{xpath.sub('/', './')}")
+ def xmlnodes_at_css(css, content, count = nil)
+ xmlnodes_at_path(:css, css, content)
+ end
+ def xmlnodes_at_xpath(css, content, count = nil)
+ xmlnodes_at_path(:xpath, css, 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 "XPath #{xpath} yielded #{results.length} elements rather than #{count} for:\n#{html}"
+ flunk "#{type_name} #{path} yielded #{results.length} elements rather than #{count} for:\n#{content}"
elsif (count.nil? && results.empty?)
- flunk "XPath #{xpath} not found in:\n#{html}"
+ flunk "#{type_name} #{path} not found in:\n#{content}"
else
assert true
end
end
+ def xmldoc_from_string(content)
+ match = content.match(/\s*<!DOCTYPE (.*)/)
+ if !match
+ doc = Nokogiri::HTML::DocumentFragment.parse(content)
+ elsif match[1].start_with? 'html'
+ doc = Nokogiri::HTML::Document.parse(content)
+ else
+ doc = Nokogiri::XML::Document.parse(content)
+ end
+ end
+
def document_from_string(src, opts = {})
Asciidoctor::Document.new(src.lines.entries, opts)
end
+ def block_from_string(src, opts = {})
+ opts[:header_footer] = false
+ doc = Asciidoctor::Document.new(src.lines.entries, opts)
+ doc.blocks.first
+ end
+
def render_string(src, opts = {})
document_from_string(src, opts).render
+ end
+
+ def render_embedded_string(src, opts = {})
+ opts[:header_footer] = false
+ document_from_string(src, opts).render
+ end
+
+ def parse_header_metadata(source)
+ reader = Asciidoctor::Reader.new source.lines.entries
+ [Asciidoctor::Lexer.parse_header_metadata(reader), reader]
end
end
###
#