1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64565 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2018-08-27 14:25:00 +00:00
parent a641384815
commit 8180b5bfc0
54 changed files with 606 additions and 111 deletions

View file

@ -5,6 +5,8 @@ AllCops:
DisplayCopNames: true DisplayCopNames: true
Exclude: Exclude:
- command_line/fixtures/bad_syntax.rb - command_line/fixtures/bad_syntax.rb
- language/fixtures/utf16-le-bom.rb
- language/fixtures/utf16-be-bom.rb
DisabledByDefault: true DisabledByDefault: true
Layout/TrailingWhitespace: Layout/TrailingWhitespace:

View file

@ -110,12 +110,6 @@ Lint/RescueException:
- 'language/rescue_spec.rb' - 'language/rescue_spec.rb'
- 'library/erb/filename_spec.rb' - 'library/erb/filename_spec.rb'
# Offense count: 1
# Cop supports --auto-correct.
Lint/ScriptPermission:
Exclude:
- 'command_line/fixtures/bin/launcher.rb'
# Offense count: 2 # Offense count: 2
# Configuration parameters: IgnoreImplicitReferences. # Configuration parameters: IgnoreImplicitReferences.
Lint/ShadowedArgument: Lint/ShadowedArgument:

View file

@ -1,3 +1,5 @@
require_relative '../spec_helper'
describe "The -a command line option" do describe "The -a command line option" do
before :each do before :each do
@names = fixture __FILE__, "full_names.txt" @names = fixture __FILE__, "full_names.txt"

View file

@ -0,0 +1,33 @@
require_relative '../spec_helper'
describe 'The --encoding command line option' do
before :each do
@test_string = "print [Encoding.default_external.name, Encoding.default_internal&.name].inspect"
end
describe 'sets Encoding.default_external and optionally Encoding.default_internal' do
it "if given a single encoding with an =" do
ruby_exe(@test_string, options: '--disable-gems --encoding=big5').should == [Encoding::Big5.name, nil].inspect
end
it "if given a single encoding as a separate argument" do
ruby_exe(@test_string, options: '--disable-gems --encoding big5').should == [Encoding::Big5.name, nil].inspect
end
it "if given two encodings with an =" do
ruby_exe(@test_string, options: '--disable-gems --encoding=big5:utf-32be').should == [Encoding::Big5.name, Encoding::UTF_32BE.name].inspect
end
it "if given two encodings as a separate argument" do
ruby_exe(@test_string, options: '--disable-gems --encoding big5:utf-32be').should == [Encoding::Big5.name, Encoding::UTF_32BE.name].inspect
end
it "if given two encodings as a separate argument" do
ruby_exe(@test_string, options: '--disable-gems --encoding big5:utf-32be').should == [Encoding::Big5.name, Encoding::UTF_32BE.name].inspect
end
end
it "does not accept a third encoding" do
ruby_exe(@test_string, options: '--disable-gems --encoding big5:utf-32be:utf-32le', args: '2>&1').should =~ /extra argument for --encoding: utf-32le/
end
end

View file

@ -0,0 +1,15 @@
require_relative '../spec_helper'
describe 'The --external-encoding command line option sets Encoding.default_external' do
before :each do
@test_string = "print Encoding.default_external.name"
end
it "if given an encoding with an =" do
ruby_exe(@test_string, options: '--external-encoding=big5').should == Encoding::Big5.name
end
it "if given an encoding as a separate argument" do
ruby_exe(@test_string, options: '--external-encoding big5').should == Encoding::Big5.name
end
end

View file

@ -0,0 +1,15 @@
require_relative '../spec_helper'
describe 'The --internal-encoding command line option sets Encoding.default_internal' do
before :each do
@test_string = "print Encoding.default_internal.name"
end
it "if given an encoding with an =" do
ruby_exe(@test_string, options: '--internal-encoding=big5').should == Encoding::Big5.name
end
it "if given an encoding as a separate argument" do
ruby_exe(@test_string, options: '--internal-encoding big5').should == Encoding::Big5.name
end
end

