require 'spec_helper' SimpleCov.command_name('Table') unless RUBY_VERSION.to_s < '1.9.0' describe 'Table, Unit' do let(:clazz) { CukeModeler::Table } let(:table) { clazz.new } describe 'common behavior' do it_should_behave_like 'a nested element' it_should_behave_like 'a bare bones element' it_should_behave_like 'a prepopulated element' it_should_behave_like 'a raw element' end describe 'unique behavior' do it 'can be parsed from stand alone text' do source = '| a table |' expect { @element = clazz.new(source) }.to_not raise_error # Sanity check in case instantiation failed in a non-explosive manner @element.row_elements.collect { |row| row.cells }.should == [['a table']] # todo - remove once #contents is no longer supported @element.contents.should == [['a table']] end it 'provides a descriptive filename when being parsed from stand alone text' do source = 'bad table text' expect { clazz.new(source) }.to raise_error(/'cuke_modeler_stand_alone_table\.feature'/) end it 'stores the original data generated by the parsing adapter', :gherkin4 => true do table = clazz.new("| a table |") raw_data = table.raw_element expect(raw_data.keys).to match_array([:type, :location, :rows]) expect(raw_data[:type]).to eq(:DataTable) end it 'stores the original data generated by the parsing adapter', :gherkin3 => true do table = clazz.new("| a table |") raw_data = table.raw_element expect(raw_data.keys).to match_array([:type, :location, :rows]) expect(raw_data[:type]).to eq(:DataTable) end it 'stores the original data generated by the parsing adapter', :gherkin2 => true do table = clazz.new("| a table |") raw_data = table.raw_element expect(raw_data).to match_array([{"cells" => ["a table"], "line" => 4}]) end # todo - remove once #contents is no longer supported it 'has contents' do table.should respond_to(:contents) end # todo - remove once #contents is no longer supported it 'can change its contents' do expect(table).to respond_to(:contents=) table.contents = :some_contents table.contents.should == :some_contents table.contents = :some_other_contents table.contents.should == :some_other_contents end # todo - remove once #contents is no longer supported it 'starts with no contents' do table.contents.should == [] end it 'has row elements' do table.should respond_to(:row_elements) end it 'can get and set its row elements' do expect(table).to respond_to(:row_elements=) table.row_elements = :some_row_elements table.row_elements.should == :some_row_elements table.row_elements = :some_other_row_elements table.row_elements.should == :some_other_row_elements end it 'starts with no row elements' do table.row_elements.should == [] end # todo - remove once #contents is no longer supported it 'stores its contents as a nested array of strings' do source = "| cell 1 | cell 2 |\n| cell 3 | cell 4 |" table = clazz.new(source) contents = table.contents contents.is_a?(Array).should be_true contents.each do |row| row.is_a?(Array).should be_true row.each { |cell| cell.is_a?(String).should be_true } end end describe 'table output edge cases' do it 'is a String' do table.to_s.should be_a(String) end context 'a new table object' do let(:table) { clazz.new } it 'can output an empty table' do expect { table.to_s }.to_not raise_error end # todo - remove once #contents is no longer supported it 'can output a table that only has contents' do table.contents = ['some contents'] expect { table.to_s }.to_not raise_error end end end end end