require 'spec_helper' describe 'Row, Integration' do let(:clazz) { CukeModeler::Row } describe 'common behavior' do it_should_behave_like 'a model, integration' end describe 'unique behavior' do it 'can be instantiated with the minimum viable Gherkin' do source = '| a | row |' expect { clazz.new(source) }.to_not raise_error end it 'provides a descriptive filename when being parsed from stand alone text' do source = " |bad |row| text| \n @foo " expect { clazz.new(source) }.to raise_error(/'cuke_modeler_stand_alone_row\.feature'/) end it 'stores the original data generated by the parsing adapter', :gherkin4 => true do example_row = clazz.new("| a | row |") data = example_row.parsing_data expect(data.keys).to match_array([:type, :location, :cells]) expect(data[:type]).to eq(:TableRow) end it 'stores the original data generated by the parsing adapter', :gherkin3 => true do example_row = clazz.new("| a | row |") data = example_row.parsing_data expect(data.keys).to match_array([:type, :location, :cells]) expect(data[:type]).to eq('TableRow') end it 'stores the original data generated by the parsing adapter', :gherkin2 => true do example_row = clazz.new("| a | row |") data = example_row.parsing_data expect(data.keys).to match_array(['cells', 'line']) expect(data['line']).to eq(4) end it 'properly sets its child models' do source = '| cell 1 | cell 2 |' row = clazz.new(source) cell_1 = row.cells.first cell_2 = row.cells.last expect(cell_1.parent_model).to equal(row) expect(cell_2.parent_model).to equal(row) end describe 'getting ancestors' do before(:each) do source = ['Feature: Test feature', '', ' Scenario: Test test', ' * a step:', ' | a | table |'] source = source.join("\n") file_path = "#{@default_file_directory}/row_test_file.feature" File.open(file_path, 'w') { |file| file.write(source) } end let(:directory) { CukeModeler::Directory.new(@default_file_directory) } let(:row) { directory.feature_files.first.feature.tests.first.steps.first.block.rows.first } it 'can get its directory' do ancestor = row.get_ancestor(:directory) expect(ancestor).to equal(directory) end it 'can get its feature file' do ancestor = row.get_ancestor(:feature_file) expect(ancestor).to equal(directory.feature_files.first) end it 'can get its feature' do ancestor = row.get_ancestor(:feature) expect(ancestor).to equal(directory.feature_files.first.feature) end it 'can get its step' do ancestor = row.get_ancestor(:step) expect(ancestor).to equal(directory.feature_files.first.feature.tests.first.steps.first) end it 'can get its table' do ancestor = row.get_ancestor(:table) expect(ancestor).to equal(directory.feature_files.first.feature.tests.first.steps.first.block) end context 'a row that is part of a scenario' do before(:each) do source = 'Feature: Test feature Scenario: Test test * a step: | a | table |' file_path = "#{@default_file_directory}/row_test_file.feature" File.open(file_path, 'w') { |file| file.write(source) } end let(:directory) { CukeModeler::Directory.new(@default_file_directory) } let(:row) { directory.feature_files.first.feature.tests.first.steps.first.block.rows.first } it 'can get its scenario' do ancestor = row.get_ancestor(:scenario) expect(ancestor).to equal(directory.feature_files.first.feature.tests.first) end end context 'a row that is part of a background' do before(:each) do source = 'Feature: Test feature Background: Test background * a step: | a | table |' file_path = "#{@default_file_directory}/row_test_file.feature" File.open(file_path, 'w') { |file| file.write(source) } end let(:directory) { CukeModeler::Directory.new(@default_file_directory) } let(:row) { directory.feature_files.first.feature.background.steps.first.block.rows.first } it 'can get its background' do ancestor = row.get_ancestor(:background) expect(ancestor).to equal(directory.feature_files.first.feature.background) end end context 'a row that is part of an outline' do before(:each) do source = 'Feature: Test feature Scenario Outline: Test outline * a step Examples: | param | | value |' file_path = "#{@default_file_directory}/row_test_file.feature" File.open(file_path, 'w') { |file| file.write(source) } end let(:directory) { CukeModeler::Directory.new(@default_file_directory) } let(:row) { directory.feature_files.first.feature.tests.first.examples.first.rows.first } it 'can get its outline' do ancestor = row.get_ancestor(:outline) expect(ancestor).to equal(directory.feature_files.first.feature.tests.first) end it 'can get its example' do ancestor = row.get_ancestor(:example) expect(ancestor).to equal(directory.feature_files.first.feature.tests.first.examples.first) end end it 'returns nil if it does not have the requested type of ancestor' do ancestor = row.get_ancestor(:outline) expect(ancestor).to be_nil end end describe 'model population' do context 'from source text' do let(:source_text) { '| cell 1 | cell 2 |' } let(:row) { clazz.new(source_text) } it "models the row's cells" do cell_values = row.cells.collect { |cell| cell.value } expect(cell_values).to match_array(['cell 1', 'cell 2']) end it "models the row's source line" do source_text = "Feature: Test feature Scenario Outline: Test outline * a step Examples: | param | | value |" row = CukeModeler::Feature.new(source_text).tests.first.examples.first.rows.first expect(row.source_line).to eq(6) end end end describe 'row output' do it 'can be remade from its own output' do source = ['| value1 | value2 |'] source = source.join("\n") row = clazz.new(source) row_output = row.to_s remade_row_output = clazz.new(row_output).to_s expect(remade_row_output).to eq(row_output) end context 'from source text' do it 'can output a row' do source = '| some value |' row = clazz.new(source) expect(row.to_s).to eq('| some value |') end it 'can output a row with multiple cells' do source = '| some value | some other value |' row = clazz.new(source) expect(row.to_s).to eq('| some value | some other value |') end end end end end