pax_global_header00006660000000000000000000000064120446512660014520gustar00rootroot0000000000000052 comment=5036c3688f592f796496d2f2f0752846fc4bfb63 ruby-cutest-1.2.0/000077500000000000000000000000001204465126600140065ustar00rootroot00000000000000ruby-cutest-1.2.0/LICENSE000066400000000000000000000020641204465126600150150ustar00rootroot00000000000000Copyright (c) 2010 Damian Janowski & Michel Martens 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. ruby-cutest-1.2.0/README.markdown000066400000000000000000000107451204465126600165160ustar00rootroot00000000000000Cutest ======= Forking tests. Description ----------- Each test file is run in a forked process to avoid shared state. Once a failure is found, you get a report detailing what failed and how to locate the error and the rest of the file is skipped. You can use the `scope` command around tests: it guarantees that no instance variables are shared between tests. There are two commands very similar in nature, but with a subtle difference that makes them easy to combine in order to satisfy different needs: `prepare` and `setup`. The `prepare` blocks are executed before each test. If you call `prepare` many times, each passed block is appended to an array. When the test is run, all those prepare blocks are executed in order. The result of the block is discarded, so it is only useful for preparing the environment (flushing the database, removing a directory, etc.). The `setup` block is executed before each test and the result is passed as a parameter to the `test` block. Unlike `prepare`, each definition of `setup` overrides the previous one. Even if you can declare instance variables and share them between tests, the recommended usage is to pass the result of the block as a parameter to the `test` blocks. The `test` method executes the passed block after running `prepare` and `setup`. This is where assertions must be declared. Three assertions are available: `assert`, that accepts a value and raises if it's false or nil; `assert_equal`, that raises if its arguments are not equal; and `assert_raise`, that executes a passed block and compares the raised exception to the expected one. In all cases, if the expectation is no met, an `AssertionFailed` exception is raised. Usage ----- In your Rakefile: require "cutest" task :test do Cutest.run(Dir["test/*.rb"]) end task :default => :test Or from the command line: $ cutest test/*.rb In your tests: setup do {:a => 23, :b => 43} end test "should receive the result of the setup block as a parameter" do |params| assert params == {:a => 23, :b => 43} end test "should evaluate the setup block before each test" do |params| params[:a] = nil end test "should preserve the original values from the setup" do |params| assert 23 == params[:a] end An example working with a prepare block: prepare do Ohm.flush end setup do Ohm.redis.get("foo") end test do |foo| assert foo.nil? end And working with scopes: setup do @foo = true end @bar = true scope do test "should not share instance variables" do |foo| assert !defined?(@foo) assert !defined?(@bar) assert foo == true end end The tests in these two examples will pass. Unlike other testing frameworks, Cutest does not compile all the tests before running them. Handling errors --------------- If you get an error when running the tests, this is what you will see: Exception: assert_equal 24, params[:a] # 24 != 23 test/setup.rb +14 Command Line Interface ---------------------- The tool `cutest` accepts a list of files and sends them to `Cutest.run`. If you need to require a file or library before running the tests, as is the case with test helpers, use the `-r` flag: $ cutest -r test/helper.rb test/*_test.rb If you want to check which version you are running, try the `-v` flag. Installation ------------ $ gem install cutest License ------- Copyright (c) 2010 Damian Janowski and Michel Martens 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. ruby-cutest-1.2.0/Rakefile000066400000000000000000000002171204465126600154530ustar00rootroot00000000000000require File.expand_path("../lib/cutest", __FILE__) $VERBOSE = true task :test do Cutest.run(Dir["test/*.rb"]) end task :default => :test ruby-cutest-1.2.0/bin/000077500000000000000000000000001204465126600145565ustar00rootroot00000000000000ruby-cutest-1.2.0/bin/cutest000077500000000000000000000006061204465126600160150ustar00rootroot00000000000000#!/usr/bin/env ruby if ARGV.empty? puts "usage: cutest [-r lib] [-v] file ..." exit end require_relative "../lib/cutest" require_relative "../lib/cutest/vendor/clap" files = Clap.run ARGV, "-r" => lambda { |file| require file }, "-o" => lambda { |name| cutest[:only] = name }, "-v" => lambda { puts Cutest::VERSION } if files.any? Cutest.run(Dir[*files]) end ruby-cutest-1.2.0/cutest.gemspec000066400000000000000000000011411204465126600166570ustar00rootroot00000000000000require "./lib/cutest" Gem::Specification.new do |s| s.name = "cutest" s.version = Cutest::VERSION s.summary = "Forking tests." s.description = "Run tests in separate processes to avoid shared state." s.authors = ["Damian Janowski", "Michel Martens"] s.email = ["djanowski@dimaion.com", "michel@soveran.com"] s.homepage = "http://github.com/djanowski/cutest" s.files = Dir[ "LICENSE", "README.markdown", "Rakefile", "lib/**/*.rb", "*.gemspec", "test/**/*.*" ] s.executables.push "cutest" end ruby-cutest-1.2.0/lib/000077500000000000000000000000001204465126600145545ustar00rootroot00000000000000ruby-cutest-1.2.0/lib/cutest.rb000066400000000000000000000112141204465126600164070ustar00rootroot00000000000000class Cutest unless defined?(VERSION) VERSION = "1.2.0" FILTER = %r[/(ruby|jruby|rbx)[-/]([0-9\.])+] CACHE = Hash.new { |h, k| h[k] = File.readlines(k) } end def self.run(files) files.each do |file| run_file(file) Process.wait break unless $?.success? end puts end def self.run_file(file) fork do begin load(file) rescue LoadError, SyntaxError display_error exit 1 rescue StandardError trace = $!.backtrace pivot = trace.index { |line| line.match(file) } puts "\n test: %s" % cutest[:test] if pivot other = trace[0..pivot].select { |line| line !~ FILTER } other.reverse.each { |line| display_trace(line) } else display_trace(trace.first) end display_error exit 1 end end end def self.code(fn, ln) begin CACHE[fn][ln.to_i - 1].strip rescue "(Can't display line)" end end def self.display_error print "\n#{$!.class}: " print "#{$!.message}\n" end def self.display_trace(line) fn, ln = line.split(":") puts " line: #{code(fn, ln)}" puts " file: #{fn} +#{ln}" end class AssertionFailed < StandardError end class Scope def initialize(&scope) @scope = scope end def call instance_eval(&@scope) end end end module Kernel private # Use Thread.current[:cutest] to store information about test preparation # and setup. Thread.current[:cutest] ||= { :prepare => [] } # Shortcut to access Thread.current[:cutest]. def cutest Thread.current[:cutest] end # Create a class where the block will be evaluated. Recommended to improve # isolation between tests. def scope(&block) Cutest::Scope.new(&block).call end # Prepare the environment in order to run the tests. This method can be # called many times, and each new block is appended to a list of # preparation blocks. When a test is executed, all the preparation blocks # are ran in the order they were declared. If called without a block, it # returns the array of preparation blocks. def prepare(&block) cutest[:prepare] << block if block_given? cutest[:prepare] end # Setup parameters for the tests. The block passed to setup is evaluated # before running each test, and the result of the setup block is passed to # the test as a parameter. If the setup and the tests are declared at the # same level (in the global scope or in a sub scope), it is possible to use # instance variables, but the parameter passing pattern is recommended to # ensure there are no side effects. # # If the setup blocks are declared in the global scope and the tests are # declared in sub scopes, the parameter passing usage is required. # # Setup blocks can be defined many times, but each new definition overrides # the previous one. It is recommended to split the tests in many different # files (the report is per file, not per assertion). Usually one setup # block per file is enough, but nothing forbids having different scopes # with different setup blocks. def setup(&block) cutest[:setup] = block if block_given? cutest[:setup] end # Kernel includes a test method for performing tests on files. undef test if defined? test # Call the prepare and setup blocks before executing the test. Even # though the assertions can live anywhere (it's not mandatory to put them # inside test blocks), it is necessary to wrap them in test blocks in order # to execute preparation and setup blocks. def test(name = nil, &block) cutest[:test] = name if !cutest[:only] || cutest[:only] == name prepare.each { |blk| blk.call } block.call(setup && setup.call) end end # Assert that value is not nil or false. def assert(value) flunk("expression returned #{value.inspect}") unless value success end # Assert that two values are equal. def assert_equal(value, other) flunk("#{value.inspect} != #{other.inspect}") unless value == other success end # Assert that the block doesn't raise the expected exception. def assert_raise(expected = Exception) begin yield rescue => exception ensure flunk("got #{exception.inspect} instead") unless exception.kind_of?(expected) success end end # Stop the tests and raise an error where the message is the last line # executed before flunking. def flunk(message = nil) exception = Cutest::AssertionFailed.new(message) exception.set_backtrace([caller[1]]) raise exception end # Executed when an assertion succeeds. def success print "." end end ruby-cutest-1.2.0/lib/cutest/000077500000000000000000000000001204465126600160635ustar00rootroot00000000000000ruby-cutest-1.2.0/lib/cutest/vendor/000077500000000000000000000000001204465126600173605ustar00rootroot00000000000000ruby-cutest-1.2.0/lib/cutest/vendor/clap.rb000077500000000000000000000014311204465126600206260ustar00rootroot00000000000000class Clap attr :argv attr :opts def self.run(args, opts) new(args, opts).run end def initialize(argv, opts) @argv = argv.dup @opts = opts end def run args = [] while argv.any? item = argv.shift flag = opts[item] if flag # Work around lambda semantics in 1.8.7. arity = [flag.arity, 0].max # Raise if there are not enough parameters # available for the flag. if argv.size < arity raise ArgumentError end # Call the lambda with N items from argv, # where N is the lambda's arity. flag.call(*argv.shift(arity)) else # Collect the items that don't correspond to # flags. args << item end end args end end ruby-cutest-1.2.0/metadata.yml000066400000000000000000000026371204465126600163210ustar00rootroot00000000000000--- !ruby/object:Gem::Specification name: cutest version: !ruby/object:Gem::Version version: 1.2.0 prerelease: platform: ruby authors: - Damian Janowski - Michel Martens autorequire: bindir: bin cert_chain: [] date: 2012-10-09 00:00:00.000000000 Z dependencies: [] description: Run tests in separate processes to avoid shared state. email: - djanowski@dimaion.com - michel@soveran.com executables: - cutest extensions: [] extra_rdoc_files: [] files: - LICENSE - README.markdown - Rakefile - lib/cutest/vendor/clap.rb - lib/cutest.rb - cutest.gemspec - test/assert.rb - test/assert_equal.rb - test/assert_raise.rb - test/fixtures/exception.rb - test/fixtures/fail_custom_assertion.rb - test/fixtures/failure.rb - test/fixtures/failure_in_loaded_file.rb - test/fixtures/success.rb - test/prepare.rb - test/run.rb - test/scopes.rb - test/setup.rb - !binary |- YmluL2N1dGVzdA== homepage: http://github.com/djanowski/cutest licenses: [] post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 1.8.11 signing_key: specification_version: 3 summary: Forking tests. test_files: [] ruby-cutest-1.2.0/test/000077500000000000000000000000001204465126600147655ustar00rootroot00000000000000ruby-cutest-1.2.0/test/assert.rb000066400000000000000000000002511204465126600166110ustar00rootroot00000000000000test "succeeds if the value is true" do assert true end test "raises if the assertion fails" do assert_raise(Cutest::AssertionFailed) do assert false end end ruby-cutest-1.2.0/test/assert_equal.rb000066400000000000000000000002251204465126600200010ustar00rootroot00000000000000test do assert_equal 1, 1 end test "raises if the assertion fails" do assert_raise(Cutest::AssertionFailed) do assert_equal 1, 2 end end ruby-cutest-1.2.0/test/assert_raise.rb000066400000000000000000000004201204465126600177720ustar00rootroot00000000000000test "catches the right exception" do assert_raise(RuntimeError) do raise RuntimeError end end test "raises if the expectation is not met" do assert_raise(Cutest::AssertionFailed) do assert_raise(RuntimeError) do raise ArgumentError end end end ruby-cutest-1.2.0/test/fixtures/000077500000000000000000000000001204465126600166365ustar00rootroot00000000000000ruby-cutest-1.2.0/test/fixtures/exception.rb000066400000000000000000000001111204465126600211520ustar00rootroot00000000000000def foo raise "Oops" end test "some unhandled exception" do foo end ruby-cutest-1.2.0/test/fixtures/fail_custom_assertion.rb000066400000000000000000000002151204465126600235550ustar00rootroot00000000000000def assert_empty(string) flunk("not empty") unless string.empty? success end test "failed custom assertion" do assert_empty "foo" end ruby-cutest-1.2.0/test/fixtures/failure.rb000066400000000000000000000000561204465126600206130ustar00rootroot00000000000000test "failed assertion" do assert false end ruby-cutest-1.2.0/test/fixtures/failure_in_loaded_file.rb000066400000000000000000000000411204465126600236020ustar00rootroot00000000000000load("test/fixtures/failure.rb") ruby-cutest-1.2.0/test/fixtures/success.rb000066400000000000000000000001241204465126600206300ustar00rootroot00000000000000def foo raise "Invalid code" end test "external exceptions" do assert true end ruby-cutest-1.2.0/test/prepare.rb000066400000000000000000000004721204465126600167530ustar00rootroot00000000000000prepare do $foo = [] end prepare do $foo << true end test "all the prepare blocks are called" do assert $foo == [true] end prepare do $foo << false end test "and are cumulative" do assert $foo == [true, false] end scope do test "and run inside scopes" do assert $foo = [true, false] end end ruby-cutest-1.2.0/test/run.rb000066400000000000000000000030521204465126600161160ustar00rootroot00000000000000test "output of successful run" do expected = ".\n" out = %x{./bin/cutest test/fixtures/success.rb} assert_equal(expected, out) end test "output of failed run" do expected = "\n" + " test: failed assertion\n" + " line: assert false\n" + " file: test/fixtures/failure.rb +2\n\n" + "Cutest::AssertionFailed: expression returned false\n\n" out = %x{./bin/cutest test/fixtures/failure.rb} assert_equal(expected, out) end test "output of failed run" do expected = "\n" + " test: some unhandled exception\n" + " line: raise \"Oops\"\n" + " file: test/fixtures/exception.rb +2\n\n" + "RuntimeError: Oops\n\n" out = %x{./bin/cutest test/fixtures/exception.rb} assert_equal(expected, out) end test "output of custom assertion" do expected = "\n" + " test: failed custom assertion\n" + " line: assert_empty \"foo\"\n" + " file: test/fixtures/fail_custom_assertion.rb +7\n\n" + "Cutest::AssertionFailed: not empty\n\n" out = %x{./bin/cutest test/fixtures/fail_custom_assertion.rb} assert_equal(expected, out) end test "output of failure in nested file" do expected = "\n" + " test: failed assertion\n" + " line: assert false\n" + " file: test/fixtures/failure.rb +2\n\n" + "Cutest::AssertionFailed: expression returned false\n\n" out = %x{./bin/cutest test/fixtures/failure_in_loaded_file.rb} assert_equal(expected, out) end ruby-cutest-1.2.0/test/scopes.rb000066400000000000000000000005051204465126600166060ustar00rootroot00000000000000@bar = true scope do @foo = true test "something" do assert defined?(@foo) assert !defined?(@bar) end end scope do test "something" do assert !defined?(@foo) assert !defined?(@bar) end end scope do @baz = true scope do test "something" do assert !defined?(@baz) end end end ruby-cutest-1.2.0/test/setup.rb000066400000000000000000000011111204465126600164440ustar00rootroot00000000000000setup do {:a => 23, :b => 43} end test "should receive the result of the setup block as a parameter" do |params| assert params == {:a => 23, :b => 43} end test "if the params are modified..." do |params| params[:a] = nil end test "...it should preserve the original values from the setup" do |params| assert_equal 23, params[:a] end setup do "Hello world!" end test "only the most recently defined setup block is executed" do |value| assert "Hello world!" == value end scope do test "works inside scopes too" do |value| assert "Hello world!" == value end end