File: match_data_spec.cr

package info (click to toggle)
crystal 1.6.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,956 kB
  • sloc: javascript: 1,712; sh: 592; cpp: 541; makefile: 243; ansic: 119; python: 105; xml: 32
file content (303 lines) | stat: -rw-r--r-- 9,150 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
require "spec"

describe "Regex::MatchData" do
  it "does inspect" do
    /f(o)(x)/.match("the fox").inspect.should eq(%(Regex::MatchData("fox" 1:"o" 2:"x")))
    /f(o)(x)?/.match("the fort").inspect.should eq(%(Regex::MatchData("fo" 1:"o" 2:nil)))
    /fox/.match("the fox").inspect.should eq(%(Regex::MatchData("fox")))
  end

  it "does to_s" do
    /f(o)(x)/.match("the fox").to_s.should eq(%(Regex::MatchData("fox" 1:"o" 2:"x")))
    /f(?<lettero>o)(?<letterx>x)/.match("the fox").to_s.should eq(%(Regex::MatchData("fox" lettero:"o" letterx:"x")))
    /fox/.match("the fox").to_s.should eq(%(Regex::MatchData("fox")))
  end

  it "does pretty_print" do
    /f(o)(x)?/.match("the fo").pretty_inspect.should eq(%(Regex::MatchData("fo" 1:"o" 2:nil)))

    expected = <<-REGEX
      Regex::MatchData("foooo"
       first:"f"
       second:"oooo"
       third:"ooo"
       fourth:"oo"
       fifth:"o")
      REGEX

    /(?<first>f)(?<second>o(?<third>o(?<fourth>o(?<fifth>o))))/.match("fooooo").pretty_inspect.should eq(expected)
  end

  it "does size" do
    "Crystal".match(/[p-s]/).not_nil!.size.should eq(1)
    "Crystal".match(/r(ys)/).not_nil!.size.should eq(2)
    "Crystal".match(/r(ys)(?<ok>ta)/).not_nil!.size.should eq(3)
  end

  describe "#[]" do
    it "captures empty group" do
      ("foo" =~ /(?<g1>z?)foo/).should eq(0)
      $~[1].should eq("")
      $~["g1"].should eq("")
    end

    it "capture named group" do
      ("fooba" =~ /f(?<g1>o+)(?<g2>bar?)/).should eq(0)
      $~["g1"].should eq("oo")
      $~["g2"].should eq("ba")
    end

    it "captures duplicated named group" do
      re = /(?:(?<g1>foo)|(?<g1>bar))*/

      ("foo" =~ re).should eq(0)
      $~["g1"].should eq("foo")

      ("bar" =~ re).should eq(0)
      $~["g1"].should eq("bar")

      ("foobar" =~ re).should eq(0)
      $~["g1"].should eq("bar")

      ("barfoo" =~ re).should eq(0)
      $~["g1"].should eq("foo")
    end

    it "can use negative index" do
      "foo" =~ /(f)(oo)/
      $~[-1].should eq("oo")
      $~[-2].should eq("f")
      $~[-3].should eq("foo")
      expect_raises(IndexError, "Invalid capture group index: -4") { $~[-4] }
    end

    it "raises exception when named group doesn't exist" do
      ("foo" =~ /foo/).should eq(0)
      expect_raises(KeyError, "Capture group 'group' does not exist") { $~["group"] }
    end

    it "raises exception on optional empty group" do
      ("foo" =~ /(?<g1>z)?foo/).should eq(0)
      expect_raises(IndexError, "Capture group 1 was not matched") { $~[1] }
      expect_raises(KeyError, "Capture group 'g1' was not matched") { $~["g1"] }
    end

    it "raises if outside match range with []" do
      "foo" =~ /foo/
      expect_raises(IndexError, "Invalid capture group index: 1") { $~[1] }
    end

    it "raises if special variable accessed on invalid capture group" do
      "spice" =~ /spice(s)?/
      expect_raises(IndexError, "Capture group 1 was not matched") { $1 }
      expect_raises(IndexError, "Invalid capture group index: 3") { $3 }
    end

    it "can use range" do
      "ab" =~ /(a)(b)/
      $~[1..2].should eq(["a", "b"])
      $~[1..].should eq(["a", "b"])
      $~[..].should eq(["ab", "a", "b"])
      expect_raises(IndexError) { $~[4..] }
    end

    it "can use start and count" do
      "ab" =~ /(a)(b)/
      $~[1, 2].should eq(["a", "b"])
      expect_raises(IndexError) { $~[4, 1] }
    end
  end

  describe "#[]?" do
    it "capture empty group" do
      ("foo" =~ /(?<g1>z?)foo/).should eq(0)
      $~[1]?.should eq("")
      $~["g1"]?.should eq("")
    end

    it "capture optional empty group" do
      ("foo" =~ /(?<g1>z)?foo/).should eq(0)
      $~[1]?.should be_nil
      $~["g1"]?.should be_nil
    end

    it "capture named group" do
      ("fooba" =~ /f(?<g1>o+)(?<g2>bar?)/).should eq(0)
      $~["g1"]?.should eq("oo")
      $~["g2"]?.should eq("ba")
    end

    it "captures duplicated named group" do
      re = /(?:(?<g1>foo)|(?<g1>bar))*/

      ("foo" =~ re).should eq(0)
      $~["g1"]?.should eq("foo")

      ("bar" =~ re).should eq(0)
      $~["g1"]?.should eq("bar")

      ("foobar" =~ re).should eq(0)
      $~["g1"]?.should eq("bar")

      ("barfoo" =~ re).should eq(0)
      $~["g1"]?.should eq("foo")
    end

    it "can use negative index" do
      "foo" =~ /(b)?(f)(oo)/
      $~[-1]?.should eq("oo")
      $~[-2]?.should eq("f")
      $~[-3]?.should be_nil
      $~[-4]?.should eq("foo")
    end

    it "returns nil exception when named group doesn't exist" do
      ("foo" =~ /foo/).should eq(0)
      $~["group"]?.should be_nil
    end

    it "returns nil if outside match range with []" do
      "foo" =~ /foo/
      $~[1]?.should be_nil
    end

    it "can use range" do
      "ab" =~ /(a)(b)/
      $~[1..2]?.should eq(["a", "b"])
      $~[1..]?.should eq(["a", "b"])
      $~[..]?.should eq(["ab", "a", "b"])
      $~[4..]?.should be_nil
    end

    it "can use start and count" do
      "ab" =~ /(a)(b)/
      $~[1, 2]?.should eq(["a", "b"])
      $~[4, 1]?.should be_nil
    end
  end

  describe "#post_match" do
    it "returns an empty string when there's nothing after" do
      "Crystal".match(/ystal/).not_nil!.post_match.should eq ""
    end

    it "returns the part of the string after the match" do
      "Crystal".match(/yst/).not_nil!.post_match.should eq "al"
    end

    it "works with unicode" do
      "há日本語".match(/本/).not_nil!.post_match.should eq "語"
    end
  end

  describe "#pre_match" do
    it "returns an empty string when there's nothing before" do
      "Crystal".match(/Cryst/).not_nil!.pre_match.should eq ""
    end

    it "returns the part of the string before the match" do
      "Crystal".match(/yst/).not_nil!.pre_match.should eq "Cr"
    end

    it "works with unicode" do
      "há日本語".match(/本/).not_nil!.pre_match.should eq "há日"
    end
  end

  describe "#captures" do
    it "gets an array of unnamed captures" do
      "Crystal".match(/(Cr)y/).not_nil!.captures.should eq(["Cr"])
      "Crystal".match(/(Cr)(?<name1>y)(st)(?<name2>al)/).not_nil!.captures.should eq(["Cr", "st"])
    end

    it "gets an array of unnamed captures with optional" do
      "Crystal".match(/(Cr)(s)?/).not_nil!.captures.should eq(["Cr", nil])
      "Crystal".match(/(Cr)(?<name1>s)?(tal)?/).not_nil!.captures.should eq(["Cr", nil])
    end
  end

  describe "#named_captures" do
    it "gets a hash of named captures" do
      "Crystal".match(/(?<name1>Cr)y/).not_nil!.named_captures.should eq({"name1" => "Cr"})
      "Crystal".match(/(Cr)(?<name1>y)(st)(?<name2>al)/).not_nil!.named_captures.should eq({"name1" => "y", "name2" => "al"})
    end

    it "gets a hash of named captures with optional" do
      "Crystal".match(/(?<name1>Cr)(?<name2>s)?/).not_nil!.named_captures.should eq({"name1" => "Cr", "name2" => nil})
      "Crystal".match(/(Cr)(?<name1>s)?(t)?(?<name2>al)?/).not_nil!.named_captures.should eq({"name1" => nil, "name2" => nil})
    end

    it "gets a hash of named captures with duplicated name" do
      "Crystal".match(/(?<name>Cr)y(?<name>s)/).not_nil!.named_captures.should eq({"name" => "s"})
    end
  end

  describe "#to_a" do
    it "converts into an array" do
      "Crystal".match(/(?<name1>Cr)(y)/).not_nil!.to_a.should eq(["Cry", "Cr", "y"])
      "Crystal".match(/(Cr)(?<name1>y)(st)(?<name2>al)/).not_nil!.to_a.should eq(["Crystal", "Cr", "y", "st", "al"])
    end

    it "converts into an array having nil" do
      "Crystal".match(/(?<name1>Cr)(s)?/).not_nil!.to_a.should eq(["Cr", "Cr", nil])
      "Crystal".match(/(Cr)(?<name1>s)?(yst)?(?<name2>al)?/).not_nil!.to_a.should eq(["Crystal", "Cr", nil, "yst", "al"])
    end
  end

  describe "#to_h" do
    it "converts into a hash" do
      "Crystal".match(/(?<name1>Cr)(y)/).not_nil!.to_h.should eq({
              0 => "Cry",
        "name1" => "Cr",
              2 => "y",
      })
      "Crystal".match(/(Cr)(?<name1>y)(st)(?<name2>al)/).not_nil!.to_h.should eq({
              0 => "Crystal",
              1 => "Cr",
        "name1" => "y",
              3 => "st",
        "name2" => "al",
      })
    end

    it "converts into a hash having nil" do
      "Crystal".match(/(?<name1>Cr)(s)?/).not_nil!.to_h.should eq({
              0 => "Cr",
        "name1" => "Cr",
              2 => nil,
      })
      "Crystal".match(/(Cr)(?<name1>s)?(yst)?(?<name2>al)?/).not_nil!.to_h.should eq({
              0 => "Crystal",
              1 => "Cr",
        "name1" => nil,
              3 => "yst",
        "name2" => "al",
      })
    end

    it "converts into a hash with duplicated names" do
      "Crystal".match(/(Cr)(?<name>s)?(yst)?(?<name>al)?/).not_nil!.to_h.should eq({
             0 => "Crystal",
             1 => "Cr",
        "name" => "al",
             3 => "yst",
      })
    end
  end

  it "can check equality" do
    re = /((?<hello>he)llo)/
    m1 = re.match("hello")
    m2 = re.match("hello")
    m1.should be_truthy
    m2.should be_truthy
    m1.should eq(m2)
  end

  it "hashes" do
    re = /(a|b)/
    hash = re.match("a").hash
    hash.should eq(re.match("a").hash)
    hash.should_not eq(re.match("b").hash)
  end
end