Skip to content
Snippets Groups Projects
Commit 2ee1b0f8 authored by Lucas Kanashiro's avatar Lucas Kanashiro
Browse files

Import Upstream version 1.0.0

parents
No related branches found
No related tags found
No related merge requests found
Showing with 565 additions and 0 deletions
.rspec 0 → 100644
--format documentation
--color
--require spec_helper
## [Unreleased]
## [1.0.0] - 2022-12-31
- https://github.com/tomykaira/rspec-parameterized/blob/master/CHANGELOG.md#v100-2022-12-31
- Drop support Ruby < 2.6 (explicitly)
# frozen_string_literal: true
source "https://rubygems.org"
# Specify your gem's dependencies in rspec-parameterized-core.gemspec
gemspec
The MIT License (MIT)
Copyright (c) 2022 sue445
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.
README.md 0 → 100644
# Rspec::Parameterized::Core
`rspec-parameterized-core` provides parameterized test syntax in [rspec](https://rspec.info/).
[![Gem Version](https://badge.fury.io/rb/rspec-parameterized-core.svg)](https://badge.fury.io/rb/rspec-parameterized-core)
[![RSpec](https://github.com/rspec-parameterized/rspec-parameterized-core/actions/workflows/rspec.yml/badge.svg)](https://github.com/rspec-parameterized/rspec-parameterized-core/actions/workflows/rspec.yml)
## Installation
```ruby
# Install all components
group :test do
gem "rspec-parameterized", ">= 1.0.0"
end
# Install only rspec-parameterized-core
group :test do
gem "rspec-parameterized-core", ">= 1.0.0"
end
```
## Usage
### Nested Array Style
```ruby
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
```ruby
# 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
```
### Verbose Syntax
```ruby
# 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
```
### Custom names
```ruby
# 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
```
### lazy and ref types
```ruby
# 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](https://gist.github.com/1881139).
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/rspec-parameterized/rspec-parameterized-core.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
task default: %i[spec]
require 'rspec/parameterized/core'
require "rspec/parameterized/core/version"
require 'parser'
require 'unparser'
require 'proc_to_ast'
require 'rspec/parameterized/core/helper_methods'
require 'rspec/parameterized/core/example_helper_methods'
module RSpec
module Parameterized
module Core
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)
@parameter ||= nil
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)
@parameterized_pending_cases ||= []
@parameterized_pending_cases.each { |e|
define_cases(@parameter, *e[0], &e[1])
}
end
def define_cases(parameter, *args, &block)
instance = new # for evaluate let methods.
instance.extend HelperMethods
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
include ExampleHelperMethods
pairs.each do |name, val|
let(name) { recursive_apply(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
end
module Core
class ExampleGroup
extend ::RSpec::Parameterized::Core::ExampleGroupMethods
end
end
end
module RSpec
module Parameterized
module Core
class Arg
def apply(obj) ; end
end
end
end
end
module RSpec
module Parameterized
module Core
module ExampleHelperMethods
def recursive_apply(val)
return val.apply(self) if HelperMethods.applicable?(val)
if val.is_a?(Array)
return val.map { |child_val| recursive_apply(child_val) }
end
if val.is_a?(Hash)
return val.map { |key, value| [recursive_apply(key), recursive_apply(value)] }.to_h
end
val
end
end
end
end
end
require 'rspec/parameterized/core/ref_arg'
require 'rspec/parameterized/core/lazy_arg'
module RSpec
module Parameterized
module Core
module HelperMethods
def ref(symbol)
RefArg.new(symbol)
end
def lazy(&block)
LazyArg.new(&block)
end
def self.applicable?(arg)
arg.is_a? Arg
end
end
end
end
end
module RSpec
module Parameterized
module Core
class LazyArg < Arg
def initialize(&block)
@block = block
end
def apply(obj)
obj.instance_eval(&@block)
end
def inspect
"#{@block.to_raw_source}"
rescue Parser::SyntaxError
super.inspect
end
end
end
end
end
require 'rspec/parameterized/core/arg'
module RSpec
module Parameterized
module Core
class RefArg < Arg
def initialize(symbol)
@symbol = symbol
end
def apply(obj)
obj.send(@symbol)
end
def inspect
"#{@symbol}"
end
end
end
end
end
# frozen_string_literal: true
module Rspec
module Parameterized
module Core
VERSION = "1.0.0"
end
end
end
# frozen_string_literal: true
require_relative "lib/rspec/parameterized/core/version"
Gem::Specification.new do |spec|
spec.name = "rspec-parameterized-core"
spec.version = Rspec::Parameterized::Core::VERSION
spec.authors = ["sue445", "tomykaira", "joker1007"]
spec.email = ["sue445@sue445.net", "tomykaira@gmail.com"]
spec.description = %q{RSpec::Parameterized supports simple parameterized test syntax in rspec.}
spec.summary = %q{RSpec::Parameterized supports simple parameterized test syntax in rspec.
I was inspired by [udzura's mock](https://gist.github.com/1881139).}
spec.homepage = "https://github.com/rspec-parameterized/rspec-parameterized-core"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
spec.metadata["rubygems_mfa_required"] = "true"
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_dependency "parser"
spec.add_dependency "proc_to_ast"
spec.add_dependency "rspec", ">= 2.13", "< 4"
spec.add_dependency "unparser"
spec.add_development_dependency "rake", ">= 12.0.0"
# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
end
module Rspec
module Parameterized
module Core
VERSION: String
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
end
end
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