0.0
Low commit activity in last 3 years
No release in over a year
Simple Parameterized Test for RSpec
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
>= 0
>= 0
~> 13.0

Runtime

~> 3.0
 Project Readme

RSpec::Paramz Gem Version Build Status

Simple Parameterized Test for RSpec.

Inspired by tomykaira/rspec-parameterized.

Requirements

  • Ruby 2.6 or later
    • This gem uses RubyVM::AbstractSyntaxTree

Installation

Add this line to your application's Gemfile:

gem 'rspec-paramz'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rspec-paramz

Usage

See paramz_spec.rb for example.

RSpec.describe RSpec::Paramz do
  describe '#paramz' do
    context 'format 1' do
      paramz(
        [:a, :b, :answer],
        1,   2,  3,
        3,   8,  11,
        5,   -8, -3,
      ) do
        it 'should do additions' do
          expect(a + b).to eq answer
        end
      end

      #=>
      # [a = 1 | b = 2 | answer = 3]
      #   should do additions
      # [a = 3 | b = 8 | answer = 11]
      #   should do additions
      # [a = 5 | b = -8 | answer = -3]
      #   should do additions
    end

    context 'format 2' do
      paramz(
        [:a, :b, subject: -> { a + b }],
        1,   2,  3,
        3,   8,  11,
        5,   -8, -3,
      )

      #=>
      # [a = 1 | b = 2 | subject { a + b } = 3]
      #   should == 3
      # [a = 3 | b = 8 | subject { a + b } = 11]
      #   should == 11
      # [a = 5 | b = -8 | subject { a + b } = -3]
      #   should == -3
    end

    context 'format 2-a' do
      paramz(
        [:a, :b, subject: -> { a + b }],
        1,   2,  3,
        3,   8,  11,
        5,   -8, -3,
      ) do
        it 'should do additions' do
          expect(subject).to eq(a + b)
        end
      end

      #=>
      # [a = 1 | b = 2 | subject { a + b } = 3]
      #   should do additions
      # [a = 3 | b = 8 | subject { a + b } = 11]
      #   should do additions
      # [a = 5 | b = -8 | subject { a + b } = -3]
      #   should do additions
    end

    context 'format 3' do
      paramz(
        [:a, :b, subject: { answer: -> { a + b } }],
        1,   2,  3,
        3,   8,  11,
        5,   -8, -3,
      )

      #=>
      # [a = 1 | b = 2 | subject(:answer) { a + b } = 3]
      #   should == 3
      # [a = 3 | b = 8 | subject(:answer) { a + b } = 11]
      #   should == 11
      # [a = 5 | b = -8 | subject(:answer) { a + b } = -3]
      #   should == -3
    end

    context 'format 3-b' do
      paramz(
        [:a, :b, subject: { answer: -> { a + b } }],
        1,   2,  3,
        3,   8,  11,
        5,   -8, -3,
      ) do
        it 'should do additions' do
          expect(subject).to eq(a + b)
        end

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

      #=>
      # [a = 1 | b = 2 | subject(:answer) { a + b } = 3]
      #   should do additions
      #   should do additions
      # [a = 3 | b = 8 | subject(:answer) { a + b } = 11]
      #   should do additions
      #   should do additions
      # [a = 5 | b = -8 | subject(:answer) { a + b } = -3]
      #   should do additions
      #   should do additions
    end

    context 'format 4' do
      subject { a + b }

      paramz(
        [:a, :b, :subject],
        1,   2,   3,
        0,   0,   0,
        -1,  -2,  -3,
      )

      #=>
      # [a = 1 | b = 2 | subject = 3]
      #   should == 3
      # [a = 0 | b = 0 | subject = 0]
      #   should == 0
      # [a = -1 | b = -2 | subject = -3]
      #   should == -3
    end

    context 'rspec-let' do
      let(:one)   { 1 }
      let(:two)   { one * 2 }
      let(:three) { one * 3 }

      context 'default' do
        paramz(
          [:a,       :b,          :answer],
          -> { one }, 0,          1,
          -> { one }, -> { one }, -> { 2 },
          -> { one }, -> { two }, -> { three },
        ) do
          it 'should do additions' do
            expect(a + b).to eq answer
          end
        end

        #=>
        # [a = { one } | b = 0 | answer = 1]
        #   should do additions
        # [a = { one } | b = { one } | answer = { 2 }]
        #   should do additions
        # [a = { one } | b = { two } | answer = { three }]
        #   should do additions
      end

      context 'default 2' do
        subject { a + b }
        paramz(
          [:a,       :b,          :subject],
          -> { one }, 0,          1,
          -> { one }, -> { one }, -> { 2 },
          -> { one }, -> { two }, -> { three },
        )

        #=>
        # [a = { one } | b = 0 | subject = 1]
        #   should == 1
        # [a = { one } | b = { one } | subject = { 2 }]
        #   should == 2
        # [a = { one } | b = { two } | subject = { three }]
        #   should == 3
      end

      context 'default 3' do
        paramz(
          [:a,       :b,          subject: -> { a + b }],
          -> { one }, 0,          1,
          -> { one }, -> { one }, -> { 2 },
          -> { one }, -> { two }, -> { three },
        )

        #=>
        # [a = { one } | b = 0 | subject { a + b } = 1]
        #   should == 1
        # [a = { one } | b = { one } | subject { a + b } = { 2 }]
        #   should == 2
        # [a = { one } | b = { two } | subject { a + b } = { three }]
        #   should == 3
      end

      context 'using RSpec::Paramz::Syntax' do
        using RSpec::Paramz::Syntax
        paramz(
          [:a,    :b,     :answer],
          :one.&, 0,      1,
          :one.&, :one.&, -> { 2 },
          :one.&, :two.&, :three.&,
        ) do
          it 'should do additions' do
            expect(a + b).to eq answer
          end
        end

        #=>
        # [a = { one } | b = 0 | answer = 1]
        #   should do additions
        # [a = { one } | b = { one } | answer = { 2 }]
        #   should do additions
        # [a = { one } | b = { two } | answer = { three }]
        #   should do additions
      end
    end

    context 'nil or empty' do
      paramz(
        [:a,   :b,  :answer],
        "foo", nil,   "foo",
        "",    nil,   "",
        nil,   "bar", nil
      ) do
        it 'should do additions' do
          expect(a&.to_s).to eq answer
        end
      end

      #=>
      # [a = "foo" | b = nil | answer = "foo"]
      #   should do additions
      # [a = "" | b = nil | answer = ""]
      #   should do additions
      # [a = nil | b = "bar" | answer = nil]
      #   should do additions
    end
  end
end

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 tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fukayatsu/rspec-paramz. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the RSpec::Paramz project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.