View file

@ -1,3 +1,5 @@
require_relative '../spec_helper'
describe "The -n command line option" do describe "The -n command line option" do
before :each do before :each do
@names = fixture __FILE__, "names.txt" @names = fixture __FILE__, "names.txt"

View file

@ -1,3 +1,5 @@
require_relative '../spec_helper'
describe "The -p command line option" do describe "The -p command line option" do
before :each do before :each do
@names = fixture __FILE__, "names.txt" @names = fixture __FILE__, "names.txt"

View file

@ -1,3 +1,5 @@
require_relative '../spec_helper'
describe "ruby -E" do describe "ruby -E" do
it "sets the external encoding with '-E external'" do it "sets the external encoding with '-E external'" do
result = ruby_exe("print Encoding.default_external", options: '-E euc-jp') result = ruby_exe("print Encoding.default_external", options: '-E euc-jp')

View file

@ -1,3 +1,5 @@
require_relative '../spec_helper'
describe "the -F command line option" do describe "the -F command line option" do
before :each do before :each do
@passwd = fixture __FILE__, "passwd_file.txt" @passwd = fixture __FILE__, "passwd_file.txt"

View file

@ -1,33 +1,66 @@
describe 'The -K command line option sets __ENCODING__' do require_relative '../spec_helper'
describe 'The -K command line option' do
before :each do
@test_string = "print [__ENCODING__&.name, Encoding.default_external&.name, Encoding.default_internal&.name].inspect"
end
describe 'sets __ENCODING__ and Encoding.default_external' do
it "to Encoding::ASCII_8BIT with -Ka" do it "to Encoding::ASCII_8BIT with -Ka" do
ruby_exe("print __ENCODING__", options: '-Ka').should == Encoding::ASCII_8BIT.to_s ruby_exe(@test_string, options: '-Ka').should ==
[Encoding::ASCII_8BIT.name, Encoding::ASCII_8BIT.name, nil].inspect
end end
it "to Encoding::ASCII_8BIT with -KA" do it "to Encoding::ASCII_8BIT with -KA" do
ruby_exe("print __ENCODING__", options: '-KA').should == Encoding::ASCII_8BIT.to_s ruby_exe(@test_string, options: '-KA').should ==
[Encoding::ASCII_8BIT.name, Encoding::ASCII_8BIT.name, nil].inspect
end
it "to Encoding::ASCII_8BIT with -Kn" do
ruby_exe(@test_string, options: '-Kn').should ==
[Encoding::ASCII_8BIT.name, Encoding::ASCII_8BIT.name, nil].inspect
end
it "to Encoding::ASCII_8BIT with -KN" do
ruby_exe(@test_string, options: '-KN').should ==
[Encoding::ASCII_8BIT.name, Encoding::ASCII_8BIT.name, nil].inspect
end end
it "to Encoding::EUC_JP with -Ke" do it "to Encoding::EUC_JP with -Ke" do
ruby_exe("print __ENCODING__", options: '-Ke').should == Encoding::EUC_JP.to_s ruby_exe(@test_string, options: '-Ke').should ==
[Encoding::EUC_JP.name, Encoding::EUC_JP.name, nil].inspect
end end
it "to Encoding::EUC_JP with -KE" do it "to Encoding::EUC_JP with -KE" do
ruby_exe("print __ENCODING__", options: '-KE').should == Encoding::EUC_JP.to_s ruby_exe(@test_string, options: '-KE').should ==
[Encoding::EUC_JP.name, Encoding::EUC_JP.name, nil].inspect
end end
it "to Encoding::UTF_8 with -Ku" do it "to Encoding::UTF_8 with -Ku" do
ruby_exe("print __ENCODING__", options: '-Ku').should == Encoding::UTF_8.to_s ruby_exe(@test_string, options: '-Ku').should ==
[Encoding::UTF_8.name, Encoding::UTF_8.name, nil].inspect
end end
it "to Encoding::UTF_8 with -KU" do it "to Encoding::UTF_8 with -KU" do
ruby_exe("print __ENCODING__", options: '-KU').should == Encoding::UTF_8.to_s ruby_exe(@test_string, options: '-KU').should ==
[Encoding::UTF_8.name, Encoding::UTF_8.name, nil].inspect
end end
it "to Encoding::Windows_31J with -Ks" do it "to Encoding::Windows_31J with -Ks" do
ruby_exe("print __ENCODING__", options: '-Ks').should == Encoding::Windows_31J.to_s ruby_exe(@test_string, options: '-Ks').should ==
[Encoding::Windows_31J.name, Encoding::Windows_31J.name, nil].inspect
end end
it "to Encoding::Windows_31J with -KS" do it "to Encoding::Windows_31J with -KS" do
ruby_exe("print __ENCODING__", options: '-KS').should == Encoding::Windows_31J.to_s ruby_exe(@test_string, options: '-KS').should ==
[Encoding::Windows_31J.name, Encoding::Windows_31J.name, nil].inspect
end
end
platform_is_not :windows do
it "ignores unknown codes" do
ruby_exe(@test_string, options: '-KZ').should ==
[Encoding::UTF_8.name, Encoding::UTF_8.name, nil].inspect
end
end end
end end

