Class: Solea::MethodTest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name) ⇒ MethodTest

Returns a new instance of MethodTest.



46
47
48
49
50
51
# File 'lib/solea.rb', line 46

def initialize(method_name)
  @method_name = method_name
  @tests       = []
  @exceptions  = []
  @notes       = []
end

Instance Attribute Details

#method_nameObject

Returns the value of attribute method_name.



44
45
46
# File 'lib/solea.rb', line 44

def method_name
  @method_name
end

#passObject

Returns the value of attribute pass.



44
45
46
# File 'lib/solea.rb', line 44

def pass
  @pass
end

#testsObject

Returns the value of attribute tests.



44
45
46
# File 'lib/solea.rb', line 44

def tests
  @tests
end

Instance Method Details

#assert(&block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/solea.rb', line 77

def assert(&block)
  @hypothesis_caller = caller_locations(1,1)[0]
  begin
    @pass = block.call(@example)
  rescue Exception => e
    @pass = false
    @exceptions << [e, @hypothesis_caller]
  end
  @tests << self.dup
end

#assert!(&block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/solea.rb', line 88

def assert!(&block)
  @hypothesis_caller = caller_locations(1,1)[0]
  begin
    @pass = !block.call(@example)
  rescue Exception => e
    @pass = false
    @exceptions << [e, @hypothesis_caller]
  end
  @tests << self.dup
end

#before(&block) ⇒ Object



53
54
55
# File 'lib/solea.rb', line 53

def before(&block)
  @before = yield
end

#describe(&block) ⇒ Object



57
58
59
60
# File 'lib/solea.rb', line 57

def describe(&block)
  @description = yield
  @notes = []
end

#example(&block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/solea.rb', line 66

def example(&block)
  @example_caller = caller_locations(1,1)[0]
  @exceptions = []
  begin
    @example = block.call(@before)
  rescue Exception => e
    @example = block
    @exceptions << [e, @example_caller]
  end
end

#expect(exception_expected = nil, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/solea.rb', line 99

def expect(exception_expected = nil, &block)
  @hypothesis_caller = caller_locations(1,1)[0]
  if exception_expected
    test_for_exception(exception_expected, block)
  else
    begin
      @pass = yield == @example
    rescue Exception => e
      @pass = false
      @exceptions << [e, @hypothesis_caller]
    end
  end
  @tests << self.dup
end

#expect!(&block) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/solea.rb', line 114

def expect!(&block)
  @hypothesis_caller = caller_locations(1,1)[0]
  begin
    @pass = yield != @example
  rescue Exception => e
    @pass = false
    @exceptions << [e, @hypothesis_caller]
  end
  @tests << self.dup
end

#metadataObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/solea.rb', line 141

def 
  { :before      => @before,
    :description => @description,
    :example     => @example,
    :filename    => (@hypothesis_caller.path if @hypothesis_caller),
    :lineno      => (@hypothesis_caller.lineno if @hypothesis_caller),
    :exceptions  => @exceptions,
    :notes       => @notes,
    :pass        => @pass
  }
end

#note(&block) ⇒ Object



62
63
64
# File 'lib/solea.rb', line 62

def note(&block)
  @notes << yield
end


153
154
155
# File 'lib/solea.rb', line 153

def print(&block)
  puts block.call(self)
end

#reportObject



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/solea.rb', line 157

def report
  report_caller = caller_locations(1,1)[0]
  report = "method name: #{" " * ("description".length - "method name".length)}\"#{@method_name}\"\n"
  .each do |k, v|
    spaces = " " * ("description".length - k.length)
    v = "\"#{v}\"" if v.is_a?(String)
    v = "[#{"." * v.length}]" if v.is_a?(Array) || v.is_a?(Hash)
    report += "#{k}: #{spaces}#{v}\n"
  end
  report
end

#test_for_exception(exception_expected, block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/solea.rb', line 125

def test_for_exception(exception_expected, block)
  begin
    block_code = block.call(@example)
  rescue Exception => e
    block_code = block
    @exceptions << [e, @hypothesis_caller]
  end
  if block_code.nil?
    test = @example.is_a?(Proc)
  else
    test = block_code.is_a?(Proc)
  end
  @pass = test  if exception_expected == :fail
  @pass = !test if exception_expected == :pass
end