Class: TestEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/test_env.rb,
lib/test_env.rb

Defined Under Namespace

Classes: Assertion, Assertions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ TestEnvironment

Returns a new instance of TestEnvironment.



77
78
79
80
81
# File 'lib/test_env.rb', line 77

def initialize(obj)
  @obj, @setups = obj, []
  
  @assertions = Assertions.new
end

Instance Attribute Details

#assertionsObject (readonly)

Returns the value of attribute assertions.



75
76
77
# File 'lib/test_env.rb', line 75

def assertions
  @assertions
end

Class Method Details

.test_instance_of(obj, *args, &blk) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/test_env.rb', line 50

def test_instance_of(obj, *args, &blk)
  raise(ArgumentError, 'a block is required') unless block_given?
  
  instance_of_obj = obj.new(*args)
  
  instance_of_self = new(instance_of_obj)
  instance_of_self.instance_exec(instance_of_obj, &blk)
  
  
  puts "", "=" * 80, ""
  
  puts "Testing an instance of #{obj}:"
  
  instance_of_self.run_setups
  instance_of_self.assertions.call_all
  
  puts instance_of_self.assertions.to_s
  
  puts "", "=" * 80, ""
  puts "Assertions: #{instance_of_self.assertions.count}"
  puts "Passed: #{instance_of_self.assertions.passed_count}"
  puts "Failed: #{instance_of_self.assertions.failed_count}"
end

Instance Method Details

#assert(msg = nil, &blk) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
# File 'lib/test_env.rb', line 102

def assert(msg=nil, &blk)
  raise(ArgumentError, 'a block is required') unless block_given?
  
  @assertions << Assertion.new(msg, blk)
end

#run_setupsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/test_env.rb', line 88

def run_setups
  #===--- One instance_exec
  # @obj.instance_exec(@setups) do |setups|
    # setups.each { |s| s.call }
  # end
  
  #===--- Multi instance_exec
  @setups.each do |setup|
    # @obj.instance_eval(&setup) # Shouldnt it be this?
    @obj.instance_exec(&setup)
  end

end

#setup(&blk) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
# File 'lib/test_env.rb', line 83

def setup(&blk)
  raise(ArgumentError, 'a block is required') unless block_given?
  @setups << blk
end