summaryrefslogtreecommitdiff
path: root/spec/ruby/core/kernel/eval_spec.rb
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-27 12:30:05 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-27 12:30:05 +0000
commit9dc121cc577ae7a010bca7efedb79088e3cf7331 (patch)
treec11a153c7eac91a1e19ed058d5c28f0f7d583622 /spec/ruby/core/kernel/eval_spec.rb
parentfc1f3f14d386b557281ff9a8f19da060befe182e (diff)
Update to ruby/spec@a454137
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63768 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/kernel/eval_spec.rb')
-rw-r--r--spec/ruby/core/kernel/eval_spec.rb128
1 files changed, 128 insertions, 0 deletions
diff --git a/spec/ruby/core/kernel/eval_spec.rb b/spec/ruby/core/kernel/eval_spec.rb
index ad06af7b05..fbae8ff3c7 100644
--- a/spec/ruby/core/kernel/eval_spec.rb
+++ b/spec/ruby/core/kernel/eval_spec.rb
@@ -213,4 +213,132 @@ describe "Kernel#eval" do
code = fixture __FILE__, "eval_return_without_lambda.rb"
ruby_exe(code).chomp.should == "a,b,c,e,LocalJumpError,f"
end
+
+ describe "with a magic encoding comment" do
+ it "uses the magic comment encoding for the encoding of literal strings" do
+ code = "# encoding: UTF-8\n'é'.encoding".b
+ code.encoding.should == Encoding::BINARY
+ eval(code).should == Encoding::UTF_8
+ end
+
+ it "uses the magic comment encoding for parsing constants" do
+ code = <<CODE.b
+# encoding: UTF-8
+class EvalSpecs
+ Vπ = 3.14
+end
+CODE
+ code.encoding.should == Encoding::BINARY
+ eval(code)
+ EvalSpecs.constants(false).should include(:"Vπ")
+ EvalSpecs::Vπ.should == 3.14
+ end
+
+ it "allows an emacs-style magic comment encoding" do
+ code = <<CODE.b
+# -*- encoding: UTF-8 -*-
+class EvalSpecs
+Vπemacs = 3.14
+end
+CODE
+ code.encoding.should == Encoding::BINARY
+ eval(code)
+ EvalSpecs.constants(false).should include(:"Vπemacs")
+ EvalSpecs::Vπemacs.should == 3.14
+ end
+
+ it "allows spaces before the magic encoding comment" do
+ code = <<CODE.b
+\t \t # encoding: UTF-8
+class EvalSpecs
+ Vπspaces = 3.14
+end
+CODE
+ code.encoding.should == Encoding::BINARY
+ eval(code)
+ EvalSpecs.constants(false).should include(:"Vπspaces")
+ EvalSpecs::Vπspaces.should == 3.14
+ end
+
+ it "allows a shebang line before the magic encoding comment" do
+ code = <<CODE.b
+#!/usr/bin/env ruby
+# encoding: UTF-8
+class EvalSpecs
+ Vπshebang = 3.14
+end
+CODE
+ code.encoding.should == Encoding::BINARY
+ eval(code)
+ EvalSpecs.constants(false).should include(:"Vπshebang")
+ EvalSpecs::Vπshebang.should == 3.14
+ end
+
+ it "allows a shebang line and some spaces before the magic encoding comment" do
+ code = <<CODE.b
+#!/usr/bin/env ruby
+ # encoding: UTF-8
+class EvalSpecs
+ Vπshebang_spaces = 3.14
+end
+CODE
+ code.encoding.should == Encoding::BINARY
+ eval(code)
+ EvalSpecs.constants(false).should include(:"Vπshebang_spaces")
+ EvalSpecs::Vπshebang_spaces.should == 3.14
+ end
+
+ it "allows a magic encoding comment and a subsequent frozen_string_literal magic comment" do
+ # Make sure frozen_string_literal is not default true
+ eval("'foo'".b).frozen?.should be_false
+
+ code = <<CODE.b
+# encoding: UTF-8
+# frozen_string_literal: true
+class EvalSpecs
+ Vπstring = "frozen"
+end
+CODE
+ code.encoding.should == Encoding::BINARY
+ eval(code)
+ EvalSpecs.constants(false).should include(:"Vπstring")
+ EvalSpecs::Vπstring.should == "frozen"
+ EvalSpecs::Vπstring.encoding.should == Encoding::UTF_8
+ EvalSpecs::Vπstring.frozen?.should be_true
+ end
+
+ it "allows a magic encoding comment and a frozen_string_literal magic comment on the same line in emacs style" do
+ code = <<CODE.b
+# -*- encoding: UTF-8; frozen_string_literal: true -*-
+class EvalSpecs
+Vπsame_line = "frozen"
+end
+CODE
+ code.encoding.should == Encoding::BINARY
+ eval(code)
+ EvalSpecs.constants(false).should include(:"Vπsame_line")
+ EvalSpecs::Vπsame_line.should == "frozen"
+ EvalSpecs::Vπsame_line.encoding.should == Encoding::UTF_8
+ EvalSpecs::Vπsame_line.frozen?.should be_true
+ end
+
+ it "ignores the magic encoding comment if it is after a frozen_string_literal magic comment" do
+ code = <<CODE.b
+# frozen_string_literal: true
+# encoding: UTF-8
+class EvalSpecs
+ Vπfrozen_first = "frozen"
+end
+CODE
+ code.encoding.should == Encoding::BINARY
+ eval(code)
+ EvalSpecs.constants(false).should_not include(:"Vπfrozen_first")
+ binary_constant = "Vπfrozen_first".b.to_sym
+ EvalSpecs.constants(false).should include(binary_constant)
+ value = EvalSpecs.const_get(binary_constant)
+ value.should == "frozen"
+ value.encoding.should == Encoding::BINARY
+ value.frozen?.should be_true
+ end
+ end
end