turnip 2.0.2 → 2.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 97ce8b4d21720145507381ad113a78d36bb552a0
4
- data.tar.gz: 8dae7e25fb5fe1dd0d4fff49ed42de48da147c3c
3
+ metadata.gz: 7d33fc5658dab981447e096b6da7db0efeed9fae
4
+ data.tar.gz: a83b232810d235ec830fdcade9327e339c7dc104
5
5
  SHA512:
6
- metadata.gz: 8b02c8f565a8b495e5e244f12ed6994df4897e6e6b343edb53dd46590a324adfbc5a099c53109caf7597524259f66d303bd5fd3af437f404ad39cc843b39bfef
7
- data.tar.gz: bd50fdde50f4f1de7ae33843ac93f9e9fcc275bebba93f6bc6b60d8bb10f74f2b818ff2b268ceec9994339598d3564633468e192c705d5125a530a6d57c2a288
6
+ metadata.gz: c4081ba73a394b32bd771c894203c9dad8211d29ddc20988f000a55ff20cff2c2a556ea2a280106988216b3aaf903b8c9d781194ee8b72172481326db630709b
7
+ data.tar.gz: c3ccbe7bb743449eee4b75fa79502bcfe28dab9b06600b479646760cd669b6582d30d4a0022251d5a276842375045fb74f00ad231d4652abc64b9809572805af
@@ -6,7 +6,6 @@ cache: bundler
6
6
  branches:
7
7
  only:
8
8
  - master
9
- - 2_0_0_rc1
10
9
 
11
10
  matrix:
12
11
  allow_failures:
@@ -15,9 +14,9 @@ matrix:
15
14
  - rvm: ruby-head
16
15
 
17
16
  rvm:
18
- - 2.0.0
19
17
  - 2.1
20
18
  - 2.2
19
+ - 2.3.0
21
20
  - ruby-head
22
21
  - jruby-19mode
23
22
 
data/README.md CHANGED
@@ -48,8 +48,8 @@ Please create a topic branch for every separate change you make.
48
48
 
49
49
  ### 1. Ruby
50
50
 
51
- - Support Ruby 2.x
52
- - Does not support Ruby 1.9.X
51
+ - Support Ruby 2.1 or higher
52
+ - Does not support Ruby 1.9.X and 2.0.X
53
53
  - Does not work on Ruby 1.8.X
54
54
 
55
55
  ### 2. RSpec
@@ -392,6 +392,16 @@ Then I should see 'Danger! Monsters ahead!'
392
392
  And I should not see 'Game over!'
