1require 'rubygems'
2require 'minitest/autorun'
3require 'minitest/benchmark' if ENV['BENCHMARK']
4
5require 'fileutils'
6require 'pp'
7require 'tempfile'
8require 'tmpdir'
9require 'stringio'
10
11require 'rdoc'
12
13##
14# RDoc::TestCase is an abstract TestCase to provide common setup and teardown
15# across all RDoc tests.  The test case uses minitest, so all the assertions
16# of minitest may be used.
17#
18# The testcase provides the following:
19#
20# * A reset code-object tree
21# * A reset markup preprocessor (RDoc::Markup::PreProcess)
22# * The <code>@RM</code> alias of RDoc::Markup (for less typing)
23# * <code>@pwd</code> containing the current working directory
24# * FileUtils, pp, Tempfile, Dir.tmpdir and StringIO
25
26class RDoc::TestCase < MiniTest::Unit::TestCase
27
28  ##
29  # Abstract test-case setup
30
31  def setup
32    super
33
34    @top_level = nil
35
36    @have_encoding = Object.const_defined? :Encoding
37
38    @RM = RDoc::Markup
39
40    RDoc::Markup::PreProcess.reset
41
42    @pwd = Dir.pwd
43
44    @store = RDoc::Store.new
45
46    @rdoc = RDoc::RDoc.new
47    @rdoc.store = @store
48
49    g = Object.new
50    def g.class_dir() end
51    def g.file_dir() end
52    @rdoc.generator = g
53  end
54
55  ##
56  # Shortcut for RDoc::Markup::BlankLine.new
57
58  def blank_line
59    @RM::BlankLine.new
60  end
61
62  ##
63  # Shortcut for RDoc::Markup::BlockQuote.new with +contents+
64
65  def block *contents
66    @RM::BlockQuote.new(*contents)
67  end
68
69  ##
70  # Creates an RDoc::Comment with +text+ which was defined on +top_level+.
71  # By default the comment has the 'rdoc' format.
72
73  def comment text, top_level = @top_level
74    RDoc::Comment.new text, top_level
75  end
76
77  ##
78  # Shortcut for RDoc::Markup::Document.new with +contents+
79
80  def doc *contents
81    @RM::Document.new(*contents)
82  end
83
84  ##
85  # Shortcut for RDoc::Markup::HardBreak.new
86
87  def hard_break
88    @RM::HardBreak.new
89  end
90
91  ##
92  # Shortcut for RDoc::Markup::Heading.new with +level+ and +text+
93
94  def head level, text
95    @RM::Heading.new level, text
96  end
97
98  ##
99  # Shortcut for RDoc::Markup::ListItem.new with +label+ and +parts+
100
101  def item label = nil, *parts
102    @RM::ListItem.new label, *parts
103  end
104
105  ##
106  # Shortcut for RDoc::Markup::List.new with +type+ and +items+
107
108  def list type = nil, *items
109    @RM::List.new type, *items
110  end
111
112  ##
113  # Shortcut for RDoc::Markup::Paragraph.new with +contents+
114
115  def para *a
116    @RM::Paragraph.new(*a)
117  end
118
119  ##
120  # Shortcut for RDoc::Markup::Rule.new with +weight+
121
122  def rule weight
123    @RM::Rule.new weight
124  end
125
126  ##
127  # Shortcut for RDoc::Markup::Raw.new with +contents+
128
129  def raw *contents
130    @RM::Raw.new(*contents)
131  end
132
133  ##
134  # Creates a temporary directory changes the current directory to it for the
135  # duration of the block.
136  #
137  # Depends upon Dir.mktmpdir
138
139  def temp_dir
140    skip "No Dir::mktmpdir, upgrade your ruby" unless Dir.respond_to? :mktmpdir
141
142    Dir.mktmpdir do |temp_dir|
143      Dir.chdir temp_dir do
144        yield temp_dir
145      end
146    end
147  end
148
149  ##
150  # Shortcut for RDoc::Markup::Verbatim.new with +parts+
151
152  def verb *parts
153    @RM::Verbatim.new(*parts)
154  end
155
156  ##
157  # run capture_io with setting $VERBOSE = true
158
159  def verbose_capture_io
160    capture_io do
161      begin
162        orig_verbose = $VERBOSE
163        $VERBOSE = true
164        yield
165      ensure
166        $VERBOSE = orig_verbose
167      end
168    end
169  end
170end
171
172# This hack allows autoload to work when Dir.pwd is changed for Ruby 1.8 since
173# -I paths are not expanded.
174$LOAD_PATH.each do |load_path|
175  break if load_path[0] == ?/
176  load_path.replace File.expand_path load_path
177end if RUBY_VERSION < '1.9'
178
179