Module: Rasta::Fixture::RastaFixture

Includes:
BaseFixture
Defined in:
lib/rasta/fixture/rasta_fixture.rb

Defined Under Namespace

Modules: TestCaseHelperMethods

Constant Summary collapse

GREEN =
35
RED =
40

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseFixture

#current_failure_count, #initialize_run, #run, #run_rspec_test, #select_output_cell, #tab_extension=, #teardown_tests

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



8
9
10
# File 'lib/rasta/fixture/rasta_fixture.rb', line 8

def comment
  @comment
end

#pendingObject

Returns the value of attribute pending.



8
9
10
# File 'lib/rasta/fixture/rasta_fixture.rb', line 8

def pending
  @pending
end

Instance Method Details

#call_fixture_attribute(name, value) ⇒ Object

For cell headers detected as test fixture attributes, set the attribute



56
57
58
59
# File 'lib/rasta/fixture/rasta_fixture.rb', line 56

def call_fixture_attribute(name, value)
  @metrics.inc(:attribute_count)
  @test_fixture.send("#{name}=", value)
end

#call_fixture_method(cell) ⇒ Object

For cell headers detected as test fixture methods, call the method and run the rspec test



63
64
65
66
67
# File 'lib/rasta/fixture/rasta_fixture.rb', line 63

def call_fixture_method(cell)
  @metrics.inc(:method_count)
  create_rspec_test(cell)
  run_rspec_test
end

#call_test_fixture(cell) ⇒ Object

Check to see if the cell’s header is an attribute or a method and call it or raise an error



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rasta/fixture/rasta_fixture.rb', line 38

def call_test_fixture(cell)
  name = cell.header
  return if cell.value.nil? || cell.header.nil?
  if cell.header == 'pending'
    @test_fixture.pending = cell.value
  elsif self.methods.include?(name + '=') 
    call_fixture_attribute(name, cell.value)
  else
    # don't specifically check that the method
    # exists so we can allow for things like
    # dynamic methods. An exception still gets thrown
    # so we should be able to handle missing methods
    call_fixture_method(cell)
  end
end

#create_rspec_test(cell) ⇒ Object

Given a cell that is a method call create an rspec test that can check the return value and handle any exceptions



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rasta/fixture/rasta_fixture.rb', line 72

def create_rspec_test(cell)
  select_output_cell(cell)
  test_method_name = "#{cell.header}()"
  test_fixture = @test_fixture
  describe "#{cell.sheet.name}[#{cell.name}]" do 
    include TestCaseHelperMethods
    
    before(:all) do
      @fixture = test_fixture
      @cell = cell
      @@actual_value = nil
      # If the cell's value is an exception, parse it out so we can handle properly
      @exception_expected = !(@cell.value.to_s =~ /^error\s*((?:(?!:\s).)*):?\s*(.*)/).nil?
      if @exception_expected
        @exception = eval($1) if $1 != ''
        @exception_message = $2 if $2 != ''
      end
    end
    
    it "#{test_method_name} should handle exceptions" do
      set_pending_status
      check_for_errors
    end
    it "#{test_method_name} should == #{cell.value.to_s}" do 
      set_pending_status
      check_test_result
    end
    
    after(:all) do
      @fixture = nil
    end
  end
end

#generate_rspec_testsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rasta/fixture/rasta_fixture.rb', line 13

def generate_rspec_tests
  @metrics.reset_page_counts
  @test_fixture = self        
  initial_failure_count = current_failure_count
  
  try(:before_all)
  @sheet.each do |record|
    @metrics.reset_record_counts
    @current_record = record
    @metrics.inc(:record_count)
    @test_fixture = self.dup #make a copy so attributes don't bleed between rows
    try(:before_each)
    record.each do |cell|
      call_test_fixture(cell)
    end 
    try(:after_each)
    @test_fixture = self
  end
  try(:after_all)
  
  update_tab_color(current_failure_count > initial_failure_count)
end

#try(method) ⇒ Object

Call a method in the test fixture and if it throws an exception, create an rspec test. This is not for standard tests, but for setup and teardown tasks that should happen but are not part of the actual test cases.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rasta/fixture/rasta_fixture.rb', line 159

def try(method)
  return if !method
  if @test_fixture.methods.include?(method.to_s)
    begin
      @test_fixture.send method
    rescue SystemExit
      exit
    rescue Interrupt
      exits
    rescue => error_message
      # If the method gets an error, re-raise the error
      # in the context of rspec so the results pick it up
      describe "#{@test_fixture.class}[#{@current_record.to_s}] #{method.to_s}()" do
        it "should not throw an exception" do
          lambda{raise error_message}.should_not raise_error
        end
      end
      run_rspec_test
    end
  end  
end