2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../spec_helper'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2018-08-27 10:25:00 -04:00
|
|
|
# See core/kernel/eval_spec.rb for more magic comments specs for eval()
|
|
|
|
describe :magic_comments, shared: true do
|
2018-08-27 12:22:21 -04:00
|
|
|
before :each do
|
|
|
|
@default = @method == :locale ? Encoding.find('locale') : Encoding::UTF_8
|
|
|
|
end
|
|
|
|
|
2018-08-27 10:25:00 -04:00
|
|
|
it "are optional" do
|
2018-08-27 12:22:21 -04:00
|
|
|
@object.call('no_magic_comment.rb').should == @default.name
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 10:25:00 -04:00
|
|
|
it "are case-insensitive" do
|
|
|
|
@object.call('case_magic_comment.rb').should == Encoding::Big5.name
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "must be at the first line" do
|
2018-08-27 12:22:21 -04:00
|
|
|
@object.call('second_line_magic_comment.rb').should == @default.name
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "must be the first token of the line" do
|
2018-08-27 12:22:21 -04:00
|
|
|
@object.call('second_token_magic_comment.rb').should == @default.name
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be after the shebang" do
|
2018-08-27 10:25:00 -04:00
|
|
|
@object.call('shebang_magic_comment.rb').should == Encoding::Big5.name
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can take Emacs style" do
|
2018-08-27 10:25:00 -04:00
|
|
|
@object.call('emacs_magic_comment.rb').should == Encoding::Big5.name
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can take vim style" do
|
2018-08-27 10:25:00 -04:00
|
|
|
@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
|
2018-08-27 12:22:21 -04:00
|
|
|
describe "in stdin" do
|
|
|
|
it_behaves_like :magic_comments, :locale, -> 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
|
2018-08-27 10:25:00 -04:00
|
|
|
|
2018-08-27 12:22:21 -04:00
|
|
|
platform_is_not :windows do
|
2018-08-27 10:25:00 -04:00
|
|
|
describe "in an -e argument" do
|
2018-08-27 12:22:21 -04:00
|
|
|
it_behaves_like :magic_comments, :locale, -> file {
|
2018-08-27 10:25:00 -04:00
|
|
|
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
|
2018-08-27 12:22:21 -04:00
|
|
|
it_behaves_like :magic_comments, :UTF8, -> file {
|
2018-08-27 10:25:00 -04:00
|
|
|
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
|
2018-08-27 12:22:21 -04:00
|
|
|
it_behaves_like :magic_comments, :UTF8, -> file {
|
2018-08-27 10:25:00 -04:00
|
|
|
load fixture(__FILE__, file)
|
|
|
|
$magic_comment_result
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "in a required file" do
|
2018-08-27 12:22:21 -04:00
|
|
|
it_behaves_like :magic_comments, :UTF8, -> file {
|
2018-08-27 10:25:00 -04:00
|
|
|
require fixture(__FILE__, file)
|
|
|
|
$magic_comment_result
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "in an eval" do
|
2018-08-27 12:22:21 -04:00
|
|
|
it_behaves_like :magic_comments, :UTF8, -> file {
|
2018-08-27 10:25:00 -04:00
|
|
|
# Use UTF-8, as it is the default source encoding for files
|
|
|
|
eval(File.read(fixture(__FILE__, file), encoding: 'utf-8'))
|
|
|
|
}
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|