re2 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ require "spec_helper"
2
+ require "re2/string"
3
+
4
+ class String
5
+ include RE2::String
6
+ end
7
+
8
+ describe RE2::String do
9
+ describe "#re2_sub" do
10
+ it "delegates to RE2.Replace to perform replacement" do
11
+ "My name is Robert Paulson".re2_sub('Robert', 'Crobert').must_equal("My name is Crobert Paulson")
12
+ end
13
+ end
14
+
15
+ describe "#re2_gsub" do
16
+ it "delegates to RE2.GlobalReplace to perform replacement" do
17
+ "My name is Robert Paulson".re2_gsub('a', 'e').must_equal("My neme is Robert Peulson")
18
+ end
19
+ end
20
+
21
+ describe "#re2_match" do
22
+ it "delegates to RE2::Regexp#match to perform matches" do
23
+ md = "My name is Robert Paulson".re2_match('My name is (\S+) (\S+)')
24
+ md.must_be_instance_of(RE2::MatchData)
25
+ md[0].must_equal("My name is Robert Paulson")
26
+ md[1].must_equal("Robert")
27
+ md[2].must_equal("Paulson")
28
+ end
29
+
30
+ it "supports limiting the number of matches" do
31
+ md = "My name is Robert Paulson".re2_match('My name is (\S+) (\S+)', 0)
32
+ md.must_equal(true)
33
+ end
34
+ end
35
+
36
+ describe "#re2_escape" do
37
+ it "escapes the string for use in regular expressions" do
38
+ "1.5-2.0?".re2_escape.must_equal('1\.5\-2\.0\?')
39
+ end
40
+ end
41
+
42
+ describe "#re2_quote" do
43
+ it "escapes the string for use in regular expressions" do
44
+ "1.5-2.0?".re2_quote.must_equal('1\.5\-2\.0\?')
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+
3
+ describe RE2 do
4
+ describe "#Replace" do
5
+ it "only replaces the first occurrence of the pattern" do
6
+ RE2.Replace("woo", "o", "a").must_equal("wao")
7
+ end
8
+
9
+ it "performs replacement based on regular expressions" do
10
+ RE2.Replace("woo", "o+", "e").must_equal("we")
11
+ end
12
+
13
+ it "supports flags in patterns" do
14
+ RE2.Replace("Good morning", "(?i)gOOD MORNING", "hi").must_equal("hi")
15
+ end
16
+
17
+ it "performs replacements in-place" do
18
+ name = "Robert"
19
+ replacement = RE2.Replace(name, "R", "Cr")
20
+ replacement.must_equal("Crobert")
21
+ name.must_be_same_as(replacement)
22
+ end
23
+
24
+ it "supports passing an RE2::Regexp as the pattern" do
25
+ re = RE2::Regexp.new('wo{2}')
26
+ RE2.Replace("woo", re, "miaow").must_equal("miaow")
27
+ end
28
+
29
+ it "respects any passed RE2::Regexp's flags" do
30
+ re = RE2::Regexp.new('gOOD MORNING', :case_sensitive => false)
31
+ RE2.Replace("Good morning", re, "hi").must_equal("hi")
32
+ end
33
+
34
+ it "raises an error if the string is frozen" do
35
+ frozen_name = "Arnold".freeze
36
+
37
+ proc { RE2.Replace(frozen_name, "o", "a") }.must_raise(TypeError, RuntimeError)
38
+ end
39
+ end
40
+
41
+ describe "#GlobalReplace" do
42
+ it "replaces every occurrence of a pattern" do
43
+ RE2.GlobalReplace("woo", "o", "a").must_equal("waa")
44
+ end
45
+
46
+ it "performs replacement based on regular expressions" do
47
+ RE2.GlobalReplace("woohoo", "o+", "e").must_equal("wehe")
48
+ end
49
+
50
+ it "supports flags in patterns" do
51
+ RE2.GlobalReplace("Robert", "(?i)r", "w").must_equal("wobewt")
52
+ end
53
+
54
+ it "performs replacement in-place" do
55
+ name = "Robert"
56
+ replacement = RE2.GlobalReplace(name, "(?i)R", "w")
57
+ replacement.must_equal("wobewt")
58
+ name.must_be_same_as(replacement)
59
+ end
60
+
61
+ it "supports passing an RE2::Regexp as the pattern" do
62
+ re = RE2::Regexp.new('wo{2,}')
63
+ RE2.GlobalReplace("woowooo", re, "miaow").must_equal("miaowmiaow")
64
+ end
65
+
66
+ it "respects any passed RE2::Regexp's flags" do
67
+ re = RE2::Regexp.new('gOOD MORNING', :case_sensitive => false)
68
+ RE2.GlobalReplace("Good morning Good morning", re, "hi").must_equal("hi hi")
69
+ end
70
+
71
+ it "raises an error if the string is frozen" do
72
+ frozen_name = "Arnold".freeze
73
+
74
+ proc { RE2.GlobalReplace(frozen_name, "o", "a") }.must_raise(TypeError, RuntimeError)
75
+ end
76
+ end
77
+
78
+ describe "#QuoteMeta" do
79
+ it "escapes a string so it can be used as a regular expression" do
80
+ RE2.QuoteMeta("1.5-2.0?").must_equal('1\.5\-2\.0\?')
81
+ end
82
+ end
83
+ end
84
+
@@ -0,0 +1,3 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require "re2"
3
+ require "minitest/autorun"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: re2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-27 00:00:00.000000000 Z
12
+ date: 2012-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake-compiler
16
- requirement: &70320711075760 !ruby/object:Gem::Requirement
16
+ requirement: &70129597606260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,18 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70320711075760
24
+ version_requirements: *70129597606260
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &70129597605760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70129597605760
25
36
  description: Ruby bindings to re2, "an efficient, principled regular expression library".
