class Loupe::Test
The parent class for tests. Tests should inherit from this class in order to be run.
Attributes
@return [Loupe::Color]
@return [String]
@return [Integer]
@return [String]
@return [Loupe::Reporter]
Public Class Methods
@param number [Integer] @return [void]
# File lib/loupe/test.rb, line 77 def self.add_line_number(number) classes[@current_class] << number end
@return [Hash<Class, Array<Integer>>]
# File lib/loupe/test.rb, line 71 def self.classes @classes ||= {} end
@param test_class [Class] @return [void]
# File lib/loupe/test.rb, line 83 def self.inherited(test_class) @current_class = test_class classes[test_class] = [] super end
@param reporter [Loupe::Reporter] @param method_name [Symbol] @param options [Hash<Symbol, BasicObject>] @return [Loupe::Test]
# File lib/loupe/test.rb, line 111 def initialize(reporter, method_name, options = {}) @reporter = reporter @color = Color.new(options[:color]) @name = method_name @method = method(method_name) @file, @line_number = @method.source_location end
Run a single test with designated by `method_name`
@param method_name [Symbol] @param options [Hash<Symbol, BasicObject>] @return [Loupe::Reporter]
# File lib/loupe/test.rb, line 99 def self.run(method_name, options = {}) reporter = options[:interactive] ? PagedReporter.new(options) : PlainReporter.new(options) new(reporter, method_name, options).run reporter rescue Expectation::ExpectationFailed reporter end
@return [Array<Symbol>]
# File lib/loupe/test.rb, line 90 def self.test_list instance_methods(false).grep(/^test.*/) end
Public Instance Methods
@return [void]
# File lib/loupe/test.rb, line 134 def after; end
@return [void]
# File lib/loupe/test.rb, line 131 def before; end
Run the instantiated test, which corresponds to a single method. @return [void]
# File lib/loupe/test.rb, line 122 def run @reporter.increment_test_count before @method.call after @reporter.increment_success_count end
Protected Instance Methods
expect(target)
Initial construct for any expectation. Instantiates an Expectation
object on which verifications can be performed. Any expectation can be chained to reuse the object if the `target` is the same.
Example:
expect(collection) .to_not_be_empty .to_include(object) .be_an_instance_of(Array)
@return [Loupe::Expectation]
# File lib/loupe/test.rb, line 151 def expect(target) Expectation.new(target, self) end
expect_output_to_be_empty
{ block }
Expects the output generated by `block` to be empty for both `$stdout` and `$stderr`. That is, expects the `block` to not print anything to either `$stdout` or `$stderr`. For matching to the output of the `block`, see {#expect_output_to_match}.
Example: expect_output_to_be_empty
do
puts "bar" if false
end
@return [void]
# File lib/loupe/test.rb, line 217 def expect_output_to_be_empty(&block) expect_output_to_match("", "", &block) end
expect_output_to_match
(stdout, stderr) { block }
Expects the output generated by the execution of `block` to match the matchers used for `stdout` and `stderr`. If the `block` only prints to one of the two, simply pass `nil` for the one that is not of interest.
Example: expect_output_to_match
(“foo”) do
puts "foo"
end
expect_output_to_match
(nil, /error: .*/) do
$stderr.puts "error: operation failed"
end
@return [void]
# File lib/loupe/test.rb, line 171 def expect_output_to_match(stdout = nil, stderr = nil, &block) raise ArgumentError, "expect_output_to_match requires a block to capture output." unless block out, err = capture_io(&block) match_or_equal(stdout, out) if stdout match_or_equal(stderr, err) if stderr end
expect_output_to_not_be_empty
{ block }
Expects the output generated by `block` to not be empty for both `$stdout` and `$stderr`. That is, expects the `block` to print something to either `$stdout` or `$stderr`. For matching to the output of the `block`, see {#expect_output_to_not_match}.
Example: expect_output_to_not_be_empty
do
puts "foo"
end
@return [void]
# File lib/loupe/test.rb, line 233 def expect_output_to_not_be_empty(&block) expect_output_to_not_match("", "", &block) end
expect_output_to_not_match
(stdout, stderr) { block }
Expects the output generated by the execution of `block` to not match the matchers used for `stdout` and `stderr`. If the `block` only prints to one of the two, simply pass `nil` for the one that is not of interest.
Example: expect_output_to_not_match
(“foo”) do
puts "bar"
end
expect_output_to_not_match
(nil, /error: network failed.*/) do
$stderr.puts "error: record not unique"
end
@return [void]
# File lib/loupe/test.rb, line 196 def expect_output_to_not_match(stdout = nil, stderr = nil, &block) raise ArgumentError, "expect_output_to_not_match requires a block to capture output." unless block out, err = capture_io(&block) refute_match_or_equal(stdout, out) if stdout refute_match_or_equal(stderr, err) if stderr end
Private Instance Methods
@return [Array<String>]
# File lib/loupe/test.rb, line 254 def capture_io new_stdout = StringIO.new new_stderr = StringIO.new stdout = $stdout stderr = $stderr $stdout = new_stdout $stderr = new_stderr yield [new_stdout.string, new_stderr.string] ensure $stdout = stdout $stderr = stderr end
@param matcher [Regexp, String] @param output [String] @return [void]
# File lib/loupe/test.rb, line 242 def match_or_equal(matcher, output) matcher.is_a?(Regexp) ? expect(matcher).to_match(output) : expect(matcher).to_be_equal_to(output) end
@param matcher [Regexp, String] @param output [String] @return [void]
# File lib/loupe/test.rb, line 249 def refute_match_or_equal(matcher, output) matcher.is_a?(Regexp) ? expect(matcher).to_not_match(output) : expect(matcher).to_not_be_equal_to(output) end