File: conditionals_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 (69 lines) | stat: -rw-r--r-- 3,042 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
require 'spec_helper'

RSpec.describe('Conditional parsing') do
  include_examples 'parse', /(?<A>a)(?(<A>)T|F)/,
    [1]       => [:conditional, :open, Conditional::Expression, to_s: '(?(<A>)T|F)', reference: 'A'],
    [1, 0]    => [:conditional, :condition, Conditional::Condition, to_s: '(<A>)', reference: 'A'],
    [1, 1]    => [:expression,  :sequence,  Conditional::Branch, to_s: 'T'],
    [1, 1, 0] => [:literal, text: 'T'],
    [1, 2]    => [:expression,  :sequence,  Conditional::Branch, to_s: 'F'],
    [1, 2, 0] => [:literal, text: 'F']

  include_examples 'parse', /(a)(?(1)T|F)/,
    [1]       => [:conditional, :open, Conditional::Expression, to_s: '(?(1)T|F)', reference: 1],
    [1, 0]    => [:conditional, :condition, Conditional::Condition, to_s: '(1)', reference: 1],
    [1, 1]    => [:expression,  :sequence,  Conditional::Branch, to_s: 'T'],
    [1, 1, 0] => [:literal, text: 'T'],
    [1, 2]    => [:expression,  :sequence,  Conditional::Branch, to_s: 'F'],
    [1, 2, 0] => [:literal, text: 'F']

  include_examples 'parse', /(foo)(?(1)\d+|(\w)){42}/,
    [1]       => [Conditional::Expression, quantified?: true, to_s: '(?(1)\d+|(\w)){42}'],
    [1, 0]    => [Conditional::Condition, quantified?: false],
    [1, 1]    => [Conditional::Branch, quantified?: false],
    [1, 1, 0] => [:digit, quantified?: true, to_s: '\d+'],
    [1, 2]    => [Conditional::Branch, quantified?: false]

  # test nested and mixed with alternations
  include_examples 'parse', <<-EOS.gsub(/\s/, ''),
      (
        (a)
        |
        (b)
        |
        (
          (
            ?(2)
            (c(d|e)+)?
            |
            (
              ?(3)
              f
              |
              (
                ?(4)
                (g|(h)(i))
              )
            )
          )
        )
      )
    EOS
    [0]                         => [Group::Capture, count: 1],
    [0, 0]                      => [Alternation, count: 3],
    [0, 0, 2]                   => [Alternative, count: 1],
    [0, 0, 2, 0]                => [Group::Capture, count: 1],
    [0, 0, 2, 0, 0]             => [Conditional::Expression, count: 3, conditional_level: 0],
    [0, 0, 2, 0, 0, 0]          => [Conditional::Condition, to_s: '(2)', conditional_level: 1],
    [0, 0, 2, 0, 0, 1]          => [Conditional::Branch, to_s: '(c(d|e)+)?', conditional_level: 1],
    [0, 0, 2, 0, 0, 2]          => [Conditional::Branch, to_s: '(?(3)f|(?(4)(g|(h)(i))))', conditional_level: 1],
    [0, 0, 2, 0, 0, 2, 0]       => [Conditional::Expression, count: 3, conditional_level: 1],
    [0, 0, 2, 0, 0, 2, 0, 0]    => [Conditional::Condition, to_s: '(3)', conditional_level: 2],
    [0, 0, 2, 0, 0, 2, 0, 1]    => [Conditional::Branch, count: 1, to_s: 'f', conditional_level: 2],
    [0, 0, 2, 0, 0, 2, 0, 1, 0] => [Literal, text: 'f', conditional_level: 2]

  # test empty branch
  include_examples 'parse', /(?<A>a)(?(<A>)T|)/,
    [1]    => [Conditional::Expression, count: 3, to_s: '(?(<A>)T|)'],
    [1, 2] => [Conditional::Branch, to_s: '']
end