393
393
  ```
394
394
 
395
+ You can also define custom placeholder without specific regexp, that matches the same value of the default placeholder like this:
396
+
397
+ ```ruby
398
+ placeholder :monster_name do
399
+ default do |name|
400
+ Monster.find_by!(name: name)
401
+ end
402
+ end
403
+ ```
404
+
395
405
  ## Table Steps
396
406
 
397
407
  Turnip also supports steps that take a table as a parameter similar to Cucumber:
@@ -65,14 +65,14 @@ step "the monster should be dead" do
65
65
  end
66
66
 
67
67
  step "there are the following monsters:" do |table|
68
- @monsters = {}
68
+ $monsters = {}
69
69
  table.hashes.each do |hash|
70
- @monsters[hash['Name']] = hash['Hitpoints'].to_i
70
+ $monsters[hash['Name']] = hash['Hitpoints'].to_i
71
71
  end
72
72
  end
73
73
 
74
- step ":name should have :count hitpoints" do |name, count|
75
- @monsters[name].should eq(count.to_i)
74
+ step ":monster should have :count hitpoints" do |monster, count|
75
+ monster.should eq(count.to_i)
76
76
  end
77
77
 
78
78
  step "the monster sings the following song" do |song|
@@ -118,3 +118,9 @@ placeholder :color do
118
118
  color.to_sym
119
119
  end
120
120
  end
121
+
122
+ placeholder :monster do
123
+ default do |name|
124
+ $monsters[name]
125
+ end
126
+ end
@@ -27,8 +27,8 @@ module Turnip
27
27
 
28
28
  def default
29
29
  @default ||= new(:default) do
30
- match %r((?:"([^"]*)"|'([^']*)'|([[:alnum:]_-]+))) do |first, second, third|
31
- first or second or third
30
+ default do |value|
31
+ value
32
32
  end
33
33
  end
34
34
  end
@@ -37,6 +37,7 @@ module Turnip
37
37
  def initialize(name, &block)
38
38
  @name = name
39
39
  @matches = []
40
+ @default = nil
40
41
  instance_eval(&block)
41
42
  end
42
43
 
@@ -49,18 +50,31 @@ module Turnip
49
50
  @matches << Match.new(regexp, block)
50
51
  end
51
52
 
53
+ def default(&block)
54
+ @default ||= Match.new(
55
+ %r{['"]?((?:(?<=")[^"]*)(?=")|(?:(?<=')[^']*(?='))|(?<!['"])[[:alnum:]_-]+(?!['"]))['"]?},
56
+ block
57
+ )
58
+ end
59
+
52
60
  def regexp
53
- Regexp.new(@matches.map(&:regexp).join('|'))
61
+ Regexp.new(placeholder_matches.map(&:regexp).join('|'))
54
62
  end
55
63
 
56
64
  private
57
65
 
58
66
  def find_match(value)
59
- @matches.each do |m|
67
+ placeholder_matches.each do |m|
60
68
  result = value.scan(m.regexp)
61
69
  return m, result.flatten unless result.empty?
62
70
  end
63
71
  nil
64
72
  end
73
+
74
+ def placeholder_matches
75
+ matches = @matches
76
+ matches += [@default] if @default
77
+ matches
78
+ end
65
79
  end
66
80
  end
@@ -1,3 +1,3 @@
1
1
  module Turnip
2
- VERSION = '2.0.2'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -1,67 +1,138 @@
1
1
  require 'turnip/placeholder'
2
2
 
3
3
  describe Turnip::Placeholder do
4
- def anchor(exp)
5
- Regexp.new("^#{exp}$")
6
- end
4
+ describe '.resolve' do
5
+ before do
6
+ described_class.add(:test) do
7
+ match(/foo/)
8
+ match(/\d/)
9
+ end
10
+ end
7
11
 
8
- describe ".resolve" do
9
- it "returns a regexp for the given placeholder" do
10
- placeholder = Turnip::Placeholder.add(:test) { match(/foo/); match(/\d/) }
11
- resolved = Turnip::Placeholder.resolve(:test)
12
- "foo".should =~ anchor(resolved)
13
- "5".should =~ anchor(resolved)
14
- "bar".should_not =~ anchor(resolved)
12
+ it 'returns a regexp for the given placeholder' do
13
+ resolved = described_class.resolve(:test)
14
+
15
+ expect('foo').to match(resolved)
16
+ expect('bar').not_to match(resolved)
17
+ expect('5').to match(resolved)
15
18
  end
16
19
 
17
- it "fall through to using the standard placeholder regexp" do
18
- resolved = Turnip::Placeholder.resolve(:does_not_exist)
19
- "foo".should =~ anchor(resolved)
20
- '"this is a test"'.should =~ anchor(resolved)
21
- "foo bar".should_not =~ anchor(resolved)
20
+ it 'fall through to using the standard placeholder regexp' do
21
+ resolved = described_class.resolve(:does_not_exist)
22
+
23
+ match_standard_regexp_strings = [
24
+ [ %q(non_space_string), 'non_space_string' ],
25
+ [ %q('around single quote'), 'around single quote' ],
26
+ [ %q("around double quote"), 'around double quote' ],
27
+ ]
28
+
29
+ match_standard_regexp_strings.each do |step, expect_str|
30
+ actual_str = resolved.match(step).captures.find { |m| !m.nil? }
31
+ expect(expect_str).to eq(actual_str)
32
+ end
33
+
34
+ mismatch_standard_regexp_strings = [
35
+ [ %q(with space string), 'with space string' ],
36
+ [ %q('single to double"), 'single to double' ],
37
+ [ %q("double to single'), 'double to single' ],
38
+ [ %q("double to none), 'double to none' ],
39
+ [ %q(none to single'), 'none to single' ],
40
+ ]
41
+
42
+ mismatch_standard_regexp_strings.each do |step, expect_str|
43
+ actual_str = resolved.match(step).captures.find { |m| !m.nil? }
44
+ expect(expect_str).not_to eq(actual_str)
45
+ end
22
46
  end
23
47
  end
24
48
 
25
- describe ".apply" do
26
- it "returns a regexp for the given placeholder" do
27
- placeholder = Turnip::Placeholder.add(:test) do
49
+ describe '.apply' do
50
+ it 'recognize multiple placeholders and return block value' do
51
+ described_class.add :test1 do
28
52
  match(/foo/) { :foo_bar }
29
53
  match(/\d/) { |num| num.to_i }
30
54
  end
31
- Turnip::Placeholder.apply(:test, "foo").should eq(:foo_bar)
32
- Turnip::Placeholder.apply(:test, "5").should eq(5)
33
- Turnip::Placeholder.apply(:test, "bar").should eq("bar")
34
- end
35
55
 
36
- it "extracts any captured expressions and passes them to the block" do
37
- placeholder = Turnip::Placeholder.add(:test) do
38
- match(/mo(nk)(ey)/) { |nk, ey| nk.to_s.reverse + '|' + ey.to_s.upcase }
56
+ described_class.add :test2 do
57
+ match(/bar/) { :bar_foo }
58
+ match(/\d/) { |num| num.to_i * 2 }
39
59
  end
40
- Turnip::Placeholder.apply(:test, "monkey").should eq('kn|EY')
41
- Turnip::Placeholder.apply(:test, "bar").should eq("bar")
60
+
61
+ expect(described_class.apply(:test1, 'foo')).to eq(:foo_bar)
62
+ expect(described_class.apply(:test1, 'bar')).to eq('bar')
63
+ expect(described_class.apply(:test1, '5')).to eq(5)
64
+
65
+ expect(described_class.apply(:test2, 'foo')).to eq('foo')
66
+ expect(described_class.apply(:test2, 'bar')).to eq(:bar_foo)
67
+ expect(described_class.apply(:test2, '5')).to eq(10)
42
68
  end
43
69
  end
44
70
 
45
- describe "#regexp" do
46
- it "should match a given fragment" do
47
- placeholder = Turnip::Placeholder.new(:test) { match(/foo/) }
48
- "foo".should =~ placeholder.regexp
71
+ describe '#apply' do
72
+ it 'extracts a captured expression and passes to the block' do
73
+ placeholder = described_class.new(:test) do
74
+ default { |value| value.gsub(' ', '').to_sym }
75
+ match(/foo/) { :foo_bar }
76
+ match(/\d/) { |num| num.to_i }
77
+
78
+ # It will be ignored (does not override first `default`)
79
+ default { |value| value.gsub(' ', '-').to_sym }
80
+ end
81
+
82
+ expect(placeholder.apply('foo')).to eq :foo_bar
83
+ expect(placeholder.apply('bar')).to eq :bar
84
+ expect(placeholder.apply('"fizz buzz"')).to eq :fizzbuzz
85
+ expect(placeholder.apply("'fizz buzz'")).to eq :fizzbuzz
86
+ expect(placeholder.apply('5')).to eq 5
49
87
  end
50
88
 
51
- it "should match multiple fragments" do
52
- placeholder = Turnip::Placeholder.new(:test) { match(/foo/); match(/\d/) }
53
- "foo".should =~ placeholder.regexp
54
- "5".should =~ placeholder.regexp
89
+ it 'extracts any captured expressions and passes to the block' do
90
+ placeholder = described_class.new(:test) do
91
+ match(/mo(nk)(ey)/) { |nk, ey| nk.to_s.reverse + '|' + ey.to_s.upcase }
92
+ end
93
+
94
+ expect(placeholder.apply('monkey')).to eq('kn|EY')
95
+ expect(placeholder.apply('bar')).to eq('bar')
55
96
  end
97
+ end
98
+
99
+ describe '#regexp' do
100
+ context 'placeholder has a matcher' do
101
+ let :placeholder do
102
+ described_class.new(:test) do
103
+ match(/foo/)
104
+ end
105
+ end
56
106
 
57
- it "should not match an incorrect fragment" do
58
- placeholder = Turnip::Placeholder.new(:test) { match(/foo/) }
59
- "bar".should_not =~ placeholder.regexp
107
+ it 'should match a given fragment' do
108
+ expect('foo').to match(placeholder.regexp)
109
+ expect('the fool').to match(placeholder.regexp)
110
+ end
111
+
112
+ it 'should not match an incorrect fragment' do
113
+ expect('bar').not_to match(placeholder.regexp)
114
+ end
60
115
  end
61
116
 
62
- it "should not multiple incorrect fragments" do
63
- placeholder = Turnip::Placeholder.new(:test) { match(/foo/); match(/\d/) }
64
- "bar".should_not =~ placeholder.regexp
117
+ context 'placeholder has multiple matchers' do
118
+ let :placeholder do
119
+ described_class.new(:test) do
120
+ match(/foo/)
121
+ match(/\d/)
122
+ end
123
+ end
124
+
125
+ it 'should match multiple fragments' do
126
+ expect('foo').to match(placeholder.regexp)
127
+ expect('5').to match(placeholder.regexp)
128
+
129
+ expect('the fool').to match(placeholder.regexp)
130
+ expect('12345678').to match(placeholder.regexp)
131
+ end
132
+
133
+ it 'should not multiple incorrect fragments' do
134
+ expect('bar').not_to match(placeholder.regexp)
135
+ end
65
136
  end
66
137
  end
67
138
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turnip
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-13 00:00:00.000000000 Z
11
+ date: 2016-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec