diff options
Diffstat (limited to 'spec/ruby/language/string_spec.rb')
| -rw-r--r-- | spec/ruby/language/string_spec.rb | 127 |
1 files changed, 57 insertions, 70 deletions
diff --git a/spec/ruby/language/string_spec.rb b/spec/ruby/language/string_spec.rb index 083a7f5db5..dbec2652ed 100644 --- a/spec/ruby/language/string_spec.rb +++ b/spec/ruby/language/string_spec.rb @@ -1,6 +1,6 @@ # -*- encoding: binary -*- -require_relative '../spec_helper' +require File.expand_path('../../spec_helper', __FILE__) # TODO: rewrite these horrid specs. it "are..." seriously?! @@ -32,11 +32,6 @@ describe "Ruby character strings" do "#@my_ip".should == 'xxx' end - it "does not interpolate invalid variable names" do - "#@".should == '#@' - "#$%".should == '#$%' - end - it "has characters [.(=?!# end simple # interpolation" do "#@ip[".should == 'xxx[' "#@ip.".should == 'xxx.' @@ -47,13 +42,24 @@ describe "Ruby character strings" do "#@ip#@ip".should == 'xxxxxx' end - it "don't get confused by partial interpolation character sequences" do - "#@".should == '#@' - "#@ ".should == '#@ ' - "#@@".should == '#@@' - "#@@ ".should == '#@@ ' - "#$ ".should == '#$ ' - "#\$".should == '#$' + 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 + end + + 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 end it "allows using non-alnum characters as string delimiters" do @@ -180,11 +186,11 @@ describe "Ruby character strings" do # TODO: spec other source encodings describe "with ASCII_8BIT source encoding" do it "produces an ASCII string when escaping ASCII characters via \\u" do - "\u0000".encoding.should == Encoding::BINARY + "\u0000".encoding.should == Encoding::ASCII_8BIT end it "produces an ASCII string when escaping ASCII characters via \\u{}" do - "\u{0000}".encoding.should == Encoding::BINARY + "\u{0000}".encoding.should == Encoding::ASCII_8BIT end it "produces a UTF-8-encoded string when escaping non-ASCII characters via \\u" do @@ -218,78 +224,59 @@ describe "Ruby String literals" do long_string_literals.should == "Beautiful is better than ugly.Explicit is better than implicit." end - describe "with a magic frozen comment" do - it "produce the same object each time" do - ruby_exe(fixture(__FILE__, "freeze_magic_comment_one_literal.rb")).chomp.should == "true" - end + ruby_version_is "2.3" do + describe "with a magic frozen comment" do + it "produce the same object each time" do + ruby_exe(fixture(__FILE__, "freeze_magic_comment_one_literal.rb")).chomp.should == "true" + end - it "produce the same object for literals with the same content" do - ruby_exe(fixture(__FILE__, "freeze_magic_comment_two_literals.rb")).chomp.should == "true" - end + it "produce the same object for literals with the same content" do + ruby_exe(fixture(__FILE__, "freeze_magic_comment_two_literals.rb")).chomp.should == "true" + end - it "produce the same object for literals with the same content in different files" do - ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files.rb")).chomp.should == "true" - end + it "produce the same object for literals with the same content in different files" do + ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files.rb")).chomp.should == "true" + end - guard -> { !(eval("'test'").frozen? && "test".equal?("test")) } do - it "produces different objects for literals with the same content in different files if the other file doesn't have the comment and String literals aren't frozen by default" do + it "produce different objects for literals with the same content in different files if the other file doesn't have the comment" do ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files_no_comment.rb")).chomp.should == "true" end - end - guard -> { eval("'test'").frozen? && "test".equal?("test") } do - it "produces the same objects for literals with the same content in different files if the other file doesn't have the comment and String literals are frozen by default" do - ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files_no_comment.rb")).chomp.should == "false" + it "produce different objects for literals with the same content in different files if they have different encodings" do + ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files_diff_enc.rb")).chomp.should == "true" end end - - it "produce different objects for literals with the same content in different files if they have different encodings" do - ruby_exe(fixture(__FILE__, "freeze_magic_comment_across_files_diff_enc.rb")).chomp.should == "true" - end end end -describe "Ruby String interpolation" do - it "permits an empty expression" do - s = "#{}" # rubocop:disable Lint/EmptyInterpolation - s.should.empty? - s.should_not.frozen? - end - - it "returns a string with the source encoding by default" do - "a#{"b"}c".encoding.should == Encoding::BINARY - eval('"a#{"b"}c"'.dup.force_encoding("us-ascii")).encoding.should == Encoding::US_ASCII - eval("# coding: US-ASCII \n 'a#{"b"}c'").encoding.should == Encoding::US_ASCII - end +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") - it "returns a string with the source encoding, even if the components have another encoding" do - a = "abc".dup.force_encoding("euc-jp") - "#{a}".encoding.should == Encoding::BINARY + str = "#{a} x #{b}" - b = "abc".encode("utf-8") - "#{b}".encoding.should == Encoding::BINARY - end + str.should == "\xe3\x81\x82\x20\x78\x20\x61\x62\x63".force_encoding("utf-8") + str.encoding.should == Encoding::UTF_8 + end - it "raises an Encoding::CompatibilityError if the Encodings are not compatible" do - a = "\u3042" - b = "\xff".dup.force_encoding "binary" + 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") - -> { "#{a} #{b}" }.should raise_error(Encoding::CompatibilityError) - end + 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 "creates a non-frozen String" do - code = <<~'RUBY' - "a#{6*7}c" - RUBY - eval(code).should_not.frozen? - end + it "raises an Encoding::CompatibilityError if the Encodings are not compatible" do + a = "\u3042" + b = "\xff".force_encoding "ascii-8bit" - it "creates a non-frozen String when # frozen-string-literal: true is used" do - code = <<~'RUBY' - # frozen-string-literal: true - "a#{6*7}c" - RUBY - eval(code).should_not.frozen? + lambda { "#{a} #{b}" }.should raise_error(Encoding::CompatibilityError) + end end end |
