Skip to content

Commit dbb8cca

Browse files
authored
Merge pull request #8 from MarioRuiz/v2.3.0
V2.3.0
2 parents 3b09ad4 + 9f75ac2 commit dbb8cca

File tree

8 files changed

+129
-23
lines changed

8 files changed

+129
-23
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
language: ruby
22
rvm:
3-
- 2.4
4-
- 2.5
5-
- 2.6
3+
- 2.7
4+
- 3.0
65
- ruby-head
76
branches:
87
except:

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,17 @@ StringPattern.optimistic = true
428428
#>SAAERfixedtext988
429429
```
430430

431+
#### block_list
432+
433+
To specify which words will be avoided from the results
434+
435+
```ruby
436+
StringPattern.block_list = ['example', 'wrong', 'ugly']
437+
StringPattern.block_list_enabled = true
438+
"2-20:Tn".gen #>AAñ34Ef99éNOP
439+
```
440+
441+
431442
## Contributing
432443

433444
Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/string_pattern.

lib/string/pattern/add_to_ruby.rb

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ def to_sp
9090
pats = ""
9191
patg = [] # for (aa|bb|cc) group
9292
set = false
93+
set_negate = false
94+
options = []
9395
capture = false
9496

9597
range = ""
9698
fixed_text = false
97-
last_char = (regexp.to_s.gsub("?-mix:", "").length) - 2
99+
options = regexp.to_s.scan(/\A\(\?([mix]*)\-[mix]*:/).join.split('')
100+
last_char = (regexp.to_s.gsub(/\A\(\?[mix]*\-[mix]*:/, "").length) - 2
98101
Regexp::Scanner.scan regexp do |type, token, text, ts, te|
99102
if type == :escape
100103
if token == :dot
@@ -126,9 +129,9 @@ def to_sp
126129
pata[-1] += pats.chop
127130
else
128131
if pats.size == 2
129-
pata << pats.chop #jal
132+
pata << pats.chop
130133
else
131-
pata << "1:[#{pats}" #jal
134+
pata << "1:[#{pats}"
132135
end
133136
if last_char == te and type == :literal and token == :literal
134137
pata << text
@@ -147,7 +150,6 @@ def to_sp
147150
pats = ""
148151
end
149152
fixed_text = false
150-
151153
case token
152154
when :open
153155
set = true
@@ -158,7 +160,13 @@ def to_sp
158160
if pats[-1] == "["
159161
pats.chop!
160162
else
161-
pats += "]"
163+
if set_negate
164+
pats+="%]*"
165+
set_negate = false
166+
else
167+
pats += "]"
168+
end
169+
162170
end
163171
elsif type == :group
164172
capture = false
@@ -169,6 +177,11 @@ def to_sp
169177
pats = ""
170178
end
171179
end
180+
when :negate
181+
if set and pats[-1] == '['
182+
pats+="%"
183+
set_negate = true
184+
end
172185
when :capture
173186
capture = true if type == :group
174187
when :alternation
@@ -182,6 +195,7 @@ def to_sp
182195
end
183196
end
184197
when :range
198+
pats.chop! if options.include?('i')
185199
range = pats[-1]
186200
pats.chop!
187201
when :digit
@@ -212,28 +226,44 @@ def to_sp
212226
pats = text[-1]
213227
else
214228
pats += text
229+
pats += text.upcase if options.include?('i')
215230
end
216231
else
217232
range = range + "-" + text
218233
if range == "a-z"
219-
pats = "x" + pats
234+
if options.include?('i')
235+
pats = "L" + pats
236+
else
237+
pats = "x" + pats
238+
end
220239
elsif range == "A-Z"
221-
pats = "X" + pats
240+
if options.include?('i')
241+
pats = "L" + pats
242+
else
243+
pats = "X" + pats
244+
end
222245
elsif range == "0-9"
223246
pats = "n" + pats
224247
else
225-
pats += if set
226-
(range[0]..range[2]).to_a.join
227-
else
228-
"[" + (range[0]..range[2]).to_a.join + "]"
229-
end
248+
if set
249+
pats += (range[0]..range[2]).to_a.join
250+
if options.include?('i')
251+
pats += (range[0]..range[2]).to_a.join.upcase
252+
end
253+
else
254+
trange = (range[0]..range[2]).to_a.join
255+
if options.include?('i')
256+
trange += trange.upcase
257+
end
258+
pats += "[" + trange + "]"
259+
end
230260
end
231261
range = ""
232262
end
233263
pats = "[" + pats + "]" unless set
234264
when :interval
235265
size = text.sub(",", "-").sub("{", "").sub("}", "")
236-
size.chop! if size[-1] == "-"
266+
size+=(default_infinite+size.chop.to_i).to_s if size[-1] == "-"
237267
pats = size + ":" + pats
238268
if !patg.empty?
239269
patg << pats

lib/string/pattern/generate.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,16 @@ def StringPattern.generate(pattern, expected_errors: [], **synonyms)
568568
good_result = true
569569
end
570570
end
571+
if @block_list_enabled
572+
if @block_list.is_a?(Array)
573+
@block_list.each do |bl|
574+
if string.match?(/#{bl}/i)
575+
good_result = false
576+
break
577+
end
578+
end
579+
end
580+
end
571581
end until good_result or tries > 10000
572582
unless good_result
573583
puts "Not possible to generate the string on StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"

lib/string_pattern.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525
# In case using regular expressions the maximum when using * or + for repetitions
2626
# word_separator: (String, default: '_')
2727
# When generating words using symbol types 'w' or 'p' the character to separate the english or spanish words.
28+
# block_list: (Array, default: empty)
29+
# Array of words to be avoided from resultant strings.
30+
# block_list_enabled: (TrueFalse, default: false)
31+
# If true block_list will be take in consideration
2832
class StringPattern
2933
class << self
30-
attr_accessor :national_chars, :optimistic, :dont_repeat, :cache, :cache_values, :default_infinite, :word_separator
34+
attr_accessor :national_chars, :optimistic, :dont_repeat, :cache, :cache_values, :default_infinite, :word_separator, :block_list, :block_list_enabled
3135
end
3236
@national_chars = (("a".."z").to_a + ("A".."Z").to_a).join
3337
@optimistic = true
@@ -36,6 +40,8 @@ class << self
3640
@dont_repeat = false
3741
@default_infinite = 10
3842
@word_separator = "_"
43+
@block_list_enabled = false
44+
@block_list = []
3945
NUMBER_SET = ("0".."9").to_a
4046
SPECIAL_SET = [" ", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "{", "}", "[", "]", "'", ";", ":", "?", ">", "<", "`", "|", "/", '"']
4147
ALPHA_SET_LOWER = ("a".."z").to_a
@@ -56,4 +62,5 @@ def self.national_chars=(par)
5662
@cache = Hash.new()
5763
@national_chars = par
5864
end
65+
5966
end

