Class: PryTest::TestWrapper

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/pry-test/test_wrapper.rb

Overview

A wrapper class for individual tests. Exists for the purpose of isolating the test method so it can run in its own thread.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_class, desc) { ... } ⇒ TestWrapper

Constructor.

Parameters:

  • test_class (PryTest::Test) —

    The test class that defines the test being wrapped.

  • desc (String) —

    The test description.

Yields:

  • The block that defines the test code.



16
17
18
19
20
21
22
# File 'lib/pry-test/test_wrapper.rb', line 16

def initialize(test_class, desc, &block)
  super() # inits MonitorMixin
  @test_class = test_class
  @desc = desc
  @asserts = []
  create_method(:test, &block)
end

Instance Attribute Details

#asserts ⇒ Object (readonly)

Returns the value of attribute asserts.



10
11
12
# File 'lib/pry-test/test_wrapper.rb', line 10

def asserts
  @asserts
end

#desc ⇒ Object (readonly)

Returns the value of attribute desc.



10
11
12
# File 'lib/pry-test/test_wrapper.rb', line 10

def desc
  @desc
end

#test_class ⇒ Object (readonly)

Returns the value of attribute test_class.



10
11
12
# File 'lib/pry-test/test_wrapper.rb', line 10

def test_class
  @test_class
end

Instance Method Details

#after ⇒ Object



36
37
# File 'lib/pry-test/test_wrapper.rb', line 36

def after
end

#assert(value) ⇒ Object

A basic assert method to be used within tests.

Examples:

class SimpleTest < PryTest::Test
  test "common sense" do
    assert 1 > 0
  end
end

Parameters:

  • value (Object) —

    The value to assert.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pry-test/test_wrapper.rb', line 65

def assert(value)
  info = assert_info(value, caller_locations(1..1).first)
  asserts << info.merge(value: value)

  unless value
    Pry.start unless @options[:disable_pry]

    # TODO: I don't really like the coupling to the runner here
    PryTest::Runner.terminate if @options[:fail_fast]
  end

  value
end

#before ⇒ Object

callback stubs



33
34
# File 'lib/pry-test/test_wrapper.rb', line 33

def before
end

#create_method(name) { ... } ⇒ Object

Creates a method on this instance.

Parameters:

  • name (Symbol) —

    The name of the method.

Yields:

  • The block of code that will serve as the method's implementation.



27
28
29
30
# File 'lib/pry-test/test_wrapper.rb', line 27

def create_method(name, &block)
  eigen = class << self; self; end
  eigen.send(:define_method, name, &block)
end

#duration ⇒ Object

Rounded duration.



112
113
114
115
# File 'lib/pry-test/test_wrapper.rb', line 112

def duration
  return nil if @duration.nil?
  (@duration * 10**4).ceil.to_f / 10**4
end

#failed_asserts ⇒ Object

Returns a list of all failed asserts.



107
108
109
# File 'lib/pry-test/test_wrapper.rb', line 107

def failed_asserts
  asserts.select { |a| !a[:value] }
end

#invoke(formatter, options = {}) ⇒ Object

Runs the test code.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pry-test/test_wrapper.rb', line 42

def invoke(formatter, options = {})
  @formatter = formatter
  @options = options
  @formatter.before_test(self)
  start = Time.now
  before
  test
  @invoked = true
  @duration = Time.now - start
  after
  @formatter.after_test(self) unless asserts.empty?
end

#invoked? ⇒ Boolean

Indicates if this test has been invoked.

Returns:

  • (Boolean)


95
96
97
# File 'lib/pry-test/test_wrapper.rb', line 95

def invoked?
  !!@invoked
end

#passed? ⇒ Boolean

Indicates if this test passed.

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/pry-test/test_wrapper.rb', line 100

def passed?
  return false unless invoked?
  return true if asserts.empty?
  asserts.map { |a| !!a[:value] }.uniq == [true]
end

#refute(value) ⇒ Object

A basic refute method to be used within tests.

Examples:

class SimpleTest < PryTest::Test
  test "common sense" do
    refute 0 > 1
  end
end

Parameters:

  • value (Object) —

    The value to refute.



89
90
91
# File 'lib/pry-test/test_wrapper.rb', line 89

def refute(value)
  assert !value
end