View file

@ -1,3 +1,5 @@
require_relative '../spec_helper'
describe "ruby -U" do describe "ruby -U" do
it "sets Encoding.default_internal to UTF-8" do it "sets Encoding.default_internal to UTF-8" do
ruby_exe('print Encoding.default_internal.name', ruby_exe('print Encoding.default_internal.name',

View file

@ -1,3 +1,5 @@
require_relative '../spec_helper'
describe "The -x command line option" do describe "The -x command line option" do
it "runs code after the first /\#!.*ruby.*/-ish line in target file" do it "runs code after the first /\#!.*ruby.*/-ish line in target file" do
embedded_ruby = fixture __FILE__, "bin/embedded_ruby.txt" embedded_ruby = fixture __FILE__, "bin/embedded_ruby.txt"

0
spec/ruby/command_line/fixtures/bin/launcher.rb Normal file → Executable file
View file

View file

@ -214,6 +214,7 @@ describe "Kernel#eval" do
ruby_exe(code).chomp.should == "a,b,c,e,LocalJumpError,f" ruby_exe(code).chomp.should == "a,b,c,e,LocalJumpError,f"
end end
# See language/magic_comment_spec.rb for more magic comments specs
describe "with a magic encoding comment" do describe "with a magic encoding comment" do
it "uses the magic comment encoding for the encoding of literal strings" do it "uses the magic comment encoding for the encoding of literal strings" do
code = "# encoding: UTF-8\n'é'.encoding".b code = "# encoding: UTF-8\n'é'.encoding".b

View file

@ -0,0 +1,13 @@
module SingletonMethodsSpecs
module Prepended
def mspec_test_kernel_singleton_methods
end
public :mspec_test_kernel_singleton_methods
end
::Module.prepend Prepended
module SelfExtending
extend self
end
end

View file

@ -36,6 +36,19 @@ describe :kernel_singleton_methods_modules, shared: true do
it "does not return any included methods for a class including a module" do it "does not return any included methods for a class including a module" do
ReflectSpecs::D.singleton_methods(*@object).should include(:ds_pro, :ds_pub) ReflectSpecs::D.singleton_methods(*@object).should include(:ds_pro, :ds_pub)
end end
it "for a module does not return methods in a module prepended to Module itself" do
require_relative 'fixtures/singleton_methods'
mod = SingletonMethodsSpecs::SelfExtending
mod.method(:mspec_test_kernel_singleton_methods).owner.should == SingletonMethodsSpecs::Prepended
ancestors = mod.singleton_class.ancestors
ancestors[0...2].should == [ mod.singleton_class, mod ]
ancestors.should include(SingletonMethodsSpecs::Prepended)
# Do not search prepended modules of `Module`, as that's a non-singleton class
mod.singleton_methods.should == []
end
end end
describe :kernel_singleton_methods_supers, shared: true do describe :kernel_singleton_methods_supers, shared: true do
@ -145,7 +158,6 @@ describe "Kernel#singleton_methods" do
it_behaves_like :kernel_singleton_methods_supers, nil, true it_behaves_like :kernel_singleton_methods_supers, nil, true
it_behaves_like :kernel_singleton_methods_modules, nil, true it_behaves_like :kernel_singleton_methods_modules, nil, true
it_behaves_like :kernel_singleton_methods_private_supers, nil, true it_behaves_like :kernel_singleton_methods_private_supers, nil, true
end end
describe "when passed false" do describe "when passed false" do

View file

@ -594,6 +594,30 @@ describe "Module#refine" do
end end
end end
it 'and alias aliases a method within a refinement module, but not outside it' do
Module.new do
using Module.new {
refine Array do
alias :orig_count :count
end
}
[1,2].orig_count.should == 2
end
lambda { [1,2].orig_count }.should raise_error(NoMethodError)
end
it 'and alias_method aliases a method within a refinement module, but not outside it' do
Module.new do
using Module.new {
refine Array do
alias_method :orig_count, :count
end
}
[1,2].orig_count.should == 2
end
lambda { [1,2].orig_count }.should raise_error(NoMethodError)
end
# Refinements are inherited by module inclusion. # Refinements are inherited by module inclusion.
# That is, using activates all refinements in the ancestors of the specified module. # That is, using activates all refinements in the ancestors of the specified module.
# Refinements in a descendant have priority over refinements in an ancestor. # Refinements in a descendant have priority over refinements in an ancestor.

View file

@ -158,9 +158,28 @@ platform_is_not :windows do
$?.exitstatus.should == 0 $?.exitstatus.should == 0
end end
it "returns 'DEFAULT' for the initial SIGINT handler" do
ruby_exe('print trap(:INT) { abort }').should == 'DEFAULT'
end
it "returns SYSTEM_DEFAULT if passed DEFAULT and no handler was ever set" do it "returns SYSTEM_DEFAULT if passed DEFAULT and no handler was ever set" do
Signal.trap("PROF", "DEFAULT").should == "SYSTEM_DEFAULT" Signal.trap("PROF", "DEFAULT").should == "SYSTEM_DEFAULT"
end end
it "accepts 'SYSTEM_DEFAULT' and uses the OS handler for SIGPIPE" do
code = <<-RUBY
p Signal.trap('PIPE', 'SYSTEM_DEFAULT')
r, w = IO.pipe
r.close
loop { w.write("a"*1024) }
RUBY
out = ruby_exe(code)
status = $?
out.should == "nil\n"
status.signaled?.should == true
status.termsig.should be_kind_of(Integer)
Signal.signame(status.termsig).should == "PIPE"
end
end end
end end

View file

@ -44,6 +44,11 @@ class MSpecScript
[/_spec.rb$/, '_tags.txt'] [/_spec.rb$/, '_tags.txt']
] ]
set :toplevel_constants_excludes, [
/\wSpecs?$/,
/^CS_CONST\d/,
]
# Enable features # Enable features
MSpec.enable_feature :fiber MSpec.enable_feature :fiber
MSpec.enable_feature :fiber_library MSpec.enable_feature :fiber_library

