diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 46d8be9..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Ruby - -on: - push: - branches: - - master - - pull_request: - -jobs: - build: - runs-on: ubuntu-latest - name: Ruby ${{ matrix.ruby }} - strategy: - matrix: - ruby: - - '3.2.0' - - steps: - - uses: actions/checkout@v3 - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ matrix.ruby }} - bundler-cache: true - - name: Run the default task - run: bundle exec rake diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml new file mode 100644 index 0000000..0a6db33 --- /dev/null +++ b/.github/workflows/rspec.yml @@ -0,0 +1,32 @@ +name: RSpec + +on: + push: + branches: + - main + pull_request: + +jobs: + test: + + runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: + - '2.6' + - '2.7' + - '3.0' + - '3.1' + - '3.2' + + steps: + - uses: actions/checkout@v3 + - name: Set up Ruby + # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby, + # change this to (see https://github.com/ruby/setup-ruby#versioning): + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + - name: Run tests + run: bundle exec rake diff --git a/.gitignore b/.gitignore index b04a8c8..5c249f1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ # rspec failure tracking .rspec_status + +Gemfile.lock diff --git a/.rubocop.yml b/.rubocop.yml deleted file mode 100644 index e3462a7..0000000 --- a/.rubocop.yml +++ /dev/null @@ -1,13 +0,0 @@ -AllCops: - TargetRubyVersion: 2.6 - -Style/StringLiterals: - Enabled: true - EnforcedStyle: double_quotes - -Style/StringLiteralsInInterpolation: - Enabled: true - EnforcedStyle: double_quotes - -Layout/LineLength: - Max: 120 diff --git a/Gemfile b/Gemfile index 1ac9f66..d282b68 100644 --- a/Gemfile +++ b/Gemfile @@ -4,9 +4,3 @@ source "https://rubygems.org" # Specify your gem's dependencies in rspec-parameterized-table_syntax.gemspec gemspec - -gem "rake", "~> 13.0" - -gem "rspec", "~> 3.0" - -gem "rubocop", "~> 1.21" diff --git a/Rakefile b/Rakefile index cca7175..a94dd8a 100644 --- a/Rakefile +++ b/Rakefile @@ -5,8 +5,4 @@ require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) -require "rubocop/rake_task" - -RuboCop::RakeTask.new - -task default: %i[spec rubocop] +task default: %i[spec] diff --git a/lib/rspec/parameterized/table.rb b/lib/rspec/parameterized/table.rb deleted file mode 100644 index dc165f2..0000000 --- a/lib/rspec/parameterized/table.rb +++ /dev/null @@ -1,49 +0,0 @@ -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 diff --git a/lib/rspec/parameterized/table_syntax.rb b/lib/rspec/parameterized/table_syntax.rb index 847c99f..348eb17 100644 --- a/lib/rspec/parameterized/table_syntax.rb +++ b/lib/rspec/parameterized/table_syntax.rb @@ -1,30 +1,13 @@ # frozen_string_literal: true -require_relative "table_syntax/version" -require 'rspec/parameterized/table' +require "rspec/parameterized/table_syntax/version" +require 'rspec/parameterized/table_syntax/table' +require 'rspec/parameterized/table_syntax/table_syntax_implement' +require "rspec/parameterized/core" require 'binding_of_caller' module RSpec module Parameterized - module TableSyntaxImplement - def |(other) - where_binding = binding.of_caller(1) # get where block binding - caller_instance = eval("self", where_binding) # 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 - end - module TableSyntax if Gem::Version.create(RUBY_VERSION) >= Gem::Version.create("3.2.0.rc1") refine Object do diff --git a/lib/rspec/parameterized/table_syntax/table.rb b/lib/rspec/parameterized/table_syntax/table.rb new file mode 100644 index 0000000..169f462 --- /dev/null +++ b/lib/rspec/parameterized/table_syntax/table.rb @@ -0,0 +1,51 @@ +module RSpec + module Parameterized + module TableSyntax + 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 +end diff --git a/lib/rspec/parameterized/table_syntax/table_syntax_implement.rb b/lib/rspec/parameterized/table_syntax/table_syntax_implement.rb new file mode 100644 index 0000000..845fbc5 --- /dev/null +++ b/lib/rspec/parameterized/table_syntax/table_syntax_implement.rb @@ -0,0 +1,24 @@ +module RSpec + module Parameterized + module TableSyntax + module TableSyntaxImplement + def |(other) + where_binding = binding.of_caller(1) # get where block binding + caller_instance = eval("self", where_binding) # get caller instance (ExampleGroup) + + if caller_instance.instance_variable_defined?(:@__parameter_table) + table = caller_instance.instance_variable_get(:@__parameter_table) + else + table = RSpec::Parameterized::TableSyntax::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 + end + end + end +end diff --git a/rspec-parameterized-table_syntax.gemspec b/rspec-parameterized-table_syntax.gemspec index 554f089..b0c8d08 100644 --- a/rspec-parameterized-table_syntax.gemspec +++ b/rspec-parameterized-table_syntax.gemspec @@ -5,20 +5,20 @@ require_relative "lib/rspec/parameterized/table_syntax/version" Gem::Specification.new do |spec| spec.name = "rspec-parameterized-table_syntax" spec.version = Rspec::Parameterized::TableSyntax::VERSION - spec.authors = ["sue445"] - spec.email = ["sue445@sue445.net"] + spec.authors = ["sue445", "tomykaira", "joker1007"] + spec.email = ["sue445@sue445.net", "tomykaira@gmail.com"] - spec.summary = "TODO: Write a short summary, because RubyGems requires one." - spec.description = "TODO: Write a longer description or delete this line." - spec.homepage = "TODO: Put your gem's website or public repo URL here." + 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-table_syntax" spec.license = "MIT" spec.required_ruby_version = ">= 2.6.0" - spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'" - spec.metadata["homepage_uri"] = spec.homepage - spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here." - spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." + spec.metadata["source_code_uri"] = spec.homepage + spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md" # 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. @@ -31,8 +31,11 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - # Uncomment to register a new dependency of your gem - # spec.add_dependency "example-gem", "~> 1.0" + spec.add_dependency "binding_of_caller" + spec.add_dependency "rspec-parameterized-core" + + spec.add_development_dependency "rake" + spec.add_development_dependency "rspec" # For more information and examples about making a new gem, check out our # guide at: https://bundler.io/guides/creating_gem.html diff --git a/spec/parametarized_spec.rb b/spec/parametarized_spec.rb deleted file mode 100644 index 326a42d..0000000 --- a/spec/parametarized_spec.rb +++ /dev/null @@ -1,264 +0,0 @@ -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 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 "table separated with pipe" do - where_table(:a, :b, :answer) do - 1 | 2 | 3 - "hello " | "world" | "hello world" - [1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6] - 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 "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] - 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) } - 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 - - context "when the table has only a row" do - where_table(: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 - - 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 - - describe "child 2 (where_table)" do - where_table(: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 diff --git a/spec/rspec/parameterized/table_syntax_spec.rb b/spec/rspec/parameterized/table_syntax_spec.rb index e8bb340..7383a7b 100644 --- a/spec/rspec/parameterized/table_syntax_spec.rb +++ b/spec/rspec/parameterized/table_syntax_spec.rb @@ -1,11 +1,83 @@ -# frozen_string_literal: true +RSpec.describe RSpec::Parameterized::TableSyntax do + describe "table separated with pipe (using TableSyntax)" do + using RSpec::Parameterized::TableSyntax -RSpec.describe Rspec::Parameterized::TableSyntax do - it "has a version number" do - expect(Rspec::Parameterized::TableSyntax::VERSION).not_to be nil + where(:a, :b, :answer) do + 1 | 2 | 3 + "hello " | "world" | "hello world" + [1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6] + 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) } + end + + with_them do + it "a plus b is answer" do + expect(a + b).to instance_exec(&matcher) + end + end + end + + 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 - it "does something useful" do - expect(false).to eq(true) + 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 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 end end