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
|
# -*- encoding: us-ascii -*-
require File.expand_path('../../spec_helper', __FILE__)
# TODO: rewrite these horrid specs. it "are..." seriously?!
describe "Ruby character strings" do
before(:each) do
@ip = 'xxx' # used for interpolation
$ip = 'xxx'
@@ip = 'xxx'
end
after :all do
Object.__send__(:remove_class_variable, :@@ip)
end
it "don't get interpolated when put in single quotes" do
'#{@ip}'.should == '#{@ip}'
end
it 'get interpolated with #{} when put in double quotes' do
"#{@ip}".should == 'xxx'
end
it "interpolate instance variables just with the # character" do
"#@ip".should == 'xxx'
end
it "interpolate global variables just with the # character" do
"#$ip".should == 'xxx'
end
it "interpolate class variables just with the # character" do
"#@@ip".should == 'xxx'
end
it "allow underscore as part of a variable name in a simple interpolation" do
@my_ip = 'xxx'
"#@my_ip".should == 'xxx'
end
it "have characters [.(=?!# end simple # interpolation" do
"#@ip[".should == 'xxx['
"#@ip.".should == 'xxx.'
"#@ip(".should == 'xxx('
"#@ip=".should == 'xxx='
"#@ip?".should == 'xxx?'
"#@ip!".should == 'xxx!'
"#@ip#@ip".should == 'xxxxxx'
end
it "taints the result of interpolation when an interpolated value is tainted" do
"#{"".taint}".tainted?.should be_true
@ip.taint
"#@ip".tainted?.should be_true
@@ip.taint
"#@@ip".tainted?.should be_true
$ip.taint
"#$ip".tainted?.should be_true
end
ruby_version_is "1.9" do
it "untrusts the result of interpolation when an interpolated value is untrusted" do
"#{"".untrust}".untrusted?.should be_true
@ip.untrust
"#@ip".untrusted?.should be_true
@@ip.untrust
"#@@ip".untrusted?.should be_true
$ip.untrust
"#$ip".untrusted?.should be_true
end
end
it "allow using non-alnum characters as string delimiters" do
%(hey #{@ip}).should == "hey xxx"
%[hey #{@ip}].should == "hey xxx"
%{hey #{@ip}}.should == "hey xxx"
%<hey #{@ip}>.should == "hey xxx"
%!hey #{@ip}!.should == "hey xxx"
%@hey #{@ip}@.should == "hey xxx"
%#hey hey#.should == "hey hey"
%%hey #{@ip}%.should == "hey xxx"
%^hey #{@ip}^.should == "hey xxx"
%&hey #{@ip}&.should == "hey xxx"
%*hey #{@ip}*.should == "hey xxx"
%-hey #{@ip}-.should == "hey xxx"
%_hey #{@ip}_.should == "hey xxx"
%=hey #{@ip}=.should == "hey xxx"
%+hey #{@ip}+.should == "hey xxx"
%~hey #{@ip}~.should == "hey xxx"
%:hey #{@ip}:.should == "hey xxx"
%;hey #{@ip};.should == "hey xxx"
%"hey #{@ip}".should == "hey xxx"
%|hey #{@ip}|.should == "hey xxx"
%?hey #{@ip}?.should == "hey xxx"
%/hey #{@ip}/.should == "hey xxx"
%,hey #{@ip},.should == "hey xxx"
%.hey #{@ip}..should == "hey xxx"
# surprised? huh
%'hey #{@ip}'.should == "hey xxx"
%\hey #{@ip}\.should == "hey xxx"
%`hey #{@ip}`.should == "hey xxx"
%$hey #{@ip}$.should == "hey xxx"
end
it "using percent with 'q', stopping interpolation" do
%q(#{@ip}).should == '#{@ip}'
end
it "using percent with 'Q' to interpolate" do
%Q(#{@ip}).should == 'xxx'
end
# The backslashes :
#
# \t (tab), \n (newline), \r (carriage return), \f (form feed), \b
# (backspace), \a (bell), \e (escape), \s (whitespace), \nnn (octal),
# \xnn (hexadecimal), \cx (control x), \C-x (control x), \M-x (meta x),
# \M-\C-x (meta control x)
it "backslashes follow the same rules as interpolation" do
"\t\n\r\f\b\a\e\s\075\x62\cx".should == "\t\n\r\f\b\a\e =b\030"
'\t\n\r\f\b\a\e =b\030'.should == "\\t\\n\\r\\f\\b\\a\\e =b\\030"
end
it "allow HEREDOC with <<identifier, interpolated" do
s = <<HERE
foo bar#{@ip}
HERE
s.should == "foo barxxx\n"
end
it 'allow HEREDOC with <<"identifier", interpolated' do
s = <<"HERE"
foo bar#{@ip}
HERE
s.should == "foo barxxx\n"
end
it "allow HEREDOC with <<'identifier', no interpolation" do
s = <<'HERE'
foo bar#{@ip}
HERE
s.should == 'foo bar#{@ip}' + "\n"
end
it "allow HEREDOC with <<-identifier, allowing to indent identifier, interpolated" do
s = <<-HERE
foo bar#{@ip}
HERE
s.should == " foo barxxx\n"
end
it 'allow HEREDOC with <<-"identifier", allowing to indent identifier, interpolated' do
s = <<-"HERE"
foo bar#{@ip}
HERE
s.should == " foo barxxx\n"
end
it "allow HEREDOC with <<-'identifier', allowing to indent identifier, no interpolation" do
s = <<-'HERE'
foo bar#{@ip}
HERE
s.should == ' foo bar#{@ip}' + "\n"
end
it "call #to_s when the object is not a String" do
obj = mock('to_s')
obj.stub!(:to_s).and_return('42')
"#{obj}".should == '42'
end
it "call #to_s as a private method" do
obj = mock('to_s')
obj.stub!(:to_s).and_return('42')
class << obj
private :to_s
end
"#{obj}".should == '42'
end
it "uses an internal representation when #to_s doesn't return a String" do
obj = mock('to_s')
obj.stub!(:to_s).and_return(42)
# See rubyspec commit 787c132d by yugui. There is value in
# ensuring that this behavior works. So rather than removing
# this spec completely, the only thing that can be asserted
# is that if you interpolate an object that fails to return
# a String, you will still get a String and not raise an
# exception.
"#{obj}".should be_an_instance_of(String)
end
it "allow a dynamic string to parse a nested do...end block as an argument to a call without parens, interpolated" do
s = eval 'eval "#{proc do; 1; end.call}"'
s.should == 1
end
ruby_version_is '1.9' do
it "are produced from character shortcuts" do
?z.should == 'z'
end
it "are produced from control character shortcuts" do
# Control-Z
?\C-z.should == "\x1A"
# Meta-Z
?\M-z.should == "\xFA"
# Meta-Control-Z
?\M-\C-z.should == "\x9A"
end
describe "Unicode escaping" do
it "can be done with \\u and four hex digits" do
[ ["\u0000", 0x0000],
["\u2020", 0x2020]
].should be_computed_by(:ord)
end
it "can be done with \\u{} and one to six hex digits" do
[ ["\u{a}", 0xa],
["\u{ab}", 0xab],
["\u{abc}", 0xabc],
["\u{1abc}", 0x1abc],
["\u{12abc}", 0x12abc],
["\u{100000}", 0x100000]
].should be_computed_by(:ord)
end
# TODO: spec other source encodings
describe "with US-ASCII source encoding" do
it "produces an ASCII string when escaping ASCII characters via \\u" do
"\u0000".encoding.should == Encoding::US_ASCII
end
it "produces an ASCII string when escaping ASCII characters via \\u{}" do
"\u{0000}".encoding.should == Encoding::US_ASCII
end
it "produces a UTF-8-encoded string when escaping non-ASCII characters via \\u" do
"\u1234".encoding.should == Encoding::UTF_8
end
it "produces a UTF-8-encoded string when escaping non-ASCII characters via \\u{}" do
"\u{1234}".encoding.should == Encoding::UTF_8
end
end
end
end
end
# TODO: rewrite all specs above this
with_feature :encoding do
describe "Ruby String interpolation" do
it "creates a String having an Encoding compatible with all components" do
a = "\u3042"
b = "abc".encode("ascii-8bit")
str = "#{a} x #{b}"
str.should == "\xe3\x81\x82\x20\x78\x20\x61\x62\x63".force_encoding("utf-8")
str.encoding.should == Encoding::UTF_8
end
it "creates a String having the Encoding of the components when all are the same Encoding" do
a = "abc".force_encoding("euc-jp")
b = "def".force_encoding("euc-jp")
str = '"#{a} x #{b}"'.force_encoding("euc-jp")
result = eval(str)
result.should == "\x61\x62\x63\x20\x78\x20\x64\x65\x66".force_encoding("euc-jp")
result.encoding.should == Encoding::EUC_JP
end
it "raises an Encoding::CompatibilityError if the Encodings are not compatible" do
a = "\u3042"
b = "\xff".force_encoding "ascii-8bit"
lambda { "#{a} #{b}" }.should raise_error(Encoding::CompatibilityError)
end
end
end
|