1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Move spec/rubyspec to spec/ruby for consistency

* Other ruby implementations use the spec/ruby directory.
  [Misc #13792] [ruby-core:82287]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2017-09-20 20:18:52 +00:00
parent 75bfc6440d
commit 1d15d5f080
4370 changed files with 0 additions and 0 deletions

5
spec/ruby/.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
/Gemfile.lock
/rubyspec_temp
/ext
/.ruby-version
/.ruby-gemset

40
spec/ruby/.rubocop.yml Normal file
View file

@ -0,0 +1,40 @@
inherit_from: .rubocop_todo.yml
AllCops:
TargetRubyVersion: 2.4
DisplayCopNames: true
Exclude:
- command_line/fixtures/bad_syntax.rb
DisabledByDefault: true
Lint:
Enabled: true
Lint/AssignmentInCondition:
Enabled: false
Lint/LiteralInCondition:
Enabled: false
Lint/UnifiedInteger:
Enabled: false
Lint/UnusedBlockArgument:
Enabled: false
Lint/UnusedMethodArgument:
Enabled: false
Lint/UselessAssignment:
Enabled: false
Lint/UselessComparison:
Enabled: false
# The cop registers too many false positives to `.should == something`
Lint/Void:
Enabled: false
Lint/EmptyExpression:
Exclude:
- 'language/**/*.rb'

211
spec/ruby/.rubocop_todo.yml Normal file
View file

@ -0,0 +1,211 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-06-18 19:12:38 +0200 using RuboCop version 0.49.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.
# Offense count: 6
Lint/AmbiguousBlockAssociation:
Exclude:
- 'core/kernel/trace_var_spec.rb'
- 'language/fixtures/argv_encoding.rb'
- 'language/fixtures/break.rb'
- 'shared/process/abort.rb'
# Offense count: 3
Lint/CircularArgumentReference:
Exclude:
- 'language/block_spec.rb'
- 'language/def_spec.rb'
- 'language/lambda_spec.rb'
# Offense count: 2
Lint/DuplicateCaseCondition:
Exclude:
- 'language/case_spec.rb'
# Offense count: 6
Lint/DuplicateMethods:
Exclude:
- 'core/array/fixtures/encoded_strings.rb'
- 'core/method/fixtures/classes.rb'
- 'core/module/fixtures/classes.rb'
- 'core/unboundmethod/fixtures/classes.rb'
- 'fixtures/class.rb'
# Offense count: 4
Lint/EmptyWhen:
Exclude:
- 'language/case_spec.rb'
# Offense count: 5
Lint/EnsureReturn:
Exclude:
- 'language/fixtures/ensure.rb'
- 'language/fixtures/return.rb'
- 'language/return_spec.rb'
# Offense count: 10
Lint/FloatOutOfRange:
Exclude:
- 'core/string/modulo_spec.rb'
# Offense count: 46
Lint/FormatParameterMismatch:
Exclude:
- 'core/kernel/sprintf_spec.rb'
- 'core/string/modulo_spec.rb'
# Offense count: 25
Lint/HandleExceptions:
Enabled: false
# Offense count: 2
Lint/ImplicitStringConcatenation:
Exclude:
- 'language/string_spec.rb'
# Offense count: 4
Lint/IneffectiveAccessModifier:
Exclude:
- 'core/kernel/fixtures/classes.rb'
- 'core/module/fixtures/classes.rb'
- 'language/fixtures/private.rb'
# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: runtime_error, standard_error
Lint/InheritException:
Exclude:
- 'core/enumerator/lazy/fixtures/classes.rb'
- 'core/exception/fixtures/common.rb'
- 'core/module/fixtures/autoload_ex1.rb'
# Offense count: 3
# Cop supports --auto-correct.
Lint/LiteralInInterpolation:
Exclude:
- 'language/defined_spec.rb'
- 'language/fixtures/squiggly_heredoc.rb'
# Offense count: 16
Lint/Loop:
Exclude:
- 'language/until_spec.rb'
- 'language/while_spec.rb'
# Offense count: 8
# Cop supports --auto-correct.
Lint/MultipleCompare:
Exclude:
- 'language/precedence_spec.rb'
# Offense count: 8
Lint/NestedMethodDefinition:
Exclude:
- 'language/def_spec.rb'
# Offense count: 12
Lint/ParenthesesAsGroupedExpression:
Exclude:
- 'command_line/rubyopt_spec.rb'
- 'core/string/fixtures/freeze_magic_comment.rb'
- 'language/block_spec.rb'
- 'language/fixtures/send.rb'
- 'language/method_spec.rb'
- 'library/socket/socket/getaddrinfo_spec.rb'
# Offense count: 22
Lint/RescueException:
Exclude:
- 'command_line/fixtures/debug_info.rb'
- 'core/dir/fileno_spec.rb'
- 'core/exception/cause_spec.rb'
- 'core/exception/no_method_error_spec.rb'
- 'core/kernel/fixtures/autoload_frozen.rb'
- 'core/module/autoload_spec.rb'
- 'core/mutex/sleep_spec.rb'
- 'core/process/euid_spec.rb'
- 'core/process/setsid_spec.rb'
- 'core/process/uid_spec.rb'
- 'core/thread/abort_on_exception_spec.rb'
- 'core/thread/shared/exit.rb'
- 'language/rescue_spec.rb'
- 'library/erb/filename_spec.rb'
# Offense count: 1
Lint/ScriptPermission:
Exclude:
- 'command_line/fixtures/bin/launcher.rb'
# Offense count: 10
Lint/ShadowingOuterLocalVariable:
Exclude:
- 'core/binding/local_variables_spec.rb'
- 'language/block_spec.rb'
- 'language/proc_spec.rb'
# Offense count: 2
# Cop supports --auto-correct.
Lint/StringConversionInInterpolation:
Exclude:
- 'core/io/print_spec.rb'
# Offense count: 9
Lint/UnderscorePrefixedVariableName:
Exclude:
- 'core/io/pipe_spec.rb'
- 'core/io/popen_spec.rb'
- 'language/block_spec.rb'
# Offense count: 91
# Cop supports --auto-correct.
Lint/UnneededSplatExpansion:
Exclude:
- 'core/array/element_reference_spec.rb'
- 'core/array/unshift_spec.rb'
- 'core/enumerable/fixtures/classes.rb'
- 'core/enumerable/max_by_spec.rb'
- 'core/enumerable/min_by_spec.rb'
- 'core/enumerable/minmax_by_spec.rb'
- 'core/enumerator/lazy/fixtures/classes.rb'
- 'core/file/basename_spec.rb'
- 'core/kernel/p_spec.rb'
- 'language/array_spec.rb'
- 'language/break_spec.rb'
- 'language/case_spec.rb'
- 'language/next_spec.rb'
- 'language/send_spec.rb'
- 'language/variables_spec.rb'
# Offense count: 53
Lint/UnreachableCode:
Exclude:
- 'core/enumerator/lazy/fixtures/classes.rb'
- 'core/kernel/catch_spec.rb'
- 'core/kernel/throw_spec.rb'
- 'language/break_spec.rb'
- 'language/fixtures/break.rb'
- 'language/fixtures/break_lambda_toplevel.rb'
- 'language/fixtures/break_lambda_toplevel_block.rb'
- 'language/fixtures/break_lambda_toplevel_method.rb'
- 'language/fixtures/return.rb'
- 'language/next_spec.rb'
- 'language/return_spec.rb'
- 'optional/capi/kernel_spec.rb'
- 'shared/kernel/raise.rb'
# Offense count: 9
# Configuration parameters: ContextCreatingMethods, MethodCreatingMethods.
Lint/UselessAccessModifier:
Exclude:
- 'core/module/define_method_spec.rb'
- 'core/module/fixtures/classes.rb'
- 'core/module/module_function_spec.rb'
- 'core/module/private_class_method_spec.rb'
- 'core/module/private_spec.rb'
- 'core/module/protected_spec.rb'
- 'core/module/public_spec.rb'

34
spec/ruby/.travis.yml Normal file
View file

@ -0,0 +1,34 @@
sudo: false
language: ruby
install:
- git clone https://github.com/ruby/mspec.git ../mspec
script:
- if [ -n "$RUBOCOP" ]; then gem install rubocop -v 0.49.1 && rubocop; fi
- ../mspec/bin/mspec $MSPEC_OPTS
matrix:
include:
- os: osx
rvm: 2.4.0
- os: linux
rvm: 2.4.1
env: MSPEC_OPTS="-R2 -ff"
- os: linux
rvm: 2.2.7
- os: linux
rvm: 2.3.4
- os: linux
rvm: 2.4.1
env: CHECK_LEAKS=true RUBOCOP=true
- os: linux
rvm: ruby-head
allow_failures:
- os: linux
rvm: ruby-head
branches:
only:
- master
- /^try/
notifications:
email:
on_success: change
on_failure: change

File diff suppressed because it is too large Load diff

64
spec/ruby/CONTRIBUTING.md Normal file
View file

@ -0,0 +1,64 @@
Contributions are much appreciated.
Please open a pull request or add an issue to discuss what you intend to work on.
If the pull requests passes the CI and conforms to the existing style of specs, it will be merged.
### File organization
Spec are grouped in 5 separate top-level groups:
* `command_line`: for the ruby executable command-line flags (`-v`, `-e`, etc)
* `language`: for the language keywords and syntax constructs (`if`, `def`, `A::B`, etc)
* `core`: for the core methods (`Fixnum#+`, `String#upcase`, no need to require anything)
* `library`: for the standard libraries methods (`CSV.new`, `YAML.parse`, need to require the stdlib)
* `optional/capi`: for functions available to the Ruby C-extension API
The exact file for methods is decided by the `#owner` of a method, for instance for `#group_by`:
```ruby
> [].method(:group_by)
=> #<Method: Array(Enumerable)#group_by>
> [].method(:group_by).owner
=> Enumerable
```
Which should therefore be specified in `core/enumerable/group_by_spec.rb`.
### MkSpec - a tool to generate the spec structure
If you want to create new specs, you should use `mkspec`, part of [MSpec](http://github.com/ruby/mspec).
$ ../mspec/bin/mkspec -h
#### Creating files for unspecified modules or classes
For instance, to create specs for `forwardable`:
$ ../mspec/bin/mkspec -b library -rforwardable -c Forwardable
Specify `core` or `library` as the `base`.
#### Finding unspecified core methods
This is very easy, just run the command below in your `spec` directory.
`ruby` must be a recent version of MRI.
$ ruby --disable-gem ../mspec/bin/mkspec
You might also want to search for:
it "needs to be reviewed for spec completeness"
which indicates the file was generated but the method unspecified.
### Guards
Different guards are available as defined by mspec.
In general, the usage of guards should be minimized as possible.
There are no guards to define implementation-specific behavior because
the Ruby Spec Suite defines common behavior and not implementation details.
Use the implementation test suite for these.
If an implementation does not support some feature, simply tag the related specs as failing instead.
### Style
Do not leave any trailing space and respect the existing style.

22
spec/ruby/LICENSE Normal file
View file

@ -0,0 +1,22 @@
Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

96
spec/ruby/README.md Normal file
View file

@ -0,0 +1,96 @@
# The Ruby Spec Suite
[![Build Status](https://travis-ci.org/ruby/spec.svg)](https://travis-ci.org/ruby/spec)
[![Build Status](https://ci.appveyor.com/api/projects/status/1gs6f399320o44b1?svg=true)](https://ci.appveyor.com/project/eregon/spec-x948i)
The Ruby Spec Suite is a test suite for the behavior of the Ruby programming language.
It is not a standardized specification like the ISO one, and does not aim to become one.
Instead, it is a practical tool to describe and test the behavior of Ruby with code.
Every example code has a textual description, which presents several advantages:
* It is easier to understand the intent of the author
* It documents how recent versions of Ruby should behave
* It helps Ruby implementations to agree on a common behavior
The specs are written with syntax similar to RSpec 2.
They are run with MSpec, the purpose-built framework for running the Ruby Spec Suite.
For more information, see the [MSpec](http://github.com/ruby/mspec) project.
The specs describe the [language syntax](language/), the [core library](core/), the [standard library](library/), the [C API for extensions](optional/capi) and the [command line flags](command_line/).
The language specs are grouped by keyword while the core and standard library specs are grouped by class and method.
ruby/spec is known to be tested in these implementations for every commit:
* [MRI](http://rubyci.org/) on 30 platforms and 4 versions
* [JRuby](https://github.com/jruby/jruby/tree/master/spec/ruby) on Travis for both 1.7 and 9.x
* [TruffleRuby](https://github.com/graalvm/truffleruby) on Travis
* [Opal](https://github.com/opal/opal/tree/master/spec) on Travis
ruby/spec describes the behavior of Ruby 2.2 and more recent Ruby versions.
More precisely, every latest stable MRI release [passes](https://rubyci.org/) all specs of ruby/spec
(latest 2.2.x, 2.3.x, 2.4.x, etc).
For older specs try these commits:
* Ruby 2.0.0-p647 - [Suite](https://github.com/ruby/spec/commit/245862558761d5abc676843ef74f86c9bcc8ea8d) using [MSpec](https://github.com/ruby/mspec/commit/f90efa068791064f955de7a843e96e2d7d3041c2) (may encounter 2 failures)
* Ruby 2.1.9 - [Suite](https://github.com/ruby/spec/commit/f029e65241374386077ac500add557ae65069b55) using [MSpec](https://github.com/ruby/mspec/commit/55568ea3918c6380e64db8c567d732fa5781efed)
### Running the specs
First, clone this repository:
$ git clone https://github.com/ruby/spec.git
Then move to it:
$ cd spec
Clone [MSpec](http://github.com/ruby/mspec):
$ git clone https://github.com/ruby/mspec.git ../mspec
And run the spec suite:
$ ../mspec/bin/mspec
This will execute all the specs using the executable named `ruby` on your current PATH.
### Running Specs with a Specific Ruby Implementation
Use the `-t` option to specify the Ruby implementation with which to run the specs.
The argument may be a full path to the Ruby binary.
$ ../mspec/bin/mspec -t /path/to/some/bin/ruby
### Running Selected Specs
To run a single spec file, pass the filename to `mspec`:
$ ../mspec/bin/mspec core/kernel/kind_of_spec.rb
You can also pass a directory, in which case all specs in that directories will be run:
$ ../mspec/bin/mspec core/kernel
Finally, you can also run them per group as defined in `default.mspec`.
The following command will run all language specs:
$ ../mspec/bin/mspec :language
In similar fashion, the following commands run the respective specs:
$ ../mspec/bin/mspec :core
$ ../mspec/bin/mspec :library
$ ../mspec/bin/mspec :capi
### Contributing
See [CONTRIBUTING.md](https://github.com/ruby/spec/blob/master/CONTRIBUTING.md).
### History and RubySpec
This project was originally born from [Rubinius](https://github.com/rubinius/rubinius) tests being converted to the spec style.
These specs were later extracted to their own project, RubySpec, with a specific vision and principles.
At the end of 2014, Brian Shirai, the creator of RubySpec, decided to [end RubySpec](http://rubinius.com/2014/12/31/matz-s-ruby-developers-don-t-use-rubyspec/).
A couple months later, the different repositories were merged and [the project was revived](http://eregon.github.io/rubyspec/2015/07/29/rubyspec-is-reborn.html).
On 12 January 2016, the name was changed to "The Ruby Spec Suite" for clarity and to let the RubySpec ideology rest in peace.

8
spec/ruby/TODO Normal file
View file

@ -0,0 +1,8 @@
* Decide a way to test methods that are only visible given a specific
command-line option. For example, Kernel#gsub with -n/-p on 1.9.
* Look at automating discovery of guarded bugs which have been fixed.
* Use mocks for all Math functions that coerce with #to_f; currently a fixture
is used.
* investigate slow specs (run with -fp) and make them faster.
* restore some caller specs from 642bf529
* restore refinements specs and update. See 56c5528f and f20a62e8.

19
spec/ruby/appveyor.yml Normal file
View file

@ -0,0 +1,19 @@
---
version: "{build}"
clone_depth: 5
environment:
matrix:
- RUBY_VERSION: 23-x64
install:
- SET PATH=C:\Ruby%RUBY_VERSION%\bin;%PATH%
- ruby --version
- call "C:\Ruby23-x64\DevKit\devkitvars.bat"
- git clone https://github.com/ruby/mspec.git ../mspec
build: off
test_script:
- SET CHECK_LEAKS=true
- ../mspec/bin/mspec -ff
branches:
only:
- master
- /^try/

View file

@ -0,0 +1,17 @@
describe "The -a command line option" do
before :each do
@names = fixture __FILE__, "full_names.txt"
end
it "runs the code in loop conditional on Kernel.gets()" do
ruby_exe("puts $F.last", options: "-n -a", escape: true,
args: " < #{@names}").should ==
"jones\nfield\ngrey\n"
end
it "sets $-a" do
ruby_exe("puts $-a", options: "-n -a", escape: true,
args: " < #{@names}").should ==
"true\ntrue\ntrue\n"
end
end

View file

@ -0,0 +1,13 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "The -c command line option" do
it "checks syntax in given file" do
ruby_exe(nil, args: "-c #{__FILE__}").chomp.should == "Syntax OK"
end
it "checks syntax in -e strings" do
ruby_exe(nil, args: "-c -e 'puts 1' -e 'hello world'").chomp.should == "Syntax OK"
end
#Also needs spec for reading from STDIN
end

View file

@ -0,0 +1,22 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "The -d command line option" do
before :each do
@script = fixture __FILE__, "debug.rb"
end
it "sets $DEBUG to true" do
ruby_exe(@script, options: "-d",
args: "0 2> #{File::NULL}").chomp.should == "$DEBUG true"
end
it "sets $VERBOSE to true" do
ruby_exe(@script, options: "-d",
args: "1 2> #{File::NULL}").chomp.should == "$VERBOSE true"
end
it "sets $-d to true" do
ruby_exe(@script, options: "-d",
args: "2 2> #{File::NULL}").chomp.should == "$-d true"
end
end

View file

@ -0,0 +1,41 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "The -e command line option" do
it "evaluates the given string" do
ruby_exe("puts 'foo'").chomp.should == "foo"
end
it "joins multiple strings with newlines" do
ruby_exe(nil, args: %Q{-e "puts 'hello" -e "world'" 2>&1}).chomp.should == "hello\nworld"
end
it "uses 'main' as self" do
ruby_exe("puts self", escape: false).chomp.should == "main"
end
it "uses '-e' as file" do
ruby_exe("puts __FILE__", escape: false).chomp.should == "-e"
end
it "uses '-e' in $0" do
system(*ruby_exe, '-e', 'exit $0 == "-e"').should == true
end
#needs to test return => LocalJumpError
describe "with -n and a Fixnum range" do
before :each do
@script = "-ne 'print if %s' #{fixture(__FILE__, "conditional_range.txt")}"
end
it "mimics an awk conditional by comparing an inclusive-end range with $." do
ruby_exe(nil, args: (@script % "2..3")).should == "2\n3\n"
ruby_exe(nil, args: (@script % "2..2")).should == "2\n"
end
it "mimics a sed conditional by comparing an exclusive-end range with $." do
ruby_exe(nil, args: (@script % "2...3")).should == "2\n3\n"
ruby_exe(nil, args: (@script % "2...2")).should == "2\n3\n4\n5\n"
end
end
end

View file

@ -0,0 +1,34 @@
describe "The -n command line option" do
before :each do
@names = fixture __FILE__, "names.txt"
end
it "runs the code in loop conditional on Kernel.gets()" do
ruby_exe("puts $_", options: "-n", escape: true,
args: " < #{@names}").should ==
"alice\nbob\njames\n"
end
it "only evaluates BEGIN blocks once" do
ruby_exe("BEGIN { puts \"hi\" }; puts $_", options: "-n", escape: true,
args: " < #{@names}").should ==
"hi\nalice\nbob\njames\n"
end
it "only evaluates END blocks once" do
ruby_exe("puts $_; END {puts \"bye\"}", options: "-n", escape: true,
args: " < #{@names}").should ==
"alice\nbob\njames\nbye\n"
end
it "allows summing over a whole file" do
script = <<-script
BEGIN { $total = 0 }
$total += 1
END { puts $total }
script
ruby_exe(script, options: "-n", escape: true,
args: " < #{@names}").should ==
"3\n"
end
end

View file

@ -0,0 +1,17 @@
describe "The -p command line option" do
before :each do
@names = fixture __FILE__, "names.txt"
end
it "runs the code in loop conditional on Kernel.gets() and prints $_" do
ruby_exe("$_ = $_.upcase", options: "-p", escape: true,
args: " < #{@names}").should ==
"ALICE\nBOB\nJAMES\n"
end
it "sets $-p" do
ruby_exe("$_ = $-p", options: "-p", escape: true,
args: " < #{@names}").should ==
"truetruetrue"
end
end

View file

@ -0,0 +1,13 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "The -r command line option" do
before :each do
@script = fixture __FILE__, "require.rb"
@test_file = fixture __FILE__, "test_file"
end
it "requires the specified file" do
result = ruby_exe(@script, options: "-r #{@test_file}")
result.should include(@test_file + ".rb")
end
end

View file

@ -0,0 +1,52 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "The -s command line option" do
describe "when using -- to stop parsing" do
it "sets the value to true without an explicit value" do
ruby_exe(nil, options: "-s -e 'p $n'",
args: "-- -n").chomp.should == "true"
end
it "parses single letter args into globals" do
ruby_exe(nil, options: "-s -e 'puts $n'",
args: "-- -n=blah").chomp.should == "blah"
end
it "parses long args into globals" do
ruby_exe(nil, options: "-s -e 'puts $_name'",
args: "-- --name=blah").chomp.should == "blah"
end
it "converts extra dashes into underscores" do
ruby_exe(nil, options: "-s -e 'puts $___name__test__'",
args: "-- ----name--test--=blah").chomp.should == "blah"
end
end
describe "when running a script" do
before :all do
@script = fixture __FILE__, "dash_s_script.rb"
end
it "sets the value to true without an explicit value" do
ruby_exe(@script, options: "-s",
args: "-n 0").chomp.should == "true"
end
it "parses single letter args into globals" do
ruby_exe(@script, options: "-s",
args: "-n=blah 1").chomp.should == "blah"
end
it "parses long args into globals" do
ruby_exe(@script, options: "-s",
args: "--name=blah 2").chomp.should == "blah"
end
it "converts extra dashes into underscores" do
ruby_exe(@script, options: "-s",
args: "----name--test--=blah 3").chomp.should == "blah"
end
end
end

View file

@ -0,0 +1,18 @@
require File.expand_path('../../spec_helper', __FILE__)
describe 'The -C command line option' do
before :all do
@script = fixture(__FILE__, 'dash_upper_c_script.rb')
@tempdir = File.dirname(@script)
end
it 'changes the PWD when using a file' do
output = ruby_exe(@script, options: "-C #{@tempdir}")
output.should == @tempdir
end
it 'changes the PWD when using -e' do
output = ruby_exe(nil, options: "-C #{@tempdir} -e 'print Dir.pwd'")
output.should == @tempdir
end
end

View file

@ -0,0 +1,7 @@
describe "ruby -E" do
it "raises a RuntimeError if used with -U" do
ruby_exe("p 1",
options: '-Eascii:ascii -U',
args: '2>&1').should =~ /RuntimeError/
end
end

View file

@ -0,0 +1,11 @@
describe "the -F command line option" do
before :each do
@passwd = fixture __FILE__, "passwd_file.txt"
end
it "specifies the field separator pattern for -a" do
ruby_exe("puts $F[0]", options: "-naF:", escape: true,
args: " < #{@passwd}").should ==
"nobody\nroot\ndaemon\n"
end
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "The -I command line option" do
before :each do
@script = fixture __FILE__, "loadpath.rb"
end
it "adds the path to the load path ($:)" do
ruby_exe(@script, options: "-I fixtures").should include("fixtures")
end
end

View file

@ -0,0 +1,33 @@
describe 'The -K command line option sets __ENCODING__' do
it "to Encoding::ASCII_8BIT with -Ka" do
ruby_exe("print __ENCODING__", options: '-Ka').should == Encoding::ASCII_8BIT.to_s
end
it "to Encoding::ASCII_8BIT with -KA" do
ruby_exe("print __ENCODING__", options: '-KA').should == Encoding::ASCII_8BIT.to_s
end
it "to Encoding::EUC_JP with -Ke" do
ruby_exe("print __ENCODING__", options: '-Ke').should == Encoding::EUC_JP.to_s
end
it "to Encoding::EUC_JP with -KE" do
ruby_exe("print __ENCODING__", options: '-KE').should == Encoding::EUC_JP.to_s
end
it "to Encoding::UTF_8 with -Ku" do
ruby_exe("print __ENCODING__", options: '-Ku').should == Encoding::UTF_8.to_s
end
it "to Encoding::UTF_8 with -KU" do
ruby_exe("print __ENCODING__", options: '-KU').should == Encoding::UTF_8.to_s
end
it "to Encoding::Windows_31J with -Ks" do
ruby_exe("print __ENCODING__", options: '-Ks').should == Encoding::Windows_31J.to_s
end
it "to Encoding::Windows_31J with -KS" do
ruby_exe("print __ENCODING__", options: '-KS').should == Encoding::Windows_31J.to_s
end
end

View file

@ -0,0 +1,29 @@
require File.expand_path('../../spec_helper', __FILE__)
describe 'The -S command line option' do
before :each do
@path = [ENV['PATH'], fixture(__FILE__, "bin")].join(':')
end
platform_is_not :windows do
# On VirtualBox shared directory (vboxsf) all files are world writable
# and MRI shows warning when including world writable path in ENV['PATH'].
# This is why we are using /success$/ matching in the following cases.
it "runs launcher found in PATH, but only code after the first /\#!.*ruby.*/-ish line in target file" do
result = ruby_exe(nil, options: '-S hybrid_launcher.sh', env: { 'PATH' => @path }, args: '2>&1')
result.should =~ /success$/
end
it "runs launcher found in PATH" do
result = ruby_exe(nil, options: '-S launcher.rb', env: { 'PATH' => @path }, args: '2>&1')
result.should =~ /success$/
end
it "runs launcher found in PATH and sets the exit status to 1 if it fails" do
result = ruby_exe(nil, options: '-S dash_s_fail', env: { 'PATH' => @path }, args: '2>&1')
result.should =~ /\bdie\b/
$?.exitstatus.should == 1
end
end
end

View file

@ -0,0 +1,41 @@
describe "ruby -U" do
it "sets Encoding.default_internal to UTF-8" do
ruby_exe('print Encoding.default_internal.name',
options: '-U').should == 'UTF-8'
end
it "does nothing different if specified multiple times" do
ruby_exe('print Encoding.default_internal.name',
options: '-U -U').should == 'UTF-8'
end
it "is overruled by Encoding.default_internal=" do
ruby_exe('Encoding.default_internal="ascii"; print Encoding.default_internal.name',
options: '-U').should == 'US-ASCII'
end
it "does not affect the default external encoding" do
ruby_exe('Encoding.default_external="ascii"; print Encoding.default_external.name',
options: '-U').should == 'US-ASCII'
end
it "does not affect the source encoding" do
ruby_exe("print __ENCODING__.name",
options: '-U -KE').should == 'EUC-JP'
ruby_exe("print __ENCODING__.name",
options: '-KE -U').should == 'EUC-JP'
end
# I assume IO redirection will break on Windows...
it "raises a RuntimeError if used with -Eext:int" do
ruby_exe("p 1",
options: '-U -Eascii:ascii',
args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError if used with -E:int" do
ruby_exe("p 1",
options: '-U -E:ascii',
args: '2>&1').should =~ /RuntimeError/
end
end

View file

@ -0,0 +1,20 @@
require File.expand_path('../../spec_helper', __FILE__)
require File.expand_path('../shared/verbose', __FILE__)
describe "The -W command line option" do
before :each do
@script = fixture __FILE__, "verbose.rb"
end
it "with 0 sets $VERBOSE to nil" do
ruby_exe(@script, options: "-W0").chomp.should == "nil"
end
it "with 1 sets $VERBOSE to false" do
ruby_exe(@script, options: "-W1").chomp.should == "false"
end
end
describe "The -W command line option with 2" do
it_behaves_like :command_line_verbose, "-W2"
end

View file

@ -0,0 +1,13 @@
require File.expand_path('../../spec_helper', __FILE__)
require File.expand_path('../shared/verbose', __FILE__)
describe "The -v command line option" do
it_behaves_like :command_line_verbose, "-v"
describe "when used alone" do
it "prints version and ends" do
version = ruby_exe(nil, args: '--version')
ruby_exe(nil, args: '-v').should == version
end
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../spec_helper', __FILE__)
require File.expand_path('../shared/verbose', __FILE__)
describe "The -w command line option" do
it_behaves_like :command_line_verbose, "-w"
end

View file

@ -0,0 +1,21 @@
describe "The -x command line option" do
it "runs code after the first /\#!.*ruby.*/-ish line in target file" do
embedded_ruby = fixture __FILE__, "bin/embedded_ruby.txt"
result = ruby_exe(embedded_ruby, options: '-x')
result.should == "success\n"
end
it "fails when /\#!.*ruby.*/-ish line in target file is not found" do
bad_embedded_ruby = fixture __FILE__, "bin/bad_embedded_ruby.txt"
result = ruby_exe(bad_embedded_ruby, options: '-x', args: '2>&1')
result.should include "no Ruby script found in input"
end
it "behaves as -x was set when non-ruby shebang is encountered on first line" do
embedded_ruby = fixture __FILE__, "bin/hybrid_launcher.sh"
result = ruby_exe(embedded_ruby)
result.should == "success\n"
end
it "needs to be reviewed for spec completeness"
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "The error message caused by an exception" do
it "is not printed to stdout" do
out = ruby_exe("this_does_not_exist", args: "2> #{File::NULL}")
out.chomp.empty?.should == true
out = ruby_exe("end #syntax error", args: "2> #{File::NULL}")
out.chomp.empty?.should == true
end
end

View file

@ -0,0 +1 @@
f {

View file

@ -0,0 +1,3 @@
@@@This line is not value Ruby
#!rub_y
puts 'success'

View file

@ -0,0 +1 @@
raise 'die'

View file

@ -0,0 +1,3 @@
@@@This line is not value Ruby
#!ruby
puts 'success'

View file

@ -0,0 +1,4 @@
#!/usr/bin/env bash
exec somehow this file
#!ruby
puts 'success'

View file

@ -0,0 +1,2 @@
#!ruby
puts 'success'

View file

@ -0,0 +1,5 @@
1
2
3
4
5

View file

@ -0,0 +1,12 @@
which = ARGV.shift.to_i
case which
when 0
p $n
when 1
puts $n
when 2
puts $_name
when 3
puts $___name__test__
end

View file

@ -0,0 +1 @@
print Dir.pwd

View file

@ -0,0 +1,10 @@
which = ARGV.first.to_i
case which
when 0
puts "$DEBUG #{$DEBUG}"
when 1
puts "$VERBOSE #{$VERBOSE}"
when 2
puts "$-d #{$-d}"
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
a = 'string'
b = a
c = b
d = c
e = d
begin
a << 'new part'
rescue Exception => e
print e.message
end

View file

@ -0,0 +1,3 @@
require_relative 'freeze_flag_required'
p "abc".object_id == $second_literal_id

View file

@ -0,0 +1,3 @@
require_relative 'freeze_flag_required_diff_enc'
p "abc".object_id != $second_literal_id

View file

@ -0,0 +1,2 @@
ids = Array.new(2) { "abc".object_id }
p ids.first == ids.last

View file

@ -0,0 +1 @@
$second_literal_id = "abc".object_id

View file

@ -0,0 +1 @@
p "abc".object_id == "abc".object_id

View file

@ -0,0 +1,3 @@
alice jones
bob field
james grey

View file

@ -0,0 +1 @@
puts $:

View file

@ -0,0 +1,3 @@
alice
bob
james

View file

@ -0,0 +1,3 @@
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false

View file

@ -0,0 +1 @@
puts $"

View file

@ -0,0 +1 @@
puts "rubyopt.rb required"

View file

@ -0,0 +1 @@
"test file"

View file

@ -0,0 +1 @@
puts $VERBOSE.inspect

View file

@ -0,0 +1,30 @@
require File.expand_path('../../spec_helper', __FILE__)
ruby_version_is "2.3" do
describe "The --enable-frozen-string-literal flag causes string literals to" do
it "produce the same object each time" do
ruby_exe(fixture(__FILE__, "freeze_flag_one_literal.rb"), options: "--enable-frozen-string-literal").chomp.should == "true"
end
it "produce the same object for literals with the same content" do
ruby_exe(fixture(__FILE__, "freeze_flag_two_literals.rb"), options: "--enable-frozen-string-literal").chomp.should == "true"
end
it "produce the same object for literals with the same content in different files" do
ruby_exe(fixture(__FILE__, "freeze_flag_across_files.rb"), options: "--enable-frozen-string-literal").chomp.should == "true"
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_flag_across_files_diff_enc.rb"), options: "--enable-frozen-string-literal").chomp.should == "true"
end
end
describe "The --debug flag produces" do
it "debugging info on attempted frozen string modification" do
error_str = ruby_exe(fixture(__FILE__, 'debug_info.rb'), options: '--debug', args: "2>&1")
error_str.should include("can't modify frozen String, created at ")
error_str.should include("command_line/fixtures/debug_info.rb:2")
end
end
end

View file

@ -0,0 +1,160 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "Processing RUBYOPT" do
before (:each) do
@rubyopt, ENV["RUBYOPT"] = ENV["RUBYOPT"], nil
end
after (:each) do
ENV["RUBYOPT"] = @rubyopt
end
it "adds the -I path to $LOAD_PATH" do
ENV["RUBYOPT"] = "-Ioptrubyspecincl"
result = ruby_exe("puts $LOAD_PATH.grep(/byspecin/)", escape: true)
result.chomp[-15..-1].should == "optrubyspecincl"
end
it "sets $DEBUG to true for '-d'" do
ENV["RUBYOPT"] = '-d'
command = %[puts "value of $DEBUG is \#{$DEBUG}"]
result = ruby_exe(command, escape: true, args: "2>&1")
result.should =~ /value of \$DEBUG is true/
end
it "prints the version number for '-v'" do
ENV["RUBYOPT"] = '-v'
ruby_exe("")[/\A.*/].should == RUBY_DESCRIPTION
end
it "sets $VERBOSE to true for '-w'" do
ENV["RUBYOPT"] = '-w'
ruby_exe("p $VERBOSE", escape: true).chomp.should == "true"
end
it "sets $VERBOSE to true for '-W'" do
ENV["RUBYOPT"] = '-W'
ruby_exe("p $VERBOSE", escape: true).chomp.should == "true"
end
it "sets $VERBOSE to nil for '-W0'" do
ENV["RUBYOPT"] = '-W0'
ruby_exe("p $VERBOSE", escape: true).chomp.should == "nil"
end
it "sets $VERBOSE to false for '-W1'" do
ENV["RUBYOPT"] = '-W1'
ruby_exe("p $VERBOSE", escape: true).chomp.should == "false"
end
it "sets $VERBOSE to true for '-W2'" do
ENV["RUBYOPT"] = '-W2'
ruby_exe("p $VERBOSE", escape: true).chomp.should == "true"
end
it "requires the file for '-r'" do
f = fixture __FILE__, "rubyopt"
ENV["RUBYOPT"] = "-r#{f}"
ruby_exe("0", args: '2>&1').should =~ /^rubyopt.rb required/
end
it "raises a RuntimeError for '-a'" do
ENV["RUBYOPT"] = '-a'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-p'" do
ENV["RUBYOPT"] = '-p'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-n'" do
ENV["RUBYOPT"] = '-n'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-y'" do
ENV["RUBYOPT"] = '-y'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-c'" do
ENV["RUBYOPT"] = '-c'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-s'" do
ENV["RUBYOPT"] = '-s'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-h'" do
ENV["RUBYOPT"] = '-h'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '--help'" do
ENV["RUBYOPT"] = '--help'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-l'" do
ENV["RUBYOPT"] = '-l'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-S'" do
ENV["RUBYOPT"] = '-S irb'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-e'" do
ENV["RUBYOPT"] = '-e0'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-i'" do
ENV["RUBYOPT"] = '-i.bak'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-x'" do
ENV["RUBYOPT"] = '-x'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-C'" do
ENV["RUBYOPT"] = '-C'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-X'" do
ENV["RUBYOPT"] = '-X.'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-F'" do
ENV["RUBYOPT"] = '-F'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '-0'" do
ENV["RUBYOPT"] = '-0'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '--copyright'" do
ENV["RUBYOPT"] = '--copyright'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '--version'" do
ENV["RUBYOPT"] = '--version'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
it "raises a RuntimeError for '--yydebug'" do
ENV["RUBYOPT"] = '--yydebug'
ruby_exe("", args: '2>&1').should =~ /RuntimeError/
end
end

View file

@ -0,0 +1,9 @@
describe :command_line_verbose, shared: true do
before :each do
@script = fixture __FILE__, "verbose.rb"
end
it "sets $VERBOSE to true" do
ruby_exe(@script, options: @method).chomp.split.last.should == "true"
end
end

View file

@ -0,0 +1,13 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "The interpreter" do
it "prints an error when given a file with invalid syntax" do
out = ruby_exe(fixture(__FILE__, "bad_syntax.rb"), args: "2>&1")
out.should include "syntax error"
end
it "prints an error when given code via -e with invalid syntax" do
out = ruby_exe(nil, args: "-e 'a{' 2>&1")
out.should include "syntax error"
end
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF" do
it "is extended by the Enumerable module" do
ARGF.should be_kind_of(Enumerable)
end
it "is an instance of ARGF.class" do
ARGF.should be_an_instance_of(ARGF.class)
end
end

View file

@ -0,0 +1,19 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.argv" do
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
end
it "returns ARGV for the initial ARGF" do
ARGF.argv.should equal ARGV
end
it "returns the remaining arguments to treat" do
argf [@file1, @file2] do
# @file1 is stored in current file
@argf.argv.should == [@file2]
end
end
end

View file

@ -0,0 +1,43 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.binmode" do
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
@bin_file = fixture __FILE__, "bin_file.txt"
end
it "returns self" do
argf [@bin_file] do
@argf.binmode.should equal @argf
end
end
platform_is :windows do
it "puts reading into binmode" do
argf [@bin_file, @bin_file] do
@argf.gets.should == "test\n"
@argf.binmode
@argf.gets.should == "test\r\n"
end
end
it "puts alls subsequent stream reading through ARGF into binmode" do
argf [@bin_file, @bin_file] do
@argf.binmode
@argf.gets.should == "test\r\n"
@argf.gets.should == "test\r\n"
end
end
end
it "sets the file's encoding to ASCII-8BIT" do
argf [@bin_file, @file1] do
@argf.binmode
@argf.binmode?.should == true
@argf.gets.encoding.should == Encoding::ASCII_8BIT
@argf.skip
@argf.read.encoding.should == Encoding::ASCII_8BIT
end
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/each_byte', __FILE__)
describe "ARGF.bytes" do
it_behaves_like :argf_each_byte, :bytes
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/each_char', __FILE__)
describe "ARGF.chars" do
it_behaves_like :argf_each_char, :chars
end

View file

@ -0,0 +1,46 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.close" do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
end
it "closes the current open stream" do
argf [@file1_name, @file2_name] do
io = @argf.to_io
@argf.close
io.closed?.should be_true
end
end
it "returns self" do
argf [@file1_name, @file2_name] do
@argf.close.should equal(@argf)
end
end
ruby_version_is ""..."2.3" do
it "raises an IOError if called on a closed stream" do
argf [@file1_name] do
lambda { @argf.close }.should_not raise_error
lambda { @argf.close }.should raise_error(IOError)
end
end
end
ruby_version_is "2.3" do
it "doesn't raise an IOError if called on a closed stream" do
argf [@file1_name] do
lambda { @argf.close }.should_not raise_error
lambda { @argf.close }.should_not raise_error
end
end
end
end
describe "ARGF.close" do
it "does not close STDIN" do
ruby_exe("ARGV.replace(['-']); ARGF.close; print ARGF.closed?").should == "false"
end
end

View file

@ -0,0 +1,18 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.closed?" do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
end
it "returns true if the current stream has been closed" do
argf [@file1_name, @file2_name] do
stream = @argf.to_io
stream.close
@argf.closed?.should be_true
stream.reopen(@argf.filename, 'r')
end
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/each_codepoint', __FILE__)
describe "ARGF.codepoints" do
it_behaves_like :argf_each_codepoint, :codepoints
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/each_byte', __FILE__)
describe "ARGF.each_byte" do
it_behaves_like :argf_each_byte, :each_byte
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/each_char', __FILE__)
describe "ARGF.each_char" do
it_behaves_like :argf_each_char, :each_char
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/each_codepoint', __FILE__)
describe "ARGF.each_codepoint" do
it_behaves_like :argf_each_codepoint, :each_codepoint
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/each_line', __FILE__)
describe "ARGF.each_line" do
it_behaves_like :argf_each_line, :each_line
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/each_line', __FILE__)
describe "ARGF.each" do
it_behaves_like :argf_each_line, :each
end

View file

@ -0,0 +1,10 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/eof', __FILE__)
describe "ARGF.eof" do
it_behaves_like :argf_eof, :eof
end
describe "ARGF.eof?" do
it_behaves_like :argf_eof, :eof?
end

View file

@ -0,0 +1,21 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.file" do
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
end
# NOTE: this test assumes that fixtures files have two lines each
it "returns the current file object on each file" do
argf [@file1, @file2] do
result = []
# returns first current file even when not yet open
result << @argf.file.path
result << @argf.file.path while @argf.gets
# returns last current file even when closed
result << @argf.file.path
result.should == [@file1, @file1, @file1, @file2, @file2, @file2]
end
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/filename', __FILE__)
describe "ARGF.filename" do
it_behaves_like :argf_filename, :filename
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/fileno', __FILE__)
describe "ARGF.fileno" do
it_behaves_like :argf_fileno, :fileno
end

View file

@ -0,0 +1,2 @@
test
test

View file

@ -0,0 +1,2 @@
file1.1
file1.2

View file

@ -0,0 +1,2 @@
line2.1
line2.2

View file

@ -0,0 +1,3 @@
puts $FILENAME while ARGF.gets
# returns last current file even when closed
puts $FILENAME

View file

@ -0,0 +1,5 @@
puts $.
ARGF.gets
puts $.
ARGF.gets
puts $.

View file

@ -0,0 +1,5 @@
puts ARGF.lineno
ARGF.gets
puts ARGF.lineno
ARGF.rewind
puts ARGF.lineno

View file

@ -0,0 +1,2 @@
stdin.1
stdin.2

View file

@ -0,0 +1,20 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/getc', __FILE__)
describe "ARGF.getc" do
it_behaves_like :argf_getc, :getc
end
describe "ARGF.getc" do
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
end
it "returns nil when end of stream reached" do
argf [@file1, @file2] do
@argf.read
@argf.getc.should == nil
end
end
end

View file

@ -0,0 +1,51 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/gets', __FILE__)
describe "ARGF.gets" do
it_behaves_like :argf_gets, :gets
end
describe "ARGF.gets" do
it_behaves_like :argf_gets_inplace_edit, :gets
end
describe "ARGF.gets" do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@file1 = File.readlines @file1_name
@file2 = File.readlines @file2_name
end
it "returns nil when reaching end of files" do
argf [@file1_name, @file2_name] do
total = @file1.size + @file2.size
total.times { @argf.gets }
@argf.gets.should == nil
end
end
with_feature :encoding do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = nil
end
after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end
it "reads the contents of the file with default encoding" do
Encoding.default_external = Encoding::US_ASCII
argf [@file1_name, @file2_name] do
@argf.gets.encoding.should == Encoding::US_ASCII
end
end
end
end

View file

@ -0,0 +1,30 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.lineno" do
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
end
# NOTE: this test assumes that fixtures files have two lines each
# TODO: break this into four specs
it "returns the current line number on each file" do
argf [@file1, @file2] do
@argf.lineno = 0
@argf.gets
@argf.lineno.should == 1
@argf.gets
@argf.lineno.should == 2
@argf.gets
@argf.lineno.should == 3
@argf.gets
@argf.lineno.should == 4
end
end
it "aliases to $." do
script = fixture __FILE__, "lineno.rb"
out = ruby_exe(script, args: [@file1, @file2])
out.should == "0\n1\n2\n"
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/each_line', __FILE__)
describe "ARGF.lines" do
it_behaves_like :argf_each_line, :lines
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/filename', __FILE__)
describe "ARGF.path" do
it_behaves_like :argf_filename, :path
end

View file

@ -0,0 +1,38 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/pos', __FILE__)
describe "ARGF.pos" do
it_behaves_like :argf_pos, :pos
end
describe "ARGF.pos=" do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@file1 = File.readlines @file1_name
@file2 = File.readlines @file2_name
end
# NOTE: this test assumes that fixtures files have two lines each
it "sets the correct position in files" do
argf [@file1_name, @file2_name] do
@argf.pos = @file1.first.size
@argf.gets.should == @file1.last
@argf.pos = 0
@argf.gets.should == @file1.first
# finish reading file1
@argf.gets
@argf.gets
@argf.pos = 1
@argf.gets.should == @file2.first[1..-1]
@argf.pos = @file2.first.size + @file2.last.size - 1
@argf.gets.should == @file2.last[-1,1]
@argf.pos = 1000
@argf.read.should == ""
end
end
end

View file

@ -0,0 +1,82 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/read', __FILE__)
platform_is_not :windows do
describe 'ARGF.read_nonblock' do
it_behaves_like :argf_read, :read_nonblock
before do
@file1_name = fixture(__FILE__, 'file1.txt')
@file2_name = fixture(__FILE__, 'file2.txt')
@file1 = File.read(@file1_name)
@file2 = File.read(@file2_name)
@chunk1 = File.read(@file1_name, 4)
@chunk2 = File.read(@file2_name, 4)
end
it 'reads up to the given amount of bytes' do
argf [@file1_name] do
@argf.read_nonblock(4).should == @chunk1
end
end
describe 'when using multiple files' do
it 'reads up to the given amount of bytes from the first file' do
argf [@file1_name, @file2_name] do
@argf.read_nonblock(4).should == @chunk1
end
end
it 'returns an empty String when reading after having read the first file in its entirety' do
argf [@file1_name, @file2_name] do
@argf.read_nonblock(File.size(@file1_name)).should == @file1
@argf.read_nonblock(4).should == ''
end
end
end
it 'reads up to the given bytes from STDIN' do
stdin = ruby_exe('print ARGF.read_nonblock(4)', :args => "< #{@file1_name}")
stdin.should == @chunk1
end
it 'reads up to the given bytes from a file when a file and STDIN are present' do
stdin = ruby_exe("print ARGF.read_nonblock(4)", :args => "#{@file1_name} - < #{@file2_name}")
stdin.should == @chunk1
end
context "with STDIN" do
before do
@r, @w = IO.pipe
@stdin = $stdin
$stdin = @r
end
after do
$stdin = @stdin
@w.close
@r.close unless @r.closed?
end
it 'raises IO::EAGAINWaitReadable when empty' do
argf ['-'] do
lambda {
@argf.read_nonblock(4)
}.should raise_error(IO::EAGAINWaitReadable)
end
end
ruby_version_is "2.3" do
it 'returns :wait_readable when the :exception is set to false' do
argf ['-'] do
@argf.read_nonblock(4, nil, exception: false).should == :wait_readable
end
end
end
end
end
end

View file

@ -0,0 +1,87 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/read', __FILE__)
describe "ARGF.read" do
it_behaves_like :argf_read, :read
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@stdin_name = fixture __FILE__, "stdin.txt"
@file1 = File.read @file1_name
@file2 = File.read @file2_name
@stdin = File.read @stdin_name
end
it "reads the contents of a file" do
argf [@file1_name] do
@argf.read().should == @file1
end
end
it "treats first nil argument as no length limit" do
argf [@file1_name] do
@argf.read(nil).should == @file1
end
end
it "reads the contents of two files" do
argf [@file1_name, @file2_name] do
@argf.read.should == @file1 + @file2
end
end
it "reads the contents of one file and some characters from the second" do
argf [@file1_name, @file2_name] do
len = @file1.size + (@file2.size / 2)
@argf.read(len).should == (@file1 + @file2)[0,len]
end
end
it "reads across two files consecutively" do
argf [@file1_name, @file2_name] do
@argf.read(@file1.size - 2).should == @file1[0..-3]
@argf.read(2+5).should == @file1[-2..-1] + @file2[0,5]
end
end
it "reads the contents of stdin" do
stdin = ruby_exe("print ARGF.read", args: "< #{@stdin_name}")
stdin.should == @stdin
end
it "reads the contents of one file and stdin" do
stdin = ruby_exe("print ARGF.read", args: "#{@file1_name} - < #{@stdin_name}")
stdin.should == @file1 + @stdin
end
it "reads the contents of the same file twice" do
argf [@file1_name, @file1_name] do
@argf.read.should == @file1 + @file1
end
end
with_feature :encoding do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = nil
end
after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end
it "reads the contents of the file with default encoding" do
Encoding.default_external = Encoding::US_ASCII
argf [@file1_name, @file2_name] do
@argf.read.encoding.should == Encoding::US_ASCII
end
end
end
end

View file

@ -0,0 +1,19 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/getc', __FILE__)
describe "ARGF.getc" do
it_behaves_like :argf_getc, :readchar
end
describe "ARGF.readchar" do
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
end
it "raises EOFError when end of stream reached" do
argf [@file1, @file2] do
lambda { while @argf.readchar; end }.should raise_error(EOFError)
end
end
end

View file

@ -0,0 +1,23 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/gets', __FILE__)
describe "ARGF.readline" do
it_behaves_like :argf_gets, :readline
end
describe "ARGF.readline" do
it_behaves_like :argf_gets_inplace_edit, :readline
end
describe "ARGF.readline" do
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
end
it "raises an EOFError when reaching end of files" do
argf [@file1, @file2] do
lambda { while @argf.readline; end }.should raise_error(EOFError)
end
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/readlines', __FILE__)
describe "ARGF.readlines" do
it_behaves_like :argf_readlines, :readlines
end

View file

@ -0,0 +1,77 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/read', __FILE__)
describe "ARGF.readpartial" do
it_behaves_like :argf_read, :readpartial
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@stdin_name = fixture __FILE__, "stdin.txt"
@file1 = File.read @file1_name
@file2 = File.read @file2_name
@stdin = File.read @stdin_name
end
it "raises an ArgumentError if called without a maximum read length" do
argf [@file1_name] do
lambda { @argf.readpartial }.should raise_error(ArgumentError)
end
end
it "reads maximum number of bytes from one file at a time" do
argf [@file1_name, @file2_name] do
len = @file1.size + @file2.size
@argf.readpartial(len).should == @file1
end
end
it "clears output buffer even if EOFError is raised because @argf is at end" do
begin
output = "to be cleared"
argf [@file1_name] do
@argf.read
@argf.readpartial(1, output)
end
rescue EOFError
output.should == ""
end
end
it "reads maximum number of bytes from one file at a time" do
argf [@file1_name, @file2_name] do
len = @file1.size + @file2.size
@argf.readpartial(len).should == @file1
end
end
it "returns an empty string if EOFError is raised while reading any but the last file" do
argf [@file1_name, @file2_name] do
@argf.readpartial(@file1.size)
@argf.readpartial(1).should == ""
end
end
ruby_version_is "2.3" do
it "raises an EOFError if the exception was raised while reading the last file" do
argf [@file1_name, @file2_name] do
@argf.readpartial(@file1.size)
@argf.readpartial(1)
@argf.readpartial(@file2.size)
lambda { @argf.readpartial(1) }.should raise_error(EOFError)
lambda { @argf.readpartial(1) }.should raise_error(EOFError)
end
end
end
it "raises an EOFError if the exception was raised while reading STDIN" do
ruby_str = <<-STR
print ARGF.readpartial(#{@stdin.size})
ARGF.readpartial(1) rescue print $!.class
STR
stdin = ruby_exe(ruby_str, args: "< #{@stdin_name}", escape: true)
stdin.should == @stdin + "EOFError"
end
end

View file

@ -0,0 +1,39 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.rewind" do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@file1 = File.readlines @file1_name
@file2 = File.readlines @file2_name
end
# NOTE: this test assumes that fixtures files have two lines each
it "goes back to beginning of current file" do
argf [@file1_name, @file2_name] do
@argf.gets
@argf.rewind
@argf.gets.should == @file1.first
@argf.gets # finish reading file1
@argf.gets
@argf.rewind
@argf.gets.should == @file2.first
end
end
it "resets ARGF.lineno to 0" do
script = fixture __FILE__, "rewind.rb"
out = ruby_exe(script, args: [@file1_name, @file2_name])
out.should == "0\n1\n0\n"
end
it "raises an ArgumentError when end of stream reached" do
argf [@file1_name, @file2_name] do
@argf.read
lambda { @argf.rewind }.should raise_error(ArgumentError)
end
end
end

View file

@ -0,0 +1,63 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.seek" do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@file1 = File.readlines @file1_name
@file2 = File.readlines @file2_name
end
it "sets the absolute position relative to beginning of file" do
argf [@file1_name, @file2_name] do
@argf.seek 2
@argf.gets.should == @file1.first[2..-1]
@argf.seek @file1.first.size
@argf.gets.should == @file1.last
@argf.seek 0, IO::SEEK_END
@argf.gets.should == @file2.first
end
end
it "sets the position relative to current position in file" do
argf [@file1_name, @file2_name] do
@argf.seek(0, IO::SEEK_CUR)
@argf.gets.should == @file1.first
@argf.seek(-@file1.first.size+2, IO::SEEK_CUR)
@argf.gets.should == @file1.first[2..-1]
@argf.seek(1, IO::SEEK_CUR)
@argf.gets.should == @file1.last[1..-1]
@argf.seek(3, IO::SEEK_CUR)
@argf.gets.should == @file2.first
@argf.seek(@file1.last.size, IO::SEEK_CUR)
@argf.gets.should == nil
end
end
it "sets the absolute position relative to end of file" do
argf [@file1_name, @file2_name] do
@argf.seek(-@file1.first.size-@file1.last.size, IO::SEEK_END)
@argf.gets.should == @file1.first
@argf.seek(-6, IO::SEEK_END)
@argf.gets.should == @file1.last[-6..-1]
@argf.seek(-4, IO::SEEK_END)
@argf.gets.should == @file1.last[4..-1]
@argf.gets.should == @file2.first
@argf.seek(-6, IO::SEEK_END)
@argf.gets.should == @file2.last[-6..-1]
end
end
end
describe "ARGF.seek" do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
end
it "takes at least one argument (offset)" do
argf [@file1_name] do
lambda { @argf.seek }.should raise_error(ArgumentError)
end
end
end

View file

@ -0,0 +1,41 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "ARGF.set_encoding" do
before :each do
@file = fixture __FILE__, "file1.txt"
end
it "sets the external encoding when passed an encoding instance" do
argf [@file] do
@argf.set_encoding(Encoding::US_ASCII)
@argf.external_encoding.should == Encoding::US_ASCII
@argf.gets.encoding.should == Encoding::US_ASCII
end
end
it "sets the external encoding when passed an encoding name" do
argf [@file] do
@argf.set_encoding("us-ascii")
@argf.external_encoding.should == Encoding::US_ASCII
@argf.gets.encoding.should == Encoding::US_ASCII
end
end
it "sets the external, internal encoding when passed two encoding instances" do
argf [@file] do
@argf.set_encoding(Encoding::US_ASCII, Encoding::EUC_JP)
@argf.external_encoding.should == Encoding::US_ASCII
@argf.internal_encoding.should == Encoding::EUC_JP
@argf.gets.encoding.should == Encoding::EUC_JP
end
end
it "sets the external, internal encoding when passed 'ext:int' String" do
argf [@file] do
@argf.set_encoding("us-ascii:euc-jp")
@argf.external_encoding.should == Encoding::US_ASCII
@argf.internal_encoding.should == Encoding::EUC_JP
@argf.gets.encoding.should == Encoding::EUC_JP
end
end
end

View file

@ -0,0 +1,58 @@
describe :argf_each_byte, shared: true do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@bytes = []
File.read(@file1_name).each_byte { |b| @bytes << b }
File.read(@file2_name).each_byte { |b| @bytes << b }
end
it "yields each byte of all streams to the passed block" do
argf [@file1_name, @file2_name] do
bytes = []
@argf.send(@method) { |b| bytes << b }
bytes.should == @bytes
end
end
it "returns self when passed a block" do
argf [@file1_name, @file2_name] do
@argf.send(@method) {}.should equal(@argf)
end
end
it "returns an Enumerator when passed no block" do
argf [@file1_name, @file2_name] do
enum = @argf.send(@method)
enum.should be_an_instance_of(Enumerator)
bytes = []
enum.each { |b| bytes << b }
bytes.should == @bytes
end
end
describe "when no block is given" do
it "returns an Enumerator" do
argf [@file1_name, @file2_name] do
enum = @argf.send(@method)
enum.should be_an_instance_of(Enumerator)
bytes = []
enum.each { |b| bytes << b }
bytes.should == @bytes
end
end
describe "returned Enumerator" do
describe "size" do
it "should return nil" do
argf [@file1_name, @file2_name] do
@argf.send(@method).size.should == nil
end
end
end
end
end
end

View file

@ -0,0 +1,58 @@
describe :argf_each_char, shared: true do
before :each do
@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@chars = []
File.read(@file1_name).each_char { |c| @chars << c }
File.read(@file2_name).each_char { |c| @chars << c }
end
it "yields each char of all streams to the passed block" do
argf [@file1_name, @file2_name] do
chars = []
@argf.send(@method) { |c| chars << c }
chars.should == @chars
end
end
it "returns self when passed a block" do
argf [@file1_name, @file2_name] do
@argf.send(@method) {}.should equal(@argf)
end
end
it "returns an Enumerator when passed no block" do
argf [@file1_name, @file2_name] do
enum = @argf.send(@method)
enum.should be_an_instance_of(Enumerator)
chars = []
enum.each { |c| chars << c }
chars.should == @chars
end
end
describe "when no block is given" do
it "returns an Enumerator" do
argf [@file1_name, @file2_name] do
enum = @argf.send(@method)
enum.should be_an_instance_of(Enumerator)
chars = []
enum.each { |c| chars << c }
chars.should == @chars
end
end
describe "returned Enumerator" do
describe "size" do
it "should return nil" do
argf [@file1_name, @file2_name] do
@argf.send(@method).size.should == nil
end
end
end
end
end
end

Some files were not shown because too many files have changed in this diff Show more