View file

@ -0,0 +1,2 @@
# encoding: big5
$magic_comment_result = '§A¦n'.bytes.inspect

View file

@ -0,0 +1,2 @@
# CoDiNg: bIg5
$magic_comment_result = __ENCODING__.name

View file

@ -0,0 +1,2 @@
# -*- encoding: big5 -*-
$magic_comment_result = __ENCODING__.name

View file

@ -0,0 +1,2 @@
# encoding: big5
$magic_comment_result = __ENCODING__.name

View file

@ -0,0 +1 @@
$magic_comment_result = __ENCODING__.name

View file

@ -0,0 +1,3 @@
at_exit {
print $magic_comment_result
}

View file

@ -0,0 +1,3 @@
# encoding: big5
$magic_comment_result = __ENCODING__.name

View file

@ -0,0 +1,2 @@
1 + 1 # encoding: big5
$magic_comment_result = __ENCODING__.name

View file

@ -0,0 +1,3 @@
#!/usr/bin/ruby
# encoding: big5
$magic_comment_result = __ENCODING__.name

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,2 @@
# encoding: utf-8
puts 'hello'

View file

@ -0,0 +1,2 @@
# encoding: utf-8
puts 'hello'

View file

@ -0,0 +1,2 @@
# vim: filetype=ruby, fileencoding=big5, tabsize=3, shiftwidth=3
$magic_comment_result = __ENCODING__.name

