Class: GreenPepper::CollectionExample

Inherits:
ExampleWithFixture show all
Defined in:
lib/greenpepper/example/collectionexample.rb

Direct Known Subclasses

CollectionSetExample, ListOfExample

Constant Summary collapse

FIRST_DATA_ROW =
2

Constants inherited from ExampleWithFixture

ExampleWithFixture::EXAMPLE_NAME_COLUMN, ExampleWithFixture::EXAMPLE_NAME_ROW, ExampleWithFixture::FIXTURE_NAME_COLUMN, ExampleWithFixture::FIXTURE_NAME_ROW, ExampleWithFixture::HEADER_ROW

Instance Attribute Summary collapse

Attributes inherited from ExampleWithFixture

#fixture_arguments, #fixture_name, #headers

Instance Method Summary collapse

Methods inherited from ExampleWithFixture

#add_fixture_argument, #execute

Constructor Details

#initialize(fixture_name) ⇒ CollectionExample

Returns a new instance of CollectionExample.



20
21
22
23
24
# File 'lib/greenpepper/example/collectionexample.rb', line 20

def initialize(fixture_name)
  super fixture_name
  @headers_array = []
  @data = []
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'lib/greenpepper/example/collectionexample.rb', line 16

def data
  @data
end

#headers_arrayObject (readonly)

Returns the value of attribute headers_array.



16
17
18
# File 'lib/greenpepper/example/collectionexample.rb', line 16

def headers_array
  @headers_array
end

Instance Method Details

#add_header(header) ⇒ Object



34
35
36
# File 'lib/greenpepper/example/collectionexample.rb', line 34

def add_header(header)
  @headers_array.push header
end

#add_headers(headers) ⇒ Object



30
31
32
# File 'lib/greenpepper/example/collectionexample.rb', line 30

def add_headers(headers)
  @headers_array += headers
end

#add_row(data) ⇒ Object



26
27
28
# File 'lib/greenpepper/example/collectionexample.rb', line 26

def add_row(data)
  @data.push data
end

#do_execute(results, fixture_class) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
105
106
# File 'lib/greenpepper/example/collectionexample.rb', line 38

def do_execute(results, fixture_class)
  fixture = instantiate_fixture fixture_class
  
  if fixture.respond_to? 'query'
    begin
      collection = fixture.query
    rescue
      results.add FIXTURE_NAME_ROW, FIXTURE_NAME_COLUMN, WriteExceptionExampleResult.new($!)
      return results
    end
  else
    ex = GreenPepperMissingMethodError.new(
      "Cannot find method 'query' on fixture #{fixture.class}.")
    results.add FIXTURE_NAME_ROW, FIXTURE_NAME_COLUMN, WriteExceptionExampleResult.new(ex)
    return results
  end

  header_error = false
  collection_object = collection[FIXTURE_NAME_ROW]

  index_list = [] 
  @headers_array.each_with_index do |header, header_index|

    if header =='' ||!collection_object.respond_to?(header)
      error = GreenPepperMissingHeaderError.new(
        "Cannot find '#{header}' on object '#{fixture.class}'.")
      results.add HEADER_ROW, header_index, WriteExceptionExampleResult.new(error)
      index_list << header_index
    else
      results.add HEADER_ROW, header_index, NoExampleResult.new
    end
  end
  index_list.each{|index| @headers_array[index] = nil if index != -1}

  collection_data = []
  collection.each do |collection_element|
    collection_row = []
    @headers_array.each do |header|        
      if header != nil
        begin
          field_value = NameResolver.get_instance_field(
            collection_element, header)
          collection_row << field_value
        rescue => error
          collection_row << error
        end
      else
        collection_row << nil
      end
    end
    collection_data << collection_row
  end
  
  
  example_data = []
  @data.each_index do |row_index|
    example_row = []
    @headers_array.each_with_index do |header, header_index|
      if header != nil
        example_row << TypeConverter.instance.auto_convert_string(
          collection_object, header, @data[row_index][header_index])
      else
        example_row << nil
      end
    end
    example_data << example_row
  end
  do_execute_collection(results, example_data, collection_data)
end