File: subexpression_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 (34 lines) | stat: -rw-r--r-- 1,706 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
require 'spec_helper'

RSpec.describe(Regexp::Expression::Subexpression) do
  # check #ts, #te
  include_examples 'parse', /abcd|ghij|klmn|pqur/,
    [0]    => [Alternation, ts: 0,  te: 19],
    [0, 0] => [Alternative, ts: 0,  te: 4],
    [0, 1] => [Alternative, ts: 5,  te: 9],
    [0, 2] => [Alternative, ts: 10, te: 14],
    [0, 3] => [Alternative, ts: 15, te: 19]

  # check #nesting_level
  include_examples 'parse', /a(b(\d|[ef-g[h]]))/,
    [0]                   => [Literal,              to_s: 'a',            nesting_level: 1],
    [1, 0]                => [Literal,              to_s: 'b',            nesting_level: 2],
    [1, 1, 0]             => [Alternation,          to_s: '\d|[ef-g[h]]', nesting_level: 3],
    [1, 1, 0, 0]          => [Alternative,          to_s: '\d',           nesting_level: 4],
    [1, 1, 0, 0, 0]       => [CharacterType::Digit, to_s: '\d',           nesting_level: 5],
    [1, 1, 0, 1]          => [Alternative,          to_s: '[ef-g[h]]',    nesting_level: 4],
    [1, 1, 0, 1, 0]       => [CharacterSet,         to_s: '[ef-g[h]]',    nesting_level: 5],
    [1, 1, 0, 1, 0, 0]    => [Literal,              to_s: 'e',            nesting_level: 6],
    [1, 1, 0, 1, 0, 1]    => [CharacterSet::Range,  to_s: 'f-g',          nesting_level: 6],
    [1, 1, 0, 1, 0, 1, 0] => [Literal,              to_s: 'f',            nesting_level: 7],
    [1, 1, 0, 1, 0, 2, 0] => [Literal,              to_s: 'h',            nesting_level: 7]

  specify('#dig') do
    root = RP.parse(/(((a)))/)

    expect(root.dig(0).to_s).to eq '(((a)))'
    expect(root.dig(0, 0, 0, 0).to_s).to eq 'a'
    expect(root.dig(0, 0, 0, 0, 0)).to be_nil
    expect(root.dig(3, 7)).to be_nil
  end
end