chop 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fae72c2ccddb7b9d3cf1e2af62273f6e3fa108b
4
- data.tar.gz: 0b333a8b0dfedf4c1da7ae3d56e01dae37684a70
3
+ metadata.gz: 8dae29be1ed5e8747e2a7ccee7504b65968d7b0a
4
+ data.tar.gz: f0647eff2fd3aae0039af3079ccc977224d7d53f
5
5
  SHA512:
6
- metadata.gz: 95a559fee2d75b1c7ec85114d55464a11285a582cbefa9b6fce85c5f7c958e58b66bbaaa5fe9d21c72e7d769353aea3f1f6b467ebcfda4816888383c9ec8b791
7
- data.tar.gz: 1b993d05855c234058edb68cb7164acaa70ff18c40b1693e75371567e4e3c4050ca9410f8d18c71970d1c24e1434deaf380041772ae346acaf79f3be76eeb51b
6
+ metadata.gz: 89883bd7d309b914d545c591ba51119061d66cfb01b9227e39b0fa5ddd333108647f3aad4c396e5b7f173bc9b51ac521bc0dbf3533c9e2e612ce3dd1bbe5de5c
7
+ data.tar.gz: 89691d599ef482ae8563bf730200b16168fa8f14a3d4ce15c84c45bc22dc4933d210bde4fa7fc94f9bac0ba837827aa764b5076acf226535bc04593104df6a4a
data/lib/chop.rb CHANGED
@@ -3,6 +3,7 @@ require "chop/builder"
3
3
  require "chop/table"
4
4
  require "chop/definition_list"
5
5
  require "chop/unordered_list"
6
+ require "chop/form"
6
7
  require "chop/dsl"
7
8
 
8
9
  module Chop
data/lib/chop/base.rb ADDED
@@ -0,0 +1,95 @@
1
+ require "active_support/core_ext/object/blank"
2
+
3
+ module Chop
4
+ class Base < Struct.new(:selector, :table, :session, :block)
5
+ def self.diff! selector, table, session: Capybara.current_session, &block
6
+ new(selector, table, session, block).diff!
7
+ end
8
+
9
+ attr_accessor :rows_finder
10
+ attr_accessor :cells_finder
11
+ attr_accessor :transformations
12
+
13
+ def initialize selector = nil, table = nil, session = Capybara.current_session, block = nil, &other_block
14
+ super
15
+ self.selector ||= default_selector
16
+ self.rows_finder = default_rows_finder
17
+ self.cells_finder = default_cells_finder
18
+ self.transformations = []
19
+ instance_eval &block if block.respond_to?(:call)
20
+ instance_eval &other_block if block_given?
21
+ end
22
+
23
+ def transformation &block
24
+ transformations << block
25
+ end
26
+
27
+ def normalize
28
+ transformation do |raw|
29
+ max = raw.map(&:count).max
30
+ raw.map! do |row|
31
+ row << "" while row.length < max
32
+ row
33
+ end
34
+ end
35
+ end
36
+
37
+ def rows &block
38
+ self.rows_finder = block
39
+ end
40
+
41
+ def cells &block
42
+ self.cells_finder = block
43
+ end
44
+
45
+ def allow_not_found
46
+ @allow_not_found = true
47
+ end
48
+
49
+ def to_a
50
+ results = rows_finder.call(root).map do |row|
51
+ row_to_text(row)
52
+ end
53
+ normalize
54
+ transformations.each { |transformation| transformation.call(results) }
55
+ results
56
+ end
57
+
58
+ def diff! cucumber_table = table
59
+ cucumber_table.diff! to_a
60
+ end
61
+
62
+ def hashes
63
+ rows = to_a.dup
64
+ header = rows.shift
65
+ rows.map do |row|
66
+ Hash[header.zip(row)]
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def root
73
+ @root ||= begin
74
+ session.find(selector)
75
+ rescue Capybara::ElementNotFound
76
+ raise unless @allow_not_found
77
+ Capybara::Node::Simple.new("")
78
+ end
79
+ end
80
+
81
+ def row_to_text row
82
+ cells_finder.call(row).map do |cell|
83
+ cell_to_text(cell)
84
+ end
85
+ end
86
+
87
+ def cell_to_text cell
88
+ text = cell.text
89
+ if text.blank? and image = cell.all("img").first
90
+ text = image["alt"]
91
+ end
92
+ text
93
+ end
94
+ end
95
+ end
@@ -1,49 +1,7 @@
1
- require "active_support/core_ext/object/blank"
1
+ require "chop/base"
2
2
 
3
3
  module Chop
