Skip to content
Browse files

Added support for tables

  • Loading branch information...
1 parent 14f147b commit 0796e2897a8b183ed91a268b99436d6207c4a7e6 @jnicklas committed Nov 4, 2011
Showing with 100 additions and 2 deletions.
  1. +8 −0 examples/steps.rb
  2. +8 −0 examples/table.feature
  3. +1 −0 lib/turnip.rb
  4. +6 −1 lib/turnip/builder.rb
  5. +28 −0 lib/turnip/table.rb
  6. +1 −1 spec/integration_spec.rb
  7. +48 −0 spec/table_spec.rb
View
8 examples/steps.rb
@@ -57,6 +57,14 @@
end
step "there are the following monsters:" do |table|
+ @monsters = {}
+ table.hashes.each do |hash|
+ @monsters[hash['Name']] = hash['Hitpoints'].to_i
+ end
+end
+
+step ":name should have :count hitpoints" do |name, count|
+ @monsters[name].should eq(count.to_i)
end
step "the monster sings the following song" do |song|
View
8 examples/table.feature
@@ -0,0 +1,8 @@
+Feature: A feature with tables
+ Scenario: This is a feature with a table
+ Given there are the following monsters:
+ | Name | Hitpoints |
+ | Blaaarg | 23 |
+ | Moorg | 12 |
+ Then "Blaaarg" should have 23 hitpoints
+ And "Moorg" should have 12 hitpoints
View
1 lib/turnip.rb
@@ -9,6 +9,7 @@ module Turnip
autoload :Builder, 'turnip/builder'
autoload :StepDefinition, 'turnip/step_definition'
autoload :Placeholder, 'turnip/placeholder'
+ autoload :Table, 'turnip/table'
class << self
attr_accessor :type
View
7 lib/turnip/builder.rb
@@ -125,7 +125,12 @@ def examples(examples)
end
def step(step)
- @current_step_context.steps << Step.new(step.name, step.doc_string && step.doc_string.value)
+ if step.doc_string
+ extra_arg = step.doc_string.value
+ elsif step.rows
+ extra_arg = Turnip::Table.new(step.rows.map { |row| row.cells(&:value) })
+ end
+ @current_step_context.steps << Step.new(step.name, extra_arg)
end
def eof
View
28 lib/turnip/table.rb
@@ -0,0 +1,28 @@
+module Turnip
+ class Table
+ attr_reader :raw
+ alias_method :to_a, :raw
+
+ include Enumerable
+
+ def initialize(raw)
+ @raw = raw
+ end
+
+ def headers
+ @raw.first
+ end
+
+ def rows
+ @raw.drop(1)
+ end
+
+ def hashes
+ rows.map { |row| Hash[headers.zip(row)] }
+ end
+
+ def each
+ @raw.each { |row| yield(row) }
+ end
+ end
+end
View
2 spec/integration_spec.rb
@@ -11,6 +11,6 @@
end
it "prints out failures and successes" do
- @result.should include('13 examples, 1 failure, 1 pending')
+ @result.should include('14 examples, 1 failure, 1 pending')
end
end
View
48 spec/table_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+describe Turnip::Table do
+ describe '#raw' do
+ it 'returns the raw table' do
+ table = Turnip::Table.new([['foo', 'bar'], ['quox', '42']])
+ table.raw.should == [['foo', 'bar'], ['quox', '42']]
+ end
+ end
+
+ describe '#to_a' do
+ it 'returns the raw table' do
+ table = Turnip::Table.new([['foo', 'bar'], ['quox', '42']])
+ table.to_a.should == [['foo', 'bar'], ['quox', '42']]
+ end
+ end
+
+ describe '#headers' do
+ it 'returns the first row' do
+ table = Turnip::Table.new([['foo', 'bar'], ['quox', '42']])
+ table.headers.should == ['foo', 'bar']
+ end
+ end
+
+ describe '#rows' do
+ it 'returns the rows beyond the first' do
+ table = Turnip::Table.new([['foo', 'bar'], ['moo', '55'], ['quox', '42']])
+ table.rows.should == [['moo', '55'], ['quox', '42']]
+ end
+ end
+
+ describe '#hashes' do
+ it 'returns a list of hashe based on the headers' do
+ table = Turnip::Table.new([['foo', 'bar'], ['moo', '55'], ['quox', '42']])
+ table.hashes.should == [
+ {'foo' => 'moo', 'bar' => '55'},
+ {'foo' => 'quox', 'bar' => '42'}
+ ]
+ end
+ end
+
+ describe '#map' do
+ it 'iterates over the raw table' do
+ table = Turnip::Table.new([['moo', '55'], ['quox', '42']])
+ table.map(&:first).should == ['moo', 'quox']
+ end
+ end
+end

0 comments on commit 0796e28

Please sign in to comment.
Something went wrong with that request. Please try again.