Class: Attest::ExecutionContext

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil) ⇒ ExecutionContext

Returns a new instance of ExecutionContext.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/attest/execution_context.rb', line 13

def initialize(context=nil)
  @results = []
  @subject = self
  @persistent_context = context
  own_instance_variables = self.instance_variables
  context.instance_variables.each do |instance_variable|
    unless own_instance_variables.include? instance_variable
      self.instance_variable_set(instance_variable.to_s, context.instance_variable_get(instance_variable))
    end
  end
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



5
6
7
# File 'lib/attest/execution_context.rb', line 5

def results
  @results
end

Class Method Details

.assertionsObject



8
9
10
# File 'lib/attest/execution_context.rb', line 8

def assertions
  self.instance_methods(false).select{|method_name| method_name =~ /^should.*/ }.inspect
end

Instance Method Details

#create_and_include(module_class) ⇒ Object

worker methods



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/attest/execution_context.rb', line 119

def create_and_include(module_class)
  class_name = "#{module_class}Class"
  klass = nil
  begin
    klass = Object.const_get(class_name)
  rescue NameError => e
    class_instance = Class.new
    Object.const_set class_name, class_instance
    Object.const_get(class_name).include(Object.const_get("#{module_class}"))
    klass = Object.const_get(class_name)
  end
  klass.new
end

#should_be(expected_lambda, &block) ⇒ Object Also known as: should_be_a, should_be_an, should_match, should_match_a, should_match_an



25
26
27
28
29
# File 'lib/attest/execution_context.rb', line 25

def should_be(expected_lambda, &block)
  with_new_result do |result|
    expected_lambda.call == yield ? result.success : result.failure
  end
end

#should_be_same(expected_value, actual_value = nil, &block) ⇒ Object Also known as: should_be_same_as



47
48
49
# File 'lib/attest/execution_context.rb', line 47

def should_be_same(expected_value, actual_value=nil, &block)
  derive_result_status_from_method(expected_value, actual_value, :"equal?", &block)
end

#should_be_true(actual_value = nil, &block) ⇒ Object



66
67
68
# File 'lib/attest/execution_context.rb', line 66

def should_be_true(actual_value=nil, &block)
  derive_result_status_from_method(true, actual_value, :"==", &block)
end

#should_equal(expected_value, actual_value = nil, &block) ⇒ Object



57
58
59
# File 'lib/attest/execution_context.rb', line 57

def should_equal(expected_value, actual_value=nil, &block)
  derive_result_status_from_method(expected_value, actual_value, :"==", &block)
end

#should_failObject Also known as: should_not_succeed



75
76
77
78
79
# File 'lib/attest/execution_context.rb', line 75

def should_fail
  with_new_result do |result|
    result.failure
  end
end

#should_not_be(expected_lambda, &block) ⇒ Object Also known as: should_not_be_a, should_not_be_an, should_not_match, should_not_match_a, should_not_match_an



36
37
38
39
40
# File 'lib/attest/execution_context.rb', line 36

def should_not_be(expected_lambda, &block)
  with_new_result do |result|
    expected_lambda.call != yield ? result.success : result.failure
  end
end

#should_not_be_same(expected_value, actual_value = nil, &block) ⇒ Object Also known as: should_not_be_same_as



52
53
54
# File 'lib/attest/execution_context.rb', line 52

def should_not_be_same(expected_value, actual_value=nil, &block)
  derive_result_status_from_method_negated(expected_value, actual_value, :"equal?", &block)
end

#should_not_be_true(actual_value = nil, &block) ⇒ Object Also known as: should_be_false



70
71
72
# File 'lib/attest/execution_context.rb', line 70

def should_not_be_true(actual_value = nil, &block)
  derive_result_status_from_method(false, actual_value, :"==", &block)
end

#should_not_equal(expected_value, actual_value = nil, &block) ⇒ Object Also known as: should_not_be_equal



61
62
63
# File 'lib/attest/execution_context.rb', line 61

def should_not_equal(expected_value, actual_value=nil, &block)
  derive_result_status_from_method_negated(expected_value, actual_value, :"==", &block)
end

#should_not_raise(&block) ⇒ Object



90
91
92
93
94
95
# File 'lib/attest/execution_context.rb', line 90

def should_not_raise(&block)
  should_raise(&block)
  with_last_result do |result|
    result.success? ? result.failure : result.success
  end
end

#should_raise(type = nil, opts = {}, &block) ⇒ Object

the only opt so far is :with_message which takes a regex



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/attest/execution_context.rb', line 98

def should_raise(type=nil, opts={}, &block)
  with_new_result do |result|
    begin
      if block_given?
        yield
      end
    rescue => e
      result.update(:expected_error => e)
      if expected_error?(type, opts[:with_message], e)
        result.success
      else
        result.failure
      end
    end
    unless result.success?
      result.failure 
    end
  end
end

#should_succeedObject Also known as: should_not_fail, should_pass



82
83
84
85
86
# File 'lib/attest/execution_context.rb', line 82

def should_succeed
  with_new_result do |result|
    result.success
  end
end