/spec/frozen/language/regexp/interpolation_spec.rb

https://github.com/timon/MacRuby · Ruby · 52 lines · 36 code · 10 blank · 6 comment · 1 complexity · 1a4912017ed0f987bfc52e9bd1fec96b MD5 · raw file

  1. require File.expand_path('../../../spec_helper', __FILE__)
  2. require File.expand_path('../../fixtures/classes', __FILE__)
  3. describe "Regexps with interpolation" do
  4. it "allow interpolation of strings" do
  5. str = "foo|bar"
  6. /#{str}/.should == /foo|bar/
  7. end
  8. it "allows interpolation of literal regexps" do
  9. re = /foo|bar/
  10. /#{re}/.should == /(?-mix:foo|bar)/
  11. end
  12. it "allows interpolation of any class that responds to to_s" do
  13. o = LanguageSpecs::ClassWith_to_s.new
  14. /#{o}/.should == /class_with_to_s/
  15. end
  16. it "allows interpolation which mixes modifiers" do
  17. re = /foo/i
  18. /#{re} bar/m.should == /(?i-mx:foo) bar/m
  19. end
  20. it "allows interpolation to interact with other Regexp constructs" do
  21. str = "foo)|(bar"
  22. /(#{str})/.should == /(foo)|(bar)/
  23. str = "a"
  24. /[#{str}-z]/.should == /[a-z]/
  25. end
  26. # MacRuby TODO: These fail to parse with `macruby`, but not with `miniruby`
  27. #
  28. # it "gives precedence to escape sequences over substitution" do
  29. # str = "J"
  30. # /\c#{str}/.to_s.should == '(?-mix:\c#' + '{str})'
  31. # end
  32. it "throws RegexpError for malformed interpolation" do
  33. s = ""
  34. lambda { /(#{s}/ }.should raise_error(RegexpError)
  35. s = "("
  36. lambda { /#{s}/ }.should raise_error(RegexpError)
  37. end
  38. it "allows interpolation in extended mode" do
  39. var = "#comment\n foo #comment\n | bar"
  40. (/#{var}/x =~ "foo").should == (/foo|bar/ =~ "foo")
  41. end
  42. end