spec/string/pattern/add_to_ruby_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,37 @@
6565
it "responds to to_sp" do
6666
expect(/\d{2}/.to_sp).to eq "2:n"
6767
end
68+
it "/[^abc]+/i.to_sp" do
69+
expect(/[^abc]+/i.to_sp).to eq "1-10:[%aAbBcC%]*"
70+
end
71+
it "/[abc]+/i.to_sp" do
72+
expect(/[abc]+/i.to_sp).to eq "1-10:[aAbBcC]"
73+
end
74+
it "/[a-z]+/i.to_sp" do
75+
expect(/[a-z]+/i.to_sp).to eq "1-10:L"
76+
end
77+
it "/[a-z]+/.to_sp" do
78+
expect(/[a-z]+/.to_sp).to eq "1-10:x"
79+
end
80+
it "/[m-z]+/i.to_sp" do
81+
expect(/[m-z]+/i.to_sp).to eq '1-10:[mnopqrstuvwxyzMNOPQRSTUVWXYZ]'
82+
end
83+
it '/[m-z]+\d+\w+[ab]+/i.to_sp' do
84+
expect(/[m-z]+\d+\w+[ab]+/i.to_sp).to eq ["1-10:[mnopqrstuvwxyzMNOPQRSTUVWXYZ]", "1-10:n", "1-10:Ln_", "1-10:[aAbB]"]
85+
end
86+
it '/a{3,}/.to_sp' do
87+
expect(/a{3,}/.to_sp).to eq '3-13:[a]'
88+
end
89+
it '/a{3,8}/.to_sp' do
90+
expect(/a{3,8}/.to_sp).to eq '3-8:[a]'
91+
end
92+
it '/a{3}/.to_sp' do
93+
expect(/a{3}/.to_sp).to eq '3:[a]'
94+
end
95+
it '/a{15,}/.to_sp' do
96+
expect(/a{15,}/.to_sp).to eq '15-25:[a]'
97+
end
98+
6899
end
69100