26
37
  email: ruby.re2@librelist.com
27
38
  executables: []
@@ -31,10 +42,16 @@ extra_rdoc_files: []
31
42
  files:
32
43
  - ext/re2/extconf.rb
33
44
  - ext/re2/re2.cc
45
+ - lib/re2/string.rb
34
46
  - LICENSE.txt
35
47
  - README.md
36
48
  - Rakefile
37
- - test/re2_test.rb
49
+ - spec/spec_helper.rb
50
+ - spec/re2_spec.rb
51
+ - spec/kernel_spec.rb
52
+ - spec/re2/regexp_spec.rb
53
+ - spec/re2/match_data_spec.rb
54
+ - spec/re2/string_spec.rb
38
55
  homepage: http://github.com/mudge/re2
39
56
  licenses: []
40
57
  post_install_message:
@@ -60,5 +77,10 @@ signing_key:
60
77
  specification_version: 3
61
78
  summary: Ruby bindings to re2.
62
79
  test_files:
63
- - test/re2_test.rb
80
+ - spec/spec_helper.rb
81
+ - spec/re2_spec.rb
82
+ - spec/kernel_spec.rb
83
+ - spec/re2/regexp_spec.rb
84
+ - spec/re2/match_data_spec.rb
85
+ - spec/re2/string_spec.rb
64
86
  has_rdoc:
