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 +4 -4
- data/.travis.yml +1 -2
- data/README.md +12 -2
- data/examples/steps/steps.rb +10 -4
- data/lib/turnip/placeholder.rb +18 -4
- data/lib/turnip/version.rb +1 -1
- data/spec/placeholder_spec.rb +112 -41
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d33fc5658dab981447e096b6da7db0efeed9fae
|
4
|
+
data.tar.gz: a83b232810d235ec830fdcade9327e339c7dc104
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4081ba73a394b32bd771c894203c9dad8211d29ddc20988f000a55ff20cff2c2a556ea2a280106988216b3aaf903b8c9d781194ee8b72172481326db630709b
|
7
|
+
data.tar.gz: c3ccbe7bb743449eee4b75fa79502bcfe28dab9b06600b479646760cd669b6582d30d4a0022251d5a276842375045fb74f00ad231d4652abc64b9809572805af
|
data/.travis.yml
CHANGED
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.
|
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:
|
data/examples/steps/steps.rb
CHANGED
@@ -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
|
-
|
68
|
+
$monsters = {}
|
69
69
|
table.hashes.each do |hash|
|
70
|
-
|
70
|
+
$monsters[hash['Name']] = hash['Hitpoints'].to_i
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
step ":
|
75
|
-
|
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
|
data/lib/turnip/placeholder.rb
CHANGED
@@ -27,8 +27,8 @@ module Turnip
|
|
27
27
|
|
28
28
|
def default
|
29
29
|
@default ||= new(:default) do
|
30
|
-
|
31
|
-
|
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(
|
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
|
-
|
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
|
data/lib/turnip/version.rb
CHANGED
data/spec/placeholder_spec.rb
CHANGED
@@ -1,67 +1,138 @@
|
|
1
1
|
require 'turnip/placeholder'
|
2
2
|
|
3
3
|
describe Turnip::Placeholder do
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
18
|
-
resolved =
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
26
|
-
it
|
27
|
-
|
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
|
-
|
37
|
-
|
38
|
-
match(/
|
56
|
+
described_class.add :test2 do
|
57
|
+
match(/bar/) { :bar_foo }
|
58
|
+
match(/\d/) { |num| num.to_i * 2 }
|
39
59
|
end
|
40
|
-
|
41
|
-
|
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
|
46
|
-
it
|
47
|
-
placeholder =
|
48
|
-
|
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
|
52
|
-
placeholder =
|
53
|
-
|
54
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
63
|
-
|
64
|
-
|
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
|
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-
|
11
|
+
date: 2016-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|