View file

@ -1,62 +1,88 @@
require_relative '../spec_helper' require_relative '../spec_helper'
describe "Magic comment" do # See core/kernel/eval_spec.rb for more magic comments specs for eval()
it "is optional" do describe :magic_comments, shared: true do
eval("__ENCODING__").should be_an_instance_of(Encoding) it "are optional" do
@object.call('no_magic_comment.rb').should == Encoding::UTF_8.name
end end
it "determines __ENCODING__" do it "are case-insensitive" do
eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT @object.call('case_magic_comment.rb').should == Encoding::Big5.name
# 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
end end
it "must be at the first line" do it "must be at the first line" do
eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::US_ASCII @object.call('second_line_magic_comment.rb').should == Encoding::UTF_8.name
# encoding: ASCII-8BIT
__ENCODING__
EOS
end end
it "must be the first token of the line" do it "must be the first token of the line" do
eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::US_ASCII @object.call('second_token_magic_comment.rb').should == Encoding::UTF_8.name
1+1 # encoding: ASCII-8BIT
__ENCODING__
EOS
eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT
# encoding: ASCII-8BIT
__ENCODING__
EOS
end end
it "can be after the shebang" do it "can be after the shebang" do
eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT @object.call('shebang_magic_comment.rb').should == Encoding::Big5.name
#!/usr/bin/ruby -Ku
# encoding: ASCII-8BIT
__ENCODING__
EOS
end end
it "can take Emacs style" do it "can take Emacs style" do
eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT @object.call('emacs_magic_comment.rb').should == Encoding::Big5.name
# -*- encoding: ascii-8bit -*-
__ENCODING__
EOS
end end
it "can take vim style" do it "can take vim style" do
eval(<<EOS.force_encoding("US-ASCII")).should == Encoding::ASCII_8BIT @object.call('vim_magic_comment.rb').should == Encoding::Big5.name
# vim: filetype=ruby, fileencoding=ascii-8bit, tabsize=3, shiftwidth=3 end
__ENCODING__
EOS 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
end end

View file

@ -252,7 +252,11 @@ describe 'Optional variable assignments' do
end end
describe 'using compunded constants' do describe 'using compunded constants' do
before do before :each do
Object.send(:remove_const, :A) if defined? Object::A
end
after :each do
Object.send(:remove_const, :A) if defined? Object::A Object.send(:remove_const, :A) if defined? Object::A
end end

View file

@ -23,6 +23,18 @@ describe "The DATA constant" do
str.chomp.should == "data only" str.chomp.should == "data only"
end end
it "returns a File object with the right offset" do
ruby_exe(fixture(__FILE__, "data_offset.rb")).should == "File\n121\n"
end
it "is set even if there is no data after __END__" do
ruby_exe(fixture(__FILE__, "empty_data.rb")).should == "31\n\"\"\n"
end
it "is set even if there is no newline after __END__" do
ruby_exe(fixture(__FILE__, "no_newline_data.rb")).should == "30\n\"\"\n"
end
it "rewinds to the head of the main script" do it "rewinds to the head of the main script" do
ruby_exe(fixture(__FILE__, "data5.rb")).chomp.should == "DATA.rewind" ruby_exe(fixture(__FILE__, "data5.rb")).chomp.should == "DATA.rewind"
end end

