|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
3 | 3 | RSpec.describe(Regexp::Expression::Base) do |
4 | | - specify('#to_re') do |
5 | | - re_text = '^a*(b([cde]+))+f?$' |
6 | | - |
7 | | - re = RP.parse(re_text).to_re |
8 | | - |
9 | | - expect(re).to be_a(::Regexp) |
10 | | - expect(re_text).to eq re.source |
11 | | - end |
12 | | - |
13 | | - specify('#level') do |
14 | | - regexp = /^a(b(c(d)))e$/ |
15 | | - root = RP.parse(regexp) |
16 | | - |
17 | | - ['^', 'a', '(b(c(d)))', 'e', '$'].each_with_index do |t, i| |
18 | | - expect(root.dig(i).to_s).to eq t |
19 | | - expect(root.dig(i).level).to eq 0 |
20 | | - end |
21 | | - |
22 | | - expect(root.dig(2, 0).to_s).to eq 'b' |
23 | | - expect(root.dig(2, 0).level).to eq 1 |
24 | | - |
25 | | - expect(root.dig(2, 1, 0).to_s).to eq 'c' |
26 | | - expect(root.dig(2, 1, 0).level).to eq 2 |
27 | | - |
28 | | - expect(root.dig(2, 1, 1, 0).to_s).to eq 'd' |
29 | | - expect(root.dig(2, 1, 1, 0).level).to eq 3 |
30 | | - end |
31 | | - |
32 | | - specify('#terminal?') do |
33 | | - root = RP.parse('^a([b]+)c$') |
34 | | - |
35 | | - expect(root).not_to be_terminal |
36 | | - |
37 | | - expect(root.dig(0)).to be_terminal |
38 | | - expect(root.dig(1)).to be_terminal |
39 | | - expect(root.dig(2)).not_to be_terminal |
40 | | - expect(root.dig(2, 0)).not_to be_terminal |
41 | | - expect(root.dig(2, 0, 0)).to be_terminal |
42 | | - expect(root.dig(3)).to be_terminal |
43 | | - expect(root.dig(4)).to be_terminal |
44 | | - end |
45 | | - |
46 | | - specify('alt #terminal?') do |
47 | | - root = RP.parse('^(ab|cd)$') |
48 | | - |
49 | | - expect(root).not_to be_terminal |
50 | | - |
51 | | - expect(root.dig(0)).to be_terminal |
52 | | - expect(root.dig(1)).not_to be_terminal |
53 | | - expect(root.dig(1, 0)).not_to be_terminal |
54 | | - expect(root.dig(1, 0, 0)).not_to be_terminal |
55 | | - expect(root.dig(1, 0, 0, 0)).to be_terminal |
56 | | - expect(root.dig(1, 0, 1)).not_to be_terminal |
57 | | - expect(root.dig(1, 0, 1, 0)).to be_terminal |
58 | | - end |
59 | | - |
60 | | - specify('#coded_offset') do |
61 | | - root = RP.parse('^a*(b+(c?))$') |
62 | | - |
63 | | - expect(root.coded_offset).to eq '@0+12' |
64 | | - |
65 | | - [ |
66 | | - ['@0+1', '^'], |
67 | | - ['@1+2', 'a*'], |
68 | | - ['@3+8', '(b+(c?))'], |
69 | | - ['@11+1', '$'], |
70 | | - ].each_with_index do |check, i| |
71 | | - against = [root.dig(i).coded_offset, root.dig(i).to_s] |
72 | | - |
73 | | - expect(against).to eq check |
74 | | - end |
75 | | - |
76 | | - expect([root.dig(2, 0).coded_offset, root.dig(2, 0).to_s]).to eq ['@4+2', 'b+'] |
77 | | - expect([root.dig(2, 1).coded_offset, root.dig(2, 1).to_s]).to eq ['@6+4', '(c?)'] |
78 | | - expect([root.dig(2, 1, 0).coded_offset, root.dig(2, 1, 0).to_s]).to eq ['@7+2', 'c?'] |
79 | | - end |
80 | | - |
81 | | - specify('#quantity') do |
82 | | - expect(RP.parse(/aa/)[0].quantity).to eq [nil, nil] |
83 | | - expect(RP.parse(/a?/)[0].quantity).to eq [0, 1] |
84 | | - expect(RP.parse(/a*/)[0].quantity).to eq [0, -1] |
85 | | - expect(RP.parse(/a+/)[0].quantity).to eq [1, -1] |
86 | | - end |
87 | | - |
88 | | - specify('#repetitions') do |
89 | | - expect(RP.parse(/aa/)[0].repetitions).to eq 1..1 |
90 | | - expect(RP.parse(/a?/)[0].repetitions).to eq 0..1 |
91 | | - expect(RP.parse(/a*/)[0].repetitions).to eq 0..(Float::INFINITY) |
92 | | - expect(RP.parse(/a+/)[0].repetitions).to eq 1..(Float::INFINITY) |
93 | | - end |
94 | | - |
95 | | - specify('#base_length') do |
96 | | - expect(RP.parse(/(aa)/)[0].base_length).to eq 4 |
97 | | - expect(RP.parse(/(aa){42}/)[0].base_length).to eq 4 |
98 | | - end |
99 | | - |
100 | | - specify('#full_length') do |
101 | | - expect(RP.parse(/(aa)/)[0].full_length).to eq 4 |
102 | | - expect(RP.parse(/(aa){42}/)[0].full_length).to eq 8 |
103 | | - end |
| 4 | + # test #to_re |
| 5 | + include_examples 'parse', '^a*(b([cde]+))+f?$', |
| 6 | + [] => [Root, to_re: /^a*(b([cde]+))+f?$/] |
| 7 | + |
| 8 | + # test #level |
| 9 | + include_examples 'parse', /^a(b(c(d)))e$/, |
| 10 | + [0] => [to_s: '^', level: 0], |
| 11 | + [1] => [to_s: 'a', level: 0], |
| 12 | + [2] => [to_s: '(b(c(d)))', level: 0], |
| 13 | + [2, 0] => [to_s: 'b', level: 1], |
| 14 | + [2, 1] => [to_s: '(c(d))', level: 1], |
| 15 | + [2, 1, 0] => [to_s: 'c', level: 2], |
| 16 | + [2, 1, 1] => [to_s: '(d)', level: 2], |
| 17 | + [2, 1, 1, 0] => [to_s: 'd', level: 3], |
| 18 | + [3] => [to_s: 'e', level: 0], |
| 19 | + [4] => [to_s: '$', level: 0] |
| 20 | + |
| 21 | + # test #terminal? |
| 22 | + include_examples 'parse', /^a([b]+)c$/, |
| 23 | + [] => [Root, terminal?: false], |
| 24 | + [0] => [to_s: '^', terminal?: true], |
| 25 | + [1] => [to_s: 'a', terminal?: true], |
| 26 | + [2] => [to_s: '([b]+)', terminal?: false], |
| 27 | + [2, 0] => [to_s: '[b]+', terminal?: false], |
| 28 | + [2, 0, 0] => [to_s: 'b', terminal?: true], |
| 29 | + [3] => [to_s: 'c', terminal?: true], |
| 30 | + [4] => [to_s: '$', terminal?: true] |
| 31 | + |
| 32 | + include_examples 'parse', /^(ab|cd)$/, |
| 33 | + [] => [Root, terminal?: false], |
| 34 | + [0] => [:bol, to_s: '^', terminal?: true], |
| 35 | + [1] => [:capture, to_s: '(ab|cd)', terminal?: false], |
| 36 | + [1, 0] => [:alternation, to_s: 'ab|cd', terminal?: false], |
| 37 | + [1, 0, 0] => [:sequence, to_s: 'ab', terminal?: false], |
| 38 | + [1, 0, 0, 0] => [:literal, to_s: 'ab', terminal?: true], |
| 39 | + [1, 0, 1] => [:sequence, to_s: 'cd', terminal?: false], |
| 40 | + [1, 0, 1, 0] => [:literal, to_s: 'cd', terminal?: true], |
| 41 | + [2] => [:eol, to_s: '$', terminal?: true] |
| 42 | + |
| 43 | + # test #coded_offset |
| 44 | + include_examples 'parse', /^a*(b+(c?))$/, |
| 45 | + [] => [Root, coded_offset: '@0+12'], |
| 46 | + [0] => [to_s: '^', coded_offset: '@0+1'], |
| 47 | + [1] => [to_s: 'a*', coded_offset: '@1+2'], |
| 48 | + [2] => [to_s: '(b+(c?))', coded_offset: '@3+8'], |
| 49 | + [2, 0] => [to_s: 'b+', coded_offset: '@4+2'], |
| 50 | + [2, 1] => [to_s: '(c?)', coded_offset: '@6+4'], |
| 51 | + [2, 1, 0] => [to_s: 'c?', coded_offset: '@7+2'], |
| 52 | + [3] => [to_s: '$', coded_offset: '@11+1'] |
| 53 | + |
| 54 | + # test #quantity |
| 55 | + include_examples 'parse', /aa/, [0] => [quantity: [nil, nil]] |
| 56 | + include_examples 'parse', /a?/, [0] => [quantity: [0, 1]] |
| 57 | + include_examples 'parse', /a*/, [0] => [quantity: [0, -1]] |
| 58 | + include_examples 'parse', /a+/, [0] => [quantity: [1, -1]] |
| 59 | + |
| 60 | + # test #repetitions |
| 61 | + include_examples 'parse', /aa/, [0] => [repetitions: 1..1] |
| 62 | + include_examples 'parse', /a?/, [0] => [repetitions: 0..1] |
| 63 | + include_examples 'parse', /a*/, [0] => [repetitions: 0..(Float::INFINITY)] |
| 64 | + include_examples 'parse', /a+/, [0] => [repetitions: 1..(Float::INFINITY)] |
| 65 | + |
| 66 | + # test #base_length, #full_length |
| 67 | + include_examples 'parse', /(aa)/, [0] => [base_length: 4] |
| 68 | + include_examples 'parse', /(aa)/, [0] => [full_length: 4] |
| 69 | + include_examples 'parse', /(aa){42}/, [0] => [base_length: 4] |
| 70 | + include_examples 'parse', /(aa){42}/, [0] => [full_length: 8] |
104 | 71 | end |
0 commit comments