4
- class DefinitionList < Struct.new(:selector, :table, :session, :block)
5
- def self.diff! selector, table, session: Capybara.current_session, &block
6
- new(selector, table, session, block).diff!
7
- end
8
-
9
- attr_accessor :transformations
10
-
11
- def initialize selector = "dl", table = nil, session = Capybara.current_session, block = nil, &other_block
12
- super
13
- self.transformations = []
14
- instance_eval &block if block.respond_to?(:call)
15
- instance_eval &other_block if block_given?
16
- end
17
-
18
- def base_to_a
19
- rows.collect do |row|
20
- row_to_text(row)
21
- end
22
- end
23
-
24
- def normalized_to_a
25
- raw = base_to_a
26
- max = raw.map(&:count).max
27
- raw.map do |row|
28
- row << "" while row.length < max
29
- row
30
- end
31
- end
32
-
33
- def to_a
34
- results = normalized_to_a
35
- transformations.each { |transformation| transformation.call(results) }
36
- results
37
- end
38
-
39
- def transformation &block
40
- transformations << block
41
- end
42
-
43
- def diff! cucumber_table = table
44
- cucumber_table.diff! to_a
45
- end
46
-
4
+ class DefinitionList < Base
47
5
  def column index, &block
48
6
  transformation do |raw|
49
7
  raw.map!.with_index do |row, row_index|
@@ -66,30 +24,20 @@ module Chop
66
24
 
67
25
  private
68
26
 
69
- def rows
70
- node.all("dfn")
71
- end
72
-
73
- def node
74
- @node ||= session.find(selector)
27
+ def default_selector
28
+ "dl"
75
29
  end
76
30
 
77
- def row_to_text row
78
- cells(row).map do |cell|
79
- cell_to_text(cell)
31
+ def default_rows_finder
32
+ Proc.new do |root|
33
+ root.all("dfn")
80
34
  end
81
35
  end
82
36
 
83
- def cells row
84
- row.all("dt,dd")
85
- end
86
-
87
- def cell_to_text cell
88
- text = cell.text
89
- if text.blank? and image = cell.all("img").first
90
- text = image["alt"]
37
+ def default_cells_finder
38
+ Proc.new do |row|
39
+ row.all("dt,dd")
91
40
  end
92
- text
93
41
  end
94
42
  end
95
43
 
data/lib/chop/dsl.rb CHANGED
@@ -9,6 +9,10 @@ module Chop
9
9
  klass = const_get("Chop::#{class_name}")
10
10
  klass.diff! selector, table, session: session, &block
11
11
  end
12
+
13
+ def fill_in! table
14
+ Form.fill_in! table
15
+ end
12
16
  end
13
17
  end
14
18
 
@@ -25,6 +29,10 @@ if defined?(Cucumber::MultilineArgument::DataTable)
25
29
  super
26
30
  end
27
31
  end
32
+
33
+ def fill_in!
34
+ Chop.fill_in! self
35
+ end
28
36
  }
29
37
  end
30
38
 
data/lib/chop/form.rb ADDED
@@ -0,0 +1,18 @@
1
+ module Chop
2
+ class Form < Struct.new(:table, :session)
3
+ def self.fill_in! table, session: Capybara.current_session
4
+ new(table, session).fill_in!
5
+ end
6
+
7
+ def fill_in!
8
+ table.rows_hash.each do |label, value|
9
+ field = session.find_field(label)
10
+ if field[:type] == "file"
11
+ session.attach_file label, "features/support/fixtures/#{value}"
12
+ else
13
+ session.fill_in label, with: value
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
data/lib/chop/table.rb CHANGED
@@ -1,103 +1,23 @@
1
- require "active_support/core_ext/object/blank"
1
+ require "chop/base"
2
2
 
3
3
  module Chop
