Sha256: 364207f6306449f264c01cb7fb7a414ce98c61d7ef7f5a250aac7c02f6be9f1f

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

# encoding: utf-8

# Set up gem loading (necessary for cri dependency)
require File.dirname(__FILE__) + '/gem_loader.rb'

# Load unit testing stuff
begin
  require 'minitest/unit'
  require 'minitest/spec'
  require 'minitest/mock'
  require 'mocha'
rescue => e
  $stderr.puts "To run the nanoc unit tests, you need minitest and mocha."
  raise e
end

# Load nanoc
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
require 'nanoc3'
require 'nanoc3/cli'
require 'nanoc3/tasks'

# Load miscellaneous requirements
require 'stringio'

module Nanoc3::TestHelpers

  def if_have(*libs)
    libs.each do |lib|
      begin
        require lib
      rescue LoadError
        skip "requiring #{lib} failed"
        return
      end
    end

    yield
  end

  def if_implemented
    begin
      yield
    rescue NotImplementedError, NameError
      skip $!
      return
    end
  end

  def setup
    # Clean up
    GC.start

    # Go quiet
    unless ENV['QUIET'] == 'false'
      $stdout = StringIO.new
      $stderr = StringIO.new
    end

    # Enter tmp
    FileUtils.mkdir_p('tmp')
    FileUtils.cd('tmp')
  end

  def teardown
    # Exit tmp
    FileUtils.cd('..')
    FileUtils.rm_rf('tmp')

    # Go unquiet
    unless ENV['QUIET'] == 'false'
      $stdout = STDOUT
      $stderr = STDERR
    end
  end

  # Adapted from http://github.com/lsegal/yard-examples/tree/master/doctest
  def assert_examples_correct(object)
    P(object).tags(:example).each do |example|
      begin
        # Get input and output
        parts = example.text.split(/# ?=>/).map { |s| s.strip }
        code             = parts[0].strip
        expected_out_raw = parts[1].strip

        # Evaluate
        expected_out     = eval(parts[1])
        actual_out       = instance_eval("#{code}")
      rescue Exception => e
        e.message << " (code: #{code}; expected output: #{expected_out_raw})"
        raise e
      end

      assert_equal expected_out, actual_out,
        "Incorrect example: #{code}"
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nanoc3-3.1.9 test/helper.rb
nanoc3-3.1.8 test/helper.rb