cucumber-table 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/Rakefile +22 -0
- data/cucumber-table.gemspec +29 -0
- data/example.rb +29 -0
- data/lib/cucumber-table.rb +18 -0
- data/lib/table/exception.rb +3 -0
- data/lib/table/helpers.rb +24 -0
- data/lib/table/table.rb +88 -0
- data/lib/table/version.rb +3 -0
- data/test/spec_helper.rb +9 -0
- data/test/string_spec.rb +43 -0
- data/test/table_spec.rb +125 -0
- metadata +201 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Bachue Zhou
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# CucumberTable
|
2
|
+
|
3
|
+
A gem which can parse Cucumber-Style String to a table. You can write a table in your test case just like you are using Cucumber. Now it mainly support RSpec.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'cucumber-table'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install cucumber-table
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Examples in RSpec:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
describe 'Example 1' do
|
25
|
+
table :user_info, """
|
26
|
+
| id | name | email | company | city | country |
|
27
|
+
| 1 | Bachue | bachue.shu@gmail.com | EMC | Shanghai | China |
|
28
|
+
| 2 | Elicier | elicier@gmail.com | Shanghai Normal University | Shanghai | China |
|
29
|
+
| 3 | Xi Core | unknown | Government of People's Republic of China | Beijing | China |
|
30
|
+
| 4 | Barack Obama | unknown | Government of United States of America | Washington DC | United States of America |
|
31
|
+
"""
|
32
|
+
it 'demos how to use CucumberTable::Table' do
|
33
|
+
user_info.hashes.should == [{'id' => '1', 'name' => 'Bachue', 'email' => 'bachue.shu@gmail.com', 'company' => 'EMC', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '2', 'name' => 'Elicier', 'email' => 'elicier@gmail.com', 'company' => 'Shanghai Normal University', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '3', 'name' => 'Xi Core', 'email' => 'unknown', 'company' => 'Government of People\'s Republic of China', 'city' => 'Beijing', 'country' => 'China'}, {'id' => '4', 'name' => 'Barack Obama', 'email' => 'unknown', 'company' => 'Government of United States of America', 'city' => 'Washington DC', 'country' => 'United States of America'}]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'Example 2' do
|
38
|
+
it 'demos how to use CucumberTable::Table' do
|
39
|
+
table :user_info, """
|
40
|
+
| id | name | email | company | city | country |
|
41
|
+
| 1 | Bachue | bachue.shu@gmail.com | EMC | Shanghai | China |
|
42
|
+
| 2 | Elicier | elicier@gmail.com | Shanghai Normal University | Shanghai | China |
|
43
|
+
| 3 | Xi Core | unknown | Government of People's Republic of China | Beijing | China |
|
44
|
+
| 4 | Barack Obama | unknown | Government of United States of America | Washington DC | United States of America |
|
45
|
+
"""
|
46
|
+
|
47
|
+
user_info.hashes.should == [{'id' => '1', 'name' => 'Bachue', 'email' => 'bachue.shu@gmail.com', 'company' => 'EMC', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '2', 'name' => 'Elicier', 'email' => 'elicier@gmail.com', 'company' => 'Shanghai Normal University', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '3', 'name' => 'Xi Core', 'email' => 'unknown', 'company' => 'Government of People\'s Republic of China', 'city' => 'Beijing', 'country' => 'China'}, {'id' => '4', 'name' => 'Barack Obama', 'email' => 'unknown', 'company' => 'Government of United States of America', 'city' => 'Washington DC', 'country' => 'United States of America'}]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
The Table Object provides a lot of methods, most are from `Cucumber::Ast::Table`, to operate the table.
|
53
|
+
|
54
|
+
Test cases in `test/table_spec.rb` could help you to understand how to operate the table.
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
__DIR__ = File.expand_path(File.dirname(__FILE__))
|
4
|
+
|
5
|
+
task :default => [:test]
|
6
|
+
|
7
|
+
desc 'Run all test cases'
|
8
|
+
task :test do
|
9
|
+
exec "bundle exec rspec -fd #{__DIR__}/test/*_spec.rb"
|
10
|
+
end
|
11
|
+
|
12
|
+
cases = Dir["#{__DIR__}/test/*_spec.rb"].map {|cas| cas.sub(/^#{"#{__DIR__}/test/"}/, '').sub(/_spec\.rb$/, '') }
|
13
|
+
unless cases.empty?
|
14
|
+
namespace :test do
|
15
|
+
cases.each {|cas|
|
16
|
+
desc "To test cucumber-table/#{cas}"
|
17
|
+
task cas do
|
18
|
+
exec "bundle exec rspec -fd #{__DIR__}/test/#{cas}_spec.rb"
|
19
|
+
end
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'table/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cucumber-table"
|
8
|
+
spec.version = CucumberTable::VERSION
|
9
|
+
spec.authors = ["Bachue Zhou"]
|
10
|
+
spec.email = ["bachue.shu@gmail.com"]
|
11
|
+
spec.description = 'A gem which can parse Cucumber-Style String to a table. You can write a table in your test case just like you are using Cucumber. Now it mainly support RSpec.'
|
12
|
+
spec.summary = spec.summary
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'its'
|
25
|
+
spec.add_development_dependency 'pry'
|
26
|
+
spec.add_development_dependency 'pry-debugger', '0.2.1'
|
27
|
+
spec.add_development_dependency 'pry-stack_explorer'
|
28
|
+
spec.add_development_dependency 'pry-doc'
|
29
|
+
end
|
data/example.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
$: << File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'test/spec_helper'
|
3
|
+
|
4
|
+
describe 'Example 1' do
|
5
|
+
table :user_info, """
|
6
|
+
| id | name | email | company | city | country |
|
7
|
+
| 1 | Bachue | bachue.shu@gmail.com | EMC | Shanghai | China |
|
8
|
+
| 2 | Elicier | elicier@gmail.com | Shanghai Normal University | Shanghai | China |
|
9
|
+
| 3 | Xi Core | unknown | Government of People's Republic of China | Beijing | China |
|
10
|
+
| 4 | Barack Obama | unknown | Government of United States of America | Washington DC | United States of America |
|
11
|
+
"""
|
12
|
+
it 'demos how to use CucumberTable::Table' do
|
13
|
+
user_info.hashes.should == [{'id' => '1', 'name' => 'Bachue', 'email' => 'bachue.shu@gmail.com', 'company' => 'EMC', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '2', 'name' => 'Elicier', 'email' => 'elicier@gmail.com', 'company' => 'Shanghai Normal University', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '3', 'name' => 'Xi Core', 'email' => 'unknown', 'company' => 'Government of People\'s Republic of China', 'city' => 'Beijing', 'country' => 'China'}, {'id' => '4', 'name' => 'Barack Obama', 'email' => 'unknown', 'company' => 'Government of United States of America', 'city' => 'Washington DC', 'country' => 'United States of America'}]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'Example 2' do
|
18
|
+
it 'demos how to use CucumberTable::Table' do
|
19
|
+
table :user_info, """
|
20
|
+
| id | name | email | company | city | country |
|
21
|
+
| 1 | Bachue | bachue.shu@gmail.com | EMC | Shanghai | China |
|
22
|
+
| 2 | Elicier | elicier@gmail.com | Shanghai Normal University | Shanghai | China |
|
23
|
+
| 3 | Xi Core | unknown | Government of People's Republic of China | Beijing | China |
|
24
|
+
| 4 | Barack Obama | unknown | Government of United States of America | Washington DC | United States of America |
|
25
|
+
"""
|
26
|
+
|
27
|
+
user_info.hashes.should == [{'id' => '1', 'name' => 'Bachue', 'email' => 'bachue.shu@gmail.com', 'company' => 'EMC', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '2', 'name' => 'Elicier', 'email' => 'elicier@gmail.com', 'company' => 'Shanghai Normal University', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '3', 'name' => 'Xi Core', 'email' => 'unknown', 'company' => 'Government of People\'s Republic of China', 'city' => 'Beijing', 'country' => 'China'}, {'id' => '4', 'name' => 'Barack Obama', 'email' => 'unknown', 'company' => 'Government of United States of America', 'city' => 'Washington DC', 'country' => 'United States of America'}]
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'table/table'
|
2
|
+
require 'table/exception'
|
3
|
+
require 'table/helpers'
|
4
|
+
require 'table/version'
|
5
|
+
require 'erb'
|
6
|
+
|
7
|
+
class String
|
8
|
+
def to_table binding = nil
|
9
|
+
table = split("\n").reject {|l| l.strip.empty? }.each {|l|
|
10
|
+
raise CucumberTable::TableFormatException.new('Invalid table format') unless /^\s*\|.+\|\s*$/ =~ l
|
11
|
+
}.map { |s| s.gsub(/\|(.+)\|/, '\1').split('|').map(&:strip) }
|
12
|
+
unless table.map(&:size).uniq.size == 1 # All cols has the same count of cells
|
13
|
+
raise CucumberTable::TableFormatException.new('Invalid table format')
|
14
|
+
end
|
15
|
+
table = table.map {|row| row.map {|cell| ERB.new(cell).result(binding) }} if binding
|
16
|
+
CucumberTable::Table.new table
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
if defined? RSpec
|
2
|
+
module CucumberTable
|
3
|
+
module Helpers
|
4
|
+
def table(name, string)
|
5
|
+
cls = self.is_a?(Class) ? self : self.class
|
6
|
+
cls.send :define_method, name do
|
7
|
+
instance_variable_name = "@__#{name}__"
|
8
|
+
if instance_variable_defined?(instance_variable_name)
|
9
|
+
instance_variable_get(instance_variable_name)
|
10
|
+
else
|
11
|
+
table = string.to_table
|
12
|
+
instance_variable_set instance_variable_name, table
|
13
|
+
table
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec.configure do |c|
|
21
|
+
c.extend CucumberTable::Helpers
|
22
|
+
c.include CucumberTable::Helpers
|
23
|
+
end
|
24
|
+
end
|
data/lib/table/table.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
module CucumberTable
|
2
|
+
class Table
|
3
|
+
def initialize table
|
4
|
+
@table = table
|
5
|
+
end
|
6
|
+
|
7
|
+
def rows
|
8
|
+
@table[1..-1]
|
9
|
+
end
|
10
|
+
|
11
|
+
def raw
|
12
|
+
@table
|
13
|
+
end
|
14
|
+
|
15
|
+
def headers
|
16
|
+
@table.first
|
17
|
+
end
|
18
|
+
|
19
|
+
def hashes
|
20
|
+
rows.map {|arr| arr.each_with_index.inject({}) {|h, (ele, i)| h[headers[i]] = ele; h }}
|
21
|
+
end
|
22
|
+
|
23
|
+
def [] column
|
24
|
+
index = headers.index column.to_s
|
25
|
+
rows.map {|row| row[index]} if index
|
26
|
+
end
|
27
|
+
|
28
|
+
def map_column! column, strict = true, &conversion_proc
|
29
|
+
index = headers.index column.to_s
|
30
|
+
if index.nil?
|
31
|
+
return unless strict
|
32
|
+
raise 'column not found'
|
33
|
+
end
|
34
|
+
rows.each {|row| row[index] = conversion_proc[row[index]] } if conversion_proc
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def map_headers! mappings = {}, &block
|
39
|
+
headers.each_with_index {|header, index|
|
40
|
+
map = mappings.keys.select {|map| map === header }.first
|
41
|
+
if map
|
42
|
+
headers[index] = mappings[map]
|
43
|
+
elsif block
|
44
|
+
headers[index] = block[header]
|
45
|
+
end
|
46
|
+
}
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def map_headers mappings = {}, &block
|
51
|
+
clone = self.class.new [headers.dup, *rows]
|
52
|
+
clone.map_headers! mappings, &block
|
53
|
+
end
|
54
|
+
|
55
|
+
def rows_hash
|
56
|
+
raise TableFormatException.new('The table must be exactly two columns wide') unless width == 2
|
57
|
+
@table.inject({}) {|h, row| h[row[0]] = row[1]; h }
|
58
|
+
end
|
59
|
+
|
60
|
+
def width
|
61
|
+
headers.size
|
62
|
+
end
|
63
|
+
|
64
|
+
def transpose
|
65
|
+
new_table = headers.zip(*rows)
|
66
|
+
self.class.new new_table
|
67
|
+
end
|
68
|
+
|
69
|
+
def each_row &block
|
70
|
+
rows.each(&block)
|
71
|
+
end
|
72
|
+
|
73
|
+
def each_col
|
74
|
+
enum = Enumerator.new do |enum|
|
75
|
+
headers.size.times do |i|
|
76
|
+
enum << raw.map {|row| row[i] }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
if block_given?
|
81
|
+
loop { yield enum.next }
|
82
|
+
self
|
83
|
+
else
|
84
|
+
enum
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/test/spec_helper.rb
ADDED
data/test/string_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
$: << File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe String do
|
5
|
+
context 'to_table' do
|
6
|
+
it 'can generate a CucumberTable::Table object' do
|
7
|
+
string = <<-STRING
|
8
|
+
| name | age |
|
9
|
+
| bachue | 23 |
|
10
|
+
| elicier | 20 |
|
11
|
+
STRING
|
12
|
+
|
13
|
+
table = string.to_table
|
14
|
+
table.hashes.should == [{'name' => 'bachue', 'age' => '23'}, {'name' => 'elicier', 'age' => '20'}]
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should reject invalid format table' do
|
18
|
+
string = <<-STRING
|
19
|
+
id | name | age |
|
20
|
+
1 | bachue | 23 |
|
21
|
+
2 | elicier | 20 |
|
22
|
+
STRING
|
23
|
+
|
24
|
+
expect { string.to_table }.to raise_error
|
25
|
+
|
26
|
+
string = <<-STRING
|
27
|
+
| id | name | age
|
28
|
+
| 1 | bachue | 23
|
29
|
+
| 2 | elicier | 20
|
30
|
+
STRING
|
31
|
+
|
32
|
+
expect { string.to_table }.to raise_error
|
33
|
+
|
34
|
+
string = <<-STRING
|
35
|
+
| id | name | age |
|
36
|
+
| 1 | bachue |
|
37
|
+
| 2 | elicier | 20 |
|
38
|
+
STRING
|
39
|
+
|
40
|
+
expect { string.to_table }.to raise_error
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/test/table_spec.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
$: << File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe CucumberTable::Table do
|
5
|
+
subject(:table) { CucumberTable::Table.new [['id', 'name', 'email', 'company', 'city', 'country'], ['1', 'Bachue', 'bachue.shu@gmail.com', 'EMC', 'Shanghai', 'China'], ['2', 'Elicier', 'elicier@gmail.com', 'Shanghai Normal University', 'Shanghai', 'China'], ['3', 'Xi Core', 'unknown', 'Government of People\'s Republic of China', 'Beijing', 'China'], ['4', 'Barack Obama', 'unknown', 'Government of United States of America', 'Washington DC', 'United States of America']] }
|
6
|
+
|
7
|
+
its(:raw) { should == [['id', 'name', 'email', 'company', 'city', 'country'], ['1', 'Bachue', 'bachue.shu@gmail.com', 'EMC', 'Shanghai', 'China'], ['2', 'Elicier', 'elicier@gmail.com', 'Shanghai Normal University', 'Shanghai', 'China'], ['3', 'Xi Core', 'unknown', 'Government of People\'s Republic of China', 'Beijing', 'China'], ['4', 'Barack Obama', 'unknown', 'Government of United States of America', 'Washington DC', 'United States of America']] }
|
8
|
+
|
9
|
+
its(:headers) { should == ['id', 'name', 'email', 'company', 'city', 'country'] }
|
10
|
+
|
11
|
+
its(:rows) { should == [['1', 'Bachue', 'bachue.shu@gmail.com', 'EMC', 'Shanghai', 'China'], ['2', 'Elicier', 'elicier@gmail.com', 'Shanghai Normal University', 'Shanghai', 'China'], ['3', 'Xi Core', 'unknown', 'Government of People\'s Republic of China', 'Beijing', 'China'], ['4', 'Barack Obama', 'unknown', 'Government of United States of America', 'Washington DC', 'United States of America']] }
|
12
|
+
|
13
|
+
its(:[], 'id') { should == ['1', '2', '3', '4'] }
|
14
|
+
its(:[], 'name') { should == ['Bachue', 'Elicier', 'Xi Core', 'Barack Obama'] }
|
15
|
+
its(:[], 'email') { should == ['bachue.shu@gmail.com', 'elicier@gmail.com', 'unknown', 'unknown'] }
|
16
|
+
its(:[], 'company') { should == ['EMC', 'Shanghai Normal University', 'Government of People\'s Republic of China', 'Government of United States of America'] }
|
17
|
+
its(:[], 'city') { should == ['Shanghai', 'Shanghai', 'Beijing', 'Washington DC'] }
|
18
|
+
its(:[], 'country') { should == ["China", "China", "China", "United States of America"] }
|
19
|
+
its(:hashes) { should == [{'id' => '1', 'name' => 'Bachue', 'email' => 'bachue.shu@gmail.com', 'company' => 'EMC', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '2', 'name' => 'Elicier', 'email' => 'elicier@gmail.com', 'company' => 'Shanghai Normal University', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '3', 'name' => 'Xi Core', 'email' => 'unknown', 'company' => 'Government of People\'s Republic of China', 'city' => 'Beijing', 'country' => 'China'}, {'id' => '4', 'name' => 'Barack Obama', 'email' => 'unknown', 'company' => 'Government of United States of America', 'city' => 'Washington DC', 'country' => 'United States of America'}] }
|
20
|
+
|
21
|
+
context 'Table#map_column!' do
|
22
|
+
it 'can call #map_column!' do
|
23
|
+
table.map_column!(:id, &:to_i)
|
24
|
+
table.hashes.should == [{'id' => 1, 'name' => 'Bachue', 'email' => 'bachue.shu@gmail.com', 'company' => 'EMC', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => 2, 'name' => 'Elicier', 'email' => 'elicier@gmail.com', 'company' => 'Shanghai Normal University', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => 3, 'name' => 'Xi Core', 'email' => 'unknown', 'company' => 'Government of People\'s Republic of China', 'city' => 'Beijing', 'country' => 'China'}, {'id' => 4, 'name' => 'Barack Obama', 'email' => 'unknown', 'company' => 'Government of United States of America', 'city' => 'Washington DC', 'country' => 'United States of America'}]
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'can return nil if column is not found' do
|
28
|
+
table.map_column!(:username, false).should be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'can raise exception if column is not found' do
|
32
|
+
expect { table.map_column!(:username) }.to raise_error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'Table#map_headers!' do
|
37
|
+
it 'can accept a map with regex or String' do
|
38
|
+
table.map_headers!({/^(user)?name$/ => :name, 'company' => :company})
|
39
|
+
table.hashes.should == [{'id' => '1', :name => 'Bachue', 'email' => 'bachue.shu@gmail.com', :company => 'EMC', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '2', :name => 'Elicier', 'email' => 'elicier@gmail.com', :company => 'Shanghai Normal University', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '3', :name => 'Xi Core', 'email' => 'unknown', :company => 'Government of People\'s Republic of China', 'city' => 'Beijing', 'country' => 'China'}, {'id' => '4', :name => 'Barack Obama', 'email' => 'unknown', :company => 'Government of United States of America', 'city' => 'Washington DC', 'country' => 'United States of America'}]
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'can accept a block' do
|
43
|
+
table.map_headers!(&:to_sym)
|
44
|
+
table.hashes.should == [{:id => '1', :name => 'Bachue', :email => 'bachue.shu@gmail.com', :company => 'EMC', :city => 'Shanghai', :country => 'China'}, {:id => '2', :name => 'Elicier', :email => 'elicier@gmail.com', :company => 'Shanghai Normal University', :city => 'Shanghai', :country => 'China'}, {:id => '3', :name => 'Xi Core', :email => 'unknown', :company => 'Government of People\'s Republic of China', :city => 'Beijing', :country => 'China'}, {:id => '4', :name => 'Barack Obama', :email => 'unknown', :company => 'Government of United States of America', :city => 'Washington DC', :country => 'United States of America'}]
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'can accept both map and block' do
|
48
|
+
table.map_headers!({'id' => 'ID'}, &:to_sym)
|
49
|
+
table.hashes.should == [{'ID' => '1', :name => 'Bachue', :email => 'bachue.shu@gmail.com', :company => 'EMC', :city => 'Shanghai', :country => 'China'}, {'ID' => '2', :name => 'Elicier', :email => 'elicier@gmail.com', :company => 'Shanghai Normal University', :city => 'Shanghai', :country => 'China'}, {'ID' => '3', :name => 'Xi Core', :email => 'unknown', :company => 'Government of People\'s Republic of China', :city => 'Beijing', :country => 'China'}, {'ID' => '4', :name => 'Barack Obama', :email => 'unknown', :company => 'Government of United States of America', :city => 'Washington DC', :country => 'United States of America'}]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'Table#map_headers' do
|
54
|
+
it 'behaves like Table#map_headers! but won\'t change the origin object' do
|
55
|
+
clone = table.map_headers({'id' => 'ID'}, &:to_sym)
|
56
|
+
clone.hashes.should == [{'ID' => '1', :name => 'Bachue', :email => 'bachue.shu@gmail.com', :company => 'EMC', :city => 'Shanghai', :country => 'China'}, {'ID' => '2', :name => 'Elicier', :email => 'elicier@gmail.com', :company => 'Shanghai Normal University', :city => 'Shanghai', :country => 'China'}, {'ID' => '3', :name => 'Xi Core', :email => 'unknown', :company => 'Government of People\'s Republic of China', :city => 'Beijing', :country => 'China'}, {'ID' => '4', :name => 'Barack Obama', :email => 'unknown', :company => 'Government of United States of America', :city => 'Washington DC', :country => 'United States of America'}]
|
57
|
+
table.hashes.should == [{'id' => '1', 'name' => 'Bachue', 'email' => 'bachue.shu@gmail.com', 'company' => 'EMC', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '2', 'name' => 'Elicier', 'email' => 'elicier@gmail.com', 'company' => 'Shanghai Normal University', 'city' => 'Shanghai', 'country' => 'China'}, {'id' => '3', 'name' => 'Xi Core', 'email' => 'unknown', 'company' => 'Government of People\'s Republic of China', 'city' => 'Beijing', 'country' => 'China'}, {'id' => '4', 'name' => 'Barack Obama', 'email' => 'unknown', 'company' => 'Government of United States of America', 'city' => 'Washington DC', 'country' => 'United States of America'}]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'Table#rows_hash' do
|
62
|
+
subject(:table) { CucumberTable::Table.new [['id', '1'], ['name', 'Bachue'], ['email', 'bachue.shu@gmail.com'], ['company', 'EMC'], ['city', 'Shanghai'], ['country', 'China']] }
|
63
|
+
let(:wrong_table) { CucumberTable::Table.new [['name', 'bachue', 'elicier'], ['age', '23', '20']] }
|
64
|
+
|
65
|
+
its(:rows_hash) { should == {'id' => '1', 'name' => 'Bachue', 'email' => 'bachue.shu@gmail.com', 'company' => 'EMC', 'city' => 'Shanghai', 'country' => 'China'} }
|
66
|
+
|
67
|
+
it 'should raise exception when The table is not two columns wide' do
|
68
|
+
expect { wrong_table.rows_hash }.to raise_error
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'Table#transpose' do
|
73
|
+
let(:table) { CucumberTable::Table.new [['a', 7, 4], ['b', 9, 2]] }
|
74
|
+
it 'returns a new, transposed table' do
|
75
|
+
table.transpose.raw.should == [['a', 'b'], [7, 9], [4, 2]]
|
76
|
+
table.raw.should == [['a', 7, 4], ['b', 9, 2]]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'Table#each' do
|
81
|
+
let(:table) { CucumberTable::Table.new [['a', 1, 2], ['b', 3, 4], ['c', 5, 6]] }
|
82
|
+
it 'can enumerate for each row' do
|
83
|
+
iter = table.each_row
|
84
|
+
iter.next.should == ['b', 3, 4]
|
85
|
+
iter.next.should == ['c', 5, 6]
|
86
|
+
expect { iter.next }.to raise_error
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'can call block for each row' do
|
90
|
+
table.each_row.with_index do |row, i|
|
91
|
+
case i
|
92
|
+
when 0
|
93
|
+
row.should == ['b', 3, 4]
|
94
|
+
when 1
|
95
|
+
row.should == ['c', 5, 6]
|
96
|
+
else
|
97
|
+
fail
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'can enumerate for each col' do
|
103
|
+
iter = table.each_col
|
104
|
+
iter.next.should == ['a', 'b', 'c']
|
105
|
+
iter.next.should == [1, 3, 5]
|
106
|
+
iter.next.should == [2, 4, 6]
|
107
|
+
expect { iter.next }.to raise_error
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'can call block for each column' do
|
111
|
+
table.each_col.with_index do |row, i|
|
112
|
+
case i
|
113
|
+
when 0
|
114
|
+
row.should == ['a', 'b', 'c']
|
115
|
+
when 1
|
116
|
+
row.should == [1, 3, 5]
|
117
|
+
when 2
|
118
|
+
row.should == [2, 4, 6]
|
119
|
+
else
|
120
|
+
fail
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cucumber-table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bachue Zhou
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: its
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: pry
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: pry-debugger
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - '='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 0.2.1
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - '='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 0.2.1
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: pry-stack_explorer
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: pry-doc
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
description: A gem which can parse Cucumber-Style String to a table. You can write
|
143
|
+
a table in your test case just like you are using Cucumber. Now it mainly support
|
144
|
+
RSpec.
|
145
|
+
email:
|
146
|
+
- bachue.shu@gmail.com
|
147
|
+
executables: []
|
148
|
+
extensions: []
|
149
|
+
extra_rdoc_files: []
|
150
|
+
files:
|
151
|
+
- .gitignore
|
152
|
+
- Gemfile
|
153
|
+
- LICENSE.txt
|
154
|
+
- README.md
|
155
|
+
- Rakefile
|
156
|
+
- cucumber-table.gemspec
|
157
|
+
- example.rb
|
158
|
+
- lib/cucumber-table.rb
|
159
|
+
- lib/table/exception.rb
|
160
|
+
- lib/table/helpers.rb
|
161
|
+
- lib/table/table.rb
|
162
|
+
- lib/table/version.rb
|
163
|
+
- test/spec_helper.rb
|
164
|
+
- test/string_spec.rb
|
165
|
+
- test/table_spec.rb
|
166
|
+
homepage: ''
|
167
|
+
licenses:
|
168
|
+
- MIT
|
169
|
+
post_install_message:
|
170
|
+
rdoc_options: []
|
171
|
+
require_paths:
|
172
|
+
- lib
|
173
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
174
|
+
none: false
|
175
|
+
requirements:
|
176
|
+
- - ! '>='
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
179
|
+
segments:
|
180
|
+
- 0
|
181
|
+
hash: 694246353902340154
|
182
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
|
+
none: false
|
184
|
+
requirements:
|
185
|
+
- - ! '>='
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0'
|
188
|
+
segments:
|
189
|
+
- 0
|
190
|
+
hash: 694246353902340154
|
191
|
+
requirements: []
|
192
|
+
rubyforge_project:
|
193
|
+
rubygems_version: 1.8.25
|
194
|
+
signing_key:
|
195
|
+
specification_version: 3
|
196
|
+
summary: ''
|
197
|
+
test_files:
|
198
|
+
- test/spec_helper.rb
|
199
|
+
- test/string_spec.rb
|
200
|
+
- test/table_spec.rb
|
201
|
+
has_rdoc:
|