File: test-regex.rb

package info (click to toggle)
ruby-gnome 4.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,320 kB
  • sloc: ruby: 55,206; ansic: 29,012; xml: 333; sh: 229; cpp: 45; makefile: 42
file content (322 lines) | stat: -rw-r--r-- 8,341 bytes parent folder | download | duplicates (3)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright (C) 2015-2021  Ruby-GNOME Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

class TestRegex < Test::Unit::TestCase
  include GLibTestUtils

  def test_enum_match_flags
    assert_const_defined(GLib, :RegexMatchFlags)
    assert_kind_of(GLib::RegexMatchFlags, GLib::RegexMatchFlags::PARTIAL_HARD)
  end

  def test_enum_compile_flags
    assert_const_defined(GLib, :RegexCompileFlags)
    assert_kind_of(GLib::RegexCompileFlags, GLib::RegexCompileFlags::CASELESS)
  end

  def test_pattern
    regex = GLib::Regex.new("to??")
    assert_equal("to??", regex.pattern)
  end

  def test_compile_flags
    flags = GLib::RegexCompileFlags::CASELESS
    regex = GLib::Regex.new("to??", :compile_options => flags)
    assert_equal(flags, regex.compile_flags)
  end

  def test_match_flags
    flags = GLib::RegexMatchFlags::PARTIAL_HARD
    regex = GLib::Regex.new("to??", :match_options => flags)
    assert_equal(flags, regex.match_flags)
  end

  sub_test_case "split" do
    test "no options" do
      regex = GLib::Regex.new("\s")
      string_to_split = "a bc"
      split_strings = regex.split(string_to_split)
      assert_equal(["a", "bc"], split_strings)
    end

    test "start_position" do
      regex = GLib::Regex.new("\s")
      string_to_split = "a bc"
      split_strings = regex.split(string_to_split, :start_position => 2)
      assert_equal(["bc"], split_strings)
    end

    test "max_tokens" do
      regex = GLib::Regex.new("\s")
      string_to_split = "a bc de fg"
      split_strings = regex.split(string_to_split, :max_tokens => 2)
      assert_equal(["a", "bc de fg"], split_strings)
    end

    test "match_options" do
      regex = GLib::Regex.new("a?b?")
      string_to_split = "toto ab"
      split_strings = regex.split(string_to_split)
      assert_equal(["t", "o", "t", "o", " "],
                   split_strings)
      split_strings = regex.split(string_to_split,
                                  :match_options => :notempty)
      assert_equal(["toto ", ""], split_strings)
    end
  end

  sub_test_case "match" do
    setup do
      @regex = GLib::Regex.new("[A-Z]+")
    end

    test "no match no options" do
      match_info = @regex.match("abc def")
      assert do
        not match_info.matches?
      end
    end

    test "matched no options" do
      match_info = @regex.match("abc DEF")
      assert do
        match_info.matches?
      end
    end

    test "no match and start position option" do
      match_info = @regex.match("abc def", :start_position => 4)
      assert do
        not match_info.matches?
      end
    end

    test "matched and start position option" do
      match_info = @regex.match("abc DEF", :start_position => 4)
      assert do
        match_info.matches?
      end
    end
  end

  sub_test_case "max_backref" do
    test "none" do
      regex = GLib::Regex.new("to?o")
      assert_equal(0, regex.max_backref)
    end

    test "one" do
      regex = GLib::Regex.new("(to(?)o)\\g1")
      assert_equal(1, regex.max_backref)
    end
  end

  sub_test_case "capture_count" do
    test "none" do
      regex = GLib::Regex.new("to?o")
      assert_equal(0, regex.capture_count)
    end

    test "one" do
      regex = GLib::Regex.new("(to(\?)o)")
      assert_equal(1, regex.capture_count)
    end

    test "three" do
      regex = GLib::Regex.new("((red|white) (king|queen))")
      assert_equal(3, regex.capture_count)
    end
  end

  sub_test_case "has_cr_or_lf" do
    test "none" do
      regex = GLib::Regex.new("to?o")
      assert do
        not regex.has_cr_or_lf?
      end
    end

    test "both" do
      regex = GLib::Regex.new("to\no")
      assert do
        regex.has_cr_or_lf?
      end
    end

    test "cr" do
      regex = GLib::Regex.new("to\rtiti")
      assert do
        regex.has_cr_or_lf?
      end
    end
  end

  sub_test_case "max_lookbehind" do
    test "none" do
      regex = GLib::Regex.new("to?o")
      assert_equal(0, regex.max_lookbehind)
    end

    test "three" do
      regex = GLib::Regex.new("(?<!foo)bar")
      assert_equal(3, regex.max_lookbehind)
    end
  end

  sub_test_case "string_number" do
    test "none" do
      regex = GLib::Regex.new("too")
      assert_equal(-1, regex.string_number("a_name"))
    end

    test "one" do
      regex = GLib::Regex.new("(?<a_name>foo)bar")
      assert_equal(1, regex.string_number("a_name"))
    end

    test "two" do
      regex = GLib::Regex.new("(?<another_name>foo)(?<a_name>bar)")
      assert_equal(2, regex.string_number("a_name"))
    end
  end

  def test_regex_escape_string
    assert_equal("a\\.b\\*c", GLib::Regex.escape_string("a.b*c"))
  end

  sub_test_case "match?" do
    test "true" do
      assert do
        GLib::Regex.match?("to", "tatota")
      end
    end

    test "false" do
      assert do
        not GLib::Regex.match?("ti", "tatota")
      end
    end
  end

  sub_test_case "match_all" do
    test "no match" do
      regex = GLib::Regex.new("[A-Z]+")
      assert do
        not regex.match_all("abc def").matches?
      end
    end

    test ":start_position" do
      regex = GLib::Regex.new("[A-Z]")
      assert do
        regex.match_all("a B c", :start_position => 2).matches?
      end
      assert do
        not regex.match_all("a B c", :start_position => 3).matches?
      end
    end

    test "match all" do
      regex = GLib::Regex.new("<.*>")
      assert_equal(3, regex.match_all("<a> <b> <c>").match_count)
    end
  end

  sub_test_case "split simple" do
    test "no options" do
      split_strings = GLib::Regex.split("\s", "a bc")
      assert_equal(["a", "bc"], split_strings)
    end

    test "match_options" do
      split_strings = GLib::Regex.split("a?b?", "toto ab")
      assert_equal(["t", "o", "t", "o", " "], split_strings)

      split_strings = GLib::Regex.split("a?b?",
                                    "toto ab",
                                    :match_options => :notempty)
      assert_equal(["toto ", ""], split_strings)
    end
  end

  sub_test_case "replace" do
    test "simple" do
      regex = GLib::Regex.new("\\s")
      assert_equal("a_bc", regex.replace("a bc", "_"))
    end

    test "back reference" do
      regex = GLib::Regex.new("\\s")
      assert_equal("a_ _bc", regex.replace("a bc", "_\\0_"))
    end

    test "literal" do
      regex = GLib::Regex.new("\\s")
      assert_equal("a_\\0_bc",
                   regex.replace("a bc",
                                 "_\\0_",
                                 :literal => true))
    end

    sub_test_case "eval" do
      test "all" do
        regex = GLib::Regex.new("1|2|3|4")
        modified_string = regex.replace(" 4 3 2 1") do |match_info|
          "to"
        end
        assert_equal(" to to to to", modified_string)
      end

      test "break" do
        regex = GLib::Regex.new("1|2|3|4")
        modified_string = regex.replace(" 4 3 2 1") do |match_info|
          break "to"
        end
        assert_equal(" to 3 2 1", modified_string)
      end
    end
  end

  sub_test_case "check_replacement" do
    test "no references" do
      assert_true(GLib::Regex.check_replacement("foo\\n"))
    end

    test "with references" do
      assert_true(GLib::Regex.check_replacement("\\0\\1"))
    end

    test "invalid" do
      assert_raise(GLib::Error) do
        GLib::Regex.check_replacement("\\")
      end
    end
  end

  sub_test_case "have_reference?" do
    test "no references" do
      assert do
        not GLib::Regex.have_reference?("foo\\n")
      end
    end

    test "with references" do
      assert do
        GLib::Regex.have_reference?("\\0\\1")
      end
    end
  end
end