File: clone_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 (153 lines) | stat: -rw-r--r-- 4,749 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
require 'spec_helper'

RSpec.describe('Expression::Base#clone') do
  specify('Base#clone') do
    root = RP.parse(/^(?i:a)b+$/i)
    copy = root.clone

    expect(copy.to_s).to eq root.to_s

    expect(root.object_id).not_to eq copy.object_id
    expect(root.text).to eq copy.text
    expect(root.text.object_id).not_to eq copy.text.object_id

    root_1 = root[1]
    copy_1 = copy[1]

    expect(root_1.options).to eq copy_1.options
    expect(root_1.options.object_id).not_to eq copy_1.options.object_id

    root_2 = root[2]
    copy_2 = copy[2]

    expect(root_2).to be_quantified
    expect(copy_2).to be_quantified
    expect(root_2.quantifier.text).to eq copy_2.quantifier.text
    expect(root_2.quantifier.text.object_id).not_to eq copy_2.quantifier.text.object_id
    expect(root_2.quantifier.object_id).not_to eq copy_2.quantifier.object_id

    # regression test
    expect { root_2.clone }.not_to(change { root_2.quantifier.object_id })
    expect { root_2.clone }.not_to(change { root_2.quantifier.text.object_id })
  end

  specify('Subexpression#clone') do
    root = RP.parse(/^a(b([cde])f)g$/)
    copy = root.clone

    expect(copy.to_s).to eq root.to_s

    expect(root).to respond_to(:expressions)
    expect(copy).to respond_to(:expressions)
    expect(root.expressions.object_id).not_to eq copy.expressions.object_id
    copy.expressions.each_with_index do |exp, index|
      expect(root[index].object_id).not_to eq exp.object_id
    end
    copy[2].each_with_index do |exp, index|
      expect(root[2][index].object_id).not_to eq exp.object_id
    end

    # regression test
    expect { root.clone }.not_to(change { root.expressions.object_id })
  end

  specify('Group::Named#clone') do
    root = RP.parse('^(?<somename>a)+bc$')
    copy = root.clone

    expect(copy.to_s).to eq root.to_s

    root_1 = root[1]
    copy_1 = copy[1]

    expect(root_1.name).to eq copy_1.name
    expect(root_1.name.object_id).not_to eq copy_1.name.object_id
    expect(root_1.text).to eq copy_1.text
    expect(root_1.expressions.object_id).not_to eq copy_1.expressions.object_id
    copy_1.expressions.each_with_index do |exp, index|
      expect(root_1[index].object_id).not_to eq exp.object_id
    end

    # regression test
    expect { root_1.clone }.not_to(change { root_1.name.object_id })
  end

  specify('Group::Options#clone') do
    root = RP.parse('foo(?i)bar')
    copy = root.clone

    expect(copy.to_s).to eq root.to_s

    root_1 = root[1]
    copy_1 = copy[1]

    expect(root_1.option_changes).to eq copy_1.option_changes
    expect(root_1.option_changes.object_id).not_to eq copy_1.option_changes.object_id

    # regression test
    expect { root_1.clone }.not_to(change { root_1.option_changes.object_id })
  end

  specify('Backreference::Base#clone') do
    root = RP.parse('(foo)\1')
    copy = root.clone

    expect(copy.to_s).to eq root.to_s

    root_1 = root[1]
    copy_1 = copy[1]

    expect(root_1.referenced_expression).to eq copy_1.referenced_expression
    expect(root_1.referenced_expression.to_s).to eq copy_1.referenced_expression.to_s
    expect(root_1.referenced_expression.object_id).not_to eq copy_1.referenced_expression.object_id

    # regression test
    expect { root_1.clone }.not_to(change { root_1.referenced_expression.object_id })
  end

  specify('Sequence#clone') do
    root = RP.parse(/(a|b)/)
    copy = root.clone

    # regression test
    expect(copy.to_s).to eq root.to_s

    root_seq_op = root[0][0]
    copy_seq_op = copy[0][0]
    root_seq_1 = root[0][0][0]
    copy_seq_1 = copy[0][0][0]

    expect(root_seq_op.object_id).not_to eq copy_seq_op.object_id
    expect(root_seq_1.object_id).not_to eq copy_seq_1.object_id
    copy_seq_1.expressions.each_with_index do |exp, index|
      expect(root_seq_1[index].object_id).not_to eq exp.object_id
    end
  end

  describe('Base#unquantified_clone') do
    it 'produces a clone' do
      root = RP.parse(/^a(b([cde])f)g$/)
      copy = root.unquantified_clone

      expect(copy.to_s).to eq root.to_s

      expect(copy.object_id).not_to eq root.object_id
    end

    it 'does not carry over the callee quantifier' do
      expect(RP.parse(/a{3}/)[0]).to be_quantified
      expect(RP.parse(/a{3}/)[0].unquantified_clone).not_to be_quantified

      expect(RP.parse(/[a]{3}/)[0]).to be_quantified
      expect(RP.parse(/[a]{3}/)[0].unquantified_clone).not_to be_quantified

      expect(RP.parse(/(a|b){3}/)[0]).to be_quantified
      expect(RP.parse(/(a|b){3}/)[0].unquantified_clone).not_to be_quantified
    end

    it 'keeps quantifiers of callee children' do
      expect(RP.parse(/(a{3}){3}/)[0][0]).to be_quantified
      expect(RP.parse(/(a{3}){3}/)[0].unquantified_clone[0]).to be_quantified
    end
  end
end