Skip to content
Snippets Groups Projects
Commit 18d51613 authored by Cocoa's avatar Cocoa :speech_balloon:
Browse files

Import Upstream version 0.4.2

parents
No related branches found
No related tags found
No related merge requests found
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
gemfiles/*.lock
gemfiles/.bundle
.rspec 0 → 100644
--color
--format progress
language: ruby
cache: bundler
sudo: false
rvm:
- 2.4
- 2.5
- 2.6
- ruby-head
- jruby-9.2.7.0
gemfile:
- gemfiles/rspec3.4.gemfile
- gemfiles/rspec-head.gemfile
before_install:
- travis_retry gem update --system || travis_retry gem update --system 2.7.8
- travis_retry gem install bundler --no-document || travis_retry gem install bundler --no-document -v 1.17.3
matrix:
allow_failures:
- rvm: ruby-head
# Change Log
## Unreleased
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.4.1...master)
## [v0.4.1](https://github.com/tomykaira/rspec-parameterized/tree/v0.4.1) (2018-12-06)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.4.0...v0.4.1)
- Replace binding_of_caller with binding_ninja
- https://github.com/tomykaira/rspec-parameterized/pull/44
- https://github.com/tomykaira/rspec-parameterized/issues/48
## [v0.4.0](https://github.com/tomykaira/rspec-parameterized/tree/v0.4.0) (2017-06-13)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.3.2...v0.4.0)
**Merged pull requests:**
- Verbose test syntax [\#42](https://github.com/tomykaira/rspec-parameterized/pull/42) ([aliaksandr-martsinovich](https://github.com/aliaksandr-martsinovich))
- Customizable description [\#41](https://github.com/tomykaira/rspec-parameterized/pull/41) ([aliaksandr-martsinovich](https://github.com/aliaksandr-martsinovich))
## [v0.3.2](https://github.com/tomykaira/rspec-parameterized/tree/v0.3.2) (2016-12-26)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.3.1...v0.3.2)
**Merged pull requests:**
- Fix deprecation warning on ruby 2.4.0 [\#40](https://github.com/tomykaira/rspec-parameterized/pull/40) ([sue445](https://github.com/sue445))
- Add release note for v0.3.1 [\#39](https://github.com/tomykaira/rspec-parameterized/pull/39) ([sue445](https://github.com/sue445))
- Add rubygems version badge [\#38](https://github.com/tomykaira/rspec-parameterized/pull/38) ([sue445](https://github.com/sue445))
## [v0.3.1](https://github.com/tomykaira/rspec-parameterized/tree/v0.3.1) (2016-08-17)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.3.0...v0.3.1)
**Closed issues:**
- Support "||" syntax [\#34](https://github.com/tomykaira/rspec-parameterized/issues/34)
**Merged pull requests:**
- Set the caller of `.with\_them` to metadata of ExampleGroup [\#37](https://github.com/tomykaira/rspec-parameterized/pull/37) ([nalabjp](https://github.com/nalabjp))
- Add homepage url in gemspec [\#33](https://github.com/tomykaira/rspec-parameterized/pull/33) ([sue445](https://github.com/sue445))
- Tweak travis setting [\#32](https://github.com/tomykaira/rspec-parameterized/pull/32) ([sue445](https://github.com/sue445))
- Generate CHANGELOG [\#31](https://github.com/tomykaira/rspec-parameterized/pull/31) ([sue445](https://github.com/sue445))
## [v0.3.0](https://github.com/tomykaira/rspec-parameterized/tree/v0.3.0) (2016-02-22)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.2.0...v0.3.0)
**Closed issues:**
- Usage of variables in testcase name [\#27](https://github.com/tomykaira/rspec-parameterized/issues/27)
- Remove `where\_table` method, when release next version [\#24](https://github.com/tomykaira/rspec-parameterized/issues/24)
- Release v0.2.0 [\#23](https://github.com/tomykaira/rspec-parameterized/issues/23)
- UTF-8 is converted to ASCII8BIT through sourcify [\#5](https://github.com/tomykaira/rspec-parameterized/issues/5)
- where\_table does not work with proc [\#4](https://github.com/tomykaira/rspec-parameterized/issues/4)
**Merged pull requests:**
- Enable to browse parameter values in with\_them block [\#30](https://github.com/tomykaira/rspec-parameterized/pull/30) ([joker1007](https://github.com/joker1007))
- Remove unnecessary methods [\#29](https://github.com/tomykaira/rspec-parameterized/pull/29) ([joker1007](https://github.com/joker1007))
- Remove `where\_table` [\#28](https://github.com/tomykaira/rspec-parameterized/pull/28) ([joker1007](https://github.com/joker1007))
## [v0.2.0](https://github.com/tomykaira/rspec-parameterized/tree/v0.2.0) (2015-09-17)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.1.3...v0.2.0)
**Merged pull requests:**
- allow hash arguments [\#22](https://github.com/tomykaira/rspec-parameterized/pull/22) ([takkanm](https://github.com/takkanm))
- Update README to use expect syntax. [\#21](https://github.com/tomykaira/rspec-parameterized/pull/21) ([JunichiIto](https://github.com/JunichiIto))
## [v0.1.3](https://github.com/tomykaira/rspec-parameterized/tree/v0.1.3) (2015-02-13)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.1.2...v0.1.3)
**Closed issues:**
- rspec-parameterized-0.1.1.gem is broken [\#18](https://github.com/tomykaira/rspec-parameterized/issues/18)
**Merged pull requests:**
- Accept nil/boolean for 1st column [\#20](https://github.com/tomykaira/rspec-parameterized/pull/20) ([ryonext](https://github.com/ryonext))
- Update travis config [\#19](https://github.com/tomykaira/rspec-parameterized/pull/19) ([joker1007](https://github.com/joker1007))
## [v0.1.2](https://github.com/tomykaira/rspec-parameterized/tree/v0.1.2) (2014-09-04)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.1.1...v0.1.2)
**Merged pull requests:**
- Remove trailing '\>' character from TableSyntax [\#17](https://github.com/tomykaira/rspec-parameterized/pull/17) ([joker1007](https://github.com/joker1007))
- \[Suggest\] Where table syntax by Refinement [\#16](https://github.com/tomykaira/rspec-parameterized/pull/16) ([joker1007](https://github.com/joker1007))
## [v0.1.1](https://github.com/tomykaira/rspec-parameterized/tree/v0.1.1) (2014-05-16)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.1.0...v0.1.1)
**Merged pull requests:**
- Support RSpec3 [\#15](https://github.com/tomykaira/rspec-parameterized/pull/15) ([joker1007](https://github.com/joker1007))
## [v0.1.0](https://github.com/tomykaira/rspec-parameterized/tree/v0.1.0) (2014-04-06)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.0.9...v0.1.0)
**Merged pull requests:**
- Fix AST manipulation, for remove sourcify [\#14](https://github.com/tomykaira/rspec-parameterized/pull/14) ([joker1007](https://github.com/joker1007))
- Clear "already initialized constant Regexp::ENC\_XXX" warnings. [\#13](https://github.com/tomykaira/rspec-parameterized/pull/13) ([saturday06](https://github.com/saturday06))
## [v0.0.9](https://github.com/tomykaira/rspec-parameterized/tree/v0.0.9) (2013-07-29)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.0.8...v0.0.9)
**Merged pull requests:**
- Update rspec to v2.14 [\#12](https://github.com/tomykaira/rspec-parameterized/pull/12) ([joker1007](https://github.com/joker1007))
## [v0.0.8](https://github.com/tomykaira/rspec-parameterized/tree/v0.0.8) (2013-05-16)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.0.7...v0.0.8)
**Closed issues:**
- Please support rspec-rails 2.12 [\#9](https://github.com/tomykaira/rspec-parameterized/issues/9)
**Merged pull requests:**
- rspec: 2.12.0 -\> 2.13.0 [\#10](https://github.com/tomykaira/rspec-parameterized/pull/10) ([sue445](https://github.com/sue445))
## [v0.0.7](https://github.com/tomykaira/rspec-parameterized/tree/v0.0.7) (2012-11-13)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.0.6...v0.0.7)
## [v0.0.6](https://github.com/tomykaira/rspec-parameterized/tree/v0.0.6) (2012-09-24)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.0.5...v0.0.6)
**Merged pull requests:**
- Fix where scope, in order to use let method and matcher in where blocks [\#7](https://github.com/tomykaira/rspec-parameterized/pull/7) ([joker1007](https://github.com/joker1007))
## [v0.0.5](https://github.com/tomykaira/rspec-parameterized/tree/v0.0.5) (2012-08-22)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.0.4...v0.0.5)
## [v0.0.4](https://github.com/tomykaira/rspec-parameterized/tree/v0.0.4) (2012-08-20)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.0.3...v0.0.4)
**Merged pull requests:**
- Update to rspec 2.11.0 [\#6](https://github.com/tomykaira/rspec-parameterized/pull/6) ([mfolnovic](https://github.com/mfolnovic))
## [v0.0.3](https://github.com/tomykaira/rspec-parameterized/tree/v0.0.3) (2012-05-24)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.0.2...v0.0.3)
**Closed issues:**
- Where cannot be after with\_them [\#1](https://github.com/tomykaira/rspec-parameterized/issues/1)
**Merged pull requests:**
- fix params.inspect for document format. use Hash and Proc soucify. [\#3](https://github.com/tomykaira/rspec-parameterized/pull/3) ([joker1007](https://github.com/joker1007))
- fix where\_table method comment. method name was "where". [\#2](https://github.com/tomykaira/rspec-parameterized/pull/2) ([joker1007](https://github.com/joker1007))
## [v0.0.2](https://github.com/tomykaira/rspec-parameterized/tree/v0.0.2) (2012-05-22)
[Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.0.1...v0.0.2)
## [v0.0.1](https://github.com/tomykaira/rspec-parameterized/tree/v0.0.1) (2012-05-20)
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
\ No newline at end of file
source 'https://rubygems.org'
# Specify your gem's dependencies in rspec-parameterized.gemspec
gemspec
LICENSE 0 → 100644
Copyright (c) 2012 tomykaira
MIT License
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.
\ No newline at end of file
README.md 0 → 100644
# RSpec::Parameterized [![Gem Version](https://badge.fury.io/rb/rspec-parameterized.svg)](https://badge.fury.io/rb/rspec-parameterized) [![Build Status](https://travis-ci.org/tomykaira/rspec-parameterized.svg)](https://travis-ci.org/tomykaira/rspec-parameterized)
Support simple parameterized test syntax in rspec.
```ruby
# 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 test case name
# 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:
# when hash arguments
# 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
```
I was inspired by [udzura's mock](https://gist.github.com/1881139).
## Installation
Add this line to your application's Gemfile:
gem 'rspec-parameterized'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rspec-parameterized
## 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
Rakefile 0 → 100644
#!/usr/bin/env rake
require "bundler/gem_tasks"
task :default => [:spec]
begin
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rspec_opts = ['-c -f d']
end
end
namespace :spec do
%w(rspec2 rspec3.3 rspec3.4).each do |gemfile|
desc "Run Tests by #{gemfile}.gemfile"
task gemfile do
Bundler.with_clean_env do
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle install --path .bundle"
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake -t spec"
end
end
end
desc "Run All Tests"
task :all do
%w(rspec2 rspec3.3 rspec3.4).each do |gemfile|
Rake::Task["spec:#{gemfile}"].invoke
end
end
end
source 'https://rubygems.org'
gem "rspec"
gemspec path: "../"
source 'https://rubygems.org'
gem "rspec", "~> 3.4.0"
gemspec path: "../"
require 'rspec/parameterized'
require "rspec/parameterized/version"
require 'parser'
require 'unparser'
require 'proc_to_ast'
module RSpec
module Parameterized
autoload :TableSyntax, 'rspec/parameterized/table_syntax'
module ExampleGroupMethods
# capsulize parameter attributes
class Parameter
attr_reader :arg_names, :block
def initialize(arg_names, &block)
@arg_names, @block = arg_names, block
end
end
# Set parameters to be bound in specs under this example group.
#
# ## Example1
#
# where(:a, :b, :answer) do
# [
# [1 , 2 , 3],
# [5 , 8 , 13],
# [0 , 0 , 0]
# ]
# end
#
# ## Example2
# using RSpec::Parameterized::TableSyntax
# where(:a, :b, :answer) do
# 1 | 2 | 3 >
# 5 | 8 | 13 >
# 0 | 0 | 0
# end
#
def where(*args, &b)
if args.size == 1 && args[0].instance_of?(Hash)
naming_func = args.first.delete(:case_names)
params = args[0]
first, *rest = params.values
arg_names = params.keys
arg_values = first.product(*rest)
if naming_func && naming_func.respond_to?(:call)
arg_names << :case_name
arg_values.map! { |row| row << naming_func.call(*row) }
end
set_parameters(arg_names) {
arg_values
}
elsif args.size == 0
set_verbose_parameters(&b)
else
set_parameters(args, &b)
end
end
# Use parameters to execute the block.
# The given block is converted into +describe+s for each parameter set.
#
# ## Example
# with_them do
# it "should do additions" do
# (a + b).should == answer
# end
# end
#
def with_them(*args, &b)
opts = args.last.is_a?(Hash) ? args.pop : {}
opts[:caller] = caller unless opts[:caller]
args.push(opts)
if @parameter.nil?
@parameterized_pending_cases ||= []
@parameterized_pending_cases << [args, b]
else
define_cases(@parameter, *args, &b)
end
end
private
def set_parameters(arg_names, &b)
@parameter = Parameter.new(arg_names, &b)
if @parameterized_pending_cases
@parameterized_pending_cases.each { |e|
define_cases(@parameter, *e[0], &e[1])
}
end
end
def define_cases(parameter, *args, &block)
instance = new # for evaluate let methods.
if defined?(self.superclass::LetDefinitions)
instance.extend self.superclass::LetDefinitions
end
extracted = instance.instance_eval(&parameter.block)
param_sets = extracted.is_a?(Array) ? extracted : extracted.to_params
# for only one parameters
param_sets = param_sets.map { |x| Array[x] } if !param_sets[0].is_a?(Array)
param_sets.each do |param_set|
pairs = [parameter.arg_names, param_set].transpose.to_h
pretty_params = pairs.has_key?(:case_name) ? pairs[:case_name] : pairs.map {|name, val| "#{name}: #{params_inspect(val)}"}.join(", ")
describe(pretty_params, *args) do
pairs.each do |name, val|
let(name) { val }
end
singleton_class.module_eval do
if respond_to?(:params)
warn "ExampleGroup.params method is overrided."
end
define_method(:params) do
pairs
end
if respond_to?(:all_params)
warn "ExampleGroup.all_params method is overrided."
end
define_method(:all_params) do
param_sets
end
end
module_eval(&block)
end
end
end
def params_inspect(obj)
begin
obj.is_a?(Proc) ? obj.to_raw_source : obj.inspect
rescue Parser::SyntaxError
return obj.inspect
end
end
def set_verbose_parameters(&block)
arguments_hash = yield
arg_names = arguments_hash.values.reduce(Set.new) { |memo, pairs| memo | pairs.keys }.to_a
arg_values = []
arguments_hash.each do |name, values_hash|
row = [name]
arg_names.each do |argument_name|
row << values_hash[argument_name]
end
arg_values << row
end
arg_names.unshift(:case_name)
set_parameters(arg_names) {
arg_values
}
end
end
end
module Core
class ExampleGroup
extend ::RSpec::Parameterized::ExampleGroupMethods
end
end
end
module RSpec
module Parameterized
class Table
attr_reader :last_row
def initialize
@rows = []
@last_row = nil
end
def add_row(row)
unless @rows.find {|r| r.object_id == row.object_id}
@rows << row
@last_row = row
end
self
end
def add_param_to_last_row(param)
last_row.add_param(param)
self
end
alias :| :add_param_to_last_row
def to_a
@rows.map(&:to_a)
end
alias :to_params :to_a
class Row
def initialize(param)
@params = [param]
end
def add_param(param)
@params << param
end
def to_a
@params
end
def to_params
[@params]
end
end
end
end
end
require 'rspec/parameterized/table'
require 'binding_ninja'
module RSpec
module Parameterized
module TableSyntaxImplement
extend BindingNinja
def |(where_binding, other)
caller_instance = where_binding.receiver # get caller instance (ExampleGroup)
if caller_instance.instance_variable_defined?(:@__parameter_table)
table = caller_instance.instance_variable_get(:@__parameter_table)
else
table = RSpec::Parameterized::Table.new
caller_instance.instance_variable_set(:@__parameter_table, table)
end
row = Table::Row.new(self)
table.add_row(row)
row.add_param(other)
table
end
auto_inject_binding :|
end
module TableSyntax
refine Object do
include TableSyntaxImplement
end
if Gem::Version.create(RUBY_VERSION) >= Gem::Version.create("2.4.0")
refine Integer do
include TableSyntaxImplement
end
else
refine Fixnum do
include TableSyntaxImplement
end
refine Bignum do
include TableSyntaxImplement
end
end
refine Array do
include TableSyntaxImplement
end
refine NilClass do
include TableSyntaxImplement
end
refine TrueClass do
include TableSyntaxImplement
end
refine FalseClass do
include TableSyntaxImplement
end
end
end
end
module RSpec
module Parameterized
VERSION = "0.4.2"
end
end
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/rspec/parameterized/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["tomykaira"]
gem.email = ["tomykaira@gmail.com"]
gem.description = %q{RSpec::Parameterized supports simple parameterized test syntax in rspec.}
gem.summary = %q{RSpec::Parameterized supports simple parameterized test syntax in rspec.
I was inspired by [udzura's mock](https://gist.github.com/1881139).}
gem.homepage = "https://github.com/tomykaira/rspec-parameterized"
gem.add_dependency('rspec', '>= 2.13', '< 4')
gem.add_dependency('parser')
gem.add_dependency('unparser')
gem.add_dependency('proc_to_ast')
gem.add_dependency('binding_ninja', '>= 0.2.3')
gem.add_development_dependency('rake', '>= 12.0.0')
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "rspec-parameterized"
gem.require_paths = ["lib"]
gem.version = RSpec::Parameterized::VERSION
end
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
# RSpec::Parameterized
# Sample
# plus
# [1, 2, 3]
# should do additions
# [5, 8, 13]
# should do additions
# [0, 0, 0]
# should do additions
describe RSpec::Parameterized do
describe "where and with_them" 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
it "#{params[:a]} + #{params[:b]} == #{params[:answer]}" do
expect(a + b).to eq answer
end
end
with_them pending: "PENDING" do
it "should do additions" do
expect(a + b).to == answer
end
end
end
describe "lambda parameter" do
where(:a, :b, :answer) do
[
[1 , 2 , -> {should == 3}],
[5 , 8 , -> {should == 13}],
[0 , 0 , -> {should == 0}]
]
end
with_them do
subject {a + b}
it "should do additions" do
self.instance_exec(&answer)
end
end
end
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
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
it "should have custom name" do |example|
expect(example.metadata[:example_group][:description]).to eq case_name
end
end
context "lambda parameter" do
where do
{
"integers" => {
a: 1,
b: 2,
answer: -> {expect(subject).to eq 3},
},
"strings" => {
a: "hello ",
b: "world",
answer: -> {expect(subject).to include "lo wo"},
},
"arrays" => {
a: [1, 2, 3],
b: [4, 5, 6],
answer: -> {expect(subject.size).to eq 6}
}
}
end
with_them do
subject {a + b}
it "should do additions" do
self.instance_exec(&answer)
end
it "should have custom name" do |example|
expect(example.metadata[:example_group][:description]).to eq(case_name)
end
end
end
end
describe "Custom test case name" do
context "when regular arguments" 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
it "should have custom test name" do |example|
expect(example.metadata[:example_group][:description]).to eq case_name
end
end
end
context "when hash arguments" 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
it "should have custom names" do |example|
expect(example.metadata[:example_group][:description]).to include "+"
end
end
end
if RUBY_VERSION >= "2,1"
context "when arguments are separated with pipe (using TableSyntax)" do
using RSpec::Parameterized::TableSyntax
where(:case_name, :a, :b, :answer) do
"integers" | 1 | 2 | 3
"strings" | "hello " | "world" | "hello world"
"arrays" | [1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6]
"giant numbers" | 100000000000000000000 | 100000000000000000000 | 200000000000000000000
end
with_them do
it "a plus b is answer" do
expect(a + b).to eq answer
end
it "should have custom test name" do |example|
expect(example.metadata[:example_group][:description]).to eq case_name
end
end
end
end
end
if RUBY_VERSION >= "2.1"
describe "table separated with pipe (using TableSyntax)" do
using RSpec::Parameterized::TableSyntax
where(:a, :b, :answer) do
1 | 2 | 3
"hello " | "world" | "hello world"
[1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6]
100000000000000000000 | 100000000000000000000 | 200000000000000000000
end
with_them do
it "a plus b is answer" do
expect(a + b).to eq answer
end
end
end
describe "table separated with pipe and lambda parameter (using TableSyntax)" do
using RSpec::Parameterized::TableSyntax
where(:a, :b, :matcher) do
1 | 2 | -> { eq(3) }
"hello " | "world" | -> { eq("hello world") }
[1, 2, 3] | [4, 5, 6] | -> { be_a(Array) }
100000000000000000000 | 100000000000000000000 | -> { eq(200000000000000000000) }
end
with_them do
it "a plus b is answer" do
expect(a + b).to instance_exec(&matcher)
end
end
end
end
context "when the where block is after with_them" do
with_them do
it "should do additions" do
expect(a + b).to eq answer
end
end
with_them do
subject { a }
it { should be_a Numeric }
end
where(:a, :b, :answer) do
[
[1 , 2 , 3],
[5 , 8 , 13],
[0 , 0 , 0]
]
end
end
context "when the where block is between with_thems" do
with_them do
it "should do additions" do
expect(a + b).to eq answer
end
end
where(:a, :b, :answer) do
[
[1 , 2 , 3],
[5 , 8 , 13],
[0 , 0 , 0]
]
end
with_them do
subject { a }
it { should be_a Numeric }
end
end
context "when the where has only one parameter to be set" do
where(:x) do
[1, 2, 3]
end
with_them do
it 'can take an array of elements' do
expect(x).to eq x
end
end
end
if RUBY_VERSION >= "2.1"
context "when the table has only a row (using TableSyntax)" do
using RSpec::Parameterized::TableSyntax
where(:a, :b, :answer) do
1 | 2 | 3
end
with_them do
it "a plus b is answer" do
expect(a + b).to eq answer
end
end
end
context "when 1st column is nil or true or false" do
using RSpec::Parameterized::TableSyntax
where(:a, :result) do
nil | nil
false | false
true | true
end
with_them do
it "a is result" do
expect(a).to be result
end
end
end
end
context "when the where has let variables, defined by parent example group" do
describe "parent (define let)" do
let(:five) { 5 }
let(:eight) { 8 }
describe "child 1" do
where(:a, :b, :answer) do
[
[1 , 2 , 3],
[five , eight , 13],
]
end
with_them do
it "a plus b is answer" do
expect(a + b).to eq answer
end
end
end
if RUBY_VERSION >= "2.1"
describe "child 3 (Using TableSyntax)" do
using RSpec::Parameterized::TableSyntax
where(:a, :b, :answer) do
1 | 2 | 3
five | eight | 13
end
with_them do
it "a plus b is answer" do
expect(a + b).to eq answer
end
end
end
end
let(:eq_matcher) { eq(13) }
describe "child 3 (use matcher)" do
where(:a, :b, :matcher) do
[
[1 , 2 , eq(3) ],
[five , eight , eq_matcher],
]
end
with_them do
it "a plus b is answer" do
expect(a + b).to matcher
end
end
end
end
end
end
require 'rspec-parameterized'
RSpec.configure do |config|
config.run_all_when_everything_filtered = true
config.filter_run :current
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment