test/test_helper.rb in asciidoctor-1.5.6.2 vs test/test_helper.rb in asciidoctor-1.5.7
- old
+ new
@@ -1,7 +1,8 @@
# encoding: UTF-8
-ASCIIDOCTOR_PROJECT_DIR = File.dirname File.dirname(__FILE__)
+ASCIIDOCTOR_TEST_DIR = File.expand_path File.dirname __FILE__
+ASCIIDOCTOR_PROJECT_DIR = File.dirname ASCIIDOCTOR_TEST_DIR
Dir.chdir ASCIIDOCTOR_PROJECT_DIR
if RUBY_VERSION < '1.9'
require 'rubygems'
end
@@ -10,10 +11,11 @@
require File.join(ASCIIDOCTOR_PROJECT_DIR, 'lib', 'asciidoctor')
require 'socket'
require 'nokogiri'
+require 'tempfile'
require 'tmpdir'
autoload :FileUtils, 'fileutils'
autoload :Pathname, 'pathname'
@@ -29,11 +31,11 @@
def windows?
RbConfig::CONFIG['host_os'] =~ /win|ming/
end
def disk_root
- "#{windows? ? File.expand_path(__FILE__).split('/').first : nil}/"
+ %(#{windows? ? ASCIIDOCTOR_PROJECT_DIR.split('/')[0] : ''}/)
end
def empty_document options = {}
if options[:parse]
(Asciidoctor::Document.new [], options).parse
@@ -58,33 +60,24 @@
end
end
fixture_path(name)
end
- def fixture_path(name)
- File.join(File.expand_path(File.dirname(__FILE__)), 'fixtures', name)
+ def testdir
+ ASCIIDOCTOR_TEST_DIR
end
- def example_document(name, opts = {})
- document_from_string IO.read(sample_doc_path(name)), opts
+ def fixturedir
+ File.join testdir, 'fixtures'
end
- def assert_difference(expression, difference = 1, message = nil, &block)
- expressions = [expression]
+ def fixture_path name
+ File.join fixturedir, name
+ end
- 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
+ def example_document(name, opts = {})
+ document_from_string IO.read(sample_doc_path(name)), opts
end
def xmlnodes_at_css(css, content, count = nil)
xmlnodes_at_path(:css, css, content, count)
end
@@ -141,10 +134,39 @@
else
assert true
end
end
+ def assert_message logger, severity, expected_message, kind = String, idx = nil
+ unless idx
+ assert_equal 1, logger.messages.size
+ idx = 0
+ end
+ message = logger.messages[idx]
+ assert_equal severity, message[:severity]
+ assert_kind_of kind, message[:message]
+ if kind == String
+ actual_message = message[:message]
+ else
+ refute_nil message[:message][:source_location]
+ actual_message = message[:message].inspect
+ end
+ if expected_message.start_with? '~'
+ assert_includes actual_message, expected_message[1..-1]
+ else
+ assert_equal expected_message, actual_message
+ end
+ end
+
+ def assert_messages logger, expected_messages
+ assert_equal expected_messages.size, logger.messages.size
+ expected_messages.each_with_index do |expected_message_details, idx|
+ severity, expected_message, kind = expected_message_details
+ assert_message logger, severity, expected_message, (kind || String), idx
+ end
+ end
+
def xmldoc_from_string(content)
if content.match(RE_XMLNS_ATTRIBUTE)
Nokogiri::XML::Document.parse(content)
elsif !(doctype_match = content.match(RE_DOCTYPE))
Nokogiri::HTML::DocumentFragment.parse(content)
@@ -171,27 +193,27 @@
end
def render_string(src, opts = {})
keep_namespaces = opts.delete(:keep_namespaces)
if keep_namespaces
- document_from_string(src, opts).render
+ document_from_string(src, opts).convert
else
# this is required because nokogiri is ignorant
- result = document_from_string(src, opts).render
+ result = document_from_string(src, opts).convert
result = result.sub(RE_XMLNS_ATTRIBUTE, '') if result
result
end
end
def render_embedded_string(src, opts = {})
opts[:header_footer] = false
- document_from_string(src, opts).render
+ document_from_string(src, opts).convert
end
def render_inline_string(src, opts = {})
opts[:doctype] = :inline
- document_from_string(src, opts).render
+ document_from_string(src, opts).convert
end
def parse_header_metadata(source, doc = nil)
reader = Asciidoctor::Reader.new source.split ::Asciidoctor::LF
[::Asciidoctor::Parser.parse_header_metadata(reader, doc), reader]
@@ -209,11 +231,10 @@
opts[:attributes]['linkcss'] = ''
end
end
if (template_dir = ENV['TEMPLATE_DIR'])
opts[:template_dir] = template_dir unless opts.has_key? :template_dir
- #opts[:template_dir] = File.join(File.dirname(__FILE__), '..', '..', 'asciidoctor-backends', 'erb') unless opts.has_key? :template_dir
end
nil
end
# Decode the numeric character reference, such as 8212, to a Unicode glyph
@@ -228,18 +249,17 @@
def decode_char number
[number].pack 'U1'
end
def invoke_cli_with_filenames(argv = [], filenames = [], &block)
-
filepaths = Array.new
- filenames.each { |filename|
- if filenames.nil?|| ::Pathname.new(filename).absolute?
- filepaths.push(filename)
+ filenames.each {|filename|
+ if filenames.nil? || ::Pathname.new(filename).absolute?
+ filepaths << filename
else
- filepaths.push(File.join(File.dirname(__FILE__), 'fixtures', filename))
+ filepaths << (fixture_path filename)
end
}
invoker = Asciidoctor::Cli::Invoker.new(argv + filepaths)
@@ -253,11 +273,11 @@
def invoke_cli(argv = [], filename = 'sample.asciidoc', buffers = nil, &block)
if filename.nil? || filename == '-' || ::Pathname.new(filename).absolute?
filepath = filename
else
- filepath = File.join(File.dirname(__FILE__), 'fixtures', filename)
+ filepath = fixture_path filename
end
invoker = Asciidoctor::Cli::Invoker.new(argv + [filepath])
if buffers
invoker.redirect_streams(*buffers)
end
@@ -266,22 +286,47 @@
end
def redirect_streams
old_stdout, $stdout = $stdout, (tmp_stdout = ::StringIO.new)
old_stderr, $stderr = $stderr, (tmp_stderr = ::StringIO.new)
+ old_logger = Asciidoctor::LoggerManager.logger
+ old_logger_level = old_logger.level
+ new_logger = (Asciidoctor::LoggerManager.logger = Asciidoctor::Logger.new $stderr)
+ new_logger.level = old_logger_level
yield tmp_stdout, tmp_stderr
ensure
$stdout, $stderr = old_stdout, old_stderr
+ Asciidoctor::LoggerManager.logger = old_logger
end
def resolve_localhost
(RUBY_VERSION < '1.9' || RUBY_ENGINE == 'rbx') ? Socket.gethostname :
Socket.ip_address_list.find {|addr| addr.ipv4? }.ip_address
end
+ def using_memory_logger
+ old_logger = Asciidoctor::LoggerManager.logger
+ memory_logger = Asciidoctor::MemoryLogger.new
+ begin
+ Asciidoctor::LoggerManager.logger = memory_logger
+ yield memory_logger
+ ensure
+ Asciidoctor::LoggerManager.logger = old_logger
+ end
+ end
+
+ def in_verbose_mode
+ begin
+ old_verbose, $VERBOSE = $VERBOSE, true
+ yield
+ ensure
+ $VERBOSE = old_verbose
+ end
+ end
+
def using_test_webserver host = resolve_localhost, port = 9876
server = TCPServer.new host, port
- base_dir = File.expand_path File.dirname __FILE__
+ base_dir = testdir
t = Thread.new do
while (session = server.accept)
request = session.gets
resource = nil
if (m = /GET (\S+) HTTP\/1\.1$/.match(request.chomp))