View file

@ -0,0 +1,12 @@
# some comment
foo = <<HEREDOC
some heredoc to make the
spec more interesting
HEREDOC
p DATA.class
p DATA.pos
__END__
data offset

View file

@ -0,0 +1,3 @@
p DATA.pos
p DATA.read
__END__

View file

@ -0,0 +1,3 @@
p DATA.pos
p DATA.read
__END__

View file

@ -0,0 +1,97 @@
require_relative '../spec_helper'
describe "The $SAFE variable" do
ruby_version_is "2.6" do
after :each do
$SAFE = 0
end
end
it "is 0 by default" do
$SAFE.should == 0
proc {
$SAFE.should == 0
}.call
end
it "can be set to 0" do
proc {
$SAFE = 0
$SAFE.should == 0
}.call
end
it "can be set to 1" do
proc {
$SAFE = 1
$SAFE.should == 1
}.call
end
[2, 3, 4].each do |n|
it "cannot be set to #{n}" do
lambda {
proc {
$SAFE = n
}.call
}.should raise_error(ArgumentError, /\$SAFE=2 to 4 are obsolete/)
end
end
ruby_version_is ""..."2.6" do
it "cannot be set to values below 0" do
lambda {
proc {
$SAFE = -100
}.call
}.should raise_error(SecurityError, /tried to downgrade safe level from 0 to -100/)
end
end
it "cannot be set to values above 4" do
lambda {
proc {
$SAFE = 100
}.call
}.should raise_error(ArgumentError, /\$SAFE=2 to 4 are obsolete/)
end
ruby_version_is ""..."2.6" do
it "cannot be manually lowered" do
proc {
$SAFE = 1
lambda {
$SAFE = 0
}.should raise_error(SecurityError, /tried to downgrade safe level from 1 to 0/)
}.call
end
it "is automatically lowered when leaving a proc" do
$SAFE.should == 0
proc {
$SAFE = 1
}.call
$SAFE.should == 0
end
it "is automatically lowered when leaving a lambda" do
$SAFE.should == 0
lambda {
$SAFE = 1
}.call
$SAFE.should == 0
end
end
it "can be read when default from Thread#safe_level" do
Thread.current.safe_level.should == 0
end
it "can be read when modified from Thread#safe_level" do
proc {
$SAFE = 1
Thread.current.safe_level.should == 1
}.call
end
end

View file

@ -0,0 +1,41 @@
require_relative '../spec_helper'
describe "Source files" do
describe "encoded in UTF-8 without a BOM" do
it "can be parsed" do
ruby_exe(fixture(__FILE__, "utf8-nobom.rb"), args: "2>&1").should == "hello\n"
end
end
describe "encoded in UTF-8 with a BOM" do
it "can be parsed" do
ruby_exe(fixture(__FILE__, "utf8-bom.rb"), args: "2>&1").should == "hello\n"
end
end
describe "encoded in UTF-16 LE without a BOM" do
it "are parsed because empty as they contain a NUL byte before the encoding comment" do
ruby_exe(fixture(__FILE__, "utf16-le-nobom.rb"), args: "2>&1").should == ""
end
end
describe "encoded in UTF-16 LE with a BOM" do
it "are invalid because they contain an invalid UTF-8 sequence before the encoding comment" do
ruby_exe(fixture(__FILE__, "utf16-le-bom.rb"), args: "2>&1").should =~ /invalid multibyte char/
end
end
describe "encoded in UTF-16 BE without a BOM" do
it "are parsed as empty because they contain a NUL byte before the encoding comment" do
ruby_exe(fixture(__FILE__, "utf16-be-nobom.rb"), args: "2>&1").should == ""
end
end
describe "encoded in UTF-16 BE with a BOM" do
it "are invalid because they contain an invalid UTF-8 sequence before the encoding comment" do
ruby_exe(fixture(__FILE__, "utf16-be-bom.rb"), args: "2>&1").should =~ /invalid multibyte char/
end
end
end

