aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/language/magic_comment_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/language/magic_comment_spec.rb')
-rw-r--r--spec/ruby/language/magic_comment_spec.rb108
1 files changed, 67 insertions, 41 deletions
diff --git a/spec/ruby/language/magic_comment_spec.rb b/spec/ruby/language/magic_comment_spec.rb
index 3042a3f3df..c3abf3cc94 100644
--- a/spec/ruby/language/magic_comment_spec.rb
+++ b/spec/ruby/language/magic_comment_spec.rb
@@ -1,62 +1,88 @@
require_relative '../spec_helper'
-describe "Magic comment" do
- it "is optional" do
- eval("__ENCODING__").should be_an_instance_of(Encoding)
+# See core/kernel/eval_spec.rb for more magic comments specs for eval()
+describe :magic_comments, shared: true do
+ it "are optional" do
+ @object.call('no_magic_comment.rb').should == Encoding::UTF_8.name
end
- it "determines __ENCODING__" do
- eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT
-# encoding: ASCII-8BIT
-__ENCODING__
-EOS
- end
-
- it "is case-insensitive" do
- eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT
-# CoDiNg: aScIi-8bIt
-__ENCODING__
-EOS
+ it "are case-insensitive" do
+ @object.call('case_magic_comment.rb').should == Encoding::Big5.name
end
it "must be at the first line" do
- eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::US_ASCII
-
-# encoding: ASCII-8BIT
-__ENCODING__
-EOS
+ @object.call('second_line_magic_comment.rb').should == Encoding::UTF_8.name
end
it "must be the first token of the line" do
- eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::US_ASCII
-1+1 # encoding: ASCII-8BIT
-__ENCODING__
-EOS
- eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT
- # encoding: ASCII-8BIT
-__ENCODING__
-EOS
+ @object.call('second_token_magic_comment.rb').should == Encoding::UTF_8.name
end
it "can be after the shebang" do
- eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT
-#!/usr/bin/ruby -Ku
-# encoding: ASCII-8BIT
-__ENCODING__
-EOS
+ @object.call('shebang_magic_comment.rb').should == Encoding::Big5.name
end
it "can take Emacs style" do
- eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT
-# -*- encoding: ascii-8bit -*-
-__ENCODING__
-EOS
+ @object.call('emacs_magic_comment.rb').should == Encoding::Big5.name
end
it "can take vim style" do
- eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT
-# vim: filetype=ruby, fileencoding=ascii-8bit, tabsize=3, shiftwidth=3
-__ENCODING__
-EOS
+ @object.call('vim_magic_comment.rb').should == Encoding::Big5.name
+ end
+
+ it "determine __ENCODING__" do
+ @object.call('magic_comment.rb').should == Encoding::Big5.name
+ end
+
+ it "do not cause bytes to be mangled by passing them through the wrong encoding" do
+ @object.call('bytes_magic_comment.rb').should == [167, 65, 166, 110].inspect
+ end
+end
+
+describe "Magic comments" do
+ platform_is_not :windows do
+ describe "in stdin" do
+ it_behaves_like :magic_comments, nil, -> file {
+ print_at_exit = fixture(__FILE__, "print_magic_comment_result_at_exit.rb")
+ ruby_exe(nil, args: "< #{fixture(__FILE__, file)}", options: "-r#{print_at_exit}")
+ }
+ end
+
+ describe "in an -e argument" do
+ it_behaves_like :magic_comments, nil, -> file {
+ print_at_exit = fixture(__FILE__, "print_magic_comment_result_at_exit.rb")
+ # Use UTF-8, as it is the default source encoding for files
+ code = File.read(fixture(__FILE__, file), encoding: 'utf-8')
+ IO.popen([*ruby_exe, "-r", print_at_exit, "-e", code], &:read)
+ }
+ end
+ end
+
+ describe "in the main file" do
+ it_behaves_like :magic_comments, nil, -> file {
+ print_at_exit = fixture(__FILE__, "print_magic_comment_result_at_exit.rb")
+ ruby_exe(fixture(__FILE__, file), options: "-r#{print_at_exit}")
+ }
+ end
+
+ describe "in a loaded file" do
+ it_behaves_like :magic_comments, nil, -> file {
+ load fixture(__FILE__, file)
+ $magic_comment_result
+ }
+ end
+
+ describe "in a required file" do
+ it_behaves_like :magic_comments, nil, -> file {
+ require fixture(__FILE__, file)
+ $magic_comment_result
+ }
+ end
+
+ describe "in an eval" do
+ it_behaves_like :magic_comments, nil, -> file {
+ # Use UTF-8, as it is the default source encoding for files
+ eval(File.read(fixture(__FILE__, file), encoding: 'utf-8'))
+ }
end
end