@@ -1,265 +0,0 @@
1
- # re2 (http://github.com/mudge/re2)
2
- # Ruby bindings to re2, an "efficient, principled regular expression library"
3
- #
4
- # Copyright (c) 2010, Paul Mucur (http://mucur.name)
5
- # Released under the BSD Licence, please see LICENSE.txt
6
-
7
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
- require "re2"
9
- require "test/unit"
10
-
11
- class RE2Test < Test::Unit::TestCase
12
- def test_interface
13
- assert_respond_to RE2, :Replace
14
- assert_respond_to RE2, :GlobalReplace
15
- assert_respond_to RE2, :QuoteMeta
16
- assert_respond_to RE2::Regexp, :escape
17
- assert_respond_to RE2::Regexp, :quote
18
- assert_respond_to RE2::Regexp, :new
19
- assert_respond_to RE2::Regexp, :compile
20
-
21
- r = RE2::Regexp.new('woo')
22
- assert_respond_to r, :ok?
23
- assert_respond_to r, :options
24
- assert_respond_to r, :error
25
- assert_respond_to r, :error_arg
26
- assert_respond_to r, :program_size
27
- assert_respond_to r, :to_s
28
- assert_respond_to r, :to_str
29
- assert_respond_to r, :pattern
30
- assert_respond_to r, :inspect
31
- assert_respond_to r, :match
32
- assert_respond_to r, :match?
33
- assert_respond_to r, :=~
34
- assert_respond_to r, :===
35
- assert_respond_to r, :number_of_capturing_groups
36
- assert_respond_to r, :utf8?
37
- assert_respond_to r, :posix_syntax?
38
- assert_respond_to r, :longest_match?
39
- assert_respond_to r, :log_errors?
40
- assert_respond_to r, :max_mem
41
- assert_respond_to r, :literal?
42
- assert_respond_to r, :never_nl?
43
- assert_respond_to r, :case_sensitive?
44
- assert_respond_to r, :case_insensitive?
45
- assert_respond_to r, :casefold?
46
- assert_respond_to r, :perl_classes?
47
- assert_respond_to r, :word_boundary?
48
- assert_respond_to r, :one_line?
49
-
50
- assert_respond_to Kernel, :RE2
51
- end
52
-
53
- def test_global_re2
54
- r = RE2('w(o)(o)')
55
- assert_kind_of RE2::Regexp, r
56
- assert_respond_to r, :ok?
57
- end
58
-
59
- def test_re2_compile
60
- r = RE2::Regexp.compile('w(o)(o)')
61
- assert_kind_of RE2::Regexp, r
62
- assert_respond_to r, :ok?
63
- end
64
-
65
- def test_replace
66
- assert_equal "wao", RE2::Replace("woo", "o", "a")
67
- assert_equal "hoo", RE2::Replace("woo", "w", "h")
68
- assert_equal "we", RE2::Replace("woo", "o+", "e")
69
- assert_equal "Good morning", RE2::Replace("hi", "hih?", "Good morning")
70
- assert_equal "hi", RE2::Replace("Good morning", "(?i)gOOD MORNING", "hi")
71
-
72
- name = "Robert"
73
- name_id = name.object_id
74
- assert_equal "Crobert", RE2::Replace(name, "R", "Cr")
75
- assert_equal "Crobert", name
76
- assert_equal name_id, name.object_id
77
- end
78
-
79
- def test_replace_with_a_frozen_string
80
- frozen_name = "Arnold".freeze
81
- assert frozen_name.frozen?
82
-
83
- # Ruby 1.9 raises a RuntimeError instead of a TypeError.
84
- assert_raise TypeError, RuntimeError do
85
- RE2::Replace(frozen_name, "o", "a")
86
- end
87
- end
88
-
89
- def test_global_replace
90
- assert_equal "waa", RE2::GlobalReplace("woo", "o", "a")
91
- assert_equal "hoo", RE2::GlobalReplace("woo", "w", "h")
92
- assert_equal "we", RE2::GlobalReplace("woo", "o+", "e")
93
-
94
- name = "Robert"
95
- name_id = name.object_id
96
- assert_equal "wobewt", RE2::GlobalReplace(name, "(?i)R", "w")
97
- assert_equal "wobewt", name
98
- assert_equal name_id, name.object_id
99
- end
100
-
101
- def test_global_replace_with_a_frozen_string
102
- frozen_name = "Arnold".freeze
103
- assert frozen_name.frozen?
104
-
105
- # Ruby 1.9 raises a RuntimeError instead of a TypeError.
106
- assert_raise TypeError, RuntimeError do
107
- RE2::GlobalReplace(frozen_name, "o", "a")
108
- end
109
- end
110
-
111
- def test_compiling
112
- r = RE2::Regexp.new("woo")
113
- assert r.ok?
114
- assert_equal "#<RE2::Regexp /woo/>", r.inspect
115
- assert_equal "woo", r.to_s
116
- end
117
-
118
- def test_number_of_capturing_groups
119
- assert_equal 3, RE2('(a)(b)(c)').number_of_capturing_groups
120
- assert_equal 0, RE2('abc').number_of_capturing_groups
121
- assert_equal 2, RE2('a((b)c)').number_of_capturing_groups
122
- end
123
-
124
- def test_named_capturing_groups
125
- assert_equal(1, RE2('(?P<bob>a)').named_capturing_groups["bob"])
126
- assert_equal(1, RE2('(?P<bob>a)(o)(?P<rob>e)').named_capturing_groups["bob"])
127
- assert_equal(3, RE2('(?P<bob>a)(o)(?P<rob>e)').named_capturing_groups["rob"])
128
- end
129
-
130
- def test_matching_all_subpatterns
131
- assert_equal ["woo", "o", "o"], RE2('w(o)(o)').match('woo').to_a
132
- assert_equal ["ab", nil, "a", "b"], RE2('(\d?)(a)(b)').match('ab').to_a
133
- end
134
-
135
- def test_fetching_matchdata_out_of_range
136
- matchdata = RE2('(\d+)').match('bob 123')
137
- assert_nil matchdata[2]
138
- assert_nil matchdata[3]
139
- end
140
-
141
- def test_accessing_matches_by_name
142
- matchdata = RE2('(?P<numbers>\d+)').match("bob 123")
143
- assert_equal "123", matchdata["numbers"]
144
- assert_equal "123", matchdata[:numbers]
145
- end
146
-
147
- def test_accessing_matches_by_name_with_multiple_groups
148
- matchdata = RE2('(?P<name>\w+)(\s*)(?P<numbers>\d+)').match("bob 123")
149
- assert_equal "bob", matchdata["name"]
150
- assert_equal "bob", matchdata[:name]
151
- assert_equal " ", matchdata[2]
152
- assert_equal "123", matchdata["numbers"]
153
- assert_equal "123", matchdata[:numbers]
154
- end
155
-
156
- def test_accessing_matches_by_incorrect_names
157
- matchdata = RE2('(?P<numbers>\d+)').match("bob 123")
158
- assert_nil matchdata["missing"]
159
- assert_nil matchdata[:missing]
160
- end
161
-
162
- def test_matchdata
163
- r = RE2('(\d+)')
164
- text = "bob 123"
165
- m = r.match(text)
166
- assert_kind_of RE2::MatchData, m
167
- assert_respond_to m, :string
168
- assert_respond_to m, :size
169
- assert_respond_to m, :length
170
- assert_respond_to m, :regexp
171
- assert_respond_to m, :to_a
172
- assert_respond_to m, :[]
173
- assert_equal r, m.regexp
174
- assert_equal text, m.string
175
- assert !text.frozen?
176
- assert m.string.frozen?
177
- assert_not_equal m.string.object_id, text.object_id
178
- assert_equal '#<RE2::MatchData "123" 1:"123">', m.inspect
179
- assert_equal "123", m.to_s
180
- assert_equal "123", m[0]
181
- assert_equal "123", m[1]
182
- assert_nil m[4]
183
- assert_equal ["123"], m[0, 1]
184
- assert_equal ["123", "123"], m[0, 2]
185
- assert_equal ["123"], m[0...1]
186
- assert_equal ["123", "123"], m[0..1]
187
- m1, m2 = *r.match(text)
188
- assert_equal "123", m1
189
- assert_equal "123", m2
190
- end
191
-
192
- def test_matching_no_subpatterns
193
- assert RE2('woo').match('woo', 0)
194
- assert !RE2('bob').match('woo', 0)
195
- assert RE2('woo').match?('woo')
196
- assert !RE2('bob').match?('woo')
197
- assert RE2('woo') =~ 'woo'
198
- assert !(RE2('bob') =~ 'woo')
199
- assert !(RE2('woo') !~ 'woo')
200
- assert RE2('bob') !~ 'woo'
201
- assert RE2('woo') === 'woo'
202
- assert !(RE2('bob') === 'woo')
203
- end
204
-
205
- def test_matching_some_sub_patterns
206
- assert_equal ["woo", "o"], RE2('w(o)(o)').match('woo', 1).to_a
207
- assert_equal ["woo", "o", "o"], RE2('w(o)(o)').match('woo', 2).to_a
208
- assert_equal ["woo", "o", "o", nil], RE2('w(o)(o)').match('woo', 3).to_a
209
- assert_equal ["w", nil], RE2('w(o)?(o)?').match('w', 1).to_a
210
- assert_equal ["w", nil, nil], RE2('w(o)?(o)?').match('w', 2).to_a
211
- assert_equal ["w", nil, nil, nil], RE2('w(o)?(o)?').match('w', 3).to_a
212
- end
213
-
214
- def test_compiling_with_options
215
- r = RE2("woo", :case_sensitive => false)
216
- assert r.ok?
217
- assert r =~ "woo"
218
- assert r =~ "WOO"
219
- assert !r.options[:case_sensitive]
220
- assert r.case_insensitive?
221
- assert r.casefold?
222
- assert !r.case_sensitive?
223
- assert r.utf8?
224
- assert r.options[:utf8]
225
- end
226
-
227
- def test_replace_with_re2
228
- r = RE2("wo{2}")
229
- assert_equal "miaow", RE2::Replace("woo", r, "miaow")
230
-
231
- assert_equal "wao", RE2::Replace("woo", RE2("o"), "a")
232
- assert_equal "hoo", RE2::Replace("woo", RE2("w"), "h")
233
- assert_equal "we", RE2::Replace("woo", RE2("o+"), "e")
234
- assert_equal "Good morning", RE2::Replace("hi", RE2("hih?"), "Good morning")
235
- assert_equal "hi", RE2::Replace("Good morning", RE2("gOOD MORNING", :case_sensitive => false), "hi")
236
- end
237
-
238
- def test_global_replace_with_re2
239
- r = RE2("o")
240
- assert_equal "wii", RE2::GlobalReplace("woo", r, "i")
241
-
242
- assert_equal "waa", RE2::GlobalReplace("woo", RE2("o"), "a")
243
- assert_equal "hoo", RE2::GlobalReplace("woo", RE2("w"), "h")
244
- assert_equal "we", RE2::GlobalReplace("woo", RE2("o+"), "e")
245
- end
246
-
247
- def test_quote_meta
248
- assert_equal "1\\.5\\-2\\.0\\?", RE2::QuoteMeta("1.5-2.0?")
249
- assert_equal "1\\.5\\-2\\.0\\?", RE2::Regexp.escape("1.5-2.0?")
250
- assert_equal "1\\.5\\-2\\.0\\?", RE2::Regexp.quote("1.5-2.0?")
251
- end
252
-
253
- def test_re2_error
254
- r = RE2("woo")
255
- assert_equal "", r.error
256
- assert_equal "", r.error_arg
257
- end
258
-
259
- def test_re2_error_with_error
260
- r = RE2("wo(o", :log_errors => false)
261
- assert !r.ok?
262
- assert_equal "missing ): wo(o", r.error
263
- assert_equal "wo(o", r.error_arg
264
- end
265
- end