|
| 1 | +RSpec.describe RegexpExamples::Config do |
| 2 | + |
| 3 | + describe 'max_repeater_variance' do |
| 4 | + context 'as a passed parameter' do |
| 5 | + it 'with low limit' do |
| 6 | + expect(/[A-Z]/.examples(max_results_limit: 5)) |
| 7 | + .to match_array %w(A B C D E) |
| 8 | + end |
| 9 | + it 'with (default) high limit' do |
| 10 | + expect(/[ab]{14}/.examples.length) |
| 11 | + .to be <= 10000 # NOT 2**14 == 16384, because it's been limited |
| 12 | + end |
| 13 | + it 'with (custom) high limit' do |
| 14 | + expect(/[ab]{14}/.examples(max_results_limit: 20000).length) |
| 15 | + .to eq 16384 # NOT 10000, because it's below the limit |
| 16 | + end |
| 17 | + it 'for boolean or groups' do |
| 18 | + expect(/[ab]{3}|[cd]{3}/.examples(max_results_limit: 10).length) |
| 19 | + .to eq 10 |
| 20 | + end |
| 21 | + it 'for case insensitive examples' do |
| 22 | + expect(/[ab]{3}/i.examples(max_results_limit: 10).length) |
| 23 | + .to be <= 10 |
| 24 | + end |
| 25 | + it 'for range repeaters' do |
| 26 | + expect(/[ab]{2,3}/.examples(max_results_limit: 10).length) |
| 27 | + .to be <= 10 # NOT 4 + 8 = 12 |
| 28 | + end |
| 29 | + it 'for backreferences' do |
| 30 | + expect(/([ab]{3})\1?/.examples(max_results_limit: 10).length) |
| 31 | + .to be <= 10 # NOT 8 * 2 = 16 |
| 32 | + end |
| 33 | + it 'for a complex pattern' do |
| 34 | + expect(/(a|[bc]{2})\1{1,3}/.examples(max_results_limit: 14).length) |
| 35 | + .to be <= 14 # NOT (1 + 4) * 3 = 15 |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + context 'as a global setting' do |
| 40 | + before do |
| 41 | + @original = RegexpExamples::Config.max_results_limit |
| 42 | + RegexpExamples::Config.max_results_limit = 5 |
| 43 | + end |
| 44 | + after do |
| 45 | + RegexpExamples::Config.max_results_limit = @original |
| 46 | + end |
| 47 | + |
| 48 | + it 'sets limit without passing explicitly' do |
| 49 | + expect(/[A-Z]/.examples) |
| 50 | + .to match_array %w(A B C D E) |
| 51 | + end |
| 52 | + end |
| 53 | + end # describe 'max_results_limit' |
| 54 | + |
| 55 | + describe 'max_repeater_variance' do |
| 56 | + context 'as a passed parameter' do |
| 57 | + it 'with a larger value' do |
| 58 | + expect(/a+/.examples(max_repeater_variance: 5)) |
| 59 | + .to match_array %w(a aa aaa aaaa aaaaa aaaaaa) |
| 60 | + end |
| 61 | + it 'with a lower value' do |
| 62 | + expect(/a{4,8}/.examples(max_repeater_variance: 0)) |
| 63 | + .to eq %w(aaaa) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context 'as a global setting' do |
| 68 | + before do |
| 69 | + @original = RegexpExamples::Config.max_repeater_variance |
| 70 | + RegexpExamples::Config.max_repeater_variance = 5 |
| 71 | + end |
| 72 | + after do |
| 73 | + RegexpExamples::Config.max_repeater_variance = @original |
| 74 | + end |
| 75 | + |
| 76 | + it 'sets limit without passing explicitly' do |
| 77 | + expect(/a+/.examples) |
| 78 | + .to match_array %w(a aa aaa aaaa aaaaa aaaaaa) |
| 79 | + end |
| 80 | + end |
| 81 | + end # describe 'max_repeater_variance' |
| 82 | + |
| 83 | + describe 'max_group_results' do |
| 84 | + context 'as a passed parameter' do |
| 85 | + it 'with a larger value' do |
| 86 | + expect(/\d/.examples(max_group_results: 10)) |
| 87 | + .to match_array %w(0 1 2 3 4 5 6 7 8 9) |
| 88 | + end |
| 89 | + it 'with a lower value' do |
| 90 | + expect(/\d/.examples(max_group_results: 3)) |
| 91 | + .to match_array %w(0 1 2) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + context 'as a global setting' do |
| 96 | + before do |
| 97 | + @original = RegexpExamples::Config.max_group_results |
| 98 | + RegexpExamples::Config.max_group_results = 10 |
| 99 | + end |
| 100 | + after do |
| 101 | + RegexpExamples::Config.max_group_results = @original |
| 102 | + end |
| 103 | + |
| 104 | + it 'sets limit without passing explicitly' do |
| 105 | + expect(/\d/.examples) |
| 106 | + .to match_array %w(0 1 2 3 4 5 6 7 8 9) |
| 107 | + end |
| 108 | + end |
| 109 | + end # describe 'max_group_results' |
| 110 | + |
| 111 | + describe 'thread safety' do |
| 112 | + it 'uses thread-local global config values' do |
| 113 | + thread = Thread.new do |
| 114 | + RegexpExamples::Config.max_group_results = 1 |
| 115 | + expect(/\d/.examples).to eq %w(0) |
| 116 | + end |
| 117 | + sleep 0.1 # Give the above thread time to run |
| 118 | + expect(/\d/.examples).to eq %w(0 1 2 3 4) |
| 119 | + thread.join |
| 120 | + end |
| 121 | + |
| 122 | + it 'uses thread-local block config values' do |
| 123 | + thread = Thread.new do |
| 124 | + RegexpExamples::Config.with_configuration(max_group_results: 1) do |
| 125 | + expect(/\d/.examples).to eq %w(0) |
| 126 | + sleep 0.2 # Give the below thread time to run while this block is open |
| 127 | + end |
| 128 | + end |
| 129 | + sleep 0.1 # Give the above thread time to run |
| 130 | + expect(/\d/.examples).to eq %w(0 1 2 3 4) |
| 131 | + thread.join |
| 132 | + end |
| 133 | + end # describe 'thread safety' |
| 134 | + |
| 135 | +end |
0 commit comments