Giter VIP home page Giter VIP logo

rspec-parameterized's Introduction

RSpec::Parameterized Gem Version rspec

Support simple parameterized test syntax in rspec.

# Nested Array Style
describe "plus" do
  where(:a, :b, :answer) do
    [
      [1 , 2 , 3],
      [5 , 8 , 13],
      [0 , 0 , 0]
    ]
  end

  with_them do
    it "should do additions" do
      expect(a + b).to eq answer
    end
  end

  with_them do
    # Can browse parameters via `params` method in with_them block
    # Can browse all parameters via `all_params` method in with_them block
    it "#{params[:a]} + #{params[:b]} == #{params[:answer]}" do
      expect(a + b).to eq answer
    end
  end
end

# Hash and Array Style
# Given parameters is each value combinations
# On this case
# [
#   [1, 5, 2],
#   [1, 5, 4],
#   [1, 7, 2],
#   [1, 7, 4],
#   [1, 9, 2],
#   [1, 9, 4],
#   [3, 5, 2],
#   [3, 5, 4],
#   [3, 7, 2],
#   [3, 7, 4],
#   [3, 9, 2],
#   [3, 9, 4]
# ]
describe "Hash arguments" do
  where(a: [1, 3], b: [5, 7, 9], c: [2, 4])

  with_them do
    it "sums is even" do
      expect(a + b + c).to be_even
    end
  end
end

# Table Syntax Style (like Groovy spock)
# Need ruby-2.1 or later
describe "plus" do
  using RSpec::Parameterized::TableSyntax

  where(:a, :b, :answer) do
    1 | 2 | 3
    5 | 8 | 13
    0 | 0 | 0
  end

  with_them do
    it "should do additions" do
      expect(a + b).to eq answer
    end
  end
end

# Verbose Syntax
# For complex inputs or if you just want to be super explicit
describe "Verbose syntax" do
  where do
    {
      "positive integers" => {
        a: 1,
        b: 2,
        answer: 3,
      },
      "negative_integers" => {
        a: -1,
        b: -2,
        answer: -3,
      },
      "mixed_integers" => {
        a: 3,
        b: -3,
        answer: 0,
      },
    }
  end

  with_them do
    it "should do additions" do
      expect(a + b).to eq answer
    end
  end
end

# It's also possible to override each combination name using magic variable :case_name
# Output:
# Custom names for regular syntax
#   positive integers
#     should do additions
#   negative integers
#     should do additions
#   mixed integers
#     should do additions
describe "Custom names for regular syntax" do
  where(:case_name, :a, :b, :answer) do
    [
      ["positive integers",  6,  2,  8],
      ["negative integers", -1, -2, -3],
      ["mixed integers", -5,  3, -2],
    ]
  end

  with_them do
    it "should do additions" do
      expect(a + b).to eq answer
    end
  end
end

# Or :case_names lambda for hash syntax
# Output:
# Custom naming for hash syntax
#   1 + 5 + 2
#     sum is even
#   1 + 5 + 4
#     sum is even
#   1 + 7 + 2
#     sum is even
#   ...
describe "Custom naming for hash syntax" do
  where(case_names: ->(a, b, c){"#{a} + #{b} + #{c}"}, a: [1, 3], b: [5, 7, 9], c: [2, 4])

  with_them do
    it "sum is even" do
      expect(a + b + c).to be_even
    end
  end
end

# Use ref(:symbol) to use let/let! defined variables in the where block
# Use lazy when you want to create let/let! variables after the where block
#
# Failures will be more readable in the future - https://github.com/tomykaira/rspec-parameterized/pull/65
describe "lazy and ref types" do
  let(:one) { 1 }
  let(:four) { 4 }

  where(:a, :b, :result) do
    [
      [ref(:one), ref(:four), lazy { two + three }]
    ]
  end

  with_them do
    context "use let after where block" do
      let(:two) { 2 }
      let(:three) { 3 }

      it 'should equal 5' do
        expect(a + b).to eq result
      end
    end
  end
end

I was inspired by udzura's mock.

Support Versions

Ruby-2.6.0 or later.

Installation

group :test do
  gem "rspec-parameterized", ">= 1.0.0"
end

Usage

Require rspec-parameterized from your spec_helper.rb.

require 'rspec-parameterized'

Follow the sample spec above.

Arguments given to with_them is directly passed to describe. You can specify :pending, :focus, etc. here.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Also see

rspec-parameterized's People

Contributors

aliaksandr-martsinovich avatar dependabot[bot] avatar haito avatar hana-da avatar joker1007 avatar junichiito avatar mfolnovic avatar nalabjp avatar oatovar avatar olleolleolle avatar quintasan avatar ryy avatar shinji-yoshida avatar soartec-lab avatar sue445 avatar takkanm avatar tomykaira avatar yiyenene avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rspec-parameterized's Issues