70101
describe "from Kernel" do

spec/string/pattern/generate_spec.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,7 @@
371371
regexp = /[abc]/
372372
expect(regexp.gen.match?(regexp)).to eq true
373373
end
374-
#todo: this is failing, it is generating a string of a b or c
375-
xit 'accepts [^abc]' do
374+
it 'accepts [^abc]' do
376375
regexp = /[^abc]/
377376
expect(regexp.gen.match?(regexp)).to eq true
378377
end
@@ -465,8 +464,7 @@
465464
regexp = /a{3}/
466465
expect(regexp.gen.match?(regexp)).to eq true
467466
end
468-
#todo: 3 or more is returning now always 3
469-
xit 'accepts a{3,}' do
467+
it 'accepts a{3,}' do
470468
regexp = /a{3}/
471469
expect(regexp.gen.match?(regexp)).to eq true
472470
end
@@ -479,6 +477,26 @@
479477
expect(regexp.gen.match?(regexp)).to eq true
480478
end
481479

480+
describe "block_list" do
481+
before(:all) do
482+
StringPattern.block_list = []
483+
StringPattern.block_list_enabled = false
484+
end
485+
486+
after(:each) do
487+
StringPattern.block_list = []
488+
StringPattern.block_list_enabled = false
489+
end
490+
491+
it "doesn't return any of block list" do
492+
StringPattern.block_list_enabled = true
493+
StringPattern.block_list = ('a'..'x').to_a
494+
5.times do
495+
expect("2:L".gen).to match(/^[yz]{2}$/i)
496+
end
497+
end
498+
end
499+
482500

483501
end
484502
end

string_pattern.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'string_pattern'
3-
s.version = '2.2.3'
3+
s.version = '2.3.0'
44
s.summary = "Generate easily random strings following a simple pattern or regular expression. '10-20:Xn/x/'.generate #>qBstvc6JN8ra. Also generate words in English or Spanish. Perfect to be used in test data factories."
55
s.description = "Easily generate strings supplying a very simple pattern. '10-20:Xn/x/'.generate #>qBstvc6JN8ra. Generate random strings using a regular expression (Regexp): /[a-z0-9]{2,5}\w+/.gen . Also generate words in English or Spanish. Perfect to be used in test data factories. Also, validate if a text fulfills a specific pattern or even generate a string following a pattern and returning the wrong length, value... for testing your applications."
66
s.authors = ["Mario Ruiz"]
@@ -16,6 +16,6 @@ Gem::Specification.new do |s|
1616
s.extra_rdoc_files = ["LICENSE","README.md"]
1717
s.homepage = 'https://github.com/MarioRuiz/string_pattern'
1818
s.license = 'MIT'
19-
s.add_runtime_dependency 'regexp_parser', '~> 1.3', '>= 1.3.0'
19+
s.add_runtime_dependency 'regexp_parser', '~> 2.5', '>= 2.5.0'
2020
end
2121

0 commit comments

Comments
 (0)