/External.LCA_RESTRICTED/Languages/IronRuby/mspec/rubyspec/language/regexp/interpolation_spec.rb

https://github.com/thomo13/ironruby · Ruby · 55 lines · 44 code · 11 blank · 0 comment · 1 complexity · ecb2309b44b05b46082e8dfebee954e3 MD5 · raw file

  1. require File.dirname(__FILE__) + '/../../spec_helper'
  2. require File.expand_path(File.dirname(__FILE__) + '/../fixtures/classes')
  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 "throws NoMethodError on missing to_s" do
  17. o = LanguageSpecs::ClassWithout_to_s.new
  18. lambda { /#{o}/ }.should raise_error(NoMethodError)
  19. end
  20. it "allows interpolation which mixes modifiers" do
  21. re = /foo/i
  22. /#{re} bar/m.should == /(?i-mx:foo) bar/m
  23. end
  24. it "allows interpolation to interact with other Regexp constructs" do
  25. str = "foo)|(bar"
  26. /(#{str})/.should == /(foo)|(bar)/
  27. str = "a"
  28. /[#{str}-z]/.should == /[a-z]/
  29. end
  30. it "gives precedence to escape sequences over substitution" do
  31. str = "J"
  32. /\c#{str}/.to_s.should == '(?-mix:\c#' + '{str})'
  33. end
  34. it "throws RegexpError for malformed interpolation" do
  35. s = ""
  36. lambda { /(#{s}/ }.should raise_error(RegexpError)
  37. s = "("
  38. lambda { /#{s}/ }.should raise_error(RegexpError)
  39. end
  40. it "allows interpolation in extended mode" do
  41. var = "#comment\n foo #comment\n | bar"
  42. (/#{var}/x =~ "foo").should == (/foo|bar/ =~ "foo")
  43. end
  44. end