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
@@ -1,58 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class ExpressionSubexpression < Test::Unit::TestCase
4
-
5
- def test_subexpression_ts_te
6
- regx = /abcd|ghij|klmn|pqur/
7
- root = RP.parse(regx)
8
-
9
- alt = root.first
10
-
11
- {
12
- 0 => [ 0, 4 ],
13
- 1 => [ 5, 9 ],
14
- 2 => [ 10, 14 ],
15
- 3 => [ 15, 19 ],
16
- }.each do |index, span|
17
- sequence = alt[index]
18
-
19
- assert_equal span[0], sequence.ts
20
- assert_equal span[1], sequence.te
21
- end
22
- end
23
-
24
- def test_subexpression_nesting_level
25
- root = RP.parse(/a(b(c\d|[ef-g[h]]))/)
26
-
27
- tests = {
28
- 'a' => 1,
29
- 'b' => 2,
30
- '|' => 3,
31
- 'c\d' => 4, # first alternative
32
- 'c' => 5,
33
- '\d' => 5,
34
- '[ef-g[h]]' => 4, # second alternative
35
- 'e' => 5,
36
- '-' => 5,
37
- 'f' => 6,
38
- 'g' => 6,
39
- 'h' => 6,
40
- }
41
-
42
- root.each_expression do |exp|
43
- next unless (expected_nesting_level = tests.delete(exp.text))
44
- assert_equal exp.nesting_level, expected_nesting_level
45
- end
46
-
47
- assert tests.empty?
48
- end
49
-
50
- def test_subexpression_dig
51
- root = RP.parse(/(((a)))/)
52
-
53
- assert_equal '(((a)))', root.dig(0).to_s
54
- assert_equal 'a', root.dig(0, 0, 0, 0).to_s
55
- assert_nil root.dig(0, 0, 0, 0, 0)
56
- assert_nil root.dig(3, 7)
57
- end
58
- end
@@ -1,99 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class ExpressionTests < Test::Unit::TestCase
4
-
5
- def test_expression_type?
6
- root = RP.parse(/abcd|(ghij)|[klmn]/)
7
-
8
- alt = root.first
9
-
10
- assert_equal true, alt.type?(:meta)
11
- assert_equal false, alt.type?(:escape)
12
- assert_equal true, alt.type?([:meta, :escape])
13
- assert_equal false, alt.type?([:literal, :escape])
14
- assert_equal true, alt.type?(:*)
15
- assert_equal true, alt.type?([:*])
16
- assert_equal true, alt.type?([:literal, :escape, :*])
17
-
18
- seq_1 = alt[0]
19
- assert_equal true, seq_1.type?(:expression)
20
- assert_equal true, seq_1.first.type?(:literal)
21
-
22
- seq_2 = alt[1]
23
- assert_equal true, seq_2.type?(:*)
24
- assert_equal true, seq_2.first.type?(:group)
25
-
26
- seq_3 = alt[2]
27
- assert_equal true, seq_3.first.type?(:set)
28
- end
29
-
30
- def test_expression_is?
31
- root = RP.parse(/.+|\.?/)
32
-
33
- assert_equal true, root.is?(:*)
34
-
35
- alt = root.first
36
- assert_equal true, alt.is?(:*)
37
- assert_equal true, alt.is?(:alternation)
38
- assert_equal true, alt.is?(:alternation, :meta)
39
-
40
- seq_1 = alt[0]
41
- assert_equal true, seq_1.is?(:sequence)
42
- assert_equal true, seq_1.is?(:sequence, :expression)
43
-
44
- assert_equal true, seq_1.first.is?(:dot)
45
- assert_equal false, seq_1.first.is?(:dot, :escape)
46
- assert_equal true, seq_1.first.is?(:dot, :meta)
47
- assert_equal true, seq_1.first.is?(:dot, [:escape, :meta])
48
-
49
- seq_2 = alt[1]
50
- assert_equal true, seq_2.first.is?(:dot)
51
- assert_equal true, seq_2.first.is?(:dot, :escape)
52
- assert_equal false, seq_2.first.is?(:dot, :meta)
53
- assert_equal true, seq_2.first.is?(:dot, [:meta, :escape])
54
- end
55
-
56
- def test_expression_one_of?
57
- root = RP.parse(/\Aab(c[\w])d|e.\z/)
58
-
59
- assert_equal true, root.one_of?(:*)
60
- assert_equal true, root.one_of?(:* => :*)
61
- assert_equal true, root.one_of?(:* => [:*])
62
-
63
- alt = root.first
64
- assert_equal true, alt.one_of?(:*)
65
- assert_equal true, alt.one_of?(:meta)
66
- assert_equal true, alt.one_of?(:meta, :alternation)
67
- assert_equal false, alt.one_of?(:meta => [:dot, :bogus])
68
- assert_equal true, alt.one_of?(:meta => [:dot, :alternation])
69
-
70
- seq_1 = alt[0]
71
- assert_equal true, seq_1.one_of?(:expression)
72
- assert_equal true, seq_1.one_of?(:expression => :sequence)
73
-
74
- assert_equal true, seq_1.first.one_of?(:anchor)
75
- assert_equal true, seq_1.first.one_of?(:anchor => :bos)
76
- assert_equal false, seq_1.first.one_of?(:anchor => :eos)
77
- assert_equal true, seq_1.first.one_of?(:anchor => [:escape, :meta, :bos])
78
- assert_equal false, seq_1.first.one_of?(:anchor => [:escape, :meta, :eos])
79
-
80
- seq_2 = alt[1]
81
- assert_equal true, seq_2.first.one_of?(:literal)
82
-
83
- assert_equal true, seq_2[1].one_of?(:meta)
84
- assert_equal true, seq_2[1].one_of?(:meta => :dot)
85
- assert_equal false, seq_2[1].one_of?(:meta => :alternation)
86
- assert_equal true, seq_2[1].one_of?(:meta => [:dot])
87
-
88
- assert_equal false, seq_2.last.one_of?(:group)
89
- assert_equal false, seq_2.last.one_of?(:group => [:*])
90
- assert_equal false, seq_2.last.one_of?(:group => [:*], :meta => :*)
91
-
92
- assert_equal true, seq_2.last.one_of?(:meta => [:*], :* => :*)
93
- assert_equal true, seq_2.last.one_of?(:meta => [:*], :anchor => :*)
94
- assert_equal true, seq_2.last.one_of?(:meta => [:*], :anchor => :eos)
95
- assert_equal false, seq_2.last.one_of?(:meta => [:*], :anchor => [:bos])
96
- assert_equal true, seq_2.last.one_of?(:meta => [:*], :anchor => [:bos, :eos])
97
- end
98
-
99
- end
@@ -1,59 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class ExpressionToH < Test::Unit::TestCase
4
-
5
- def test_expression_to_h
6
- root = RP.parse('abc')
7
-
8
- hash = root.to_h
9
-
10
- assert_equal hash, {
11
- :token => :root,
12
- :type => :expression,
13
- :text => 'abc',
14
- :starts_at => 0,
15
- :length => 3,
16
- :quantifier => nil,
17
- :options => {},
18
- :level => nil,
19
- :set_level => nil,
20
- :conditional_level => nil,
21
- :expressions => [
22
- {
23
- :token => :literal,
24
- :type => :literal,
25
- :text => 'abc',
26
- :starts_at => 0,
27
- :length => 3,
28
- :quantifier => nil,
29
- :options => {},
30
- :level => 0,
31
- :set_level => 0,
32
- :conditional_level => 0
33
- }
34
- ]
35
- }
36
- end
37
-
38
- def test_expression_quantifier_to_h
39
- root = RP.parse('a{2,4}')
40
- exp = root.expressions.at(0)
41
-
42
- hash = exp.quantifier.to_h
43
-
44
- assert_equal hash, {
45
- :max => 4,
46
- :min => 2,
47
- :mode => :greedy,
48
- :text => '{2,4}',
49
- :token => :interval
50
- }
51
- end
52
-
53
- # regression test
54
- def test_conditional_expression_to_h
55
- root = RP.parse('(?<A>a)(?(<A>)b|c)', 'ruby/2.0')
56
-
57
- assert_nothing_raised { root.to_h }
58
- end
59
- end
@@ -1,104 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class ExpressionToS < Test::Unit::TestCase
4
-
5
- def test_expression_to_s_literal_alternation
6
- pattern = 'abcd|ghij|klmn|pqur'
7
-
8
- assert_equal pattern, RP.parse(pattern).to_s
9
- end
10
-
11
- def test_expression_to_s_quantified_alternations
12
- pattern = '(?:a?[b]+(c){2}|d+[e]*(f)?)|(?:g+[h]?(i){2,3}|j*[k]{3,5}(l)?)'
13
-
14
- assert_equal pattern, RP.parse(pattern).to_s
15
- end
16
-
17
- def test_expression_to_s_quantified_sets
18
- pattern = '[abc]+|[^def]{3,6}'
19
-
20
- assert_equal pattern, RP.parse(pattern).to_s
21
- end
22
-
23
- def test_expression_to_s_property_sets
24
- pattern = '[\a\b\p{Lu}\P{Z}\c\d]+'
25
-
26
- assert_equal pattern, RP.parse(pattern, 'ruby/1.9').to_s
27
- end
28
-
29
- def test_expression_to_s_groups
30
- pattern = "(a(?>b(?:c(?<n>d(?'N'e)??f)+g)*+h)*i)++"
31
-
32
- assert_equal pattern, RP.parse(pattern, 'ruby/1.9').to_s
33
- end
34
-
35
- def test_expression_to_s_assertions
36
- pattern = '(a+(?=b+(?!c+(?<=d+(?<!e+)?f+)?g+)?h+)?i+)?'
37
-
38
- assert_equal pattern, RP.parse(pattern, 'ruby/1.9').to_s
39
- end
40
-
41
- def test_expression_to_s_comments
42
- pattern = '(?#start)a(?#middle)b(?#end)'
43
-
44
- assert_equal pattern, RP.parse(pattern).to_s
45
- end
46
-
47
- def test_expression_to_s_options
48
- pattern = '(?mix:start)a(?-mix:middle)b(?i-mx:end)'
49
-
50
- assert_equal pattern, RP.parse(pattern).to_s
51
- end
52
-
53
- def test_expression_to_s_url
54
- pattern = '(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*'+
55
- '\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)'
56
-
57
- assert_equal pattern, RP.parse(pattern).to_s
58
- end
59
-
60
- def test_expression_to_s_multiline_source
61
- multiline = %r{
62
- \A
63
- a? # One letter
64
- b{2,5} # Another one
65
- [c-g]+ # A set
66
- \z
67
- }x
68
-
69
- assert_equal multiline.source, RP.parse(multiline).to_s
70
- end
71
-
72
- def test_expression_to_s_multiline_to_s
73
- multiline = %r{
74
- \A
75
- a? # One letter
76
- b{2,5} # Another one
77
- [c-g]+ # A set
78
- \z
79
- }x
80
-
81
- assert_equal multiline.to_s, RP.parse(multiline.to_s).to_s
82
- end
83
-
84
- # Free spacing expressions that use spaces between quantifiers and their
85
- # targets do not produce identical results due to the way quantifiers are
86
- # applied to expressions (members, not nodes) and the merging of consecutive
87
- # space nodes. This tests that they produce equivalent results.
88
- def test_expression_to_s_multiline_equivalence
89
- multiline = %r{
90
- \A
91
- a ? # One letter
92
- b {2,5} # Another one
93
- [c-g] + # A set
94
- \z
95
- }x
96
-
97
- str = 'bbbcged'
98
- root = RP.parse(multiline)
99
-
100
- assert_equal multiline.match(str)[0],
101
- Regexp.new(root.to_s, Regexp::EXTENDED).match(str)[0]
102
- end
103
-
104
- end
@@ -1,161 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class SubexpressionTraverse < Test::Unit::TestCase
4
-
5
- def test_subexpression_traverse
6
- root = RP.parse(/a(b(c(d)))|g[h-i]j|klmn/)
7
-
8
- enters = 0
9
- visits = 0
10
- exits = 0
11
-
12
- root.traverse {|event, exp, index|
13
- enters += 1 if event == :enter
14
- visits += 1 if event == :visit
15
- exits += 1 if event == :exit
16
- }
17
-
18
- assert_equal 9, enters
19
- assert_equal exits, enters
20
-
21
- assert_equal 9, visits
22
- end
23
-
24
- def test_subexpression_traverse_include_self
25
- root = RP.parse(/a(b(c(d)))|g[h-i]j|klmn/)
26
-
27
- enters = 0
28
- visits = 0
29
- exits = 0
30
-
31
- root.traverse(true) {|event, exp, index|
32
- enters += 1 if event == :enter
33
- visits += 1 if event == :visit
34
- exits += 1 if event == :exit
35
- }
36
-
37
- assert_equal 10, enters
38
- assert_equal exits, enters
39
-
40
- assert_equal 9, visits
41
- end
42
-
43
- def test_subexpression_walk_alias
44
- root = RP.parse(/abc/)
45
-
46
- assert_equal true, root.respond_to?(:walk)
47
- end
48
-
49
- def test_subexpression_each_expression
50
- root = RP.parse(/a(?x:b(c))|g[h-k]/)
51
-
52
- count = 0
53
- root.each_expression {|exp, index|
54
- count += 1
55
- }
56
-
57
- assert_equal 13, count
58
- end
59
-
60
- def test_subexpression_each_expression_include_self
61
- root = RP.parse(/a(?x:b(c))|g[h-k]/)
62
-
63
- count = 0
64
- root.each_expression(true) {|exp, index|
65
- count += 1
66
- }
67
-
68
- assert_equal 14, count
69
- end
70
-
71
- def test_subexpression_each_expression_indices
72
- root = RP.parse(/a(b)c/)
73
-
74
- indices = []
75
- root.each_expression {|exp, index| indices << index}
76
-
77
- assert_equal [0, 1, 0, 2], indices
78
- end
79
-
80
- def test_subexpression_each_expression_indices_include_self
81
- root = RP.parse(/a(b)c/)
82
-
83
- indices = []
84
- root.each_expression(true) {|exp, index| indices << index}
85
-
86
- assert_equal [0, 0, 1, 0, 2], indices
87
- end
88
-
89
- def test_subexpression_flat_map_without_block
90
- root = RP.parse(/a(b([c-e]+))?/)
91
-
92
- array = root.flat_map
93
-
94
- assert_equal Array, array.class
95
- assert_equal 8, array.length
96
-
97
- array.each do |item|
98
- assert_equal Array, item.class
99
- assert_equal 2, item.length
100
- assert_equal true, item.first.is_a?(Regexp::Expression::Base)
101
- assert_equal true, item.last.is_a?(Integer)
102
- end
103
- end
104
-
105
- def test_subexpression_flat_map_without_block_include_self
106
- root = RP.parse(/a(b([c-e]+))?/)
107
-
108
- array = root.flat_map(true)
109
-
110
- assert_equal Array, array.class
111
- assert_equal 9, array.length
112
- end
113
-
114
- def test_subexpression_flat_map_indices
115
- root = RP.parse(/a(b([c-e]+))?f*g/)
116
-
117
- indices = root.flat_map {|exp, index| index}
118
-
119
- assert_equal [0, 1, 0, 1, 0, 0, 0, 1, 2, 3], indices
120
- end
121
-
122
- def test_subexpression_flat_map_indices_include_self
123
- root = RP.parse(/a(b([c-e]+))?f*g/)
124
-
125
- indices = root.flat_map(true) {|exp, index| index}
126
-
127
- assert_equal [0, 0, 1, 0, 1, 0, 0, 0, 1, 2, 3], indices
128
- end
129
-
130
- def test_subexpression_flat_map_expressions
131
- root = RP.parse(/a(b(c(d)))/)
132
-
133
- levels = root.flat_map {|exp, index|
134
- [exp.level, exp.text] if exp.terminal?
135
- }.compact
136
-
137
- assert_equal [
138
- [0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']
139
- ], levels
140
- end
141
-
142
- def test_subexpression_flat_map_expressions_include_self
143
- root = RP.parse(/a(b(c(d)))/)
144
-
145
- levels = root.flat_map(true) {|exp, index|
146
- [exp.level, exp.to_s]
147
- }.compact
148
-
149
- assert_equal [
150
- [nil, 'a(b(c(d)))'],
151
- [0, 'a'],
152
- [0, '(b(c(d)))'],
153
- [1, 'b'],
154
- [1, '(c(d))'],
155
- [2, 'c'],
156
- [2, '(d)'],
157
- [3, 'd']
158
- ], levels
159
- end
160
-
161
- end