re2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,228 @@
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, :FullMatch
14
+ assert_respond_to RE2, :FullMatchN
15
+ assert_respond_to RE2, :PartialMatch
16
+ assert_respond_to RE2, :PartialMatchN
17
+ assert_respond_to RE2, :Replace
18
+ assert_respond_to RE2, :GlobalReplace
19
+ assert_respond_to RE2, :QuoteMeta
20
+ assert_respond_to RE2, :escape
21
+ assert_respond_to RE2, :quote
22
+ assert_respond_to RE2, :new
23
+ assert_respond_to RE2, :compile
24
+
25
+ r = RE2.new('woo')
26
+ assert_respond_to r, :ok?
27
+ assert_respond_to r, :options
28
+ assert_respond_to r, :error
29
+ assert_respond_to r, :error_arg
30
+ assert_respond_to r, :program_size
31
+ assert_respond_to r, :to_s
32
+ assert_respond_to r, :to_str
33
+ assert_respond_to r, :pattern
34
+ assert_respond_to r, :inspect
35
+ assert_respond_to r, :match
36
+ assert_respond_to r, :match?
37
+ assert_respond_to r, :=~
38
+ assert_respond_to r, :===
39
+ assert_respond_to r, :number_of_capturing_groups
40
+ assert_respond_to r, :utf8?
41
+ assert_respond_to r, :posix_syntax?
42
+ assert_respond_to r, :longest_match?
43
+ assert_respond_to r, :log_errors?
44
+ assert_respond_to r, :max_mem
45
+ assert_respond_to r, :literal?
46
+ assert_respond_to r, :never_nl?
47
+ assert_respond_to r, :case_sensitive?
48
+ assert_respond_to r, :case_insensitive?
49
+ assert_respond_to r, :casefold?
50
+ assert_respond_to r, :perl_classes?
51
+ assert_respond_to r, :word_boundary?
52
+ assert_respond_to r, :one_line?
53
+
54
+ assert_respond_to Kernel, :RE2
55
+ end
56
+
57
+ def test_global_re2
58
+ r = RE2('w(o)(o)')
59
+ assert_kind_of RE2, r
60
+ assert_respond_to r, :ok?
61
+ end
62
+
63
+ def test_re2_compile
64
+ r = RE2.compile('w(o)(o)')
65
+ assert_kind_of RE2, r
66
+ assert_respond_to r, :ok?
67
+ end
68
+
69
+ def test_full_match
70
+ assert RE2::FullMatch("woo", "woo")
71
+ assert RE2::FullMatch("woo", "wo+")
72
+ assert RE2::FullMatch("woo", "woo?")
73
+ assert RE2::FullMatch("woo", "wo{2}")
74
+ assert !RE2::FullMatch("woo", "wowzer")
75
+ end
76
+
77
+ def test_full_match_n
78
+ assert_equal ["oo"], RE2::FullMatchN("woo", "w(oo)")
79
+ assert_equal ["12"], RE2::FullMatchN("woo12w", 'woo(\d{2})w')
80
+ assert_equal [nil, "1", "234"], RE2::FullMatchN("w1234", 'w(a?)(\d)(\d+)')
81
+ assert_nil RE2::FullMatchN("bob", 'w(\d+)')
82
+ end
83
+
84
+ def test_full_match_n_with_compiled_pattern
85
+ assert_equal ["oo"], RE2::FullMatchN("woo", RE2.new("w(oo)"))
86
+ assert_equal ["12"], RE2::FullMatchN("woo12w", RE2.new('woo(\d{2})w'))
87
+ assert_equal [nil, "1", "234"], RE2::FullMatchN("w1234", RE2.new('w(a?)(\d)(\d+)'))
88
+ assert_nil RE2::FullMatchN("bob", RE2.new('w(\d+)'))
89
+ end
90
+
91
+ def test_partial_match
92
+ assert RE2::PartialMatch("woo", "oo")
93
+ assert RE2::PartialMatch("woo", "oo?")
94
+ assert RE2::PartialMatch("woo", "o{2}")
95
+ assert !RE2::PartialMatch("woo", "ha")
96
+ end
97
+
98
+ def test_partial_match_n
99
+ assert_equal ["oo"], RE2::PartialMatchN("awooa", "w(oo)")
100
+ assert_equal ["12"], RE2::PartialMatchN("awoo12wa", 'woo(\d{2})w')
101
+ assert_equal [nil, "1", "234"], RE2::PartialMatchN("aw1234a", 'w(a?)(\d)(\d+)')
102
+ assert_nil RE2::PartialMatchN("bob", 'w(\d+)')
103
+ end
104
+
105
+ def test_partial_match_n_with_compiled_pattern
106
+ assert_equal ["oo"], RE2::PartialMatchN("awooa", RE2.new("w(oo)"))
107
+ assert_equal ["12"], RE2::PartialMatchN("awoo12wa", RE2.new('woo(\d{2})w'))
108
+ assert_equal [nil, "1", "234"], RE2::PartialMatchN("aw1234a", RE2.new('w(a?)(\d)(\d+)'))
109
+ assert_nil RE2::PartialMatchN("bob", RE2.new('w(\d+)'))
110
+ end
111
+
112
+ def test_replace
113
+ assert_equal "wao", RE2::Replace("woo", "o", "a")
114
+ assert_equal "hoo", RE2::Replace("woo", "w", "h")
115
+ assert_equal "we", RE2::Replace("woo", "o+", "e")
116
+ assert_equal "Good morning", RE2::Replace("hi", "hih?", "Good morning")
117
+ assert_equal "hi", RE2::Replace("Good morning", "(?i)gOOD MORNING", "hi")
118
+
119
+ name = "Robert"
120
+ name_id = name.object_id
121
+ assert_equal "Crobert", RE2::Replace(name, "R", "Cr")
122
+ assert_equal "Crobert", name
123
+ assert_equal name_id, name.object_id
124
+ end
125
+
126
+ def test_global_replace
127
+ assert_equal "waa", RE2::GlobalReplace("woo", "o", "a")
128
+ assert_equal "hoo", RE2::GlobalReplace("woo", "w", "h")
129
+ assert_equal "we", RE2::GlobalReplace("woo", "o+", "e")
130
+
131
+ name = "Robert"
132
+ name_id = name.object_id
133
+ assert_equal "wobewt", RE2::GlobalReplace(name, "(?i)R", "w")
134
+ assert_equal "wobewt", name
135
+ assert_equal name_id, name.object_id
136
+ end
137
+
138
+ def test_compiling
139
+ r = RE2.new("woo")
140
+ assert r.ok?
141
+ assert_equal "/woo/", r.inspect
142
+ assert_equal "woo", r.to_s
143
+ end
144
+
145
+ def test_number_of_capturing_groups
146
+ assert_equal 3, RE2.new('(a)(b)(c)').number_of_capturing_groups
147
+ assert_equal 0, RE2.new('abc').number_of_capturing_groups
148
+ assert_equal 2, RE2.new('a((b)c)').number_of_capturing_groups
149
+ end
150
+
151
+ def test_matching_all_subpatterns
152
+ assert_equal ["woo", "o", "o"], RE2.new('w(o)(o)').match('woo')
153
+ assert_equal ["ab", nil, "a", "b"], RE2.new('(\d?)(a)(b)').match('ab')
154
+ end
155
+
156
+ def test_matching_no_subpatterns
157
+ assert RE2.new('woo').match('woo', 0)
158
+ assert !RE2.new('bob').match('woo', 0)
159
+ assert RE2.new('woo').match?('woo')
160
+ assert !RE2.new('bob').match?('woo')
161
+ assert RE2.new('woo') =~ 'woo'
162
+ assert !(RE2.new('bob') =~ 'woo')
163
+ assert !(RE2.new('woo') !~ 'woo')
164
+ assert RE2.new('bob') !~ 'woo'
165
+ assert RE2.new('woo') === 'woo'
166
+ assert !(RE2.new('bob') === 'woo')
167
+ end
168
+
169
+ def test_matching_some_sub_patterns
170
+ assert_equal ["woo", "o"], RE2.new('w(o)(o)').match('woo', 1)
171
+ assert_equal ["woo", "o", "o"], RE2.new('w(o)(o)').match('woo', 2)
172
+ assert_equal ["woo", "o", "o", nil], RE2.new('w(o)(o)').match('woo', 3)
173
+ end
174
+
175
+ def test_compiling_with_options
176
+ r = RE2.new("woo", :case_sensitive => false)
177
+ assert r.ok?
178
+ assert RE2::FullMatch("woo", r)
179
+ assert RE2::FullMatch("WOO", r)
180
+ assert !r.options[:case_sensitive]
181
+ assert r.case_insensitive?
182
+ assert r.casefold?
183
+ assert !r.case_sensitive?
184
+ assert r.utf8?
185
+ assert r.options[:utf8]
186
+ end
187
+
188
+ def test_full_match_with_re2
189
+ r = RE2.new("woo")
190
+ assert RE2::FullMatch("woo", r)
191
+ assert !RE2::FullMatch("wowzer", r)
192
+ end
193
+
194
+ def test_partial_match_with_re2
195
+ r = RE2.new("woo")
196
+ assert RE2::PartialMatch("woo", r)
197
+ assert !RE2::PartialMatch("wowzer", r)
198
+ end
199
+
200
+ def test_replace_with_re2
201
+ r = RE2.new("wo{2}")
202
+ assert_equal "miaow", RE2::Replace("woo", r, "miaow")
203
+ end
204
+
205
+ def test_global_replace_with_re2
206
+ r = RE2.new("o")
207
+ assert_equal "wii", RE2::GlobalReplace("woo", r, "i")
208
+ end
209
+
210
+ def test_quote_meta
211
+ assert_equal "1\\.5\\-2\\.0\\?", RE2::QuoteMeta("1.5-2.0?")
212
+ assert_equal "1\\.5\\-2\\.0\\?", RE2.escape("1.5-2.0?")
213
+ assert_equal "1\\.5\\-2\\.0\\?", RE2.quote("1.5-2.0?")
214
+ end
215
+
216
+ def test_re2_error
217
+ r = RE2.new("woo")
218
+ assert_equal "", r.error
219
+ assert_equal "", r.error_arg
220
+ end
221
+
222
+ def test_re2_error_with_error
223
+ r = RE2.new("wo(o", :log_errors => false)
224
+ assert !r.ok?
225
+ assert_equal "missing ): wo(o", r.error
226
+ assert_equal "wo(o", r.error_arg
227
+ end
228
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: re2
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Paul Mucur
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-27 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: ruby.re2@librelist.com
24
+ executables: []
25
+
26
+ extensions:
27
+ - ext/re2/extconf.rb
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - ext/re2/extconf.rb
32
+ - ext/re2/re2.cc
33
+ - LICENSE.txt
34
+ - README.md
35
+ - Rakefile
36
+ - test/re2_test.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/mudge/re2
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Ruby bindings to re2
71
+ test_files:
72
+ - test/re2_test.rb