From b750fafed429eb3fc6b82717362811e0b9c194de Mon Sep 17 00:00:00 2001 From: Matt Decuir Date: Tue, 31 Mar 2020 13:56:16 -0500 Subject: [PATCH] Update matcher to take list of arguments; alias it as #strip_attributes (#52) * Update matcher to take list of arguments; alias it as #strip_attributes * Only iterate over the array once --- README.md | 8 ++++++++ lib/strip_attributes/matchers.rb | 23 ++++++++++++++++------- test/matchers_test.rb | 10 ++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index de62bdf..1c57023 100644 --- a/README.md +++ b/README.md @@ -234,7 +234,9 @@ end describe User do it { is_expected.to strip_attribute(:name).collapse_spaces } it { is_expected.to strip_attribute :email } + it { is_expected.to strip_attributes(:name, :email) } it { is_expected.not_to strip_attribute :password } + it { is_expected.not_to strip_attributes(:password, :encrypted_password) } end ``` @@ -244,7 +246,9 @@ end class UserTest < ActiveSupport::TestCase should strip_attribute(:name).collapse_spaces should strip_attribute :email + should strip_attributes(:name, :email) should_not strip_attribute :password + should_not strip_attributes(:password, :encrypted_password) end ``` @@ -257,7 +261,9 @@ describe User do it "should strip attributes" do must strip_attribute(:name).collapse_spaces must strip_attribute :email + must strip_attributes(:name, :email) wont strip_attribute :password + wont strip_attributes(:password, :encrypted_password) end end ``` @@ -270,7 +276,9 @@ describe User do must { strip_attribute(:name).collapse_spaces } must { strip_attribute :email } + must { strip_attributes(:name, :email) } wont { strip_attribute :password } + wont { strip_attributes(:password, :encrypted_password) } end ``` diff --git a/lib/strip_attributes/matchers.rb b/lib/strip_attributes/matchers.rb index be2c1eb..577b8f1 100644 --- a/lib/strip_attributes/matchers.rb +++ b/lib/strip_attributes/matchers.rb @@ -6,26 +6,35 @@ module Matchers # RSpec Examples: # # it { is_expected.to strip_attribute(:first_name) } + # it { is_expected.to strip_attributes(:first_name, :last_name) } # it { is_expected.not_to strip_attribute(:password) } + # it { is_expected.not_to strip_attributes(:password, :encrypted_password) } # # Minitest Matchers Examples: # # must { strip_attribute :first_name } + # must { strip_attributes(:first_name, :last_name) } # wont { strip_attribute :password } - def strip_attribute(attribute) - StripAttributeMatcher.new(attribute) + # wont { strip_attributes(:password, :encrypted_password) } + def strip_attribute(*attributes) + StripAttributeMatcher.new(attributes) end + alias_method :strip_attributes, :strip_attribute + class StripAttributeMatcher - def initialize(attribute) - @attribute = attribute + def initialize(attributes) + @attributes = attributes @options = {} end def matches?(subject) - subject.send("#{@attribute}=", " string ") - subject.valid? - subject.send(@attribute) == "string" and collapse_spaces?(subject) + @attributes.all? do |attribute| + @attribute = attribute + subject.send("#{@attribute}=", " string ") + subject.valid? + subject.send(@attribute) == "string" and collapse_spaces?(subject) + end end def collapse_spaces diff --git a/test/matchers_test.rb b/test/matchers_test.rb index 9ff550e..249a016 100644 --- a/test/matchers_test.rb +++ b/test/matchers_test.rb @@ -92,4 +92,14 @@ class SampleMockRecord < Tableless assert true end end + + it "should take a list of arguments" do + must strip_attribute(:stripped1, :stripped2, :stripped3) + wont strip_attribute(:unstripped1, :unstripped2, :unstripped3) + end + + it "should alias strip_attribute to strip_attributes" do + must strip_attributes(:stripped1, :stripped2, :stripped3) + wont strip_attributes(:unstripped1, :unstripped2, :unstripped3) + end end