regexp_parser 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -1
  3. data/Gemfile +1 -1
  4. data/README.md +9 -13
  5. data/lib/regexp_parser/expression.rb +33 -21
  6. data/lib/regexp_parser/expression/classes/backref.rb +18 -10
  7. data/lib/regexp_parser/expression/classes/conditional.rb +4 -0
  8. data/lib/regexp_parser/expression/classes/group.rb +4 -2
  9. data/lib/regexp_parser/expression/classes/keep.rb +1 -3
  10. data/lib/regexp_parser/expression/methods/match_length.rb +172 -0
  11. data/lib/regexp_parser/expression/quantifier.rb +2 -2
  12. data/lib/regexp_parser/expression/sequence.rb +0 -4
  13. data/lib/regexp_parser/expression/subexpression.rb +3 -5
  14. data/lib/regexp_parser/lexer.rb +31 -24
  15. data/lib/regexp_parser/parser.rb +25 -3
  16. data/lib/regexp_parser/syntax/tokens.rb +2 -10
  17. data/lib/regexp_parser/version.rb +1 -1
  18. data/regexp_parser.gemspec +2 -2
  19. data/spec/expression/base_spec.rb +80 -0
  20. data/spec/expression/clone_spec.rb +120 -0
  21. data/spec/expression/conditional_spec.rb +89 -0
  22. data/spec/expression/free_space_spec.rb +27 -0
  23. data/spec/expression/methods/match_length_spec.rb +141 -0
  24. data/spec/expression/methods/strfregexp_spec.rb +224 -0
  25. data/spec/expression/methods/tests_spec.rb +97 -0
  26. data/spec/expression/methods/traverse_spec.rb +140 -0
  27. data/spec/expression/subexpression_spec.rb +50 -0
  28. data/spec/expression/to_h_spec.rb +26 -0
  29. data/spec/expression/to_s_spec.rb +100 -0
  30. data/spec/lexer/all_spec.rb +22 -0
  31. data/{test/lexer/test_conditionals.rb → spec/lexer/conditionals_spec.rb} +31 -35
  32. data/spec/lexer/escapes_spec.rb +38 -0
  33. data/spec/lexer/keep_spec.rb +22 -0
  34. data/{test/lexer/test_literals.rb → spec/lexer/literals_spec.rb} +20 -24
  35. data/{test/lexer/test_nesting.rb → spec/lexer/nesting_spec.rb} +11 -13
  36. data/spec/lexer/refcalls_spec.rb +54 -0
  37. data/spec/parser/all_spec.rb +31 -0
  38. data/spec/parser/alternation_spec.rb +88 -0
  39. data/{test/parser/test_anchors.rb → spec/parser/anchors_spec.rb} +7 -10
  40. data/spec/parser/conditionals_spec.rb +179 -0
  41. data/spec/parser/errors_spec.rb +51 -0
  42. data/spec/parser/escapes_spec.rb +132 -0
  43. data/spec/parser/free_space_spec.rb +130 -0
  44. data/spec/parser/groups_spec.rb +267 -0
  45. data/spec/parser/keep_spec.rb +19 -0
  46. data/spec/parser/posix_classes_spec.rb +27 -0
  47. data/spec/parser/properties_spec.rb +127 -0
  48. data/spec/parser/quantifiers_spec.rb +293 -0
  49. data/spec/parser/refcalls_spec.rb +237 -0
  50. data/spec/parser/set/intersections_spec.rb +127 -0
  51. data/spec/parser/set/ranges_spec.rb +111 -0
  52. data/spec/parser/sets_spec.rb +178 -0
  53. data/{test/parser/test_types.rb → spec/parser/types_spec.rb} +13 -20
  54. data/spec/scanner/all_spec.rb +18 -0
  55. data/{test/scanner/test_anchors.rb → spec/scanner/anchors_spec.rb} +8 -10
  56. data/{test/scanner/test_conditionals.rb → spec/scanner/conditionals_spec.rb} +49 -53
  57. data/spec/scanner/errors_spec.rb +90 -0
  58. data/{test/scanner/test_escapes.rb → spec/scanner/escapes_spec.rb} +8 -10
  59. data/{test/scanner/test_free_space.rb → spec/scanner/free_space_spec.rb} +48 -52
  60. data/{test/scanner/test_groups.rb → spec/scanner/groups_spec.rb} +33 -41
  61. data/spec/scanner/keep_spec.rb +33 -0
  62. data/{test/scanner/test_literals.rb → spec/scanner/literals_spec.rb} +8 -12
  63. data/{test/scanner/test_meta.rb → spec/scanner/meta_spec.rb} +8 -10
  64. data/{test/scanner/test_properties.rb → spec/scanner/properties_spec.rb} +14 -19
  65. data/{test/scanner/test_quantifiers.rb → spec/scanner/quantifiers_spec.rb} +7 -9
  66. data/{test/scanner/test_refcalls.rb → spec/scanner/refcalls_spec.rb} +9 -9
  67. data/{test/scanner/test_scripts.rb → spec/scanner/scripts_spec.rb} +8 -12
  68. data/{test/scanner/test_sets.rb → spec/scanner/sets_spec.rb} +14 -17
  69. data/spec/scanner/types_spec.rb +29 -0
  70. data/spec/scanner/unicode_blocks_spec.rb +28 -0
  71. data/spec/spec_helper.rb +14 -0
  72. data/{test → spec}/support/runner.rb +9 -8
  73. data/{test → spec}/support/warning_extractor.rb +5 -7
  74. data/spec/syntax/syntax_spec.rb +44 -0
  75. data/spec/syntax/syntax_token_map_spec.rb +23 -0
  76. data/spec/syntax/versions/1.8.6_spec.rb +38 -0
  77. data/spec/syntax/versions/1.9.1_spec.rb +23 -0
  78. data/spec/syntax/versions/1.9.3_spec.rb +22 -0
  79. data/spec/syntax/versions/2.0.0_spec.rb +28 -0
  80. data/spec/syntax/versions/2.2.0_spec.rb +22 -0
  81. data/spec/syntax/versions/aliases_spec.rb +119 -0
  82. data/spec/token/token_spec.rb +85 -0
  83. metadata +131 -140
  84. data/test/expression/test_all.rb +0 -12
  85. data/test/expression/test_base.rb +0 -90
  86. data/test/expression/test_clone.rb +0 -89
  87. data/test/expression/test_conditionals.rb +0 -113
  88. data/test/expression/test_free_space.rb +0 -35
  89. data/test/expression/test_set.rb +0 -84
  90. data/test/expression/test_strfregexp.rb +0 -230
  91. data/test/expression/test_subexpression.rb +0 -58
  92. data/test/expression/test_tests.rb +0 -99
  93. data/test/expression/test_to_h.rb +0 -59
  94. data/test/expression/test_to_s.rb +0 -104
  95. data/test/expression/test_traverse.rb +0 -161
  96. data/test/helpers.rb +0 -10
  97. data/test/lexer/test_all.rb +0 -41
  98. data/test/lexer/test_keep.rb +0 -24
  99. data/test/lexer/test_refcalls.rb +0 -56
  100. data/test/parser/set/test_intersections.rb +0 -127
  101. data/test/parser/set/test_ranges.rb +0 -111
  102. data/test/parser/test_all.rb +0 -64
  103. data/test/parser/test_alternation.rb +0 -92
  104. data/test/parser/test_conditionals.rb +0 -187
  105. data/test/parser/test_errors.rb +0 -63
  106. data/test/parser/test_escapes.rb +0 -134
  107. data/test/parser/test_free_space.rb +0 -139
  108. data/test/parser/test_groups.rb +0 -289
  109. data/test/parser/test_keep.rb +0 -21
  110. data/test/parser/test_posix_classes.rb +0 -27
  111. data/test/parser/test_properties.rb +0 -134
  112. data/test/parser/test_quantifiers.rb +0 -301
  113. data/test/parser/test_refcalls.rb +0 -186
  114. data/test/parser/test_sets.rb +0 -179
  115. data/test/scanner/test_all.rb +0 -38
  116. data/test/scanner/test_errors.rb +0 -91
  117. data/test/scanner/test_keep.rb +0 -35
  118. data/test/scanner/test_types.rb +0 -35
  119. data/test/scanner/test_unicode_blocks.rb +0 -30
  120. data/test/support/disable_autotest.rb +0 -8
  121. data/test/syntax/test_all.rb +0 -6
  122. data/test/syntax/test_syntax.rb +0 -61
  123. data/test/syntax/test_syntax_token_map.rb +0 -25
  124. data/test/syntax/versions/test_1.8.rb +0 -55
  125. data/test/syntax/versions/test_1.9.1.rb +0 -36
  126. data/test/syntax/versions/test_1.9.3.rb +0 -32
  127. data/test/syntax/versions/test_2.0.0.rb +0 -37
  128. data/test/syntax/versions/test_2.2.0.rb +0 -32
  129. data/test/syntax/versions/test_aliases.rb +0 -129
  130. data/test/syntax/versions/test_all.rb +0 -5
  131. data/test/test_all.rb +0 -5
  132. data/test/token/test_all.rb +0 -2
  133. data/test/token/test_token.rb +0 -107
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Subexpression#traverse') do
4
+ specify('Subexpression#traverse') do
5
+ root = RP.parse(/a(b(c(d)))|g[h-i]j|klmn/)
6
+
7
+ enters = 0
8
+ visits = 0
9
+ exits = 0
10
+
11
+ root.traverse do |event, _exp, _index|
12
+ enters = (enters + 1) if event == :enter
13
+ visits = (visits + 1) if event == :visit
14
+ exits = (exits + 1) if event == :exit
15
+ end
16
+
17
+ expect(enters).to eq 9
18
+ expect(enters).to eq exits
19
+
20
+ expect(visits).to eq 9
21
+ end
22
+
23
+ specify('Subexpression#traverse including self') do
24
+ root = RP.parse(/a(b(c(d)))|g[h-i]j|klmn/)
25
+
26
+ enters = 0
27
+ visits = 0
28
+ exits = 0
29
+
30
+ root.traverse(true) do |event, _exp, _index|
31
+ enters = (enters + 1) if event == :enter
32
+ visits = (visits + 1) if event == :visit
33
+ exits = (exits + 1) if event == :exit
34
+ end
35
+
36
+ expect(enters).to eq 10
37
+ expect(enters).to eq exits
38
+
39
+ expect(visits).to eq 9
40
+ end
41
+
42
+ specify('Subexpression#walk alias') do
43
+ root = RP.parse(/abc/)
44
+
45
+ expect(root).to respond_to(:walk)
46
+ end
47
+
48
+ specify('Subexpression#each_expression') do
49
+ root = RP.parse(/a(?x:b(c))|g[h-k]/)
50
+
51
+ count = 0
52
+ root.each_expression { count += 1 }
53
+
54
+ expect(count).to eq 13
55
+ end
56
+
57
+ specify('Subexpression#each_expression including self') do
58
+ root = RP.parse(/a(?x:b(c))|g[h-k]/)
59
+
60
+ count = 0
61
+ root.each_expression(true) { count += 1 }
62
+
63
+ expect(count).to eq 14
64
+ end
65
+
66
+ specify('Subexpression#each_expression indices') do
67
+ root = RP.parse(/a(b)c/)
68
+
69
+ indices = []
70
+ root.each_expression { |_exp, index| (indices << index) }
71
+
72
+ expect(indices).to eq [0, 1, 0, 2]
73
+ end
74
+
75
+ specify('Subexpression#each_expression indices including self') do
76
+ root = RP.parse(/a(b)c/)
77
+
78
+ indices = []
79
+ root.each_expression(true) { |_exp, index| (indices << index) }
80
+
81
+ expect(indices).to eq [0, 0, 1, 0, 2]
82
+ end
83
+
84
+ specify('Subexpression#flat_map without block') do
85
+ root = RP.parse(/a(b([c-e]+))?/)
86
+
87
+ array = root.flat_map
88
+
89
+ expect(array).to be_instance_of(Array)
90
+ expect(array.length).to eq 8
91
+
92
+ array.each do |item|
93
+ expect(item).to be_instance_of(Array)
94
+ expect(item.length).to eq 2
95
+ expect(item.first).to be_a(Regexp::Expression::Base)
96
+ expect(item.last).to be_a(Integer)
97
+ end
98
+ end
99
+
100
+ specify('Subexpression#flat_map without block including self') do
101
+ root = RP.parse(/a(b([c-e]+))?/)
102
+
103
+ array = root.flat_map(true)
104
+
105
+ expect(array).to be_instance_of(Array)
106
+ expect(array.length).to eq 9
107
+ end
108
+
109
+ specify('Subexpression#flat_map indices') do
110
+ root = RP.parse(/a(b([c-e]+))?f*g/)
111
+
112
+ indices = root.flat_map { |_exp, index| index }
113
+
114
+ expect(indices).to eq [0, 1, 0, 1, 0, 0, 0, 1, 2, 3]
115
+ end
116
+
117
+ specify('Subexpression#flat_map indices including self') do
118
+ root = RP.parse(/a(b([c-e]+))?f*g/)
119
+
120
+ indices = root.flat_map(true) { |_exp, index| index }
121
+
122
+ expect(indices).to eq [0, 0, 1, 0, 1, 0, 0, 0, 1, 2, 3]
123
+ end
124
+
125
+ specify('Subexpression#flat_map expressions') do
126
+ root = RP.parse(/a(b(c(d)))/)
127
+
128
+ levels = root.flat_map { |exp, _index| [exp.level, exp.text] if exp.terminal? }.compact
129
+
130
+ expect(levels).to eq [[0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']]
131
+ end
132
+
133
+ specify('Subexpression#flat_map expressions including self') do
134
+ root = RP.parse(/a(b(c(d)))/)
135
+
136
+ levels = root.flat_map(true) { |exp, _index| [exp.level, exp.to_s] }.compact
137
+
138
+ expect(levels).to eq [[nil, 'a(b(c(d)))'], [0, 'a'], [0, '(b(c(d)))'], [1, 'b'], [1, '(c(d))'], [2, 'c'], [2, '(d)'], [3, 'd']]
139
+ end
140
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe(Regexp::Expression::Subexpression) do
4
+ specify('#ts, #te') do
5
+ regx = /abcd|ghij|klmn|pqur/
6
+ root = RP.parse(regx)
7
+
8
+ alt = root.first
9
+
10
+ { 0 => [0, 4], 1 => [5, 9], 2 => [10, 14], 3 => [15, 19] }.each do |index, span|
11
+ sequence = alt[index]
12
+
13
+ expect(sequence.ts).to eq span[0]
14
+ expect(sequence.te).to eq span[1]
15
+ end
16
+ end
17
+
18
+ specify('#nesting_level') do
19
+ root = RP.parse(/a(b(\d|[ef-g[h]]))/)
20
+
21
+ tests = {
22
+ 'a' => 1,
23
+ 'b' => 2,
24
+ '\d|[ef-g[h]]' => 3, # alternation
25
+ '\d' => 4, # first alternative
26
+ '[ef-g[h]]' => 4, # second alternative
27
+ 'e' => 5,
28
+ 'f-g' => 5,
29
+ 'f' => 6,
30
+ 'g' => 6,
31
+ 'h' => 6,
32
+ }
33
+
34
+ root.each_expression do |exp|
35
+ next unless expected_nesting_level = tests.delete(exp.to_s)
36
+ expect(expected_nesting_level).to eq exp.nesting_level
37
+ end
38
+
39
+ expect(tests).to be_empty
40
+ end
41
+
42
+ specify('#dig') do
43
+ root = RP.parse(/(((a)))/)
44
+
45
+ expect(root.dig(0).to_s).to eq '(((a)))'
46
+ expect(root.dig(0, 0, 0, 0).to_s).to eq 'a'
47
+ expect(root.dig(0, 0, 0, 0, 0)).to be_nil
48
+ expect(root.dig(3, 7)).to be_nil
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Expression#to_h') do
4
+ specify('Root#to_h') do
5
+ root = RP.parse('abc')
6
+
7
+ hash = root.to_h
8
+
9
+ expect(token: :root, type: :expression, text: 'abc', starts_at: 0, length: 3, quantifier: nil, options: {}, level: nil, set_level: nil, conditional_level: nil, expressions: [{ token: :literal, type: :literal, text: 'abc', starts_at: 0, length: 3, quantifier: nil, options: {}, level: 0, set_level: 0, conditional_level: 0 }]).to eq hash
10
+ end
11
+
12
+ specify('Quantifier#to_h') do
13
+ root = RP.parse('a{2,4}')
14
+ exp = root.expressions.at(0)
15
+
16
+ hash = exp.quantifier.to_h
17
+
18
+ expect(max: 4, min: 2, mode: :greedy, text: '{2,4}', token: :interval).to eq hash
19
+ end
20
+
21
+ specify('Conditional#to_h') do
22
+ root = RP.parse('(?<A>a)(?(<A>)b|c)', 'ruby/2.0')
23
+
24
+ expect { root.to_h }.not_to(raise_error)
25
+ end
26
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Expression#to_s') do
4
+ specify('literal alternation') do
5
+ pattern = 'abcd|ghij|klmn|pqur'
6
+
7
+ expect(RP.parse(pattern).to_s).to eq pattern
8
+ end
9
+
10
+ specify('quantified alternations') do
11
+ pattern = '(?:a?[b]+(c){2}|d+[e]*(f)?)|(?:g+[h]?(i){2,3}|j*[k]{3,5}(l)?)'
12
+
13
+ expect(RP.parse(pattern).to_s).to eq pattern
14
+ end
15
+
16
+ specify('quantified sets') do
17
+ pattern = '[abc]+|[^def]{3,6}'
18
+
19
+ expect(RP.parse(pattern).to_s).to eq pattern
20
+ end
21
+
22
+ specify('property sets') do
23
+ pattern = '[\\a\\b\\p{Lu}\\P{Z}\\c\\d]+'
24
+
25
+ expect(RP.parse(pattern, 'ruby/1.9').to_s).to eq pattern
26
+ end
27
+
28
+ specify('groups') do
29
+ pattern = "(a(?>b(?:c(?<n>d(?'N'e)??f)+g)*+h)*i)++"
30
+
31
+ expect(RP.parse(pattern, 'ruby/1.9').to_s).to eq pattern
32
+ end
33
+
34
+ specify('assertions') do
35
+ pattern = '(a+(?=b+(?!c+(?<=d+(?<!e+)?f+)?g+)?h+)?i+)?'
36
+
37
+ expect(RP.parse(pattern, 'ruby/1.9').to_s).to eq pattern
38
+ end
39
+
40
+ specify('comments') do
41
+ pattern = '(?#start)a(?#middle)b(?#end)'
42
+
43
+ expect(RP.parse(pattern).to_s).to eq pattern
44
+ end
45
+
46
+ specify('options') do
47
+ pattern = '(?mix:start)a(?-mix:middle)b(?i-mx:end)'
48
+
49
+ expect(RP.parse(pattern).to_s).to eq pattern
50
+ end
51
+
52
+ specify('url') do
53
+ pattern = ('(^$)|(^(http|https):\\/\\/[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*' + '\\.[a-z]{2,5}(([0-9]{1,5})?\\/.*)?$)')
54
+
55
+ expect(RP.parse(pattern).to_s).to eq pattern
56
+ end
57
+
58
+ specify('multiline source') do
59
+ multiline = /
60
+ \A
61
+ a? # One letter
62
+ b{2,5} # Another one
63
+ [c-g]+ # A set
64
+ \z
65
+ /x
66
+
67
+ expect(RP.parse(multiline).to_s).to eq multiline.source
68
+ end
69
+
70
+ specify('multiline #to_s') do
71
+ multiline = /
72
+ \A
73
+ a? # One letter
74
+ b{2,5} # Another one
75
+ [c-g]+ # A set
76
+ \z
77
+ /x
78
+
79
+ expect(RP.parse(multiline.to_s).to_s).to eq multiline.to_s
80
+ end
81
+
82
+ # Free spacing expressions that use spaces between quantifiers and their
83
+ # targets do not produce identical results due to the way quantifiers are
84
+ # applied to expressions (members, not nodes) and the merging of consecutive
85
+ # space nodes. This tests that they produce equivalent results.
86
+ specify('multiline equivalence') do
87
+ multiline = /
88
+ \A
89
+ a ? # One letter
90
+ b {2,5} # Another one
91
+ [c-g] + # A set
92
+ \z
93
+ /x
94
+
95
+ str = 'bbbcged'
96
+ root = RP.parse(multiline)
97
+
98
+ expect(Regexp.new(root.to_s, Regexp::EXTENDED).match(str)[0]).to eq multiline.match(str)[0]
99
+ end
100
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe(Regexp::Lexer) do
4
+ specify('lexer returns an array') do
5
+ expect(RL.lex('abc')).to be_instance_of(Array)
6
+ end
7
+
8
+ specify('lexer returns tokens') do
9
+ tokens = RL.lex('^abc+[^one]{2,3}\\b\\d\\\\C-C$')
10
+ expect(tokens).to all(be_a Regexp::Token)
11
+ expect(tokens.map { |token| token.to_a.length }).to all(eq 8)
12
+ end
13
+
14
+ specify('lexer token count') do
15
+ tokens = RL.lex(/^(one|two){2,3}([^d\]efm-qz\,\-]*)(ghi)+$/i)
16
+ expect(tokens.length).to eq 28
17
+ end
18
+
19
+ specify('lexer scan alias') do
20
+ expect(RL.scan(/a|b|c/)).to eq RL.lex(/a|b|c/)
21
+ end
22
+ end
@@ -1,8 +1,6 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class LexerConditionals < Test::Unit::TestCase
4
- if RUBY_VERSION >= '2.0'
1
+ require 'spec_helper'
5
2
 
3
+ RSpec.describe('Conditional lexing') do
6
4
  # Basic lexer output and nesting tests
7
5
  tests = {
8
6
  '(?<A>a)(?(<A>)b|c)' => [3, :conditional, :open, '(?', 7, 9, 0, 0, 0],
@@ -12,23 +10,23 @@ class LexerConditionals < Test::Unit::TestCase
12
10
  }
13
11
 
14
12
  tests.each_with_index do |(pattern, (index, type, token, text, ts, te, level, set_level, conditional_level)), count|
15
- define_method "test_lexer_#{type}_#{token}_#{count}" do
13
+ specify("lexer_#{type}_#{token}_#{count}") do
16
14
  tokens = RL.lex(pattern)
17
15
  struct = tokens.at(index)
18
16
 
19
- assert_equal type, struct.type
20
- assert_equal token, struct.token
21
- assert_equal text, struct.text
22
- assert_equal ts, struct.ts
23
- assert_equal te, struct.te
24
- assert_equal level, struct.level
25
- assert_equal set_level, struct.set_level
26
- assert_equal conditional_level, struct.conditional_level
17
+ expect(struct.type).to eq type
18
+ expect(struct.token).to eq token
19
+ expect(struct.text).to eq text
20
+ expect(struct.ts).to eq ts
21
+ expect(struct.te).to eq te
22
+ expect(struct.level).to eq level
23
+ expect(struct.set_level).to eq set_level
24
+ expect(struct.conditional_level).to eq conditional_level
27
25
  end
28
26
  end
29
27
 
30
- def test_lexer_conditional_mixed_nesting
31
- regexp = /((?<A>a)(?<B>(?(<A>)b|((?(<B>)[e-g]|[h-j])))))/
28
+ specify('lexer conditional mixed nesting') do
29
+ regexp = '((?<A>a)(?<B>(?(<A>)b|((?(<B>)[e-g]|[h-j])))))'
32
30
  tokens = RL.lex(regexp)
33
31
 
34
32
  [
@@ -57,19 +55,19 @@ class LexerConditionals < Test::Unit::TestCase
57
55
  ].each do |index, type, token, text, ts, te, level, set_level, conditional_level|
58
56
  struct = tokens.at(index)
59
57
 
60
- assert_equal type, struct.type
61
- assert_equal token, struct.token
62
- assert_equal text, struct.text
63
- assert_equal ts, struct.ts
64
- assert_equal te, struct.te
65
- assert_equal level, struct.level
66
- assert_equal set_level, struct.set_level
67
- assert_equal conditional_level, struct.conditional_level
58
+ expect(struct.type).to eq type
59
+ expect(struct.token).to eq token
60
+ expect(struct.text).to eq text
61
+ expect(struct.ts).to eq ts
62
+ expect(struct.te).to eq te
63
+ expect(struct.level).to eq level
64
+ expect(struct.set_level).to eq set_level
65
+ expect(struct.conditional_level).to eq conditional_level
68
66
  end
69
67
  end
70
68
 
71
- def test_lexer_conditional_deep_nesting
72
- regexp = /(a(b(c)))(?(1)(?(2)(?(3)d|e))|(?(3)(?(2)f|g)|(?(1)f|g)))/
69
+ specify('lexer conditional deep nesting') do
70
+ regexp = '(a(b(c)))(?(1)(?(2)(?(3)d|e))|(?(3)(?(2)f|g)|(?(1)f|g)))'
73
71
  tokens = RL.lex(regexp)
74
72
 
75
73
  [
@@ -112,16 +110,14 @@ class LexerConditionals < Test::Unit::TestCase
112
110
  ].each do |index, type, token, text, ts, te, level, set_level, conditional_level|
113
111
  struct = tokens.at(index)
114
112
 
115
- assert_equal type, struct.type
116
- assert_equal token, struct.token
117
- assert_equal text, struct.text
118
- assert_equal ts, struct.ts
119
- assert_equal te, struct.te
120
- assert_equal level, struct.level
121
- assert_equal set_level, struct.set_level
122
- assert_equal conditional_level, struct.conditional_level
113
+ expect(struct.type).to eq type
114
+ expect(struct.token).to eq token
115
+ expect(struct.text).to eq text
116
+ expect(struct.ts).to eq ts
117
+ expect(struct.te).to eq te
118
+ expect(struct.level).to eq level
119
+ expect(struct.set_level).to eq set_level
120
+ expect(struct.conditional_level).to eq conditional_level
123
121
  end
124
122
  end
125
-
126
- end # if RUBY_VERSION >= '2.0'
127
123
  end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Escape lexing') do
4
+ tests = {
5
+ '\u{62}' => {
6
+ 0 => [:escape, :codepoint_list, '\u{62}', 0, 6, 0, 0, 0],
7
+ },
8
+
9
+ '\u{62 63 64}' => {
10
+ 0 => [:escape, :codepoint_list, '\u{62 63 64}', 0, 12, 0, 0, 0],
11
+ },
12
+
13
+ '\u{62 63 64}+' => {
14
+ 0 => [:escape, :codepoint_list, '\u{62 63}', 0, 9, 0, 0, 0],
15
+ 1 => [:escape, :codepoint_list, '\u{64}', 9, 15, 0, 0, 0],
16
+ 2 => [:quantifier, :one_or_more, '+', 15, 16, 0, 0, 0],
17
+ },
18
+ }
19
+
20
+ tests.each_with_index do |(pattern, checks), count|
21
+ specify("lex_escape_runs_#{count}") do
22
+ tokens = RL.lex(pattern)
23
+
24
+ checks.each do |index, (type, token, text, ts, te, level, set_level, conditional_level)|
25
+ struct = tokens.at(index)
26
+
27
+ expect(struct.type).to eq type
28
+ expect(struct.token).to eq token
29
+ expect(struct.text).to eq text
30
+ expect(struct.ts).to eq ts
31
+ expect(struct.te).to eq te
32
+ expect(struct.level).to eq level
33
+ expect(struct.set_level).to eq set_level
34
+ expect(struct.conditional_level).to eq conditional_level
35
+ end
36
+ end
37
+ end
38
+ end