File: strfregexp_spec.rb

package info (click to toggle)
ruby-regexp-parser 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 968 kB
  • sloc: ruby: 6,396; sh: 12; makefile: 6
file content (224 lines) | stat: -rw-r--r-- 6,514 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
require 'spec_helper'

RSpec.describe('Expression::Base#strfregexp') do
  specify('#strfre alias') do
    expect(RP.parse(/a/)).to respond_to(:strfre)
  end

  specify('#strfregexp level') do
    root = RP.parse(/a(b(c))/)

    expect(root.strfregexp('%l')).to eq 'root'

    a = root.first
    expect(a.strfregexp('%%l')).to eq '%0'

    b = root[1].first
    expect(b.strfregexp('<%l>')).to eq '<1>'

    c = root[1][1].first
    expect(c.strfregexp('[at: %l]')).to eq '[at: 2]'
  end

  specify('#strfregexp start end') do
    root = RP.parse(/a(b(c))/)

    expect(root.strfregexp('%s')).to eq '0'
    expect(root.strfregexp('%e')).to eq '7'

    a = root.first
    expect(a.strfregexp('%%s')).to eq '%0'
    expect(a.strfregexp('%e')).to eq '1'

    group_1 = root[1]
    expect(group_1.strfregexp('GRP:%s')).to eq 'GRP:1'
    expect(group_1.strfregexp('%e')).to eq '7'

    b = group_1.first
    expect(b.strfregexp('<@%s>')).to eq '<@2>'
    expect(b.strfregexp('%e')).to eq '3'

    c = group_1.last.first
    expect(c.strfregexp('[at: %s]')).to eq '[at: 4]'
    expect(c.strfregexp('%e')).to eq '5'
  end

  specify('#strfregexp length') do
    root = RP.parse(/a[b]c/)

    expect(root.strfregexp('%S')).to eq '5'

    a = root.first
    expect(a.strfregexp('%S')).to eq '1'

    set = root[1]
    expect(set.strfregexp('%S')).to eq '3'
  end

  specify('#strfregexp coded offset') do
    root = RP.parse(/a[b]c/)

    expect(root.strfregexp('%o')).to eq '@0+5'

    a = root.first
    expect(a.strfregexp('%o')).to eq '@0+1'

    set = root[1]
    expect(set.strfregexp('%o')).to eq '@1+3'
  end

  specify('#strfregexp type token') do
    root = RP.parse(/a[b](c)/)

    expect(root.strfregexp('%y')).to eq 'expression'
    expect(root.strfregexp('%k')).to eq 'root'
    expect(root.strfregexp('%i')).to eq 'expression:root'
    expect(root.strfregexp('%c')).to eq 'Regexp::Expression::Root'

    a = root.first
    expect(a.strfregexp('%y')).to eq 'literal'
    expect(a.strfregexp('%k')).to eq 'literal'
    expect(a.strfregexp('%i')).to eq 'literal:literal'
    expect(a.strfregexp('%c')).to eq 'Regexp::Expression::Literal'

    set = root[1]
    expect(set.strfregexp('%y')).to eq 'set'
    expect(set.strfregexp('%k')).to eq 'character'
    expect(set.strfregexp('%i')).to eq 'set:character'
    expect(set.strfregexp('%c')).to eq 'Regexp::Expression::CharacterSet'

    group = root.last
    expect(group.strfregexp('%y')).to eq 'group'
    expect(group.strfregexp('%k')).to eq 'capture'
    expect(group.strfregexp('%i')).to eq 'group:capture'
    expect(group.strfregexp('%c')).to eq 'Regexp::Expression::Group::Capture'
  end

  specify('#strfregexp quantifier') do
    root = RP.parse(/a+[b](c)?d{3,4}/)

    expect(root.strfregexp('%q')).to eq '{1}'
    expect(root.strfregexp('%Q')).to eq ''
    expect(root.strfregexp('%z, %Z')).to eq '1, 1'

    a = root.first
    expect(a.strfregexp('%q')).to eq '{1, or-more}'
    expect(a.strfregexp('%Q')).to eq '+'
    expect(a.strfregexp('%z, %Z')).to eq '1, -1'

    set = root[1]
    expect(set.strfregexp('%q')).to eq '{1}'
    expect(set.strfregexp('%Q')).to eq ''
    expect(set.strfregexp('%z, %Z')).to eq '1, 1'

    group = root[2]
    expect(group.strfregexp('%q')).to eq '{0, 1}'
    expect(group.strfregexp('%Q')).to eq '?'
    expect(group.strfregexp('%z, %Z')).to eq '0, 1'

    d = root.last
    expect(d.strfregexp('%q')).to eq '{3, 4}'
    expect(d.strfregexp('%Q')).to eq '{3,4}'
    expect(d.strfregexp('%z, %Z')).to eq '3, 4'
  end

  specify('#strfregexp text') do
    root = RP.parse(/a(b(c))|[d-gk-p]+/)

    expect(root.strfregexp('%t')).to eq 'a(b(c))|[d-gk-p]+'
    expect(root.strfregexp('%~t')).to eq 'expression:root'

    alt = root.first
    expect(alt.strfregexp('%t')).to eq 'a(b(c))|[d-gk-p]+'
    expect(alt.strfregexp('%T')).to eq 'a(b(c))|[d-gk-p]+'
    expect(alt.strfregexp('%~t')).to eq 'meta:alternation'

    seq_1 = alt.first
    expect(seq_1.strfregexp('%t')).to eq 'a(b(c))'
    expect(seq_1.strfregexp('%T')).to eq 'a(b(c))'
    expect(seq_1.strfregexp('%~t')).to eq 'expression:sequence'

    group = seq_1[1]
    expect(group.strfregexp('%t')).to eq '(b(c))'
    expect(group.strfregexp('%T')).to eq '(b(c))'
    expect(group.strfregexp('%~t')).to eq 'group:capture'

    seq_2 = alt.last
    expect(seq_2.strfregexp('%t')).to eq '[d-gk-p]+'
    expect(seq_2.strfregexp('%T')).to eq '[d-gk-p]+'

    set = seq_2.first
    expect(set.strfregexp('%t')).to eq '[d-gk-p]'
    expect(set.strfregexp('%T')).to eq '[d-gk-p]+'
    expect(set.strfregexp('%~t')).to eq 'set:character'
  end

  specify('#strfregexp combined') do
    root = RP.parse(/a{5}|[b-d]+/)

    expect(root.strfregexp('%b')).to eq '@0+11 expression:root'
    expect(root.strfregexp('%b')).to eq root.strfregexp('%o %i')

    expect(root.strfregexp('%m')).to eq '@0+11 expression:root {1}'
    expect(root.strfregexp('%m')).to eq root.strfregexp('%b %q')

    expect(root.strfregexp('%a')).to eq '@0+11 expression:root {1} a{5}|[b-d]+'
    expect(root.strfregexp('%a')).to eq root.strfregexp('%m %t')
  end

  specify('#strfregexp conditional') do
    root = RP.parse('(?<A>a)(?(<A>)b|c)')

    expect { root.strfregexp }.not_to(raise_error)
  end

  specify('#strfregexp_tree') do
    root = RP.parse(/a[b-d]*(e(f+))?/)

    expect(root.strfregexp_tree('%>%o %~t')).to eq(
      "@0+15 expression:root\n" +
      "  @0+1 a\n" +
      "  @1+6 set:character\n" +
      "    @2+3 set:range\n" +
      "      @2+1 b\n" +
      "      @4+1 d\n" +
      "  @7+8 group:capture\n" +
      "    @8+1 e\n" +
      "    @9+4 group:capture\n" +
      "      @10+2 f+"
    )
  end

  specify('#strfregexp_tree separator') do
    root = RP.parse(/a[b-d]*(e(f+))?/)

    expect(root.strfregexp_tree('%>%o %~t', true, '-SEP-')).to eq(
      "@0+15 expression:root-SEP-" +
      "  @0+1 a-SEP-" +
      "  @1+6 set:character-SEP-" +
      "    @2+3 set:range-SEP-" +
      "      @2+1 b-SEP-" +
      "      @4+1 d-SEP-" +
      "  @7+8 group:capture-SEP-" +
      "    @8+1 e-SEP-" +
      "    @9+4 group:capture-SEP-" +
      "      @10+2 f+"
    )
  end

  specify('#strfregexp_tree excluding self') do
    root = RP.parse(/a[b-d]*(e(f+))?/)

    expect(root.strfregexp_tree('%>%o %~t', false)).to eq(
      "@0+1 a\n" +
      "@1+6 set:character\n" +
      "  @2+3 set:range\n" +
      "    @2+1 b\n" +
      "    @4+1 d\n" +
      "@7+8 group:capture\n" +
      "  @8+1 e\n" +
      "  @9+4 group:capture\n" +
      "    @10+2 f+"
    )
  end
end