Skip to content

Commit 44d6b31

Browse files
author
Tom Lord
committed
move existing spec to new file + define desired API
1 parent 7ae5278 commit 44d6b31

File tree

2 files changed

+112
-53
lines changed

2 files changed

+112
-53
lines changed

spec/regexp-examples_spec.rb

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -316,24 +316,6 @@ def self.examples_are_empty(*regexps)
316316
end
317317
end
318318

319-
context 'max_repeater_variance config option' do
320-
it do
321-
expect(/a+/.examples(max_repeater_variance: 5))
322-
.to match_array %w(a aa aaa aaaa aaaaa aaaaaa)
323-
end
324-
it do
325-
expect(/a{4,8}/.examples(max_repeater_variance: 0))
326-
.to eq %w(aaaa)
327-
end
328-
end
329-
330-
context 'max_group_results config option' do
331-
it do
332-
expect(/\d/.examples(max_group_results: 10))
333-
.to match_array %w(0 1 2 3 4 5 6 7 8 9)
334-
end
335-
end
336-
337319
context 'case insensitive' do
338320
it { expect(/ab/i.examples).to match_array %w(ab aB Ab AB) }
339321
it do
@@ -377,39 +359,4 @@ def self.examples_are_empty(*regexps)
377359
end
378360
end # context 'exact examples match'
379361
end # context 'returns matching strings'
380-
381-
context 'max_results_limit config option' do
382-
it 'with low limit' do
383-
expect(/[A-Z]/.examples(max_results_limit: 5))
384-
.to match_array %w(A B C D E)
385-
end
386-
it 'with (default) high limit' do
387-
expect(/[ab]{14}/.examples.length)
388-
.to be <= 10000 # NOT 2**14 == 16384, because it's been limited
389-
end
390-
it 'with (custom) high limit' do
391-
expect(/[ab]{14}/.examples(max_results_limit: 20000).length)
392-
.to eq 16384 # NOT 10000, because it's below the limit
393-
end
394-
it 'for boolean or groups' do
395-
expect(/[ab]{3}|[cd]{3}/.examples(max_results_limit: 10).length)
396-
.to eq 10
397-
end
398-
it 'for case insensitive examples' do
399-
expect(/[ab]{3}/i.examples(max_results_limit: 10).length)
400-
.to be <= 10
401-
end
402-
it 'for range repeaters' do
403-
expect(/[ab]{2,3}/.examples(max_results_limit: 10).length)
404-
.to be <= 10 # NOT 4 + 8 = 12
405-
end
406-
it 'for backreferences' do
407-
expect(/([ab]{3})\1?/.examples(max_results_limit: 10).length)
408-
.to be <= 10 # NOT 8 * 2 = 16
409-
end
410-
it 'for a complex pattern' do
411-
expect(/(a|[bc]{2})\1{1,3}/.examples(max_results_limit: 14).length)
412-
.to be <= 14 # NOT (1 + 4) * 3 = 15
413-
end
414-
end
415362
end

spec/result_count_limiters_spec.rb

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
RSpec.describe RegexpExamples::ResultCountLimiters 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::ResultCountLimiters.max_results_limit
42+
RegexpExamples::ResultCountLimiters.max_results_limit = 5
43+
end
44+
after do
45+
RegexpExamples::ResultCountLimiters.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::ResultCountLimiters.max_repeater_variance
70+
RegexpExamples::ResultCountLimiters.max_repeater_variance = 5
71+
end
72+
after do
73+
RegexpExamples::ResultCountLimiters.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::ResultCountLimiters.max_group_results
98+
RegexpExamples::ResultCountLimiters.max_group_results = 10
99+
end
100+
after do
101+
RegexpExamples::ResultCountLimiters.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+
112+
end

0 commit comments

Comments
 (0)