Release new version for Ruby 2.5.0

Thanks for the gem! When the binding_of_caller gem is in use, all rspec-parameterized tests fail in Ruby 2.5 with the following message due to this Ruby bug:

RuntimeError:
  No such frame, gone beyond end of stack!

I've verified the master branch of this repository doesn't fail now that binding_ninja is used. Could you tag a new release?

rspec-parameterized-0.1.1.gem is broken

table.rb and table_syntax.rb are commited, but rspec-parameterized-0.1.1.gem does not contains this file for some reason.

$ wget http://rubygems.org/downloads/rspec-parameterized-0.1.1.gem
$ gem unpack rspec-parameterized-0.1.1.gem
$ cd rspec-parameterized-0.1.1
$ tree .
.
├── Gemfile
├── LICENSE
├── README.md
├── Rakefile
├── gemfiles
│   ├── rspec2.gemfile
│   └── rspec3.gemfile
├── lib
│   ├── rspec
│   │   ├── parameterized
│   │   │   └── version.rb
│   │   └── parameterized.rb
│   └── rspec-parameterized.rb
├── rspec-parameterized.gemspec
└── spec
    ├── parametarized_spec.rb
    └── spec_helper.rb

5 directories, 12 files

Please reupload to rubygems.org 🙏

rspec is failed with binding_ninja v0.2.0

When using binding_ninja v0.2.0, rspec is failed 💔
https://travis-ci.org/sue445/rspec-parameterized/builds/287121332

/home/travis/build/sue445/rspec-parameterized/spec/parametarized_spec.rb:174:in `|': uninitialized class variable @@__auto_inject_binding_options in String (NameError)
	from /home/travis/build/sue445/rspec-parameterized/spec/parametarized_spec.rb:174:in `block (4 levels) in <top (required)>'
	from /home/travis/build/sue445/rspec-parameterized/lib/rspec/parameterized.rb:104:in `instance_eval'
	from /home/travis/build/sue445/rspec-parameterized/lib/rspec/parameterized.rb:104:in `define_cases'
	from /home/travis/build/sue445/rspec-parameterized/lib/rspec/parameterized.rb:83:in `with_them'
	from /home/travis/build/sue445/rspec-parameterized/spec/parametarized_spec.rb:180:in `block (3 levels) in <top (required)>'
	from /home/travis/build/sue445/rspec-parameterized/gemfiles/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:385:in `module_exec'
	from /home/travis/build/sue445/rspec-parameterized/gemfiles/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:385:in `subclass'
	from /home/travis/build/sue445/rspec-parameterized/gemfiles/vendor/bundle/ruby/2.4.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:255:in `block in define_example_group_method'
	from /home/travis/build/sue445/rspec-parameterized/spec/parametarized_spec.rb:170:in `block (2 levels) in <top (required)>'

But when downgrade binding_ninja to v0.1.0, rspec is successful 💚

https://travis-ci.org/sue445/rspec-parameterized/builds/287123622

changes: sue445@dd6cb42

@joker1007 Can you fix this problem?

Where cannot be after with_them

I want to use something like this.

describe "plus" do
  with_them do
    it "should do additions" do
      (a + b).should == answer
    end
  end

  where(:a, :b, :answer) do
    [
      [1 , 2 , 3],
      [5 , 8 , 13],
      [0 , 0 , 0]
    ]
  end
end

table_syntax.rb Refinement#include is deprecated and will be removed in Ruby 3.2

Hi.

In using this gem, I encounterd such warning with environment variable "RUBYOPT=-W:deprecated".

/home/keisuke/work/app/vendor/bundle/ruby/3.1.0/gems/rspec-parameterized-0.5.2/lib/rspec/parameterized/table_syntax.rb:29: warning: Refinement#include is deprecated and will be removed in Ruby 3.2
/home/keisuke/work/app/vendor/bundle/ruby/3.1.0/gems/rspec-parameterized-0.5.2/lib/rspec/parameterized/table_syntax.rb:34: warning: Refinement#include is deprecated and will be removed in Ruby 3.2
/home/keisuke/work/app/vendor/bundle/ruby/3.1.0/gems/rspec-parameterized-0.5.2/lib/rspec/parameterized/table_syntax.rb:47: warning: Refinement#include is deprecated and will be removed in Ruby 3.2
/home/keisuke/work/app/vendor/bundle/ruby/3.1.0/gems/rspec-parameterized-0.5.2/lib/rspec/parameterized/table_syntax.rb:51: warning: Refinement#include is deprecated and will be removed in Ruby 3.2
/home/keisuke/work/app/vendor/bundle/ruby/3.1.0/gems/rspec-parameterized-0.5.2/lib/rspec/parameterized/table_syntax.rb:55: warning: Refinement#include is deprecated and will be removed in Ruby 3.2
/home/keisuke/work/app/vendor/bundle/ruby/3.1.0/gems/rspec-parameterized-0.5.2/lib/rspec/parameterized/table_syntax.rb:59: warning: Refinement#include is deprecated and will be removed in Ruby 3.2