4
- class Table < Struct.new(:selector, :table, :session, :block)
5
- def self.diff! selector, table, session: Capybara.current_session, &block
6
- new(selector, table, session, block).diff!
7
- end
8
-
9
- attr_accessor :transformations
10
-
11
- def initialize(selector = "table", table = nil, session = Capybara.current_session, block = nil, &other_block)
12
- super
13
- self.transformations = []
14
- instance_eval &block if block.respond_to?(:call)
15
- instance_eval &other_block if block_given?
16
- end
17
-
18
- def header_elements
19
- rows("thead")
20
- end
21
-
22
- def header
23
- header_elements.map do |row|
24
- row.all(:xpath, "./*").map(&:text)
25
- end
26
- end
27
-
28
- def body_elements
29
- rows("tbody")
30
- end
31
-
32
- def body
33
- body_elements.map do |row|
34
- row_to_text(row)
35
- end
36
- end
37
-
38
- def base_to_a
39
- header + body
40
- end
41
-
42
- def normalized_to_a
43
- raw = base_to_a
44
- max = raw.map(&:count).max
45
- raw.select { |row| row.count == max }
46
- end
47
-
48
- def to_a
49
- results = normalized_to_a
50
- transformations.each { |transformation| transformation.call(results) }
51
- results
52
- end
53
-
54
- def transformation &block
55
- transformations << block
56
- end
57
-
58
- def diff! cucumber_table = table
59
- cucumber_table.diff! to_a
60
- end
61
-
62
- def hashes
63
- rows = to_a.dup
64
- header = rows.shift
65
- rows.map do |row|
66
- Hash[header.zip(row)]
67
- end
68
- end
69
-
70
- def allow_not_found
71
- @allow_not_found = true
72
- end
73
-
4
+ class Table < Base
74
5
  private
75
6
 
76
- def rows parent = nil
77
- node.all("#{parent} tr")
78
- end
79
-
80
- def node
81
- @node ||= begin
82
- session.find(selector)
83
- rescue Capybara::ElementNotFound
84
- raise unless @allow_not_found
85
- Capybara::Node::Simple.new("")
86
- end
7
+ def default_selector
8
+ "table"
87
9
  end
88
10
 
89
- def row_to_text row
90
- row.all(:xpath, "./*").map.with_index do |cell, index|
91
- cell_to_text cell, index
11
+ def default_rows_finder
12
+ Proc.new do |root|
13
+ root.all("tr")
92
14
  end
93
15
  end
94
16
 
95
- def cell_to_text cell, index
96
- text = cell.text
97
- if text.blank? and image = cell.all("img").first
98
- text = image["alt"]
17
+ def default_cells_finder
18
+ Proc.new do |row|
19
+ row.all("td,th")
99
20
  end
100
- text
101
21
  end
102
22
  end
103
23
  end
@@ -1,63 +1,22 @@
1
- require "active_support/core_ext/object/blank"
1
+ require "chop/base"
2
2
 
3
3
  module Chop
4
- class UnorderedList < Struct.new(:selector, :table, :session, :block)
5
- def self.diff! selector, table, session: Capybara.current_session, &block
6
- new(selector, table, session, block).diff!
7
- end
8
-
9
- attr_accessor :transformations
10
-
11
- def initialize selector = "ul", table = nil, session = Capybara.current_session, block = nil, &other_block
12
- super
13
- self.transformations = []
14
- instance_eval &block if block.respond_to?(:call)
15
- instance_eval &other_block if block_given?
16
- end
17
-
18
- def base_to_a
19
- rows.collect do |row|
20
- row_to_text(row)
21
- end
22
- end
23
-
24
- def normalized_to_a
25
- raw = base_to_a
26
- max = raw.map(&:count).max
27
- raw.select { |row| row.count == max }
28
- end
29
-
30
- def to_a
31
- results = normalized_to_a
32
- transformations.each { |transformation| transformation.call(results) }
33
- results
34
- end
35
-
36
- def transformation &block
37
- transformations << block
38
- end
39
-
40
- def diff! cucumber_table = table
41
- cucumber_table.diff! to_a
42
- end
43
-
4
+ class UnorderedList < Base
44
5
  private
45
6
 
46
- def rows
47
- node.all("li")
7
+ def default_selector
8
+ "ul"
48
9
  end
49
10
 
50
- def node
51
- @node ||= session.find(selector)
11
+ def default_rows_finder
12
+ Proc.new do |root|
13
+ root.all("li")
14
+ end
52
15
  end
53
16
 
54
- def row_to_text row
55
- [row].collect do |cell|
56
- text = cell.text
57
- if text.blank? and image = cell.all("img").first
58
- text = image["alt"]
59
- end
60
- text
17
+ def default_cells_finder
18
+ Proc.new do |row|
19
+ [row]
61
20
  end
62
21
  end
63
22
  end
data/lib/chop/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Chop
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-26 00:00:00.000000000 Z
11
+ date: 2017-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -126,9 +126,11 @@ files:
126
126
  - bin/setup
127
127
  - chop.gemspec
128
128
  - lib/chop.rb
129
+ - lib/chop/base.rb
129
130
  - lib/chop/builder.rb
130
131
  - lib/chop/definition_list.rb
131
132
  - lib/chop/dsl.rb
133
+ - lib/chop/form.rb
132
134
  - lib/chop/table.rb
133
135
  - lib/chop/unordered_list.rb
134
136
  - lib/chop/version.rb