Module: Petitest::Assertions

Defined in:
lib/petitest/assertions.rb,
lib/petitest/assertions/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Instance Method Details

#assert_equal(expected, actual, message = nil) ⇒ Object


10
11
12
13
14
15
# File 'lib/petitest/assertions.rb', line 10

def assert_equal(expected, actual, message = nil)
  message ||= "Expected #{expected.inspect} to be equal to #{actual.inspect}"
  assert(message) do
    expected == actual
  end
end

#assert_match(expected, actual, message = nil) ⇒ Object


20
21
22
23
24
25
# File 'lib/petitest/assertions.rb', line 20

def assert_match(expected, actual, message = nil)
  message ||= "Expected #{expected.inspect} === #{actual.inspect}"
  assert(message) do
    expected === actual
  end
end

#assert_operator(left, operator, right, message = nil) ⇒ Object


30
31
32
33
34
35
# File 'lib/petitest/assertions.rb', line 30

def assert_operator(left, operator, right, message = nil)
  message ||= "Expected #{left.inspect} #{operator} #{right.inspect}"
  assert(message) do
    left.__send__(operator, right)
  end
end

#assert_output(pattern_for_stdout, pattern_for_stderr = nil, message = nil, &block) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/petitest/assertions.rb', line 40

def assert_output(pattern_for_stdout, pattern_for_stderr = nil, message = nil, &block)
  output_from_stdout, output_from_stderr = capture_stdout_and_stderr(&block)
  unless pattern_for_stdout.nil?
    assert(message || "Expected stdout output to match with #{pattern_for_stdout.inspect}") do
      pattern_for_stdout === output_from_stdout
    end
  end
  unless pattern_for_stdout.nil?
    assert(message || "Expected stderr output to match with #{pattern_for_stdout.inspect}") do
      pattern_for_stdout === output_from_stdout
    end
  end
end

#assert_raise(*args, &block) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/petitest/assertions.rb', line 55

def assert_raise(*args, &block)
  message = args.pop if args.last.is_a?(::String)
  expected_exception_classes = args
  expected_exception_classes << ::Exception if expected_exception_classes.empty?
  expected_exception_classes_text = expected_exception_classes.map(&:inspect).join(" or ")
  message ||= "Expected #{expected_exception_classes_text} to be raised, but not"
  assert(message) do
    actual = begin
      block.call
      nil
    rescue *expected_exception_classes => exception
      exception
    rescue ::SignalException, ::SystemExit
      raise
    rescue ::Exception => exception
      exception
    end
    expected_exception_classes.any? do |klass|
      klass === actual
    end
  end
end

#assert_to_be(phrase, *args) ⇒ Object


80
81
82
83
84
85
86
87
88
# File 'lib/petitest/assertions.rb', line 80

def assert_to_be(phrase, *args)
  actual = args.pop
  method_name = "#{phrase}?"
  args_text = "(#{args.map(&:inspect).join(', ')})" unless args.empty?
  message = "Expected #{actual.inspect}.#{method_name}#{args_text} to be truthy"
  assert(message) do
    actual.__send__(method_name, *args)
  end
end

#assert_to_have(phrase, *args) ⇒ Object


92
93
94
95
96
97
98
99
100
# File 'lib/petitest/assertions.rb', line 92

def assert_to_have(phrase, *args)
  actual = args.pop
  method_name = "has_#{phrase}?"
  args_text = "(#{args.map(&:inspect).join(', ')})" unless args.empty?
  message = "Expected #{actual.inspect}.#{method_name}#{args_text} to be truthy"
  assert(message) do
    actual.__send__(method_name, *args)
  end
end