Do you have any plans to remove Refinement#include from table_syntax?

[Proposal] Split to other gems

ref. #81 (comment)

To do this, we believe it is necessary to followings.

  1. Create new organization (e.g. https://github.com/rspec-parameterized ) for this and new gems.
    • Although it is not essential, it is convenient to have an organization because it is managed by multiple people
  2. Invite core maintainers (@joker1007 @tomykaira @sue445 )
  3. Create rspec-parameterized/rspec-parameterized-core and migrate this gem's feature (except TableSyntax).
  4. Create rspec-parameterized/rspec-parameterized-table_syntax
  5. Transfer tomykaira/rspec-parameterized to rspec-parameterized/rspec-parameterized
  6. Bump major version and publish gem (v1.0.0)

@joker1007 @tomykaira Can I do these tasks myself?

Allow case_names to be specified in table format

Currently, case_names lambda can be defined only in hash format. Can it be extended to other formats also, like table format? So that it is easier to generate the test description by string interpolation instead of defining it for each row.

syntax error appears after I install and connect rspec-parameterized gem

Hello.

I have connected rspec-parameterized gem according to instructions in read-me file.

But after it, I cannot run ant spec. I get the next errors:

/home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/unparser-0.4.7/lib/unparser.rb:116:in `require': /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/unparser-0.4.7/lib/unparser/emitter/send/unary.rb:13: syntax error, unexpected ':', expecting ')' (SyntaxError)
          '-@': '-',
               ^
/home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/unparser-0.4.7/lib/unparser/emitter/send/unary.rb:13: syntax error, unexpected ',', expecting keyword_end
/home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/unparser-0.4.7/lib/unparser/emitter/send/unary.rb:14: syntax error, unexpected ':', expecting keyword_end
          '+@': '+'
               ^
/home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/unparser-0.4.7/lib/unparser/emitter/send/unary.rb:15: syntax error, unexpected ')', expecting keyword_end
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/unparser-0.4.7/lib/unparser.rb:116:in `<top (required)>'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-parameterized-0.5.0/lib/rspec/parameterized.rb:3:in `require'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-parameterized-0.5.0/lib/rspec/parameterized.rb:3:in `<top (required)>'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-parameterized-0.5.0/lib/rspec-parameterized.rb:1:in `require'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-parameterized-0.5.0/lib/rspec-parameterized.rb:1:in `<top (required)>'
        from /home/ruslan/RubymineProjects/git_referrals/spec/spec_helper.rb:2:in `require'
        from /home/ruslan/RubymineProjects/git_referrals/spec/spec_helper.rb:2:in `<top (required)>'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/configuration.rb:1455:in `require'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/configuration.rb:1455:in `block in requires='
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/configuration.rb:1455:in `each'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/configuration.rb:1455:in `requires='
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/configuration_options.rb:112:in `block in process_options_into'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/configuration_options.rb:111:in `each'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/configuration_options.rb:111:in `process_options_into'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/configuration_options.rb:21:in `configure'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:99:in `setup'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:86:in `run'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:71:in `run'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/lib/rspec/core/runner.rb:45:in `invoke'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/gems/rspec-core-3.7.1/exe/rspec:4:in `<top (required)>'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/bin/rspec:23:in `load'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/bin/rspec:23:in `<main>'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:22:in `eval'
        from /home/ruslan/.rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:22:in `<main>'

ruby -v ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-linux]
rails -v '3.2.15'

Usage of variables in testcase name

I'd like to do following:

where(:a, :b, :result) do
      [
        [1 , 2 , 3],
        [5 , 8 , 13],
        [0 , 0 , 0]
      ]
end

with_them do
      it "#{a} + #{b} = #{result}" do
        expect(a + b).to eq result
      end
end

But it crushes with error:

`method_missing': `a` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). (RSpec::Core::ExampleGroup::WrongScopeError)

in rspec-core-3.4.2/lib/rspec/core/example_group.rb:667:

Is proc_to_ast actually required?

The proc_to_ast gem hasn't seen any activity in the last 7 years. So I was wondering if it is still required. And while lib/rspec/parameterized.rb contains a require statement, it actually looks like there is no actually use anymore since 7c4a188. Can you confirm this finding? Can the references to proc_to_ast be removed?

Please support rspec-rails 2.12

Hello.

I use rspec-parameterized and want to update rspec-rails (2.11.xx -> 2.12.0)

Gemfile

group :test do
  gem "rspec-rails", "~> 2.12.0"
  gem "rspec-parameterized", "~> 0.0.6"
end
$ bundle install

Bundler could not find compatible versions for gem "rspec-mocks":
  In Gemfile:
    rspec-parameterized (~> 0.0.6) ruby depends on
      rspec-mocks (~> 2.11.0) ruby

    rspec-rails (~> 2.12.0) ruby depends on
      rspec-mocks (2.12.0)

sometimes freeze during bundle install. (print no error logs! )

Please support rspec-rails 2.12

TableSyntax locks up test suite with Ruby 3

While looking to migrate a GitLab component to Ruby 3, we encountered a problem that sends the Ruby VM into an endless loop when loading and executing tests that use TableSyntax. The problem only appears to materialize under specific conditions, but I was able to construct a minimum executable test case here: https://gitlab.com/mkaeppler/table-syntax-ruby3/-/blob/main/spec/context_spec.rb

We tracked down the problem to the following:

  • TableSyntax relies on binding_ninja, which uses a C-extension to manipulate Ruby's method table: https://github.com/joker1007/binding_ninja/blob/master/ext/binding_ninja/binding_ninja.c#L55-L69, and prepends itself to the caller
  • When a test is executed which:
    • loads code that also extends the Object class (such as active-support/core-ext/object)
    • uses the pipe operator | from TableSyntax
    • uses expect with block syntax (I have found no explanation for this yet -- it could be a red herring -- but if I remove this expectation from the test the problem does not materialize)

then Ruby 3 will spin forever in search_method looking for the | operator while traversing the superclass chain: https://github.com/ruby/ruby/blob/v3_0_1/vm_method.c#L969-L976

I'm not totally sure yet how all of this comes together for this issue to materialize, but it is reproducible using the test case I linked (just bundle exec rspec).

The binding_ninja extension looks very sketchy. If there is a way to replace this with some equivalent pure Ruby solution like binding_of_caller, I think that would address this problem.

TableSyntax is failed with Ruby 3.0.2

Hello.

#69
It looks like it's closed, but it's not resolved.

I validated TableSyntax in Ruby 3.0.2 but it fails.
I verified it in the following environment.

$ ruby -v
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [aarch64-linux]
require "rails_helper"

RSpec.describe "Sample" do
  describe "plus" do
    using RSpec::Parameterized::TableSyntax

    where(:a, :b, :answer) do
      1 | 2 | 3
      5 | 8 | 13
      0 | 0 | 0
    end

    with_them do
      it "should do additions" do
        expect(a + b).to eq answer
      end
    end
  end
end

$ bundle exec rspec

An error occurred while loading ./spec/sample_spec.rb.
Failure/Error:
  with_them do
    it "should do additions" do
      expect(a + b).to eq answer
    end
  end

NoMethodError:
  undefined method `to_params' for 0:Integer
  Did you mean?  to_param
# /usr/local/bundle/gems/rspec-parameterized-0.5.0/lib/rspec/parameterized.rb:107:in `define_cases'
# /usr/local/bundle/gems/rspec-parameterized-0.5.0/lib/rspec/parameterized.rb:84:in `with_them'
# ./spec/sample_spec.rb:13:in `block (2 levels) in <top (required)>'
# ./spec/sample_spec.rb:4:in `block in <top (required)>'
# ./spec/sample_spec.rb:3:in `<top (required)>'
No examples found.


Finished in 0.00003 seconds (files took 2.74 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

binding_ninja isn't JRuby compatible

Hey y'all,

The new dependency on binding_ninja makes this library fail when compiling under JRuby. Here's the output:

current directory: /home/.rvm/gems/jruby-9.1.16.0/gems/binding_ninja-0.2.2/ext/binding_ninja
/home/.rvm/rubies/jruby-9.1.16.0/bin/jruby -r ./siteconf20181205-19971-1od2pas.rb extconf.rb
creating Makefile

current directory: /home/.rvm/gems/jruby-9.1.16.0/gems/binding_ninja-0.2.2/ext/binding_ninja
make "DESTDIR=" clean

current directory: /home/.rvm/gems/jruby-9.1.16.0/gems/binding_ninja-0.2.2/ext/binding_ninja
make "DESTDIR="
make: *** No rule to make target `/home/.rvm/rubies/jruby-9.1.16.0/lib/ruby/include/ruby/ruby.h', needed by `binding_ninja.o'.  Stop.

make failed, exit code 2

Looks like this library makes MRI-specific native extensions and can't be used under JRuby.

Add support to skip tests

rspec allows us to use xit to temporarily skip tests. It would be great if rspec-parameterized can also support similar functionality.

This would be useful especially when following test driven development with a list of scenarios to be covered. While working on specific scenarios, we could temporarily skip the others and reduce the noise from the test output.

Allow to customize description

It be useful to be able to change the description of the test.
a: 1, b: 2, result: 3 is less useful than

plus
  1 + 2
   should equal 3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.