Skip to content

Commit 65eb742

Browse files
committed
close #1
1 parent 5968cc6 commit 65eb742

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

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/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/generate_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,26 @@
479479
expect(regexp.gen.match?(regexp)).to eq true
480480
end
481481

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

483503
end
484504
end

0 commit comments

Comments
 (0)