View file

@ -0,0 +1,14 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'etc'
platform_is_not :windows do
describe "Etc.confstr" do
it "returns a String for Etc::CS_PATH" do
Etc.confstr(Etc::CS_PATH).should be_an_instance_of(String)
end
it "raises Errno::EINVAL for unknown configuration variables" do
-> { Etc.confstr(-1) }.should raise_error(Errno::EINVAL)
end
end
end

View file

@ -6,6 +6,15 @@ describe "Etc.group" do
it_behaves_like :etc_on_windows, :group it_behaves_like :etc_on_windows, :group
platform_is_not :windows do platform_is_not :windows do
it "returns a Etc::Group struct" do
group = Etc.group
begin
group.should be_an_instance_of(Etc::Group)
ensure
Etc.endgrent
end
end
it "raises a RuntimeError for parallel iteration" do it "raises a RuntimeError for parallel iteration" do
proc { proc {
Etc.group do | group | Etc.group do | group |

View file

@ -0,0 +1,15 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'etc'
platform_is_not :windows do
describe "Etc.passwd" do
it "returns a Etc::Passwd struct" do
passwd = Etc.passwd
begin
passwd.should be_an_instance_of(Etc::Passwd)
ensure
Etc.endpwent
end
end
end
end

View file

@ -0,0 +1,31 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'etc'
platform_is_not :windows do
describe "Etc.sysconf" do
def should_be_integer_or_nil(value)
if value.nil?
value.should == nil
else
value.should be_kind_of(Integer)
end
end
it "returns the value of POSIX.1 system configuration variables" do
Etc.sysconf(Etc::SC_ARG_MAX).should be_kind_of(Integer)
should_be_integer_or_nil(Etc.sysconf(Etc::SC_CHILD_MAX))
Etc.sysconf(Etc::SC_HOST_NAME_MAX).should be_kind_of(Integer)
Etc.sysconf(Etc::SC_LOGIN_NAME_MAX).should be_kind_of(Integer)
Etc.sysconf(Etc::SC_NGROUPS_MAX).should be_kind_of(Integer)
Etc.sysconf(Etc::SC_CLK_TCK).should be_kind_of(Integer)
Etc.sysconf(Etc::SC_OPEN_MAX).should be_kind_of(Integer)
Etc.sysconf(Etc::SC_PAGESIZE).should be_kind_of(Integer)
Etc.sysconf(Etc::SC_RE_DUP_MAX).should be_kind_of(Integer)
Etc.sysconf(Etc::SC_STREAM_MAX).should be_kind_of(Integer)
should_be_integer_or_nil(Etc.sysconf(Etc::SC_SYMLOOP_MAX))
Etc.sysconf(Etc::SC_TTY_NAME_MAX).should be_kind_of(Integer)
Etc.sysconf(Etc::SC_TZNAME_MAX).should be_kind_of(Integer)
Etc.sysconf(Etc::SC_VERSION).should be_kind_of(Integer)
end
end
end

View file

@ -0,0 +1,8 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'etc'
describe "Etc.sysconfdir" do
it "returns a String" do
Etc.sysconfdir.should be_an_instance_of(String)
end
end

View file

@ -0,0 +1,8 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'etc'
describe "Etc.systmpdir" do
it "returns a String" do
Etc.systmpdir.should be_an_instance_of(String)
end
end

View file

@ -53,6 +53,7 @@ describe 'Socket#connect_address' do
end end
end end
guard -> { SocketSpecs.ipv6_available? } do
describe 'using a socket bound to ::' do describe 'using a socket bound to ::' do
before do before do
@sock = Socket.new(:INET6, :STREAM) @sock = Socket.new(:INET6, :STREAM)
@ -91,6 +92,7 @@ describe 'Socket#connect_address' do
@sock.connect_address.protocol.should == 0 @sock.connect_address.protocol.should == 0
end end
end end
end
with_feature :unix_socket do with_feature :unix_socket do
describe 'using an unbound UNIX socket' do describe 'using an unbound UNIX socket' do

View file

@ -50,7 +50,7 @@ module SocketSpecs
def self.ipv6_available? def self.ipv6_available?
@ipv6_available ||= begin @ipv6_available ||= begin
server = TCPServer.new('::1', 0) server = TCPServer.new('::1', 0)
rescue Errno::EADDRNOTAVAIL rescue Errno::EADDRNOTAVAIL, SocketError
:no :no
else else
server.close server.close

View file

@ -20,7 +20,7 @@ void sample_wrapped_struct_mark(void* st) {
} }
VALUE sdaf_alloc_func(VALUE klass) { VALUE sdaf_alloc_func(VALUE klass) {
struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct)); struct sample_wrapped_struct* bar = malloc(sizeof(struct sample_wrapped_struct));
bar->foo = 42; bar->foo = 42;
return Data_Wrap_Struct(klass, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar); return Data_Wrap_Struct(klass, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar);
} }
@ -33,7 +33,7 @@ VALUE sdaf_get_struct(VALUE self) {
} }
VALUE sws_wrap_struct(VALUE self, VALUE val) { VALUE sws_wrap_struct(VALUE self, VALUE val) {
struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct)); struct sample_wrapped_struct* bar = malloc(sizeof(struct sample_wrapped_struct));
bar->foo = FIX2INT(val); bar->foo = FIX2INT(val);
return Data_Wrap_Struct(rb_cObject, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar); return Data_Wrap_Struct(rb_cObject, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar);
} }
@ -59,7 +59,7 @@ VALUE sws_get_struct_data_ptr(VALUE self, VALUE obj) {
VALUE sws_change_struct(VALUE self, VALUE obj, VALUE new_val) { VALUE sws_change_struct(VALUE self, VALUE obj, VALUE new_val) {
struct sample_wrapped_struct *old_struct, *new_struct; struct sample_wrapped_struct *old_struct, *new_struct;
new_struct = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct)); new_struct = malloc(sizeof(struct sample_wrapped_struct));
new_struct->foo = FIX2INT(new_val); new_struct->foo = FIX2INT(new_val);
old_struct = RDATA(obj)->data; old_struct = RDATA(obj)->data;
free(old_struct); free(old_struct);

View file

@ -64,7 +64,7 @@ end
describe "C-API Struct function" do describe "C-API Struct function" do
before :each do before :each do
@s = CApiStructSpecs.new @s = CApiStructSpecs.new
@struct = @s.rb_struct_define_under(CApiStructSpecs, "CAPIStruct", "a", "b", "c") @struct = @s.rb_struct_define_under(CApiStructSpecs, "CAPIStructUnder", "a", "b", "c")
end end
describe "rb_struct_define_under" do describe "rb_struct_define_under" do
@ -80,11 +80,15 @@ describe "C-API Struct function" do
it "has a value of nil for the member of a newly created instance" do it "has a value of nil for the member of a newly created instance" do
# Verify that attributes are on an instance basis # Verify that attributes are on an instance basis
CApiStructSpecs::CAPIStruct.new.b.should be_nil CApiStructSpecs::CAPIStructUnder.new.b.should be_nil
end
it "does not create a constant scoped under Struct for the named Struct" do
Struct.should_not have_constant(:CAPIStructUnder)
end end
it "creates a constant scoped under the namespace of the given class" do it "creates a constant scoped under the namespace of the given class" do
CApiStructSpecs.should have_constant(:CAPIStruct) CApiStructSpecs.should have_constant(:CAPIStructUnder)
end end
it "returns the member names as Symbols" do it "returns the member names as Symbols" do