mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-04-24 04:46:05 +08:00
more language/regexp specs
This commit is contained in:
79
test/test-android/app/language/regexp/escapes_spec.rb
Normal file
79
test/test-android/app/language/regexp/escapes_spec.rb
Normal file
@@ -0,0 +1,79 @@
|
||||
describe "Regexps with escape characters" do
|
||||
it "they're supported" do
|
||||
/\t/.match("\t").to_a.should == ["\t"] # horizontal tab
|
||||
# /\v/.match("\v").to_a.should == ["\v"] # vertical tab
|
||||
/\n/.match("\n").to_a.should == ["\n"] # newline
|
||||
/\r/.match("\r").to_a.should == ["\r"] # return
|
||||
/\f/.match("\f").to_a.should == ["\f"] # form feed
|
||||
/\a/.match("\a").to_a.should == ["\a"] # bell
|
||||
/\e/.match("\e").to_a.should == ["\e"] # escape
|
||||
|
||||
# \nnn octal char (encoded byte value)
|
||||
end
|
||||
|
||||
it "support quoting meta-characters via escape sequence" do
|
||||
/\\/.match("\\").to_a.should == ["\\"]
|
||||
/\//.match("/").to_a.should == ["/"]
|
||||
# parenthesis, etc
|
||||
/\(/.match("(").to_a.should == ["("]
|
||||
/\)/.match(")").to_a.should == [")"]
|
||||
/\[/.match("[").to_a.should == ["["]
|
||||
/\]/.match("]").to_a.should == ["]"]
|
||||
/\{/.match("{").to_a.should == ["{"]
|
||||
/\}/.match("}").to_a.should == ["}"]
|
||||
# alternation separator
|
||||
/\|/.match("|").to_a.should == ["|"]
|
||||
# quantifiers
|
||||
/\?/.match("?").to_a.should == ["?"]
|
||||
/\./.match(".").to_a.should == ["."]
|
||||
/\*/.match("*").to_a.should == ["*"]
|
||||
/\+/.match("+").to_a.should == ["+"]
|
||||
# line anchors
|
||||
/\^/.match("^").to_a.should == ["^"]
|
||||
/\$/.match("$").to_a.should == ["$"]
|
||||
end
|
||||
|
||||
=begin
|
||||
it "allow any character to be escaped" do
|
||||
/\y/.match("y").to_a.should == ["y"]
|
||||
end
|
||||
=end
|
||||
|
||||
it "support \\x (hex characters)" do
|
||||
/\xA/.match("\nxyz").to_a.should == ["\n"]
|
||||
/\x0A/.match("\n").to_a.should == ["\n"]
|
||||
/\xAA/.match("\nA").should be_nil
|
||||
/\x0AA/.match("\nA").to_a.should == ["\nA"]
|
||||
/\xAG/.match("\nG").to_a.should == ["\nG"]
|
||||
# Non-matches
|
||||
# lambda { eval('/\xG/') }.should raise_error(SyntaxError)
|
||||
|
||||
# \x{7HHHHHHH} wide hexadecimal char (character code point value)
|
||||
end
|
||||
|
||||
it "support \\c (control characters)" do
|
||||
#/\c \c@\c`/.match("\00\00\00").to_a.should == ["\00\00\00"]
|
||||
/\c#\cc\cC/.match("\03\03\03").to_a.should == ["\03\03\03"]
|
||||
/\c'\cG\cg/.match("\a\a\a").to_a.should == ["\a\a\a"]
|
||||
/\c(\cH\ch/.match("\b\b\b").to_a.should == ["\b\b\b"]
|
||||
/\c)\cI\ci/.match("\t\t\t").to_a.should == ["\t\t\t"]
|
||||
/\c*\cJ\cj/.match("\n\n\n").to_a.should == ["\n\n\n"]
|
||||
/\c+\cK\ck/.match("\v\v\v").to_a.should == ["\v\v\v"]
|
||||
/\c,\cL\cl/.match("\f\f\f").to_a.should == ["\f\f\f"]
|
||||
/\c-\cM\cm/.match("\r\r\r").to_a.should == ["\r\r\r"]
|
||||
|
||||
/\cJ/.match("\r").should be_nil
|
||||
|
||||
# Parsing precedence
|
||||
/\cJ+/.match("\n\n").to_a.should == ["\n\n"] # Quantifers apply to entire escape sequence
|
||||
/\\cJ/.match("\\cJ").to_a.should == ["\\cJ"]
|
||||
# lambda { eval('/[abc\x]/') }.should raise_error(SyntaxError) # \x is treated as a escape sequence even inside a character class
|
||||
# Syntax error
|
||||
# lambda { eval('/\c/') }.should raise_error(SyntaxError)
|
||||
|
||||
# \cx control char (character code point value)
|
||||
# \C-x control char (character code point value)
|
||||
# \M-x meta (x|0x80) (character code point value)
|
||||
# \M-\C-x meta control char (character code point value)
|
||||
end
|
||||
end
|
||||
22
test/test-android/app/language/regexp/grouping_spec.rb
Normal file
22
test/test-android/app/language/regexp/grouping_spec.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
describe "Regexps with grouping" do
|
||||
it 'support ()' do
|
||||
/(a)/.match("a").to_a.should == ["a", "a"]
|
||||
end
|
||||
|
||||
it "allow groups to be nested" do
|
||||
md = /(hay(st)a)ck/.match('haystack')
|
||||
md.to_a.should == ['haystack','haysta', 'st']
|
||||
end
|
||||
|
||||
=begin
|
||||
it "raise a SyntaxError when parentheses aren't balanced" do
|
||||
lambda { eval "/(hay(st)ack/" }.should raise_error(SyntaxError)
|
||||
end
|
||||
=end
|
||||
|
||||
it 'supports (?: ) (non-capturing group)' do
|
||||
/(?:foo)(bar)/.match("foobar").to_a.should == ["foobar", "bar"]
|
||||
# Parsing precedence
|
||||
/(?:xdigit:)/.match("xdigit:").to_a.should == ["xdigit:"]
|
||||
end
|
||||
end
|
||||
63
test/test-android/app/language/regexp/interpolation_spec.rb
Normal file
63
test/test-android/app/language/regexp/interpolation_spec.rb
Normal file
@@ -0,0 +1,63 @@
|
||||
class Language_Regexp_Interpolation_Mock1 < Java::Lang::Object
|
||||
def to_s
|
||||
'class_with_to_s'
|
||||
end
|
||||
end
|
||||
|
||||
describe "Regexps with interpolation" do
|
||||
|
||||
it "allow interpolation of strings" do
|
||||
str = "foo|bar"
|
||||
/#{str}/.should == /foo|bar/
|
||||
end
|
||||
|
||||
it "allows interpolation of literal regexps" do
|
||||
re = /foo|bar/
|
||||
#/#{re}/.should == /(?-mix:foo|bar)/
|
||||
/#{re}/.should == /(?m-ix:foo|bar)/
|
||||
end
|
||||
|
||||
it "allows interpolation of any class that responds to to_s" do
|
||||
o = Language_Regexp_Interpolation_Mock1.new
|
||||
/#{o}/.should == /class_with_to_s/
|
||||
end
|
||||
|
||||
it "allows interpolation which mixes modifiers" do
|
||||
re = /foo/i
|
||||
#/#{re} bar/m.should == /(?i-mx:foo) bar/m
|
||||
/#{re} bar/m.should == /(?mi-x:foo) bar/m
|
||||
end
|
||||
|
||||
it "allows interpolation to interact with other Regexp constructs" do
|
||||
str = "foo)|(bar"
|
||||
/(#{str})/.should == /(foo)|(bar)/
|
||||
|
||||
str = "a"
|
||||
/[#{str}-z]/.should == /[a-z]/
|
||||
end
|
||||
|
||||
# MacRuby TODO: These fail to parse with `macruby`, but not with `miniruby`
|
||||
#
|
||||
# it "gives precedence to escape sequences over substitution" do
|
||||
# str = "J"
|
||||
# /\c#{str}/.to_s.should == '(?-mix:\c#' + '{str})'
|
||||
# end
|
||||
|
||||
it "throws RegexpError for malformed interpolation" do
|
||||
s = ""
|
||||
lambda { /(#{s}/ }.should raise_error(RegexpError)
|
||||
s = "("
|
||||
lambda { /#{s}/ }.should raise_error(RegexpError)
|
||||
end
|
||||
|
||||
it "allows interpolation in extended mode" do
|
||||
var = "#comment\n foo #comment\n | bar"
|
||||
(/#{var}/x =~ "foo").should == (/foo|bar/ =~ "foo")
|
||||
end
|
||||
|
||||
it "allows escape sequences in interpolated regexps" do
|
||||
escape_seq = %r{"\x80"}n
|
||||
#%r{#{escape_seq}}n.should == /(?-mix:"\x80")/n
|
||||
%r{#{escape_seq}}n.should == /(?m-ix:"\x80")/n
|
||||
end
|
||||
end
|
||||
45
test/test-android/app/language/regexp/repetition_spec.rb
Normal file
45
test/test-android/app/language/regexp/repetition_spec.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
describe "Regexps with repetition" do
|
||||
it 'supports * (0 or more of previous subexpression)' do
|
||||
/a*/.match("aaa").to_a.should == ["aaa"]
|
||||
/a*/.match("bbb").to_a.should == [""]
|
||||
/<.*>/.match("<a>foo</a>").to_a.should == ["<a>foo</a>"] # it is greedy
|
||||
end
|
||||
|
||||
it 'supports *? (0 or more of previous subexpression - lazy)' do
|
||||
/a*?/.match("aaa").to_a.should == [""]
|
||||
/<.*?>/.match("<a>foo</a>").to_a.should == ["<a>"]
|
||||
end
|
||||
|
||||
it 'supports + (1 or more of previous subexpression)' do
|
||||
/a+/.match("aaa").to_a.should == ["aaa"]
|
||||
/a+/.match("bbb").should be_nil
|
||||
/<.+>/.match("<a>foo</a>").to_a.should == ["<a>foo</a>"] # it is greedy
|
||||
end
|
||||
|
||||
it 'supports +? (0 or more of previous subexpression - lazy)' do
|
||||
/a+?/.match("aaa").to_a.should == ["a"]
|
||||
/<.+?>/.match("<a>foo</a>").to_a.should == ["<a>"]
|
||||
end
|
||||
|
||||
it 'supports {m,n} (m to n of previous subexpression)' do
|
||||
/a{2,4}/.match("aaaaaa").to_a.should == ["aaaa"]
|
||||
/<.{1,}>/.match("<a>foo</a>").to_a.should == ["<a>foo</a>"] # it is greedy
|
||||
end
|
||||
|
||||
it 'supports {m,n}? (m to n of previous subexpression) - lazy)' do
|
||||
/<.{1,}?>/.match("<a>foo</a>").to_a.should == ["<a>"]
|
||||
/.([0-9]){3,5}?foo/.match("9876543210foo").to_a.should == ["543210foo", "0"]
|
||||
end
|
||||
|
||||
=begin
|
||||
# Does not pass on MacRuby/RubyMotion
|
||||
it 'does not treat {m,n}+ as possessive' do
|
||||
/foo(A{0,1}+)Abar/.match("fooAAAbar").to_a.should == ["fooAAAbar", "AA"]
|
||||
end
|
||||
=end
|
||||
|
||||
it 'supports ? (0 or 1 of previous subexpression)' do
|
||||
/a?/.match("aaa").to_a.should == ["a"]
|
||||
/a?/.match("bbb").to_a.should == [""]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user