File: anchors_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (163 lines) | stat: -rw-r--r-- 5,566 bytes parent folder | download
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
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../../fixtures/classes', __FILE__)

describe "Regexps with anchors" do
  it "supports ^ (line start anchor)" do
    # Basic matching
    /^foo/.match("foo").to_a.should == ["foo"]
    /^bar/.match("foo\nbar").to_a.should == ["bar"]
    # Basic non-matching
    /^foo/.match(" foo").should be_nil
    /foo^/.match("foo\n\n\n").should be_nil

    # A bit advanced
    /^^^foo/.match("foo").to_a.should == ["foo"]
    (/^[^f]/ =~ "foo\n\n").should == "foo\n".size and $~.to_a.should == ["\n"]
    (/($^)($^)/ =~ "foo\n\n").should == "foo\n".size and $~.to_a.should == ["", "", ""]

    # Different start of line chars
    /^bar/.match("foo\rbar").should be_nil
    /^bar/.match("foo\0bar").should be_nil

    # Trivial
    /^/.match("foo").to_a.should == [""]

    # Grouping
    /(^foo)/.match("foo").to_a.should == ["foo", "foo"]
    /(^)/.match("foo").to_a.should == ["", ""]
    /(foo\n^)(^bar)/.match("foo\nbar").to_a.should == ["foo\nbar", "foo\n", "bar"]
  end

  it "does not match ^ after trailing \n" do
    /^(?!\A)/.match("foo\n").should be_nil # There is no (empty) line after a trailing \n
  end

  it "supports $ (line end anchor)" do
    # Basic  matching
    /foo$/.match("foo").to_a.should == ["foo"]
    /foo$/.match("foo\nbar").to_a.should == ["foo"]
    # Basic non-matching
    /foo$/.match("foo ").should be_nil
    /$foo/.match("\n\n\nfoo").should be_nil

    # A bit advanced
    /foo$$$/.match("foo").to_a.should == ["foo"]
    (/[^o]$/ =~ "foo\n\n").should == ("foo\n".size - 1) and $~.to_a.should == ["\n"]

    # Different end of line chars
    /foo$/.match("foo\r\nbar").should be_nil
    /foo$/.match("foo\0bar").should be_nil

    # Trivial
    (/$/ =~ "foo").should == "foo".size and $~.to_a.should == [""]

    # Grouping
    /(foo$)/.match("foo").to_a.should == ["foo", "foo"]
    (/($)/ =~ "foo").should == "foo".size and $~.to_a.should == ["", ""]
    /(foo$)($\nbar)/.match("foo\nbar").to_a.should == ["foo\nbar", "foo", "\nbar"]
  end

  it "supports \A (string start anchor)" do
    # Basic matching
    /\Afoo/.match("foo").to_a.should == ["foo"]
    # Basic non-matching
    /\Abar/.match("foo\nbar").should be_nil
    /\Afoo/.match(" foo").should be_nil

    # A bit advanced
    /\A\A\Afoo/.match("foo").to_a.should == ["foo"]
    /(\A\Z)(\A\Z)/.match("").to_a.should == ["", "", ""]

    # Different start of line chars
    /\Abar/.match("foo\0bar").should be_nil

    # Grouping
    /(\Afoo)/.match("foo").to_a.should == ["foo", "foo"]
    /(\A)/.match("foo").to_a.should == ["", ""]
  end

  it "supports \Z (string end anchor, including before trailing \n)" do
    # Basic matching
    /foo\Z/.match("foo").to_a.should == ["foo"]
    /foo\Z/.match("foo\n").to_a.should == ["foo"]
    # Basic non-matching
    /foo\Z/.match("foo\nbar").should be_nil
    /foo\Z/.match("foo ").should be_nil

    # A bit advanced
    /foo\Z\Z\Z/.match("foo\n").to_a.should == ["foo"]
    (/($\Z)($\Z)/ =~ "foo\n").should == "foo".size and $~.to_a.should == ["", "", ""]
    (/(\z\Z)(\z\Z)/ =~ "foo\n").should == "foo\n".size and $~.to_a.should == ["", "", ""]

    # Different end of line chars
    /foo\Z/.match("foo\0bar").should be_nil
    /foo\Z/.match("foo\r\n").should be_nil

    # Grouping
    /(foo\Z)/.match("foo").to_a.should == ["foo", "foo"]
    (/(\Z)/ =~ "foo").should == "foo".size and $~.to_a.should == ["", ""]
  end

  it "supports \z (string end anchor)" do
    # Basic matching
    /foo\z/.match("foo").to_a.should == ["foo"]
    # Basic non-matching
    /foo\z/.match("foo\nbar").should be_nil
    /foo\z/.match("foo\n").should be_nil
    /foo\z/.match("foo ").should be_nil

    # A bit advanced
    /foo\z\z\z/.match("foo").to_a.should == ["foo"]
    (/($\z)($\z)/ =~ "foo").should == "foo".size and $~.to_a.should == ["", "", ""]

    # Different end of line chars
    /foo\z/.match("foo\0bar").should be_nil
    /foo\z/.match("foo\r\nbar").should be_nil

    # Grouping
    /(foo\z)/.match("foo").to_a.should == ["foo", "foo"]
    (/(\z)/ =~ "foo").should == "foo".size and $~.to_a.should == ["", ""]
  end

  it "supports \b (word boundary)" do
    # Basic matching
    /foo\b/.match("foo").to_a.should == ["foo"]
    /foo\b/.match("foo\n").to_a.should == ["foo"]
    LanguageSpecs.white_spaces.scan(/./).each do |c|
    /foo\b/.match("foo" + c).to_a.should == ["foo"]
    end
    LanguageSpecs.non_alphanum_non_space.scan(/./).each do |c|
    /foo\b/.match("foo" + c).to_a.should == ["foo"]
    end
    /foo\b/.match("foo\0").to_a.should == ["foo"]
    # Basic non-matching
    /foo\b/.match("foobar").should be_nil
    /foo\b/.match("foo123").should be_nil
    /foo\b/.match("foo_").should be_nil
  end

  it "supports \B (non-word-boundary)" do
    # Basic matching
    /foo\B/.match("foobar").to_a.should == ["foo"]
    /foo\B/.match("foo123").to_a.should == ["foo"]
    /foo\B/.match("foo_").to_a.should == ["foo"]
    # Basic non-matching
    /foo\B/.match("foo").should be_nil
    /foo\B/.match("foo\n").should be_nil
    LanguageSpecs.white_spaces.scan(/./).each do |c|
    /foo\B/.match("foo" + c).should be_nil
    end
    LanguageSpecs.non_alphanum_non_space.scan(/./).each do |c|
    /foo\B/.match("foo" + c).should be_nil
    end
    /foo\B/.match("foo\0").should be_nil
  end

  it "supports (?= ) (positive lookahead)" do
    /foo.(?=bar)/.match("foo1 foo2bar").to_a.should == ["foo2"]
  end

  it "supports (?! ) (negative lookahead)" do
    /foo.(?!bar)/.match("foo1bar foo2").to_a.should == ["foo2"]
  end
end