test/test_helper.rb in asciidoctor-0.0.9 vs test/test_helper.rb in asciidoctor-0.1.0

- old
+ new

@@ -1,6 +1,7 @@ require 'fileutils' +require 'pathname' require 'test/unit' require "#{File.expand_path(File.dirname(__FILE__))}/../lib/asciidoctor.rb" begin @@ -13,10 +14,18 @@ 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}")) @@ -27,15 +36,16 @@ end fixture_path(name) end def fixture_path(name) - File.join(File.dirname(__FILE__), "fixtures", name ) + File.join(File.expand_path(File.dirname(__FILE__)), 'fixtures', name) end - def example_document(name) - Asciidoctor::Document.new(File.readlines(sample_doc_path(name))) + 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] @@ -109,10 +119,11 @@ doc = Nokogiri::XML::Document.parse(content) end end def document_from_string(src, opts = {}) + opts[:header_footer] = true unless opts.has_key?(:header_footer) Asciidoctor::Document.new(src.lines.entries, opts) end def block_from_string(src, opts = {}) opts[:header_footer] = false @@ -130,9 +141,42 @@ end def parse_header_metadata(source) reader = Asciidoctor::Reader.new source.lines.entries [Asciidoctor::Lexer.parse_header_metadata(reader), reader] + end + + def invoke_cli_to_buffer(argv = [], filename = 'sample.asciidoc', &block) + invoke_cli(argv, filename, [StringIO.new, StringIO.new], &block) + end + + 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) + end + invoker = Asciidoctor::Cli::Invoker.new(argv + [filepath]) + if buffers + invoker.redirect_streams(*buffers) + end + invoker.invoke!(&block) + invoker + end + + def redirect_streams + old_stdout = $stdout + old_stderr = $stderr + stdout = StringIO.new + stderr = StringIO.new + $stdout = stdout + $stderr = stderr + begin + yield(stdout, stderr) + ensure + $stdout = old_stdout + $stderr = old_stderr + end end end ### #